extend module resolve to mod.rs

This commit is contained in:
Aleksey Kladov 2018-08-17 16:04:34 +03:00
parent 081c16c776
commit 41570f60bf
8 changed files with 82 additions and 19 deletions

View File

@ -5,3 +5,4 @@ authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
[dependencies]
difference = "2.0.0"
itertools = "0.7.8"

View File

@ -1,4 +1,9 @@
extern crate difference;
extern crate itertools;
use std::fmt;
use itertools::Itertools;
pub use self::difference::Changeset as __Changeset;
#[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);
}

View File

@ -12,3 +12,6 @@ rayon = "1.0.2"
fst = { git = "https://github.com/matklad/fst", branch = "subsequence"}
libsyntax2 = { path = "../libsyntax2" }
libeditor = { path = "../libeditor" }
[dev-dependencies]
assert_eq_text = { path = "../assert_eq_text" }

View File

@ -163,15 +163,21 @@ fn resolve_module(&self, id: FileId, module: ast::Module<&SyntaxRoot>) -> Vec<(F
Some(name) => name.text(),
None => return Vec::new(),
};
let id = match self.resolve_relative_path(id, &PathBuf::from(format!("../{}.rs", name))) {
Some(id) => id,
None => return Vec::new(),
};
vec![(id, FileSymbol {
name: name.clone(),
node_range: TextRange::offset_len(0.into(), 0.into()),
kind: MODULE,
})]
let paths = &[
PathBuf::from(format!("../{}.rs", name)),
PathBuf::from(format!("../{}/mod.rs", name)),
];
paths.iter()
.filter_map(|path| self.resolve_relative_path(id, path))
.map(|id| {
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> {

View 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,
);
}

View File

@ -10,4 +10,6 @@ superslice = "0.1.0"
libsyntax2 = { path = "../libsyntax2" }
smol_str = "0.1.0"
[dev-dependencies]
assert_eq_text = { path = "../assert_eq_text" }

View File

@ -6,6 +6,7 @@
use std::fmt;
use itertools::Itertools;
use assert_eq_text::{assert_eq_dbg};
use libeditor::{
File, TextUnit, TextRange, ActionResult, CursorPosition,
highlight, runnables, extend_selection, file_structure,
@ -33,7 +34,7 @@ fn main() {}
println!("Hello, {}!", 92);
"#);
let hls = highlight(&file);
dbg_eq(
assert_eq_dbg(
r#"[HighlightedRange { range: [1; 11), tag: "comment" },
HighlightedRange { range: [12; 14), tag: "keyword" },
HighlightedRange { range: [15; 19), tag: "function" },
@ -57,7 +58,7 @@ fn test_foo() {}
fn test_foo() {}
"#);
let runnables = runnables(&file);
dbg_eq(
assert_eq_dbg(
r#"[Runnable { range: [1; 13), kind: Bin },
Runnable { range: [15; 39), 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 {}
"#);
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 },
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 },
@ -147,12 +148,6 @@ fn file(text: &str) -> File {
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>>(
before: &str,
after: &str,

View File

@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0"
[dependencies]
unicode-xid = "0.1.0"
text_unit = "0.1.2"
itertools = "0.7.5"
itertools = "0.7.8"
drop_bomb = "0.1.4"
parking_lot = "0.6.0"
smol_str = "0.1.0"