mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 06:41:48 +00:00
Merge #7611
7611: Cleanups r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
12c7b66a7c
4 changed files with 63 additions and 49 deletions
|
@ -85,12 +85,16 @@ impl NavigationTarget {
|
||||||
let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
|
let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
|
||||||
if let Some(src) = module.declaration_source(db) {
|
if let Some(src) = module.declaration_source(db) {
|
||||||
let node = src.as_ref().map(|it| it.syntax());
|
let node = src.as_ref().map(|it| it.syntax());
|
||||||
let frange = node.original_file_range(db);
|
let full_range = node.original_file_range(db);
|
||||||
|
let focus_range = src
|
||||||
|
.value
|
||||||
|
.name()
|
||||||
|
.map(|name| src.with_value(name.syntax()).original_file_range(db).range);
|
||||||
let mut res = NavigationTarget::from_syntax(
|
let mut res = NavigationTarget::from_syntax(
|
||||||
frange.file_id,
|
full_range.file_id,
|
||||||
name,
|
name,
|
||||||
None,
|
focus_range,
|
||||||
frange.range,
|
full_range.range,
|
||||||
SymbolKind::Module,
|
SymbolKind::Module,
|
||||||
);
|
);
|
||||||
res.docs = module.attrs(db).docs();
|
res.docs = module.attrs(db).docs();
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
//! Utilities for creating `Analysis` instances for tests.
|
//! Utilities for creating `Analysis` instances for tests.
|
||||||
use ide_db::base_db::fixture::ChangeFixture;
|
use ide_db::base_db::fixture::ChangeFixture;
|
||||||
|
use syntax::{TextRange, TextSize};
|
||||||
use test_utils::{extract_annotations, RangeOrOffset};
|
use test_utils::{extract_annotations, RangeOrOffset};
|
||||||
|
|
||||||
use crate::{Analysis, AnalysisHost, FileId, FilePosition, FileRange};
|
use crate::{Analysis, AnalysisHost, FileId, FilePosition, FileRange};
|
||||||
|
@ -68,3 +69,18 @@ pub(crate) fn annotations(ra_fixture: &str) -> (Analysis, FilePosition, Vec<(Fil
|
||||||
.collect();
|
.collect();
|
||||||
(host.analysis(), FilePosition { file_id, offset }, annotations)
|
(host.analysis(), FilePosition { file_id, offset }, annotations)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn nav_target_annotation(ra_fixture: &str) -> (Analysis, FilePosition, FileRange) {
|
||||||
|
let (analysis, position, mut annotations) = annotations(ra_fixture);
|
||||||
|
let (mut expected, data) = annotations.pop().unwrap();
|
||||||
|
assert!(annotations.is_empty());
|
||||||
|
match data.as_str() {
|
||||||
|
"" => (),
|
||||||
|
"file" => {
|
||||||
|
expected.range =
|
||||||
|
TextRange::up_to(TextSize::of(&*analysis.file_text(expected.file_id).unwrap()))
|
||||||
|
}
|
||||||
|
data => panic!("bad data: {}", data),
|
||||||
|
}
|
||||||
|
(analysis, position, expected)
|
||||||
|
}
|
||||||
|
|
|
@ -131,22 +131,11 @@ pub(crate) fn reference_definition(
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use ide_db::base_db::FileRange;
|
use ide_db::base_db::FileRange;
|
||||||
use syntax::{TextRange, TextSize};
|
|
||||||
|
|
||||||
use crate::fixture;
|
use crate::fixture;
|
||||||
|
|
||||||
fn check(ra_fixture: &str) {
|
fn check(ra_fixture: &str) {
|
||||||
let (analysis, position, mut annotations) = fixture::annotations(ra_fixture);
|
let (analysis, position, expected) = fixture::nav_target_annotation(ra_fixture);
|
||||||
let (mut expected, data) = annotations.pop().unwrap();
|
|
||||||
match data.as_str() {
|
|
||||||
"" => (),
|
|
||||||
"file" => {
|
|
||||||
expected.range =
|
|
||||||
TextRange::up_to(TextSize::of(&*analysis.file_text(expected.file_id).unwrap()))
|
|
||||||
}
|
|
||||||
data => panic!("bad data: {}", data),
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut navs =
|
let mut navs =
|
||||||
analysis.goto_definition(position).unwrap().expect("no definition found").info;
|
analysis.goto_definition(position).unwrap().expect("no definition found").info;
|
||||||
if navs.len() == 0 {
|
if navs.len() == 0 {
|
||||||
|
|
|
@ -63,57 +63,62 @@ pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use ide_db::base_db::FileRange;
|
||||||
use test_utils::mark;
|
use test_utils::mark;
|
||||||
|
|
||||||
use crate::fixture::{self};
|
use crate::fixture;
|
||||||
|
|
||||||
|
fn check(ra_fixture: &str) {
|
||||||
|
let (analysis, position, expected) = fixture::nav_target_annotation(ra_fixture);
|
||||||
|
let mut navs = analysis.parent_module(position).unwrap();
|
||||||
|
assert_eq!(navs.len(), 1);
|
||||||
|
let nav = navs.pop().unwrap();
|
||||||
|
assert_eq!(expected, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() });
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_resolve_parent_module() {
|
fn test_resolve_parent_module() {
|
||||||
let (analysis, pos) = fixture::position(
|
check(
|
||||||
"
|
r#"
|
||||||
//- /lib.rs
|
//- /lib.rs
|
||||||
mod foo;
|
mod foo;
|
||||||
//- /foo.rs
|
//^^^
|
||||||
$0// empty
|
|
||||||
",
|
//- /foo.rs
|
||||||
|
$0// empty
|
||||||
|
"#,
|
||||||
);
|
);
|
||||||
let nav = analysis.parent_module(pos).unwrap().pop().unwrap();
|
|
||||||
nav.assert_match("foo Module FileId(0) 0..8");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_resolve_parent_module_on_module_decl() {
|
fn test_resolve_parent_module_on_module_decl() {
|
||||||
mark::check!(test_resolve_parent_module_on_module_decl);
|
mark::check!(test_resolve_parent_module_on_module_decl);
|
||||||
let (analysis, pos) = fixture::position(
|
check(
|
||||||
"
|
r#"
|
||||||
//- /lib.rs
|
//- /lib.rs
|
||||||
mod foo;
|
mod foo;
|
||||||
|
//^^^
|
||||||
|
//- /foo.rs
|
||||||
|
mod $0bar;
|
||||||
|
|
||||||
//- /foo.rs
|
//- /foo/bar.rs
|
||||||
mod $0bar;
|
// empty
|
||||||
|
"#,
|
||||||
//- /foo/bar.rs
|
|
||||||
// empty
|
|
||||||
",
|
|
||||||
);
|
);
|
||||||
let nav = analysis.parent_module(pos).unwrap().pop().unwrap();
|
|
||||||
nav.assert_match("foo Module FileId(0) 0..8");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_resolve_parent_module_for_inline() {
|
fn test_resolve_parent_module_for_inline() {
|
||||||
let (analysis, pos) = fixture::position(
|
check(
|
||||||
"
|
r#"
|
||||||
//- /lib.rs
|
//- /lib.rs
|
||||||
mod foo {
|
mod foo {
|
||||||
mod bar {
|
mod bar {
|
||||||
mod baz { $0 }
|
mod baz { $0 }
|
||||||
}
|
} //^^^
|
||||||
}
|
}
|
||||||
",
|
"#,
|
||||||
);
|
);
|
||||||
let nav = analysis.parent_module(pos).unwrap().pop().unwrap();
|
|
||||||
nav.assert_match("baz Module FileId(0) 32..44");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue