First steps for mod<|> completion

This commit is contained in:
Kirill Bulatov 2020-08-28 21:28:30 +03:00
parent 5c336e266f
commit 4bed588001
4 changed files with 65 additions and 6 deletions

View file

@ -96,6 +96,7 @@ pub trait FileLoader {
/// `#[path = "C://no/way"]`
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId>;
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>;
fn list_some_random_files_todo(&self, anchor: FileId) -> Vec<(FileId, String)>;
}
/// Database which stores all significant input facts: source code and project
@ -155,8 +156,8 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
}
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
// FIXME: this *somehow* should be platform agnostic...
let source_root = self.0.file_source_root(anchor);
let source_root = self.0.source_root(source_root);
// self.source_root(anchor)
let source_root = self.source_root(anchor);
source_root.file_set.resolve_path(anchor, path)
}
@ -164,4 +165,15 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
let source_root = self.0.file_source_root(file_id);
self.0.source_root_crates(source_root)
}
fn list_some_random_files_todo(&self, anchor: FileId) -> Vec<(FileId, String)> {
self.source_root(anchor).file_set.list_some_random_files_todo(anchor)
}
}
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)
}
}

View file

@ -1,7 +1,7 @@
//! FIXME: write short doc here
use base_db::SourceDatabase;
use hir::{Semantics, SemanticsScope, Type};
use base_db::{FileLoader, SourceDatabase};
use hir::{ModuleSource, Semantics, SemanticsScope, Type};
use ide_db::RootDatabase;
use syntax::{
algo::{find_covering_element, find_node_at_offset},
@ -112,6 +112,27 @@ impl<'a> CompletionContext<'a> {
};
let fake_ident_token =
file_with_fake_ident.syntax().token_at_offset(position.offset).right_biased().unwrap();
{
let module_names_for_import = sema
.to_module_def(position.file_id)
.and_then(|current_module| {
let definition_source = current_module.definition_source(db);
if !matches!(definition_source.value, ModuleSource::SourceFile(_)) {
return None;
}
let definition_source_file = definition_source.file_id.original_file(db);
// TODO kb for all possible candidates
let zz = db.list_some_random_files_todo(definition_source_file);
dbg!(zz);
// TODO kb exlude existing children from the candidates
let existing_children = current_module.children(db).collect::<Vec<_>>();
dbg!(existing_children);
None::<Vec<String>>
})
.unwrap_or_default();
dbg!(module_names_for_import);
};
let krate = sema.to_module_def(position.file_id).map(|m| m.krate());
let original_token =

View file

@ -74,6 +74,9 @@ impl FileLoader for RootDatabase {
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id)
}
fn list_some_random_files_todo(&self, anchor: FileId) -> Vec<(FileId, String)> {
FileLoaderDelegate(self).list_some_random_files_todo(anchor)
}
}
impl salsa::Database for RootDatabase {

View file

@ -23,8 +23,31 @@ impl FileSet {
let mut base = self.paths[&anchor].clone();
base.pop();
let path = base.join(path)?;
let res = self.files.get(&path).copied();
res
self.files.get(&path).copied()
}
pub fn list_some_random_files_todo(&self, anchor: FileId) -> Vec<(FileId, String)> {
let anchor_path = self.paths[&anchor].clone();
/*
[crates/vfs/src/file_set.rs:30] anchor_path = "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/lib.rs"
[crates/vfs/src/file_set.rs:31] self.files.keys() = [
"/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_1/test_mod_2/test_mod_3.rs",
"/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_1/test_mod_2.rs",
"/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_1.rs",
"/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/lib.rs",
"/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_3/test_mod_3_1.rs",
"/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_3.rs",
]
*/
// TODO kb determine the ways to list all applicable files
// Maybe leave list directory here only and the move the rest of the logic into the database impl?
// Need to get the following things:
// * name of the anchor_path file (file_name, validate that it's a file!)
// * list of all files in the file's contai/ning directory (file_dir)
// * list of all files in `file_dir/file_name` or just `file_dir/`, for lib.rs or mod.rs
// * consider special case for /src/bin/foo.rs as a mod<|> source
Vec::new()
}
pub fn insert(&mut self, file_id: FileId, path: VfsPath) {
self.files.insert(path.clone(), file_id);