Skip to content

Commit 8aa740d

Browse files
Happy path implemented
1 parent 0de71f7 commit 8aa740d

File tree

8 files changed

+38
-26
lines changed

8 files changed

+38
-26
lines changed

crates/base_db/src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub trait FileLoader {
9696
/// `#[path = "C://no/way"]`
9797
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId>;
9898
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>;
99-
fn possible_sudmobules(&self, module_file: FileId) -> Vec<(FileId, String)>;
99+
fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String>;
100100
}
101101

102102
/// Database which stores all significant input facts: source code and project
@@ -166,11 +166,11 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
166166
self.0.source_root_crates(source_root)
167167
}
168168

169-
fn possible_sudmobules(&self, module_file: FileId) -> Vec<(FileId, String)> {
169+
fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
170170
fn possible_sudmobules_opt(
171171
module_files: &FileSet,
172172
module_file: FileId,
173-
) -> Option<Vec<(FileId, String)>> {
173+
) -> Option<Vec<FileId>> {
174174
match module_files.file_name_and_extension(module_file)? {
175175
("mod", Some("rs")) | ("lib", Some("rs")) => {
176176
module_files.list_files(module_file, None)
@@ -181,8 +181,16 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
181181
}
182182
}
183183

184-
possible_sudmobules_opt(&self.source_root(module_file).file_set, module_file)
184+
let module_files = &self.source_root(module_file).file_set;
185+
possible_sudmobules_opt(module_files, module_file)
185186
.unwrap_or_default()
187+
.into_iter()
188+
.filter_map(|submodule_file| module_files.file_name_and_extension(submodule_file))
189+
.map(|(file_name, extension)| match extension {
190+
Some(extension) => format!("{}.{}", file_name, extension),
191+
None => file_name.to_owned(),
192+
})
193+
.collect()
186194
}
187195
}
188196

crates/hir_def/src/test_db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ impl FileLoader for TestDB {
6363
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
6464
FileLoaderDelegate(self).relevant_crates(file_id)
6565
}
66-
fn possible_sudmobules(&self, module_file: FileId) -> Vec<(FileId, String)> {
67-
FileLoaderDelegate(self).possible_sudmobules(module_file)
66+
fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
67+
FileLoaderDelegate(self).possible_sudmobule_names(module_file)
6868
}
6969
}
7070

crates/hir_expand/src/test_db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl FileLoader for TestDB {
4646
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
4747
FileLoaderDelegate(self).relevant_crates(file_id)
4848
}
49-
fn possible_sudmobules(&self, module_file: FileId) -> Vec<(FileId, String)> {
50-
FileLoaderDelegate(self).possible_sudmobules(module_file)
49+
fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
50+
FileLoaderDelegate(self).possible_sudmobule_names(module_file)
5151
}
5252
}

crates/hir_ty/src/test_db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ impl FileLoader for TestDB {
7373
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
7474
FileLoaderDelegate(self).relevant_crates(file_id)
7575
}
76-
fn possible_sudmobules(&self, module_file: FileId) -> Vec<(FileId, String)> {
77-
FileLoaderDelegate(self).possible_sudmobules(module_file)
76+
fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
77+
FileLoaderDelegate(self).possible_sudmobule_names(module_file)
7878
}
7979
}
8080

crates/ide/src/completion/completion_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl<'a> CompletionContext<'a> {
122122
}
123123
let module_definition_source_file = definition_source.file_id.original_file(db);
124124
let mod_declaration_candidates =
125-
db.possible_sudmobules(module_definition_source_file);
125+
db.possible_sudmobule_names(module_definition_source_file);
126126
dbg!(mod_declaration_candidates);
127127
// TODO kb exlude existing children from the candidates
128128
let existing_children = current_module.children(db).collect::<Vec<_>>();

crates/ide_db/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ impl FileLoader for RootDatabase {
7474
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
7575
FileLoaderDelegate(self).relevant_crates(file_id)
7676
}
77-
fn possible_sudmobules(&self, module_file: FileId) -> Vec<(FileId, String)> {
78-
FileLoaderDelegate(self).possible_sudmobules(module_file)
77+
fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
78+
FileLoaderDelegate(self).possible_sudmobule_names(module_file)
7979
}
8080
}
8181

crates/vfs/src/file_set.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,27 @@ impl FileSet {
3434
&self,
3535
anchor: FileId,
3636
anchor_relative_path: Option<&str>,
37-
) -> Option<Vec<(FileId, String)>> {
37+
) -> Option<Vec<FileId>> {
3838
let anchor_directory = {
3939
let path = self.paths[&anchor].clone();
4040
match anchor_relative_path {
4141
Some(anchor_relative_path) => path.join(anchor_relative_path),
42-
None => path.join("../"),
42+
None => path.parent(),
4343
}
4444
}?;
4545

4646
Some(
4747
self.paths
4848
.iter()
49-
.filter(|(_, path)| path.starts_with(&anchor_directory))
50-
// TODO kb need to ensure that no / exists after the anchor_directory
51-
.filter(|(_, path)| path.ends_with(".rs"))
52-
.map(|(&file_id, path)| (file_id, path.to_string()))
49+
.filter_map(|(&file_id, path)| {
50+
if path.parent()? == anchor_directory
51+
&& matches!(path.file_name_and_extension(), Some((_, Some("rs"))))
52+
{
53+
Some(file_id)
54+
} else {
55+
None
56+
}
57+
})
5358
.collect(),
5459
)
5560
}

crates/vfs/src/vfs_path.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ impl VfsPath {
4848
(VfsPathRepr::VirtualPath(_), _) => false,
4949
}
5050
}
51-
pub fn ends_with(&self, suffix: &str) -> bool {
52-
match &self.0 {
53-
VfsPathRepr::PathBuf(p) => p.ends_with(suffix),
54-
VfsPathRepr::VirtualPath(p) => p.ends_with(suffix),
51+
pub fn parent(&self) -> Option<VfsPath> {
52+
let mut parent = self.clone();
53+
if parent.pop() {
54+
Some(parent)
55+
} else {
56+
None
5557
}
5658
}
5759

@@ -265,9 +267,6 @@ impl VirtualPath {
265267
fn starts_with(&self, other: &VirtualPath) -> bool {
266268
self.0.starts_with(&other.0)
267269
}
268-
fn ends_with(&self, suffix: &str) -> bool {
269-
self.0.ends_with(suffix)
270-
}
271270
fn pop(&mut self) -> bool {
272271
let pos = match self.0.rfind('/') {
273272
Some(pos) => pos,

0 commit comments

Comments
 (0)