mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
Move most of the logic into the completion module
This commit is contained in:
parent
6ba479cd05
commit
f9c14ac720
8 changed files with 107 additions and 118 deletions
|
@ -12,7 +12,7 @@ use cfg::CfgOptions;
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
use syntax::SmolStr;
|
use syntax::SmolStr;
|
||||||
use tt::TokenExpander;
|
use tt::TokenExpander;
|
||||||
use vfs::file_set::FileSet;
|
use vfs::{file_set::FileSet, VfsPath};
|
||||||
|
|
||||||
pub use vfs::FileId;
|
pub use vfs::FileId;
|
||||||
|
|
||||||
|
@ -43,6 +43,12 @@ impl SourceRoot {
|
||||||
pub fn new_library(file_set: FileSet) -> SourceRoot {
|
pub fn new_library(file_set: FileSet) -> SourceRoot {
|
||||||
SourceRoot { is_library: true, file_set }
|
SourceRoot { is_library: true, file_set }
|
||||||
}
|
}
|
||||||
|
pub fn path_for_file(&self, file: &FileId) -> Option<&VfsPath> {
|
||||||
|
self.file_set.path_for_file(file)
|
||||||
|
}
|
||||||
|
pub fn file_for_path(&self, path: &VfsPath) -> Option<&FileId> {
|
||||||
|
self.file_set.file_for_path(path)
|
||||||
|
}
|
||||||
pub fn iter(&self) -> impl Iterator<Item = FileId> + '_ {
|
pub fn iter(&self) -> impl Iterator<Item = FileId> + '_ {
|
||||||
self.file_set.iter()
|
self.file_set.iter()
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,7 +96,6 @@ pub trait FileLoader {
|
||||||
/// `#[path = "C://no/way"]`
|
/// `#[path = "C://no/way"]`
|
||||||
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId>;
|
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId>;
|
||||||
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>;
|
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>;
|
||||||
fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Database which stores all significant input facts: source code and project
|
/// Database which stores all significant input facts: source code and project
|
||||||
|
@ -156,8 +155,8 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
|
||||||
}
|
}
|
||||||
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
|
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
|
||||||
// FIXME: this *somehow* should be platform agnostic...
|
// FIXME: this *somehow* should be platform agnostic...
|
||||||
// self.source_root(anchor)
|
let source_root = self.0.file_source_root(anchor);
|
||||||
let source_root = self.source_root(anchor);
|
let source_root = self.0.source_root(source_root);
|
||||||
source_root.file_set.resolve_path(anchor, path)
|
source_root.file_set.resolve_path(anchor, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,83 +164,4 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
|
||||||
let source_root = self.0.file_source_root(file_id);
|
let source_root = self.0.file_source_root(file_id);
|
||||||
self.0.source_root_crates(source_root)
|
self.0.source_root_crates(source_root)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
|
|
||||||
possible_sudmobule_names(&self.source_root(module_file).file_set, module_file)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: SourceDatabaseExt> FileLoaderDelegate<&'_ T> {
|
|
||||||
fn source_root(&self, anchor: FileId) -> Arc<SourceRoot> {
|
|
||||||
let source_root = self.0.file_source_root(anchor);
|
|
||||||
self.0.source_root(source_root)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn possible_sudmobule_names(module_files: &FileSet, module_file: FileId) -> Vec<String> {
|
|
||||||
let directory_to_look_for_submodules = match module_files
|
|
||||||
.path_for_file(&module_file)
|
|
||||||
.and_then(|module_file_path| get_directory_with_submodules(module_file_path))
|
|
||||||
{
|
|
||||||
Some(directory) => directory,
|
|
||||||
None => return Vec::new(),
|
|
||||||
};
|
|
||||||
module_files
|
|
||||||
.iter()
|
|
||||||
.filter(|submodule_file| submodule_file != &module_file)
|
|
||||||
.filter_map(|submodule_file| {
|
|
||||||
let submodule_path = module_files.path_for_file(&submodule_file)?;
|
|
||||||
if submodule_path.parent()? == directory_to_look_for_submodules {
|
|
||||||
submodule_path.file_name_and_extension()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.filter_map(|file_name_and_extension| {
|
|
||||||
match file_name_and_extension {
|
|
||||||
// TODO kb wrong resolution for nested non-file modules (mod tests { mod <|> })
|
|
||||||
// TODO kb in src/bin when a module is included into another,
|
|
||||||
// the included file gets "moved" into a directory below and now cannot add any other modules
|
|
||||||
("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => None,
|
|
||||||
(file_name, Some("rs")) => Some(file_name.to_owned()),
|
|
||||||
(subdirectory_name, None) => {
|
|
||||||
let mod_rs_path =
|
|
||||||
directory_to_look_for_submodules.join(subdirectory_name)?.join("mod.rs")?;
|
|
||||||
if module_files.file_for_path(&mod_rs_path).is_some() {
|
|
||||||
Some(subdirectory_name.to_owned())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_directory_with_submodules(module_file_path: &VfsPath) -> Option<VfsPath> {
|
|
||||||
let module_directory_path = module_file_path.parent()?;
|
|
||||||
match module_file_path.file_name_and_extension()? {
|
|
||||||
("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => {
|
|
||||||
Some(module_directory_path)
|
|
||||||
}
|
|
||||||
(regular_rust_file_name, Some("rs")) => {
|
|
||||||
if matches!(
|
|
||||||
(
|
|
||||||
module_directory_path
|
|
||||||
.parent()
|
|
||||||
.as_ref()
|
|
||||||
.and_then(|path| path.file_name_and_extension()),
|
|
||||||
module_directory_path.file_name_and_extension(),
|
|
||||||
),
|
|
||||||
(Some(("src", None)), Some(("bin", None)))
|
|
||||||
) {
|
|
||||||
// files in /src/bin/ can import each other directly
|
|
||||||
Some(module_directory_path)
|
|
||||||
} else {
|
|
||||||
module_directory_path.join(regular_rust_file_name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,9 +63,6 @@ impl FileLoader for TestDB {
|
||||||
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
|
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
|
||||||
FileLoaderDelegate(self).relevant_crates(file_id)
|
FileLoaderDelegate(self).relevant_crates(file_id)
|
||||||
}
|
}
|
||||||
fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
|
|
||||||
FileLoaderDelegate(self).possible_sudmobule_names(module_file)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestDB {
|
impl TestDB {
|
||||||
|
|
|
@ -46,7 +46,4 @@ impl FileLoader for TestDB {
|
||||||
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
|
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
|
||||||
FileLoaderDelegate(self).relevant_crates(file_id)
|
FileLoaderDelegate(self).relevant_crates(file_id)
|
||||||
}
|
}
|
||||||
fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
|
|
||||||
FileLoaderDelegate(self).possible_sudmobule_names(module_file)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,9 +73,6 @@ impl FileLoader for TestDB {
|
||||||
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
|
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
|
||||||
FileLoaderDelegate(self).relevant_crates(file_id)
|
FileLoaderDelegate(self).relevant_crates(file_id)
|
||||||
}
|
}
|
||||||
fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
|
|
||||||
FileLoaderDelegate(self).possible_sudmobule_names(module_file)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestDB {
|
impl TestDB {
|
||||||
|
|
|
@ -1,35 +1,61 @@
|
||||||
//! Completes mod declarations.
|
//! Completes mod declarations.
|
||||||
|
|
||||||
use base_db::FileLoader;
|
use base_db::{SourceDatabaseExt, VfsPath};
|
||||||
use hir::ModuleSource;
|
use hir::{Module, ModuleSource};
|
||||||
|
use ide_db::RootDatabase;
|
||||||
|
|
||||||
use super::{completion_context::CompletionContext, completion_item::Completions};
|
use super::{completion_context::CompletionContext, completion_item::Completions};
|
||||||
|
|
||||||
/// Complete mod declaration, i.e. `mod <|> ;`
|
/// Complete mod declaration, i.e. `mod <|> ;`
|
||||||
pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) {
|
pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
let module_names_for_import = ctx
|
let module_names_for_import = ctx
|
||||||
.sema
|
.scope
|
||||||
// TODO kb this is wrong, since we need not the file module
|
.module()
|
||||||
.to_module_def(ctx.position.file_id)
|
|
||||||
.and_then(|current_module| {
|
.and_then(|current_module| {
|
||||||
dbg!(current_module.name(ctx.db));
|
let module_path = path_to_closest_containing_module_file(current_module, ctx.db);
|
||||||
dbg!(current_module.definition_source(ctx.db));
|
|
||||||
dbg!(current_module.declaration_source(ctx.db));
|
|
||||||
let mut zz = Vec::new();
|
|
||||||
let mut vv = Some(current_module);
|
|
||||||
while let Some(ModuleSource::Module(_)) =
|
|
||||||
vv.map(|vv| vv.definition_source(ctx.db).value)
|
|
||||||
{
|
|
||||||
zz.push(current_module.name(ctx.db));
|
|
||||||
vv = current_module.parent(ctx.db);
|
|
||||||
}
|
|
||||||
dbg!(zz);
|
|
||||||
let definition_source = current_module.definition_source(ctx.db);
|
|
||||||
// TODO kb filter out declarations in possible_sudmobule_names
|
// TODO kb filter out declarations in possible_sudmobule_names
|
||||||
// let declaration_source = current_module.declaration_source(ctx.db);
|
// let declaration_source = current_module.declaration_source(ctx.db);
|
||||||
let module_definition_source_file = definition_source.file_id.original_file(ctx.db);
|
let module_definition_source_file =
|
||||||
let mod_declaration_candidates =
|
current_module.definition_source(ctx.db).file_id.original_file(ctx.db);
|
||||||
ctx.db.possible_sudmobule_names(module_definition_source_file);
|
|
||||||
|
let source_root_id = ctx.db.file_source_root(module_definition_source_file);
|
||||||
|
let source_root = ctx.db.source_root(source_root_id);
|
||||||
|
let directory_to_look_for_submodules = source_root
|
||||||
|
.path_for_file(&module_definition_source_file)
|
||||||
|
.and_then(|module_file_path| get_directory_with_submodules(module_file_path))?;
|
||||||
|
|
||||||
|
let mod_declaration_candidates = source_root
|
||||||
|
.iter()
|
||||||
|
.filter(|submodule_file| submodule_file != &module_definition_source_file)
|
||||||
|
.filter_map(|submodule_file| {
|
||||||
|
let submodule_path = source_root.path_for_file(&submodule_file)?;
|
||||||
|
if submodule_path.parent()? == directory_to_look_for_submodules {
|
||||||
|
submodule_path.file_name_and_extension()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.filter_map(|file_name_and_extension| {
|
||||||
|
match file_name_and_extension {
|
||||||
|
// TODO kb wrong resolution for nested non-file modules (mod tests { mod <|> })
|
||||||
|
// TODO kb in src/bin when a module is included into another,
|
||||||
|
// the included file gets "moved" into a directory below and now cannot add any other modules
|
||||||
|
("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => None,
|
||||||
|
(file_name, Some("rs")) => Some(file_name.to_owned()),
|
||||||
|
(subdirectory_name, None) => {
|
||||||
|
let mod_rs_path = directory_to_look_for_submodules
|
||||||
|
.join(subdirectory_name)?
|
||||||
|
.join("mod.rs")?;
|
||||||
|
if source_root.file_for_path(&mod_rs_path).is_some() {
|
||||||
|
Some(subdirectory_name.to_owned())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
dbg!(mod_declaration_candidates);
|
dbg!(mod_declaration_candidates);
|
||||||
// TODO kb exlude existing children from the candidates
|
// TODO kb exlude existing children from the candidates
|
||||||
let existing_children = current_module.children(ctx.db).collect::<Vec<_>>();
|
let existing_children = current_module.children(ctx.db).collect::<Vec<_>>();
|
||||||
|
@ -37,3 +63,51 @@ pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
})
|
})
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn path_to_closest_containing_module_file(
|
||||||
|
current_module: Module,
|
||||||
|
db: &RootDatabase,
|
||||||
|
) -> Vec<Module> {
|
||||||
|
let mut path = Vec::new();
|
||||||
|
|
||||||
|
let mut current_module = Some(current_module);
|
||||||
|
while let Some(ModuleSource::Module(_)) =
|
||||||
|
current_module.map(|module| module.definition_source(db).value)
|
||||||
|
{
|
||||||
|
if let Some(module) = current_module {
|
||||||
|
path.insert(0, module);
|
||||||
|
current_module = module.parent(db);
|
||||||
|
} else {
|
||||||
|
current_module = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
path
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_directory_with_submodules(module_file_path: &VfsPath) -> Option<VfsPath> {
|
||||||
|
let module_directory_path = module_file_path.parent()?;
|
||||||
|
match module_file_path.file_name_and_extension()? {
|
||||||
|
("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => {
|
||||||
|
Some(module_directory_path)
|
||||||
|
}
|
||||||
|
(regular_rust_file_name, Some("rs")) => {
|
||||||
|
if matches!(
|
||||||
|
(
|
||||||
|
module_directory_path
|
||||||
|
.parent()
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|path| path.file_name_and_extension()),
|
||||||
|
module_directory_path.file_name_and_extension(),
|
||||||
|
),
|
||||||
|
(Some(("src", None)), Some(("bin", None)))
|
||||||
|
) {
|
||||||
|
// files in /src/bin/ can import each other directly
|
||||||
|
Some(module_directory_path)
|
||||||
|
} else {
|
||||||
|
module_directory_path.join(regular_rust_file_name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
//! FIXME: write short doc here
|
//! FIXME: write short doc here
|
||||||
|
|
||||||
use base_db::{FileLoader, SourceDatabase};
|
use base_db::SourceDatabase;
|
||||||
use hir::{ModuleSource, Semantics, SemanticsScope, Type};
|
use hir::{Semantics, SemanticsScope, Type};
|
||||||
use ide_db::RootDatabase;
|
use ide_db::RootDatabase;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
algo::{find_covering_element, find_node_at_offset},
|
algo::{find_covering_element, find_node_at_offset},
|
||||||
|
@ -112,6 +112,7 @@ impl<'a> CompletionContext<'a> {
|
||||||
};
|
};
|
||||||
let fake_ident_token =
|
let fake_ident_token =
|
||||||
file_with_fake_ident.syntax().token_at_offset(position.offset).right_biased().unwrap();
|
file_with_fake_ident.syntax().token_at_offset(position.offset).right_biased().unwrap();
|
||||||
|
|
||||||
let krate = sema.to_module_def(position.file_id).map(|m| m.krate());
|
let krate = sema.to_module_def(position.file_id).map(|m| m.krate());
|
||||||
let original_token =
|
let original_token =
|
||||||
original_file.syntax().token_at_offset(position.offset).left_biased()?;
|
original_file.syntax().token_at_offset(position.offset).left_biased()?;
|
||||||
|
|
|
@ -74,9 +74,6 @@ impl FileLoader for RootDatabase {
|
||||||
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
|
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
|
||||||
FileLoaderDelegate(self).relevant_crates(file_id)
|
FileLoaderDelegate(self).relevant_crates(file_id)
|
||||||
}
|
}
|
||||||
fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
|
|
||||||
FileLoaderDelegate(self).possible_sudmobule_names(module_file)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl salsa::Database for RootDatabase {
|
impl salsa::Database for RootDatabase {
|
||||||
|
|
Loading…
Reference in a new issue