mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 05:45:12 +00:00
fix tests
This commit is contained in:
parent
8a5f74a24f
commit
dda916bc4d
8 changed files with 108 additions and 98 deletions
|
@ -85,31 +85,32 @@ fn name_definition(
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use test_utils::assert_eq_dbg;
|
||||
use crate::mock_analysis::analysis_and_position;
|
||||
|
||||
fn check_goto(fixuture: &str, expected: &str) {
|
||||
let (analysis, pos) = analysis_and_position(fixuture);
|
||||
|
||||
let mut navs = analysis.goto_definition(pos).unwrap().unwrap().info;
|
||||
assert_eq!(navs.len(), 1);
|
||||
let nav = navs.pop().unwrap();
|
||||
nav.assert_match(expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_definition_works_in_items() {
|
||||
let (analysis, pos) = analysis_and_position(
|
||||
check_goto(
|
||||
"
|
||||
//- /lib.rs
|
||||
struct Foo;
|
||||
enum E { X(Foo<|>) }
|
||||
",
|
||||
);
|
||||
|
||||
let symbols = analysis.goto_definition(pos).unwrap().unwrap();
|
||||
assert_eq_dbg(
|
||||
r#"[NavigationTarget { file_id: FileId(1), name: "Foo",
|
||||
kind: STRUCT_DEF, range: [0; 11),
|
||||
ptr: Some(LocalSyntaxPtr { range: [0; 11), kind: STRUCT_DEF }) }]"#,
|
||||
&symbols,
|
||||
"Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_definition_resolves_correct_name() {
|
||||
let (analysis, pos) = analysis_and_position(
|
||||
check_goto(
|
||||
"
|
||||
//- /lib.rs
|
||||
use a::Foo;
|
||||
|
@ -121,47 +122,30 @@ mod tests {
|
|||
//- /b.rs
|
||||
struct Foo;
|
||||
",
|
||||
);
|
||||
|
||||
let symbols = analysis.goto_definition(pos).unwrap().unwrap();
|
||||
assert_eq_dbg(
|
||||
r#"[NavigationTarget { file_id: FileId(2), name: "Foo",
|
||||
kind: STRUCT_DEF, range: [0; 11),
|
||||
ptr: Some(LocalSyntaxPtr { range: [0; 11), kind: STRUCT_DEF }) }]"#,
|
||||
&symbols,
|
||||
"Foo STRUCT_DEF FileId(2) [0; 11) [7; 10)",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_definition_works_for_module_declaration() {
|
||||
let (analysis, pos) = analysis_and_position(
|
||||
check_goto(
|
||||
"
|
||||
//- /lib.rs
|
||||
mod <|>foo;
|
||||
//- /foo.rs
|
||||
// empty
|
||||
",
|
||||
",
|
||||
"foo SOURCE_FILE FileId(2) [0; 10)",
|
||||
);
|
||||
|
||||
let symbols = analysis.goto_definition(pos).unwrap().unwrap();
|
||||
assert_eq_dbg(
|
||||
r#"[NavigationTarget { file_id: FileId(2), name: "foo", kind: MODULE, range: [0; 0), ptr: None }]"#,
|
||||
&symbols,
|
||||
);
|
||||
|
||||
let (analysis, pos) = analysis_and_position(
|
||||
check_goto(
|
||||
"
|
||||
//- /lib.rs
|
||||
mod <|>foo;
|
||||
//- /foo/mod.rs
|
||||
// empty
|
||||
",
|
||||
);
|
||||
|
||||
let symbols = analysis.goto_definition(pos).unwrap().unwrap();
|
||||
assert_eq_dbg(
|
||||
r#"[NavigationTarget { file_id: FileId(2), name: "foo", kind: MODULE, range: [0; 0), ptr: None }]"#,
|
||||
&symbols,
|
||||
",
|
||||
"foo SOURCE_FILE FileId(2) [0; 10)",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -92,7 +92,7 @@ impl NavigationTarget {
|
|||
let source_file = source_file.syntax();
|
||||
let node = source_file
|
||||
.descendants()
|
||||
.find(|node| node.kind() == self.kind() && node.range() == self.range())?
|
||||
.find(|node| node.kind() == self.kind() && node.range() == self.full_range())?
|
||||
.to_owned();
|
||||
Some(node)
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ use ra_syntax::{
|
|||
|
||||
use crate::{
|
||||
AnalysisChange,
|
||||
Cancelable, NavigationTarget,
|
||||
Cancelable,
|
||||
CrateId, db, Diagnostic, FileId, FilePosition, FileRange, FileSystemEdit,
|
||||
Query, RootChange, SourceChange, SourceFileEdit,
|
||||
symbol_index::{LibrarySymbolsQuery, FileSymbol},
|
||||
|
@ -98,19 +98,6 @@ impl db::RootDatabase {
|
|||
}
|
||||
|
||||
impl db::RootDatabase {
|
||||
/// This returns `Vec` because a module may be included from several places. We
|
||||
/// don't handle this case yet though, so the Vec has length at most one.
|
||||
pub(crate) fn parent_module(
|
||||
&self,
|
||||
position: FilePosition,
|
||||
) -> Cancelable<Vec<NavigationTarget>> {
|
||||
let module = match source_binder::module_from_position(self, position)? {
|
||||
None => return Ok(Vec::new()),
|
||||
Some(it) => it,
|
||||
};
|
||||
let nav = NavigationTarget::from_module(self, module)?;
|
||||
Ok(vec![nav])
|
||||
}
|
||||
/// Returns `Vec` for the same reason as `parent_module`
|
||||
pub(crate) fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
|
||||
let module = match source_binder::module_from_file_id(self, file_id)? {
|
||||
|
|
|
@ -31,6 +31,7 @@ mod extend_selection;
|
|||
mod hover;
|
||||
mod call_info;
|
||||
mod syntax_highlighting;
|
||||
mod parent_module;
|
||||
|
||||
use std::{fmt, sync::Arc};
|
||||
|
||||
|
@ -414,7 +415,7 @@ impl Analysis {
|
|||
|
||||
/// Returns a `mod name;` declaration which created the current module.
|
||||
pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<NavigationTarget>> {
|
||||
self.with_db(|db| db.parent_module(position))?
|
||||
self.with_db(|db| parent_module::parent_module(db, position))?
|
||||
}
|
||||
|
||||
/// Returns crates this file belongs too.
|
||||
|
|
|
@ -17,7 +17,7 @@ pub struct NavigationTarget {
|
|||
file_id: FileId,
|
||||
name: SmolStr,
|
||||
kind: SyntaxKind,
|
||||
range: TextRange,
|
||||
full_range: TextRange,
|
||||
focus_range: Option<TextRange>,
|
||||
// Should be DefId ideally
|
||||
ptr: Option<LocalSyntaxPtr>,
|
||||
|
@ -36,11 +36,11 @@ impl NavigationTarget {
|
|||
self.file_id
|
||||
}
|
||||
|
||||
pub fn range(&self) -> TextRange {
|
||||
self.range
|
||||
pub fn full_range(&self) -> TextRange {
|
||||
self.full_range
|
||||
}
|
||||
|
||||
/// A "most interesting" range withing the `range`.
|
||||
/// A "most interesting" range withing the `range_full`.
|
||||
///
|
||||
/// Typically, `range` is the whole syntax node, including doc comments, and
|
||||
/// `focus_range` is the range of the identifier.
|
||||
|
@ -53,7 +53,7 @@ impl NavigationTarget {
|
|||
file_id: symbol.file_id,
|
||||
name: symbol.name.clone(),
|
||||
kind: symbol.ptr.kind(),
|
||||
range: symbol.ptr.range(),
|
||||
full_range: symbol.ptr.range(),
|
||||
focus_range: None,
|
||||
ptr: Some(symbol.ptr.clone()),
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ impl NavigationTarget {
|
|||
NavigationTarget {
|
||||
file_id,
|
||||
name: entry.name().to_string().into(),
|
||||
range: entry.ptr().range(),
|
||||
full_range: entry.ptr().range(),
|
||||
focus_range: None,
|
||||
kind: NAME,
|
||||
ptr: None,
|
||||
|
@ -118,6 +118,27 @@ impl NavigationTarget {
|
|||
Ok(Some(res))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn assert_match(&self, expected: &str) {
|
||||
let actual = self.debug_render();
|
||||
test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn debug_render(&self) -> String {
|
||||
let mut buf = format!(
|
||||
"{} {:?} {:?} {:?}",
|
||||
self.name(),
|
||||
self.kind(),
|
||||
self.file_id(),
|
||||
self.full_range()
|
||||
);
|
||||
if let Some(focus_range) = self.focus_range() {
|
||||
buf.push_str(&format!(" {:?}", focus_range))
|
||||
}
|
||||
buf
|
||||
}
|
||||
|
||||
fn from_named(file_id: FileId, node: &impl ast::NameOwner) -> NavigationTarget {
|
||||
let name = node.name().map(|it| it.text().clone()).unwrap_or_default();
|
||||
let focus_range = node.name().map(|it| it.syntax().range());
|
||||
|
@ -134,7 +155,7 @@ impl NavigationTarget {
|
|||
file_id,
|
||||
name,
|
||||
kind: node.kind(),
|
||||
range: node.range(),
|
||||
full_range: node.range(),
|
||||
focus_range,
|
||||
ptr: Some(LocalSyntaxPtr::new(node)),
|
||||
}
|
||||
|
|
52
crates/ra_ide_api/src/parent_module.rs
Normal file
52
crates/ra_ide_api/src/parent_module.rs
Normal file
|
@ -0,0 +1,52 @@
|
|||
use ra_db::{Cancelable, FilePosition};
|
||||
|
||||
use crate::{NavigationTarget, db::RootDatabase};
|
||||
|
||||
/// This returns `Vec` because a module may be included from several places. We
|
||||
/// don't handle this case yet though, so the Vec has length at most one.
|
||||
pub(crate) fn parent_module(
|
||||
db: &RootDatabase,
|
||||
position: FilePosition,
|
||||
) -> Cancelable<Vec<NavigationTarget>> {
|
||||
let module = match hir::source_binder::module_from_position(db, position)? {
|
||||
None => return Ok(Vec::new()),
|
||||
Some(it) => it,
|
||||
};
|
||||
let nav = NavigationTarget::from_module(db, module)?;
|
||||
Ok(vec![nav])
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::mock_analysis::analysis_and_position;
|
||||
|
||||
#[test]
|
||||
fn test_resolve_parent_module() {
|
||||
let (analysis, pos) = analysis_and_position(
|
||||
"
|
||||
//- /lib.rs
|
||||
mod foo;
|
||||
//- /foo.rs
|
||||
<|>// empty
|
||||
",
|
||||
);
|
||||
let nav = analysis.parent_module(pos).unwrap().pop().unwrap();
|
||||
nav.assert_match("foo SOURCE_FILE FileId(2) [0; 10)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resolve_parent_module_for_inline() {
|
||||
let (analysis, pos) = analysis_and_position(
|
||||
"
|
||||
//- /lib.rs
|
||||
mod foo {
|
||||
mod bar {
|
||||
mod baz { <|> }
|
||||
}
|
||||
}
|
||||
",
|
||||
);
|
||||
let nav = analysis.parent_module(pos).unwrap().pop().unwrap();
|
||||
nav.assert_match("baz MODULE FileId(1) [32; 44)");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue