Unclutter NavigationTarget API

This commit is contained in:
Aleksey Kladov 2020-07-17 12:42:48 +02:00
parent f157a0983a
commit d7548a36a7
11 changed files with 166 additions and 198 deletions

View file

@ -154,7 +154,8 @@ mod tests {
let nav = navs.pop().unwrap(); let nav = navs.pop().unwrap();
nav.assert_match(expected); nav.assert_match(expected);
let item_pos = FilePosition { file_id: nav.file_id(), offset: nav.range().start() }; let item_pos =
FilePosition { file_id: nav.file_id, offset: nav.focus_or_full_range().start() };
let incoming_calls = analysis.incoming_calls(item_pos).unwrap().unwrap(); let incoming_calls = analysis.incoming_calls(item_pos).unwrap().unwrap();
assert_eq!(incoming_calls.len(), expected_incoming.len()); assert_eq!(incoming_calls.len(), expected_incoming.len());

View file

@ -22,15 +22,28 @@ use super::short_label::ShortLabel;
/// code, like a function or a struct, but this is not strictly required. /// code, like a function or a struct, but this is not strictly required.
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct NavigationTarget { pub struct NavigationTarget {
// FIXME: use FileRange? pub file_id: FileId,
file_id: FileId, /// Range which encompasses the whole element.
full_range: TextRange, ///
name: SmolStr, /// Should include body, doc comments, attributes, etc.
kind: SyntaxKind, ///
focus_range: Option<TextRange>, /// Clients should use this range to answer "is the cursor inside the
container_name: Option<SmolStr>, /// element?" question.
description: Option<String>, pub full_range: TextRange,
docs: Option<String>, /// A "most interesting" range withing the `full_range`.
///
/// Typically, `full_range` is the whole syntax node, including doc
/// comments, and `focus_range` is the range of the identifier. "Most
/// interesting" range within the full range, typically the range of
/// identifier.
///
/// Clients should place the cursor on this range when navigating to this target.
pub focus_range: Option<TextRange>,
pub name: SmolStr,
pub kind: SyntaxKind,
pub container_name: Option<SmolStr>,
pub description: Option<String>,
pub docs: Option<String>,
} }
pub(crate) trait ToNav { pub(crate) trait ToNav {
@ -42,44 +55,9 @@ pub(crate) trait TryToNav {
} }
impl NavigationTarget { impl NavigationTarget {
/// When `focus_range` is specified, returns it. otherwise pub fn focus_or_full_range(&self) -> TextRange {
/// returns `full_range`
pub fn range(&self) -> TextRange {
self.focus_range.unwrap_or(self.full_range) self.focus_range.unwrap_or(self.full_range)
} }
/// A "most interesting" range withing the `full_range`.
///
/// Typically, `full_range` is the whole syntax node,
/// including doc comments, and `focus_range` is the range of the identifier.
pub fn focus_range(&self) -> Option<TextRange> {
self.focus_range
}
pub fn full_range(&self) -> TextRange {
self.full_range
}
pub fn file_id(&self) -> FileId {
self.file_id
}
pub fn name(&self) -> &SmolStr {
&self.name
}
pub fn container_name(&self) -> Option<&SmolStr> {
self.container_name.as_ref()
}
pub fn kind(&self) -> SyntaxKind {
self.kind
}
pub fn docs(&self) -> Option<&str> {
self.docs.as_deref()
}
pub fn description(&self) -> Option<&str> {
self.description.as_deref()
}
pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget { pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> 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();
@ -107,17 +85,12 @@ impl NavigationTarget {
#[cfg(test)] #[cfg(test)]
pub(crate) fn debug_render(&self) -> String { pub(crate) fn debug_render(&self) -> String {
let mut buf = format!( let mut buf =
"{} {:?} {:?} {:?}", format!("{} {:?} {:?} {:?}", self.name, self.kind, self.file_id, self.full_range);
self.name(), if let Some(focus_range) = self.focus_range {
self.kind(),
self.file_id(),
self.full_range()
);
if let Some(focus_range) = self.focus_range() {
buf.push_str(&format!(" {:?}", focus_range)) buf.push_str(&format!(" {:?}", focus_range))
} }
if let Some(container_name) = self.container_name() { if let Some(container_name) = &self.container_name {
buf.push_str(&format!(" {}", container_name)) buf.push_str(&format!(" {}", container_name))
} }
buf buf

View file

@ -130,7 +130,7 @@ mod tests {
assert_eq!(navs.len(), 1); assert_eq!(navs.len(), 1);
let nav = navs.pop().unwrap(); let nav = navs.pop().unwrap();
assert_eq!(expected, FileRange { file_id: nav.file_id(), range: nav.range() }); assert_eq!(expected, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() });
} }
#[test] #[test]

View file

@ -98,7 +98,7 @@ mod tests {
let mut actual = navs let mut actual = navs
.into_iter() .into_iter()
.map(|nav| FileRange { file_id: nav.file_id(), range: nav.range() }) .map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
actual.sort_by_key(key); actual.sort_by_key(key);

View file

@ -67,7 +67,7 @@ mod tests {
let mut navs = analysis.goto_type_definition(position).unwrap().unwrap().info; let mut navs = analysis.goto_type_definition(position).unwrap().unwrap().info;
assert_eq!(navs.len(), 1); assert_eq!(navs.len(), 1);
let nav = navs.pop().unwrap(); let nav = navs.pop().unwrap();
assert_eq!(expected, FileRange { file_id: nav.file_id(), range: nav.range() }); assert_eq!(expected, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() });
} }
#[test] #[test]

View file

@ -133,8 +133,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> { fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
fn to_action(nav_target: NavigationTarget) -> HoverAction { fn to_action(nav_target: NavigationTarget) -> HoverAction {
HoverAction::Implementaion(FilePosition { HoverAction::Implementaion(FilePosition {
file_id: nav_target.file_id(), file_id: nav_target.file_id,
offset: nav_target.range().start(), offset: nav_target.focus_or_full_range().start(),
}) })
} }
@ -1357,11 +1357,11 @@ fn foo_<|>test() {}
1, 1,
), ),
full_range: 0..24, full_range: 0..24,
name: "foo_test",
kind: FN_DEF,
focus_range: Some( focus_range: Some(
11..19, 11..19,
), ),
name: "foo_test",
kind: FN_DEF,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,
@ -1400,11 +1400,11 @@ mod tests<|> {
1, 1,
), ),
full_range: 0..46, full_range: 0..46,
name: "tests",
kind: MODULE,
focus_range: Some( focus_range: Some(
4..9, 4..9,
), ),
name: "tests",
kind: MODULE,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,
@ -1439,11 +1439,11 @@ fn main() { let s<|>t = S{ f1:0 }; }
1, 1,
), ),
full_range: 0..19, full_range: 0..19,
name: "S",
kind: STRUCT_DEF,
focus_range: Some( focus_range: Some(
7..8, 7..8,
), ),
name: "S",
kind: STRUCT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"struct S", "struct S",
@ -1478,11 +1478,11 @@ fn main() { let s<|>t = S{ f1:Arg(0) }; }
1, 1,
), ),
full_range: 17..37, full_range: 17..37,
name: "S",
kind: STRUCT_DEF,
focus_range: Some( focus_range: Some(
24..25, 24..25,
), ),
name: "S",
kind: STRUCT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"struct S", "struct S",
@ -1497,11 +1497,11 @@ fn main() { let s<|>t = S{ f1:Arg(0) }; }
1, 1,
), ),
full_range: 0..16, full_range: 0..16,
name: "Arg",
kind: STRUCT_DEF,
focus_range: Some( focus_range: Some(
7..10, 7..10,
), ),
name: "Arg",
kind: STRUCT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"struct Arg", "struct Arg",
@ -1536,11 +1536,11 @@ fn main() { let s<|>t = S{ f1: S{ f1: Arg(0) } }; }
1, 1,
), ),
full_range: 17..37, full_range: 17..37,
name: "S",
kind: STRUCT_DEF,
focus_range: Some( focus_range: Some(
24..25, 24..25,
), ),
name: "S",
kind: STRUCT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"struct S", "struct S",
@ -1555,11 +1555,11 @@ fn main() { let s<|>t = S{ f1: S{ f1: Arg(0) } }; }
1, 1,
), ),
full_range: 0..16, full_range: 0..16,
name: "Arg",
kind: STRUCT_DEF,
focus_range: Some( focus_range: Some(
7..10, 7..10,
), ),
name: "Arg",
kind: STRUCT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"struct Arg", "struct Arg",
@ -1597,11 +1597,11 @@ fn main() { let s<|>t = (A(1), B(2), M::C(3) ); }
1, 1,
), ),
full_range: 0..14, full_range: 0..14,
name: "A",
kind: STRUCT_DEF,
focus_range: Some( focus_range: Some(
7..8, 7..8,
), ),
name: "A",
kind: STRUCT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"struct A", "struct A",
@ -1616,11 +1616,11 @@ fn main() { let s<|>t = (A(1), B(2), M::C(3) ); }
1, 1,
), ),
full_range: 15..29, full_range: 15..29,
name: "B",
kind: STRUCT_DEF,
focus_range: Some( focus_range: Some(
22..23, 22..23,
), ),
name: "B",
kind: STRUCT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"struct B", "struct B",
@ -1635,11 +1635,11 @@ fn main() { let s<|>t = (A(1), B(2), M::C(3) ); }
1, 1,
), ),
full_range: 42..60, full_range: 42..60,
name: "C",
kind: STRUCT_DEF,
focus_range: Some( focus_range: Some(
53..54, 53..54,
), ),
name: "C",
kind: STRUCT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"pub struct C", "pub struct C",
@ -1674,11 +1674,11 @@ fn main() { let s<|>t = foo(); }
1, 1,
), ),
full_range: 0..12, full_range: 0..12,
name: "Foo",
kind: TRAIT_DEF,
focus_range: Some( focus_range: Some(
6..9, 6..9,
), ),
name: "Foo",
kind: TRAIT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"trait Foo", "trait Foo",
@ -1714,11 +1714,11 @@ fn main() { let s<|>t = foo(); }
1, 1,
), ),
full_range: 0..15, full_range: 0..15,
name: "Foo",
kind: TRAIT_DEF,
focus_range: Some( focus_range: Some(
6..9, 6..9,
), ),
name: "Foo",
kind: TRAIT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"trait Foo", "trait Foo",
@ -1733,11 +1733,11 @@ fn main() { let s<|>t = foo(); }
1, 1,
), ),
full_range: 16..25, full_range: 16..25,
name: "S",
kind: STRUCT_DEF,
focus_range: Some( focus_range: Some(
23..24, 23..24,
), ),
name: "S",
kind: STRUCT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"struct S", "struct S",
@ -1773,11 +1773,11 @@ fn main() { let s<|>t = foo(); }
1, 1,
), ),
full_range: 0..12, full_range: 0..12,
name: "Foo",
kind: TRAIT_DEF,
focus_range: Some( focus_range: Some(
6..9, 6..9,
), ),
name: "Foo",
kind: TRAIT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"trait Foo", "trait Foo",
@ -1792,11 +1792,11 @@ fn main() { let s<|>t = foo(); }
1, 1,
), ),
full_range: 13..25, full_range: 13..25,
name: "Bar",
kind: TRAIT_DEF,
focus_range: Some( focus_range: Some(
19..22, 19..22,
), ),
name: "Bar",
kind: TRAIT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"trait Bar", "trait Bar",
@ -1835,11 +1835,11 @@ fn main() { let s<|>t = foo(); }
1, 1,
), ),
full_range: 0..15, full_range: 0..15,
name: "Foo",
kind: TRAIT_DEF,
focus_range: Some( focus_range: Some(
6..9, 6..9,
), ),
name: "Foo",
kind: TRAIT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"trait Foo", "trait Foo",
@ -1854,11 +1854,11 @@ fn main() { let s<|>t = foo(); }
1, 1,
), ),
full_range: 16..31, full_range: 16..31,
name: "Bar",
kind: TRAIT_DEF,
focus_range: Some( focus_range: Some(
22..25, 22..25,
), ),
name: "Bar",
kind: TRAIT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"trait Bar", "trait Bar",
@ -1873,11 +1873,11 @@ fn main() { let s<|>t = foo(); }
1, 1,
), ),
full_range: 32..44, full_range: 32..44,
name: "S1",
kind: STRUCT_DEF,
focus_range: Some( focus_range: Some(
39..41, 39..41,
), ),
name: "S1",
kind: STRUCT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"struct S1", "struct S1",
@ -1892,11 +1892,11 @@ fn main() { let s<|>t = foo(); }
1, 1,
), ),
full_range: 45..57, full_range: 45..57,
name: "S2",
kind: STRUCT_DEF,
focus_range: Some( focus_range: Some(
52..54, 52..54,
), ),
name: "S2",
kind: STRUCT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"struct S2", "struct S2",
@ -1929,11 +1929,11 @@ fn foo(ar<|>g: &impl Foo) {}
1, 1,
), ),
full_range: 0..12, full_range: 0..12,
name: "Foo",
kind: TRAIT_DEF,
focus_range: Some( focus_range: Some(
6..9, 6..9,
), ),
name: "Foo",
kind: TRAIT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"trait Foo", "trait Foo",
@ -1969,11 +1969,11 @@ fn foo(ar<|>g: &impl Foo + Bar<S>) {}
1, 1,
), ),
full_range: 0..12, full_range: 0..12,
name: "Foo",
kind: TRAIT_DEF,
focus_range: Some( focus_range: Some(
6..9, 6..9,
), ),
name: "Foo",
kind: TRAIT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"trait Foo", "trait Foo",
@ -1988,11 +1988,11 @@ fn foo(ar<|>g: &impl Foo + Bar<S>) {}
1, 1,
), ),
full_range: 13..28, full_range: 13..28,
name: "Bar",
kind: TRAIT_DEF,
focus_range: Some( focus_range: Some(
19..22, 19..22,
), ),
name: "Bar",
kind: TRAIT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"trait Bar", "trait Bar",
@ -2007,11 +2007,11 @@ fn foo(ar<|>g: &impl Foo + Bar<S>) {}
1, 1,
), ),
full_range: 29..39, full_range: 29..39,
name: "S",
kind: STRUCT_DEF,
focus_range: Some( focus_range: Some(
36..37, 36..37,
), ),
name: "S",
kind: STRUCT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"struct S", "struct S",
@ -2045,11 +2045,11 @@ fn foo(ar<|>g: &impl Foo<S>) {}
1, 1,
), ),
full_range: 0..15, full_range: 0..15,
name: "Foo",
kind: TRAIT_DEF,
focus_range: Some( focus_range: Some(
6..9, 6..9,
), ),
name: "Foo",
kind: TRAIT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"trait Foo", "trait Foo",
@ -2064,11 +2064,11 @@ fn foo(ar<|>g: &impl Foo<S>) {}
1, 1,
), ),
full_range: 16..27, full_range: 16..27,
name: "S",
kind: STRUCT_DEF,
focus_range: Some( focus_range: Some(
23..24, 23..24,
), ),
name: "S",
kind: STRUCT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"struct S", "struct S",
@ -2107,11 +2107,11 @@ fn main() { let s<|>t = foo(); }
1, 1,
), ),
full_range: 42..55, full_range: 42..55,
name: "B",
kind: STRUCT_DEF,
focus_range: Some( focus_range: Some(
49..50, 49..50,
), ),
name: "B",
kind: STRUCT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"struct B", "struct B",
@ -2126,11 +2126,11 @@ fn main() { let s<|>t = foo(); }
1, 1,
), ),
full_range: 0..12, full_range: 0..12,
name: "Foo",
kind: TRAIT_DEF,
focus_range: Some( focus_range: Some(
6..9, 6..9,
), ),
name: "Foo",
kind: TRAIT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"trait Foo", "trait Foo",
@ -2163,11 +2163,11 @@ fn foo(ar<|>g: &dyn Foo) {}
1, 1,
), ),
full_range: 0..12, full_range: 0..12,
name: "Foo",
kind: TRAIT_DEF,
focus_range: Some( focus_range: Some(
6..9, 6..9,
), ),
name: "Foo",
kind: TRAIT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"trait Foo", "trait Foo",
@ -2201,11 +2201,11 @@ fn foo(ar<|>g: &dyn Foo<S>) {}
1, 1,
), ),
full_range: 0..15, full_range: 0..15,
name: "Foo",
kind: TRAIT_DEF,
focus_range: Some( focus_range: Some(
6..9, 6..9,
), ),
name: "Foo",
kind: TRAIT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"trait Foo", "trait Foo",
@ -2220,11 +2220,11 @@ fn foo(ar<|>g: &dyn Foo<S>) {}
1, 1,
), ),
full_range: 16..27, full_range: 16..27,
name: "S",
kind: STRUCT_DEF,
focus_range: Some( focus_range: Some(
23..24, 23..24,
), ),
name: "S",
kind: STRUCT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"struct S", "struct S",
@ -2261,11 +2261,11 @@ fn foo(a<|>rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
1, 1,
), ),
full_range: 0..21, full_range: 0..21,
name: "ImplTrait",
kind: TRAIT_DEF,
focus_range: Some( focus_range: Some(
6..15, 6..15,
), ),
name: "ImplTrait",
kind: TRAIT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"trait ImplTrait", "trait ImplTrait",
@ -2280,11 +2280,11 @@ fn foo(a<|>rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
1, 1,
), ),
full_range: 43..57, full_range: 43..57,
name: "B",
kind: STRUCT_DEF,
focus_range: Some( focus_range: Some(
50..51, 50..51,
), ),
name: "B",
kind: STRUCT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"struct B", "struct B",
@ -2299,11 +2299,11 @@ fn foo(a<|>rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
1, 1,
), ),
full_range: 22..42, full_range: 22..42,
name: "DynTrait",
kind: TRAIT_DEF,
focus_range: Some( focus_range: Some(
28..36, 28..36,
), ),
name: "DynTrait",
kind: TRAIT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"trait DynTrait", "trait DynTrait",
@ -2318,11 +2318,11 @@ fn foo(a<|>rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
1, 1,
), ),
full_range: 58..69, full_range: 58..69,
name: "S",
kind: STRUCT_DEF,
focus_range: Some( focus_range: Some(
65..66, 65..66,
), ),
name: "S",
kind: STRUCT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"struct S", "struct S",
@ -2366,11 +2366,11 @@ fn main() { let s<|>t = test().get(); }
1, 1,
), ),
full_range: 0..62, full_range: 0..62,
name: "Foo",
kind: TRAIT_DEF,
focus_range: Some( focus_range: Some(
6..9, 6..9,
), ),
name: "Foo",
kind: TRAIT_DEF,
container_name: None, container_name: None,
description: Some( description: Some(
"trait Foo", "trait Foo",

View file

@ -545,8 +545,8 @@ mod tests {
let s = symbols.pop().unwrap(); let s = symbols.pop().unwrap();
assert_eq!(s.name(), "FooInner"); assert_eq!(s.name, "FooInner");
assert!(s.container_name().is_none()); assert!(s.container_name.is_none());
} }
#[test] #[test]
@ -561,8 +561,8 @@ fn foo() {
let s = symbols.pop().unwrap(); let s = symbols.pop().unwrap();
assert_eq!(s.name(), "FooInner"); assert_eq!(s.name, "FooInner");
assert_eq!(s.container_name(), Some(&SmolStr::new("foo"))); assert_eq!(s.container_name, Some(SmolStr::new("foo")));
let code = r#" let code = r#"
mod foo { mod foo {
@ -574,8 +574,8 @@ mod foo {
let s = symbols.pop().unwrap(); let s = symbols.pop().unwrap();
assert_eq!(s.name(), "FooInner"); assert_eq!(s.name, "FooInner");
assert_eq!(s.container_name(), Some(&SmolStr::new("foo"))); assert_eq!(s.container_name, Some(SmolStr::new("foo")));
} }
#[test] #[test]
@ -588,8 +588,8 @@ struct Foo;
let symbols = get_symbols_matching(code, "Foo"); let symbols = get_symbols_matching(code, "Foo");
let fn_match = symbols.iter().find(|s| s.name() == "foo").map(|s| s.kind()); let fn_match = symbols.iter().find(|s| s.name == "foo").map(|s| s.kind);
let struct_match = symbols.iter().find(|s| s.name() == "Foo").map(|s| s.kind()); let struct_match = symbols.iter().find(|s| s.name == "Foo").map(|s| s.kind);
assert_eq!(fn_match, Some(FN_DEF)); assert_eq!(fn_match, Some(FN_DEF));
assert_eq!(struct_match, Some(STRUCT_DEF)); assert_eq!(struct_match, Some(STRUCT_DEF));

View file

@ -74,8 +74,8 @@ impl IntoIterator for ReferenceSearchResult {
let mut v = Vec::with_capacity(self.len()); let mut v = Vec::with_capacity(self.len());
v.push(Reference { v.push(Reference {
file_range: FileRange { file_range: FileRange {
file_id: self.declaration.nav.file_id(), file_id: self.declaration.nav.file_id,
range: self.declaration.nav.range(), range: self.declaration.nav.focus_or_full_range(),
}, },
kind: self.declaration.kind, kind: self.declaration.kind,
access: self.declaration.access, access: self.declaration.access,
@ -112,7 +112,7 @@ pub(crate) fn find_all_refs(
.filter(|r| search_kind == ReferenceKind::Other || search_kind == r.kind) .filter(|r| search_kind == ReferenceKind::Other || search_kind == r.kind)
.collect(); .collect();
let decl_range = def.try_to_nav(sema.db)?.range(); let decl_range = def.try_to_nav(sema.db)?.focus_or_full_range();
let declaration = Declaration { let declaration = Declaration {
nav: def.try_to_nav(sema.db)?, nav: def.try_to_nav(sema.db)?,

View file

@ -299,11 +299,11 @@ fn bench() {}
1, 1,
), ),
full_range: 1..13, full_range: 1..13,
name: "main",
kind: FN_DEF,
focus_range: Some( focus_range: Some(
4..8, 4..8,
), ),
name: "main",
kind: FN_DEF,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,
@ -317,11 +317,11 @@ fn bench() {}
1, 1,
), ),
full_range: 15..39, full_range: 15..39,
name: "test_foo",
kind: FN_DEF,
focus_range: Some( focus_range: Some(
26..34, 26..34,
), ),
name: "test_foo",
kind: FN_DEF,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,
@ -342,11 +342,11 @@ fn bench() {}
1, 1,
), ),
full_range: 41..75, full_range: 41..75,
name: "test_foo",
kind: FN_DEF,
focus_range: Some( focus_range: Some(
62..70, 62..70,
), ),
name: "test_foo",
kind: FN_DEF,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,
@ -367,11 +367,11 @@ fn bench() {}
1, 1,
), ),
full_range: 77..99, full_range: 77..99,
name: "bench",
kind: FN_DEF,
focus_range: Some( focus_range: Some(
89..94, 89..94,
), ),
name: "bench",
kind: FN_DEF,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,
@ -410,11 +410,11 @@ fn foo() {}
1, 1,
), ),
full_range: 1..13, full_range: 1..13,
name: "main",
kind: FN_DEF,
focus_range: Some( focus_range: Some(
4..8, 4..8,
), ),
name: "main",
kind: FN_DEF,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,
@ -428,9 +428,9 @@ fn foo() {}
1, 1,
), ),
full_range: 15..57, full_range: 15..57,
focus_range: None,
name: "foo", name: "foo",
kind: FN_DEF, kind: FN_DEF,
focus_range: None,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,
@ -472,11 +472,11 @@ impl Data {
1, 1,
), ),
full_range: 1..13, full_range: 1..13,
name: "main",
kind: FN_DEF,
focus_range: Some( focus_range: Some(
4..8, 4..8,
), ),
name: "main",
kind: FN_DEF,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,
@ -490,9 +490,9 @@ impl Data {
1, 1,
), ),
full_range: 44..98, full_range: 44..98,
focus_range: None,
name: "foo", name: "foo",
kind: FN_DEF, kind: FN_DEF,
focus_range: None,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,
@ -529,11 +529,11 @@ mod test_mod {
1, 1,
), ),
full_range: 1..51, full_range: 1..51,
name: "test_mod",
kind: MODULE,
focus_range: Some( focus_range: Some(
5..13, 5..13,
), ),
name: "test_mod",
kind: MODULE,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,
@ -549,11 +549,11 @@ mod test_mod {
1, 1,
), ),
full_range: 20..49, full_range: 20..49,
name: "test_foo1",
kind: FN_DEF,
focus_range: Some( focus_range: Some(
35..44, 35..44,
), ),
name: "test_foo1",
kind: FN_DEF,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,
@ -595,11 +595,11 @@ mod foo {
1, 1,
), ),
full_range: 15..77, full_range: 15..77,
name: "test_mod",
kind: MODULE,
focus_range: Some( focus_range: Some(
19..27, 19..27,
), ),
name: "test_mod",
kind: MODULE,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,
@ -615,11 +615,11 @@ mod foo {
1, 1,
), ),
full_range: 38..71, full_range: 38..71,
name: "test_foo1",
kind: FN_DEF,
focus_range: Some( focus_range: Some(
57..66, 57..66,
), ),
name: "test_foo1",
kind: FN_DEF,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,
@ -663,11 +663,11 @@ mod foo {
1, 1,
), ),
full_range: 33..107, full_range: 33..107,
name: "test_mod",
kind: MODULE,
focus_range: Some( focus_range: Some(
37..45, 37..45,
), ),
name: "test_mod",
kind: MODULE,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,
@ -683,11 +683,11 @@ mod foo {
1, 1,
), ),
full_range: 60..97, full_range: 60..97,
name: "test_foo1",
kind: FN_DEF,
focus_range: Some( focus_range: Some(
83..92, 83..92,
), ),
name: "test_foo1",
kind: FN_DEF,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,
@ -726,11 +726,11 @@ fn test_foo1() {}
1, 1,
), ),
full_range: 1..50, full_range: 1..50,
name: "test_foo1",
kind: FN_DEF,
focus_range: Some( focus_range: Some(
36..45, 36..45,
), ),
name: "test_foo1",
kind: FN_DEF,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,
@ -774,11 +774,11 @@ fn test_foo1() {}
1, 1,
), ),
full_range: 1..72, full_range: 1..72,
name: "test_foo1",
kind: FN_DEF,
focus_range: Some( focus_range: Some(
58..67, 58..67,
), ),
name: "test_foo1",
kind: FN_DEF,
container_name: None, container_name: None,
description: None, description: None,
docs: None, docs: None,

