mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 04:53:34 +00:00
extend module resolve to mod.rs
This commit is contained in:
parent
081c16c776
commit
41570f60bf
8 changed files with 82 additions and 19 deletions
|
@ -5,3 +5,4 @@ authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
difference = "2.0.0"
|
difference = "2.0.0"
|
||||||
|
itertools = "0.7.8"
|
||||||
|
|
|
@ -1,4 +1,9 @@
|
||||||
extern crate difference;
|
extern crate difference;
|
||||||
|
extern crate itertools;
|
||||||
|
|
||||||
|
use std::fmt;
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
pub use self::difference::Changeset as __Changeset;
|
pub use self::difference::Changeset as __Changeset;
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
|
@ -23,3 +28,9 @@ macro_rules! assert_eq_text {
|
||||||
}
|
}
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn assert_eq_dbg(expected: &str, actual: &impl fmt::Debug) {
|
||||||
|
let actual = format!("{:?}", actual);
|
||||||
|
let expected = expected.lines().map(|l| l.trim()).join(" ");
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
}
|
||||||
|
|
|
@ -12,3 +12,6 @@ rayon = "1.0.2"
|
||||||
fst = { git = "https://github.com/matklad/fst", branch = "subsequence"}
|
fst = { git = "https://github.com/matklad/fst", branch = "subsequence"}
|
||||||
libsyntax2 = { path = "../libsyntax2" }
|
libsyntax2 = { path = "../libsyntax2" }
|
||||||
libeditor = { path = "../libeditor" }
|
libeditor = { path = "../libeditor" }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
assert_eq_text = { path = "../assert_eq_text" }
|
||||||
|
|
|
@ -163,15 +163,21 @@ impl World {
|
||||||
Some(name) => name.text(),
|
Some(name) => name.text(),
|
||||||
None => return Vec::new(),
|
None => return Vec::new(),
|
||||||
};
|
};
|
||||||
let id = match self.resolve_relative_path(id, &PathBuf::from(format!("../{}.rs", name))) {
|
let paths = &[
|
||||||
Some(id) => id,
|
PathBuf::from(format!("../{}.rs", name)),
|
||||||
None => return Vec::new(),
|
PathBuf::from(format!("../{}/mod.rs", name)),
|
||||||
};
|
];
|
||||||
vec![(id, FileSymbol {
|
paths.iter()
|
||||||
name: name.clone(),
|
.filter_map(|path| self.resolve_relative_path(id, path))
|
||||||
node_range: TextRange::offset_len(0.into(), 0.into()),
|
.map(|id| {
|
||||||
kind: MODULE,
|
let symbol = FileSymbol {
|
||||||
})]
|
name: name.clone(),
|
||||||
|
node_range: TextRange::offset_len(0.into(), 0.into()),
|
||||||
|
kind: MODULE,
|
||||||
|
};
|
||||||
|
(id, symbol)
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_relative_path(&self, id: FileId, path: &Path) -> Option<FileId> {
|
fn resolve_relative_path(&self, id: FileId, path: &Path) -> Option<FileId> {
|
||||||
|
|
45
crates/libanalysis/tests/tests.rs
Normal file
45
crates/libanalysis/tests/tests.rs
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
extern crate libanalysis;
|
||||||
|
extern crate assert_eq_text;
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use libanalysis::{WorldState, FileId};
|
||||||
|
use assert_eq_text::assert_eq_dbg;
|
||||||
|
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_resolve_module() {
|
||||||
|
let mut world = WorldState::new();
|
||||||
|
world.change_file(FileId(1), Some("mod foo;".to_string()));
|
||||||
|
world.change_file(FileId(2), Some("".to_string()));
|
||||||
|
|
||||||
|
let snap = world.snapshot(|id, path| {
|
||||||
|
assert_eq!(id, FileId(1));
|
||||||
|
if path == PathBuf::from("../foo/mod.rs") {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
assert_eq!(path, PathBuf::from("../foo.rs"));
|
||||||
|
Some(FileId(2))
|
||||||
|
});
|
||||||
|
let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into())
|
||||||
|
.unwrap();
|
||||||
|
assert_eq_dbg(
|
||||||
|
r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#,
|
||||||
|
&symbols,
|
||||||
|
);
|
||||||
|
|
||||||
|
let snap = world.snapshot(|id, path| {
|
||||||
|
assert_eq!(id, FileId(1));
|
||||||
|
if path == PathBuf::from("../foo.rs") {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
assert_eq!(path, PathBuf::from("../foo/mod.rs"));
|
||||||
|
Some(FileId(2))
|
||||||
|
});
|
||||||
|
let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into())
|
||||||
|
.unwrap();
|
||||||
|
assert_eq_dbg(
|
||||||
|
r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#,
|
||||||
|
&symbols,
|
||||||
|
);
|
||||||
|
}
|
|
@ -10,4 +10,6 @@ superslice = "0.1.0"
|
||||||
|
|
||||||
libsyntax2 = { path = "../libsyntax2" }
|
libsyntax2 = { path = "../libsyntax2" }
|
||||||
smol_str = "0.1.0"
|
smol_str = "0.1.0"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
assert_eq_text = { path = "../assert_eq_text" }
|
assert_eq_text = { path = "../assert_eq_text" }
|
||||||
|
|
|
@ -6,6 +6,7 @@ extern crate assert_eq_text;
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
use assert_eq_text::{assert_eq_dbg};
|
||||||
use libeditor::{
|
use libeditor::{
|
||||||
File, TextUnit, TextRange, ActionResult, CursorPosition,
|
File, TextUnit, TextRange, ActionResult, CursorPosition,
|
||||||
highlight, runnables, extend_selection, file_structure,
|
highlight, runnables, extend_selection, file_structure,
|
||||||
|
@ -33,7 +34,7 @@ fn main() {}
|
||||||
println!("Hello, {}!", 92);
|
println!("Hello, {}!", 92);
|
||||||
"#);
|
"#);
|
||||||
let hls = highlight(&file);
|
let hls = highlight(&file);
|
||||||
dbg_eq(
|
assert_eq_dbg(
|
||||||
r#"[HighlightedRange { range: [1; 11), tag: "comment" },
|
r#"[HighlightedRange { range: [1; 11), tag: "comment" },
|
||||||
HighlightedRange { range: [12; 14), tag: "keyword" },
|
HighlightedRange { range: [12; 14), tag: "keyword" },
|
||||||
HighlightedRange { range: [15; 19), tag: "function" },
|
HighlightedRange { range: [15; 19), tag: "function" },
|
||||||
|
@ -57,7 +58,7 @@ fn test_foo() {}
|
||||||
fn test_foo() {}
|
fn test_foo() {}
|
||||||
"#);
|
"#);
|
||||||
let runnables = runnables(&file);
|
let runnables = runnables(&file);
|
||||||
dbg_eq(
|
assert_eq_dbg(
|
||||||
r#"[Runnable { range: [1; 13), kind: Bin },
|
r#"[Runnable { range: [1; 13), kind: Bin },
|
||||||
Runnable { range: [15; 39), kind: Test { name: "test_foo" } },
|
Runnable { range: [15; 39), kind: Test { name: "test_foo" } },
|
||||||
Runnable { range: [41; 75), kind: Test { name: "test_foo" } }]"#,
|
Runnable { range: [41; 75), kind: Test { name: "test_foo" } }]"#,
|
||||||
|
@ -86,7 +87,7 @@ impl E {}
|
||||||
impl fmt::Debug for E {}
|
impl fmt::Debug for E {}
|
||||||
"#);
|
"#);
|
||||||
let symbols = file_structure(&file);
|
let symbols = file_structure(&file);
|
||||||
dbg_eq(
|
assert_eq_dbg(
|
||||||
r#"[StructureNode { parent: None, label: "Foo", navigation_range: [8; 11), node_range: [1; 26), kind: STRUCT_DEF },
|
r#"[StructureNode { parent: None, label: "Foo", navigation_range: [8; 11), node_range: [1; 26), kind: STRUCT_DEF },
|
||||||
StructureNode { parent: Some(0), label: "x", navigation_range: [18; 19), node_range: [18; 24), kind: NAMED_FIELD },
|
StructureNode { parent: Some(0), label: "x", navigation_range: [18; 19), node_range: [18; 24), kind: NAMED_FIELD },
|
||||||
StructureNode { parent: None, label: "m", navigation_range: [32; 33), node_range: [28; 53), kind: MODULE },
|
StructureNode { parent: None, label: "m", navigation_range: [32; 33), node_range: [28; 53), kind: MODULE },
|
||||||
|
@ -147,12 +148,6 @@ fn file(text: &str) -> File {
|
||||||
File::parse(text)
|
File::parse(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dbg_eq(expected: &str, actual: &impl fmt::Debug) {
|
|
||||||
let actual = format!("{:?}", actual);
|
|
||||||
let expected = expected.lines().map(|l| l.trim()).join(" ");
|
|
||||||
assert_eq!(expected, actual);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn check_action<F: Fn(&File, TextUnit) -> Option<ActionResult>>(
|
fn check_action<F: Fn(&File, TextUnit) -> Option<ActionResult>>(
|
||||||
before: &str,
|
before: &str,
|
||||||
after: &str,
|
after: &str,
|
||||||
|
|
|
@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
unicode-xid = "0.1.0"
|
unicode-xid = "0.1.0"
|
||||||
text_unit = "0.1.2"
|
text_unit = "0.1.2"
|
||||||
itertools = "0.7.5"
|
itertools = "0.7.8"
|
||||||
drop_bomb = "0.1.4"
|
drop_bomb = "0.1.4"
|
||||||
parking_lot = "0.6.0"
|
parking_lot = "0.6.0"
|
||||||
smol_str = "0.1.0"
|
smol_str = "0.1.0"
|
||||||
|
|
Loading…
Reference in a new issue