mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 13:18:47 +00:00
Properly handle special cases (binaries, mod.rs)
This commit is contained in:
parent
486c5c3285
commit
b2bcc5278d
3 changed files with 60 additions and 52 deletions
|
@ -167,29 +167,7 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
|
fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
|
||||||
let module_files = &self.source_root(module_file).file_set;
|
self.source_root(module_file).file_set.possible_sudmobule_names(module_file)
|
||||||
let possible_submodule_files = match module_files.file_name_and_extension(module_file) {
|
|
||||||
Some(("mod", Some("rs"))) | Some(("lib", Some("rs"))) => {
|
|
||||||
module_files.list_files_with_extensions(module_file, None)
|
|
||||||
}
|
|
||||||
// TODO kb for `src/bin/foo.rs`, we need to check for modules in `src/bin/`
|
|
||||||
Some((directory_with_module_name, Some("rs"))) => module_files
|
|
||||||
.list_files_with_extensions(
|
|
||||||
module_file,
|
|
||||||
Some(&format!("../{}/", directory_with_module_name)),
|
|
||||||
),
|
|
||||||
// TODO kb also consider the case when there's no `../module_name.rs`, but `../module_name/mod.rs`
|
|
||||||
_ => Vec::new(),
|
|
||||||
};
|
|
||||||
|
|
||||||
possible_submodule_files
|
|
||||||
.into_iter()
|
|
||||||
.filter(|(_, extension)| extension == &Some("rs"))
|
|
||||||
.filter(|(file_name, _)| file_name != &"mod")
|
|
||||||
.filter(|(file_name, _)| file_name != &"lib")
|
|
||||||
.filter(|(file_name, _)| file_name != &"main")
|
|
||||||
.map(|(file_name, _)| file_name.to_owned())
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
//! FIXME: write short doc here
|
//! FIXME: write short doc here
|
||||||
|
|
||||||
use base_db::{FileLoader, SourceDatabase};
|
use base_db::{FileLoader, 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},
|
||||||
|
@ -123,11 +123,9 @@ impl<'a> CompletionContext<'a> {
|
||||||
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(db).collect::<Vec<_>>();
|
let existing_children = current_module.children(db).collect::<Vec<_>>();
|
||||||
dbg!(existing_children);
|
|
||||||
None::<Vec<String>>
|
None::<Vec<String>>
|
||||||
})
|
})
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
dbg!(module_names_for_import);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
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());
|
||||||
|
|
|
@ -26,38 +26,70 @@ impl FileSet {
|
||||||
self.files.get(&path).copied()
|
self.files.get(&path).copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn file_name_and_extension(&self, file: FileId) -> Option<(&str, Option<&str>)> {
|
pub fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
|
||||||
self.paths[&file].file_name_and_extension()
|
let directory_to_look_for_submodules = match self.get_directory_with_submodules(module_file)
|
||||||
}
|
{
|
||||||
|
Some(directory) => directory,
|
||||||
pub fn list_files_with_extensions(
|
None => return Vec::new(),
|
||||||
&self,
|
|
||||||
anchor: FileId,
|
|
||||||
anchor_relative_path: Option<&str>,
|
|
||||||
) -> Vec<(&str, Option<&str>)> {
|
|
||||||
let anchor_directory = {
|
|
||||||
let path = self.paths[&anchor].clone();
|
|
||||||
match anchor_relative_path {
|
|
||||||
Some(anchor_relative_path) => path.join(anchor_relative_path),
|
|
||||||
None => path.parent(),
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
self.paths
|
||||||
if let Some(anchor_directory) = anchor_directory {
|
.iter()
|
||||||
self.paths
|
.filter_map(|(_, path)| {
|
||||||
.iter()
|
if path.parent()? == directory_to_look_for_submodules {
|
||||||
.filter_map(|(_, path)| {
|
path.file_name_and_extension()
|
||||||
if path.parent()? == anchor_directory {
|
} else {
|
||||||
path.file_name_and_extension()
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.filter_map(|file_name_and_extension| match file_name_and_extension {
|
||||||
|
// TODO kb do not include the module file name itself, if present
|
||||||
|
// TODO kb wrong resolution for nesСпаted 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 self.files.contains_key(&mod_rs_path) {
|
||||||
|
Some(subdirectory_name.to_owned())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
.collect()
|
_ => None,
|
||||||
} else {
|
})
|
||||||
Vec::new()
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_directory_with_submodules(&self, module_file: FileId) -> Option<VfsPath> {
|
||||||
|
let module_file_path = &self.paths[&module_file];
|
||||||
|
let module_directory_path = module_file_path.parent()?;
|
||||||
|
match module_file_path.file_name_and_extension() {
|
||||||
|
Some(("mod", Some("rs"))) | Some(("lib", Some("rs"))) | Some(("main", Some("rs"))) => {
|
||||||
|
Some(module_directory_path)
|
||||||
|
}
|
||||||
|
Some((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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(&mut self, file_id: FileId, path: VfsPath) {
|
pub fn insert(&mut self, file_id: FileId, path: VfsPath) {
|
||||||
self.files.insert(path.clone(), file_id);
|
self.files.insert(path.clone(), file_id);
|
||||||
self.paths.insert(file_id, path);
|
self.paths.insert(file_id, path);
|
||||||
|
|
Loading…
Reference in a new issue