View file

@ -341,10 +341,10 @@ pub(crate) fn handle_workspace_symbol(
fn exec_query(snap: &GlobalStateSnapshot, query: Query) -> Result<Vec<SymbolInformation>> { fn exec_query(snap: &GlobalStateSnapshot, query: Query) -> Result<Vec<SymbolInformation>> {
let mut res = Vec::new(); let mut res = Vec::new();
for nav in snap.analysis.symbol_search(query)? { for nav in snap.analysis.symbol_search(query)? {
let container_name = nav.container_name().map(|v| v.to_string()); let container_name = nav.container_name.as_ref().map(|v| v.to_string());
let info = SymbolInformation { let info = SymbolInformation {
name: nav.name().to_string(), name: nav.name.to_string(),
kind: to_proto::symbol_kind(nav.kind()), kind: to_proto::symbol_kind(nav.kind),
location: to_proto::location_from_nav(snap, nav)?, location: to_proto::location_from_nav(snap, nav)?,
container_name, container_name,
deprecated: None, deprecated: None,
@ -434,7 +434,7 @@ pub(crate) fn handle_runnables(
let mut res = Vec::new(); let mut res = Vec::new();
for runnable in snap.analysis.runnables(file_id)? { for runnable in snap.analysis.runnables(file_id)? {
if let Some(offset) = offset { if let Some(offset) = offset {
if !runnable.nav.full_range().contains_inclusive(offset) { if !runnable.nav.full_range.contains_inclusive(offset) {
continue; continue;
} }
} }
@ -874,7 +874,7 @@ pub(crate) fn handle_code_lens(
} }
let action = runnable.action(); let action = runnable.action();
let range = to_proto::range(&line_index, runnable.nav.range()); let range = to_proto::range(&line_index, runnable.nav.focus_or_full_range());
let r = to_proto::runnable(&snap, file_id, runnable)?; let r = to_proto::runnable(&snap, file_id, runnable)?;
if snap.config.lens.run { if snap.config.lens.run {
let lens = CodeLens { let lens = CodeLens {
@ -1063,7 +1063,7 @@ pub(crate) fn handle_call_hierarchy_prepare(
let RangeInfo { range: _, info: navs } = nav_info; let RangeInfo { range: _, info: navs } = nav_info;
let res = navs let res = navs
.into_iter() .into_iter()
.filter(|it| it.kind() == SyntaxKind::FN_DEF) .filter(|it| it.kind == SyntaxKind::FN_DEF)
.map(|it| to_proto::call_hierarchy_item(&snap, it)) .map(|it| to_proto::call_hierarchy_item(&snap, it))
.collect::<Result<Vec<_>>>()?; .collect::<Result<Vec<_>>>()?;
@ -1089,7 +1089,7 @@ pub(crate) fn handle_call_hierarchy_incoming(
let mut res = vec![]; let mut res = vec![];
for call_item in call_items.into_iter() { for call_item in call_items.into_iter() {
let file_id = call_item.target.file_id(); let file_id = call_item.target.file_id;
let line_index = snap.analysis.file_line_index(file_id)?; let line_index = snap.analysis.file_line_index(file_id)?;
let item = to_proto::call_hierarchy_item(&snap, call_item.target)?; let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
res.push(CallHierarchyIncomingCall { res.push(CallHierarchyIncomingCall {
@ -1124,7 +1124,7 @@ pub(crate) fn handle_call_hierarchy_outgoing(
let mut res = vec![]; let mut res = vec![];
for call_item in call_items.into_iter() { for call_item in call_items.into_iter() {
let file_id = call_item.target.file_id(); let file_id = call_item.target.file_id;
let line_index = snap.analysis.file_line_index(file_id)?; let line_index = snap.analysis.file_line_index(file_id)?;
let item = to_proto::call_hierarchy_item(&snap, call_item.target)?; let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
res.push(CallHierarchyOutgoingCall { res.push(CallHierarchyOutgoingCall {
@ -1220,13 +1220,13 @@ fn goto_location_command(snap: &GlobalStateSnapshot, nav: &NavigationTarget) ->
let link = to_proto::location_link(snap, None, nav.clone()).ok()?; let link = to_proto::location_link(snap, None, nav.clone()).ok()?;
to_value(link).ok()? to_value(link).ok()?
} else { } else {
let range = FileRange { file_id: nav.file_id(), range: nav.range() }; let range = FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() };
let location = to_proto::location(snap, range).ok()?; let location = to_proto::location(snap, range).ok()?;
to_value(location).ok()? to_value(location).ok()?
}; };
Some(Command { Some(Command {
title: nav.name().to_string(), title: nav.name.to_string(),
command: "rust-analyzer.gotoLocation".into(), command: "rust-analyzer.gotoLocation".into(),
arguments: Some(vec![value]), arguments: Some(vec![value]),
}) })

View file

@ -497,9 +497,9 @@ pub(crate) fn location_from_nav(
snap: &GlobalStateSnapshot, snap: &GlobalStateSnapshot,
nav: NavigationTarget, nav: NavigationTarget,
) -> Result<lsp_types::Location> { ) -> Result<lsp_types::Location> {
let url = url(snap, nav.file_id()); let url = url(snap, nav.file_id);
let line_index = snap.analysis.file_line_index(nav.file_id())?; let line_index = snap.analysis.file_line_index(nav.file_id)?;
let range = range(&line_index, nav.full_range()); let range = range(&line_index, nav.full_range);
let loc = lsp_types::Location::new(url, range); let loc = lsp_types::Location::new(url, range);
Ok(loc) Ok(loc)
} }
@ -531,12 +531,12 @@ fn location_info(
snap: &GlobalStateSnapshot, snap: &GlobalStateSnapshot,
target: NavigationTarget, target: NavigationTarget,
) -> Result<(lsp_types::Url, lsp_types::Range, lsp_types::Range)> { ) -> Result<(lsp_types::Url, lsp_types::Range, lsp_types::Range)> {
let line_index = snap.analysis.file_line_index(target.file_id())?; let line_index = snap.analysis.file_line_index(target.file_id)?;
let target_uri = url(snap, target.file_id()); let target_uri = url(snap, target.file_id);
let target_range = range(&line_index, target.full_range()); let target_range = range(&line_index, target.full_range);
let target_selection_range = let target_selection_range =
target.focus_range().map(|it| range(&line_index, it)).unwrap_or(target_range); target.focus_range.map(|it| range(&line_index, it)).unwrap_or(target_range);
Ok((target_uri, target_range, target_selection_range)) Ok((target_uri, target_range, target_selection_range))
} }
@ -555,13 +555,7 @@ pub(crate) fn goto_definition_response(
let locations = targets let locations = targets
.into_iter() .into_iter()
.map(|nav| { .map(|nav| {
location( location(snap, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
snap,
FileRange {
file_id: nav.file_id(),
range: nav.focus_range().unwrap_or(nav.range()),
},
)
}) })
.collect::<Result<Vec<_>>>()?; .collect::<Result<Vec<_>>>()?;
Ok(locations.into()) Ok(locations.into())
@ -666,9 +660,9 @@ pub(crate) fn call_hierarchy_item(
snap: &GlobalStateSnapshot, snap: &GlobalStateSnapshot,
target: NavigationTarget, target: NavigationTarget,
) -> Result<lsp_types::CallHierarchyItem> { ) -> Result<lsp_types::CallHierarchyItem> {
let name = target.name().to_string(); let name = target.name.to_string();
let detail = target.description().map(|it| it.to_string()); let detail = target.description.clone();
let kind = symbol_kind(target.kind()); let kind = symbol_kind(target.kind);
let (uri, range, selection_range) = location_info(snap, target)?; let (uri, range, selection_range) = location_info(snap, target)?;
Ok(lsp_types::CallHierarchyItem { name, kind, tags: None, detail, uri, range, selection_range }) Ok(lsp_types::CallHierarchyItem { name, kind, tags: None, detail, uri, range, selection_range })
} }