NavTarget doesn't assume that it points to a symbol

This commit is contained in:
Aleksey Kladov 2020-12-18 21:10:13 +03:00
parent 53f81e4e8c
commit 0e3581e823
7 changed files with 196 additions and 81 deletions

View File

@ -35,8 +35,6 @@ pub enum SymbolKind {
TypeAlias,
Trait,
Macro,
// Do we actually need this?
DocTest,
}
/// `NavigationTarget` represents and element in the editor's UI which you can
@ -64,7 +62,7 @@ pub struct NavigationTarget {
/// Clients should place the cursor on this range when navigating to this target.
pub focus_range: Option<TextRange>,
pub name: SmolStr,
pub kind: SymbolKind,
pub kind: Option<SymbolKind>,
pub container_name: Option<SmolStr>,
pub description: Option<String>,
pub docs: Option<Documentation>,
@ -110,8 +108,13 @@ impl NavigationTarget {
#[cfg(test)]
pub(crate) fn debug_render(&self) -> String {
let mut buf =
format!("{} {:?} {:?} {:?}", self.name, self.kind, self.file_id, self.full_range);
let mut buf = format!(
"{} {:?} {:?} {:?}",
self.name,
self.kind.unwrap(),
self.file_id,
self.full_range
);
if let Some(focus_range) = self.focus_range {
buf.push_str(&format!(" {:?}", focus_range))
}
@ -146,7 +149,7 @@ impl NavigationTarget {
NavigationTarget {
file_id,
name,
kind,
kind: Some(kind),
full_range,
focus_range,
container_name: None,
@ -161,7 +164,7 @@ impl ToNav for FileSymbol {
NavigationTarget {
file_id: self.file_id,
name: self.name.clone(),
kind: match self.kind {
kind: Some(match self.kind {
FileSymbolKind::Function => SymbolKind::Function,
FileSymbolKind::Struct => SymbolKind::Struct,
FileSymbolKind::Enum => SymbolKind::Enum,
@ -171,7 +174,7 @@ impl ToNav for FileSymbol {
FileSymbolKind::Const => SymbolKind::Const,
FileSymbolKind::Static => SymbolKind::Static,
FileSymbolKind::Macro => SymbolKind::Macro,
},
}),
full_range: self.range,
focus_range: self.name_range,
container_name: self.container_name.clone(),
@ -386,7 +389,7 @@ impl ToNav for hir::Local {
NavigationTarget {
file_id: full_range.file_id,
name,
kind: SymbolKind::Local,
kind: Some(SymbolKind::Local),
full_range: full_range.range,
focus_range: None,
container_name: None,
@ -410,7 +413,7 @@ impl ToNav for hir::TypeParam {
NavigationTarget {
file_id: src.file_id.original_file(db),
name: self.name(db).to_string().into(),
kind: SymbolKind::TypeParam,
kind: Some(SymbolKind::TypeParam),
full_range,
focus_range,
container_name: None,
@ -427,7 +430,7 @@ impl ToNav for hir::LifetimeParam {
NavigationTarget {
file_id: src.file_id.original_file(db),
name: self.name(db).to_string().into(),
kind: SymbolKind::LifetimeParam,
kind: Some(SymbolKind::LifetimeParam),
full_range,
focus_range: Some(full_range),
container_name: None,
@ -488,7 +491,9 @@ fn foo() { enum FooInner { } }
5..13,
),
name: "FooInner",
kind: Enum,
kind: Some(
Enum,
),
container_name: None,
description: Some(
"enum FooInner",
@ -504,7 +509,9 @@ fn foo() { enum FooInner { } }
34..42,
),
name: "FooInner",
kind: Enum,
kind: Some(
Enum,
),
container_name: Some(
"foo",
),

View File

@ -86,7 +86,7 @@ fn self_to_nav_target(self_param: ast::SelfParam, file_id: FileId) -> Option<Nav
full_range: self_param.syntax().text_range(),
focus_range: Some(self_token.text_range()),
name: self_token.text().clone(),
kind: SymbolKind::SelfParam,
kind: Some(SymbolKind::SelfParam),
container_name: None,
description: None,
docs: None,

View File

@ -2191,7 +2191,9 @@ fn foo_<|>test() {}
11..19,
),
name: "foo_test",
kind: Function,
kind: Some(
Function,
),
container_name: None,
description: None,
docs: None,
@ -2234,7 +2236,9 @@ mod tests<|> {
4..9,
),
name: "tests",
kind: Module,
kind: Some(
Module,
),
container_name: None,
description: None,
docs: None,
@ -2273,7 +2277,9 @@ fn main() { let s<|>t = S{ f1:0 }; }
7..8,
),
name: "S",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"struct S",
@ -2312,7 +2318,9 @@ fn main() { let s<|>t = S{ f1:Arg(0) }; }
24..25,
),
name: "S",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"struct S",
@ -2331,7 +2339,9 @@ fn main() { let s<|>t = S{ f1:Arg(0) }; }
7..10,
),
name: "Arg",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"struct Arg",
@ -2370,7 +2380,9 @@ fn main() { let s<|>t = S{ f1: S{ f1: Arg(0) } }; }
24..25,
),
name: "S",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"struct S",
@ -2389,7 +2401,9 @@ fn main() { let s<|>t = S{ f1: S{ f1: Arg(0) } }; }
7..10,
),
name: "Arg",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"struct Arg",
@ -2431,7 +2445,9 @@ fn main() { let s<|>t = (A(1), B(2), M::C(3) ); }
7..8,
),
name: "A",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"struct A",
@ -2450,7 +2466,9 @@ fn main() { let s<|>t = (A(1), B(2), M::C(3) ); }
22..23,
),
name: "B",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"struct B",
@ -2469,7 +2487,9 @@ fn main() { let s<|>t = (A(1), B(2), M::C(3) ); }
53..54,
),
name: "C",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"pub struct C",
@ -2508,7 +2528,9 @@ fn main() { let s<|>t = foo(); }
6..9,
),
name: "Foo",
kind: Trait,
kind: Some(
Trait,
),
container_name: None,
description: Some(
"trait Foo",
@ -2548,7 +2570,9 @@ fn main() { let s<|>t = foo(); }
6..9,
),
name: "Foo",
kind: Trait,
kind: Some(
Trait,
),
container_name: None,
description: Some(
"trait Foo",
@ -2567,7 +2591,9 @@ fn main() { let s<|>t = foo(); }
23..24,
),
name: "S",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"struct S",
@ -2607,7 +2633,9 @@ fn main() { let s<|>t = foo(); }
6..9,
),
name: "Foo",
kind: Trait,
kind: Some(
Trait,
),
container_name: None,
description: Some(
"trait Foo",
@ -2626,7 +2654,9 @@ fn main() { let s<|>t = foo(); }
19..22,
),
name: "Bar",
kind: Trait,
kind: Some(
Trait,
),
container_name: None,
description: Some(
"trait Bar",
@ -2669,7 +2699,9 @@ fn main() { let s<|>t = foo(); }
6..9,
),
name: "Foo",
kind: Trait,
kind: Some(
Trait,
),
container_name: None,
description: Some(
"trait Foo",
@ -2688,7 +2720,9 @@ fn main() { let s<|>t = foo(); }
22..25,
),
name: "Bar",
kind: Trait,
kind: Some(
Trait,
),
container_name: None,
description: Some(
"trait Bar",
@ -2707,7 +2741,9 @@ fn main() { let s<|>t = foo(); }
39..41,
),
name: "S1",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"struct S1",
@ -2726,7 +2762,9 @@ fn main() { let s<|>t = foo(); }
52..54,
),
name: "S2",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"struct S2",
@ -2763,7 +2801,9 @@ fn foo(ar<|>g: &impl Foo) {}
6..9,
),
name: "Foo",
kind: Trait,
kind: Some(
Trait,
),
container_name: None,
description: Some(
"trait Foo",
@ -2803,7 +2843,9 @@ fn foo(ar<|>g: &impl Foo + Bar<S>) {}
6..9,
),
name: "Foo",
kind: Trait,
kind: Some(
Trait,
),
container_name: None,
description: Some(
"trait Foo",
@ -2822,7 +2864,9 @@ fn foo(ar<|>g: &impl Foo + Bar<S>) {}
19..22,
),
name: "Bar",
kind: Trait,
kind: Some(
Trait,
),
container_name: None,
description: Some(
"trait Bar",
@ -2841,7 +2885,9 @@ fn foo(ar<|>g: &impl Foo + Bar<S>) {}
36..37,
),
name: "S",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"struct S",
@ -2886,7 +2932,9 @@ mod future {
140..146,
),
name: "Future",
kind: Trait,
kind: Some(
Trait,
),
container_name: None,
description: Some(
"pub trait Future",
@ -2905,7 +2953,9 @@ mod future {
7..8,
),
name: "S",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"struct S",
@ -2943,7 +2993,9 @@ fn foo(ar<|>g: &impl Foo<S>) {}
6..9,
),
name: "Foo",
kind: Trait,
kind: Some(
Trait,
),
container_name: None,
description: Some(
"trait Foo",
@ -2962,7 +3014,9 @@ fn foo(ar<|>g: &impl Foo<S>) {}
23..24,
),
name: "S",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"struct S",
@ -3005,7 +3059,9 @@ fn main() { let s<|>t = foo(); }
49..50,
),
name: "B",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"struct B",
@ -3024,7 +3080,9 @@ fn main() { let s<|>t = foo(); }
6..9,
),
name: "Foo",
kind: Trait,
kind: Some(
Trait,
),
container_name: None,
description: Some(
"trait Foo",
@ -3061,7 +3119,9 @@ fn foo(ar<|>g: &dyn Foo) {}
6..9,
),
name: "Foo",
kind: Trait,
kind: Some(
Trait,
),
container_name: None,
description: Some(
"trait Foo",
@ -3099,7 +3159,9 @@ fn foo(ar<|>g: &dyn Foo<S>) {}
6..9,
),
name: "Foo",
kind: Trait,
kind: Some(
Trait,
),
container_name: None,
description: Some(
"trait Foo",
@ -3118,7 +3180,9 @@ fn foo(ar<|>g: &dyn Foo<S>) {}
23..24,
),
name: "S",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"struct S",
@ -3159,7 +3223,9 @@ fn foo(a<|>rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
6..15,
),
name: "ImplTrait",
kind: Trait,
kind: Some(
Trait,
),
container_name: None,
description: Some(
"trait ImplTrait",
@ -3178,7 +3244,9 @@ fn foo(a<|>rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
50..51,
),
name: "B",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"struct B",
@ -3197,7 +3265,9 @@ fn foo(a<|>rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
28..36,
),
name: "DynTrait",
kind: Trait,
kind: Some(
Trait,
),
container_name: None,
description: Some(
"trait DynTrait",
@ -3216,7 +3286,9 @@ fn foo(a<|>rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
65..66,
),
name: "S",
kind: Struct,
kind: Some(
Struct,
),
container_name: None,
description: Some(
"struct S",
@ -3264,7 +3336,9 @@ fn main() { let s<|>t = test().get(); }
6..9,
),
name: "Foo",
kind: Trait,
kind: Some(
Trait,
),
container_name: None,
description: Some(
"trait Foo",

View File

@ -278,7 +278,7 @@ fn try_find_self_references(
full_range: self_param.syntax().text_range(),
focus_range: Some(param_self_token.text_range()),
name: param_self_token.text().clone(),
kind: SymbolKind::SelfParam,
kind: Some(SymbolKind::SelfParam),
container_name: None,
description: None,
docs: None,

View File

@ -208,7 +208,7 @@ fn module_def_doctest(sema: &Semantics<RootDatabase>, def: hir::ModuleDef) -> Op
nav.focus_range = None;
nav.description = None;
nav.docs = None;
nav.kind = SymbolKind::DocTest;
nav.kind = None;
let res = Runnable { nav, kind: RunnableKind::DocTest { test_id }, cfg: attrs.cfg() };
Some(res)
}
@ -356,7 +356,9 @@ fn bench() {}
4..8,
),
name: "main",
kind: Function,
kind: Some(
Function,
),
container_name: None,
description: None,
docs: None,
@ -374,7 +376,9 @@ fn bench() {}
26..34,
),
name: "test_foo",
kind: Function,
kind: Some(
Function,
),
container_name: None,
description: None,
docs: None,
@ -399,7 +403,9 @@ fn bench() {}
62..70,
),
name: "test_foo",
kind: Function,
kind: Some(
Function,
),
container_name: None,
description: None,
docs: None,
@ -424,7 +430,9 @@ fn bench() {}
89..94,
),
name: "bench",
kind: Function,
kind: Some(
Function,
),
container_name: None,
description: None,
docs: None,
@ -521,7 +529,9 @@ struct StructWithRunnable(String);
4..8,
),
name: "main",
kind: Function,
kind: Some(
Function,
),
container_name: None,
description: None,
docs: None,
@ -537,7 +547,7 @@ struct StructWithRunnable(String);
full_range: 15..74,
focus_range: None,
name: "should_have_runnable",
kind: DocTest,
kind: None,
container_name: None,
description: None,
docs: None,
@ -557,7 +567,7 @@ struct StructWithRunnable(String);
full_range: 76..148,
focus_range: None,
name: "should_have_runnable_1",
kind: DocTest,
kind: None,
container_name: None,
description: None,
docs: None,
@ -577,7 +587,7 @@ struct StructWithRunnable(String);
full_range: 150..254,
focus_range: None,
name: "should_have_runnable_2",
kind: DocTest,
kind: None,
container_name: None,
description: None,
docs: None,
@ -597,7 +607,7 @@ struct StructWithRunnable(String);
full_range: 756..821,
focus_range: None,
name: "StructWithRunnable",
kind: DocTest,
kind: None,
container_name: None,
description: None,
docs: None,
@ -643,7 +653,9 @@ impl Data {
4..8,
),
name: "main",
kind: Function,
kind: Some(
Function,
),
container_name: None,
description: None,
docs: None,
@ -659,7 +671,7 @@ impl Data {
full_range: 44..98,
focus_range: None,
name: "foo",
kind: DocTest,
kind: None,
container_name: None,
description: None,
docs: None,
@ -700,7 +712,9 @@ mod test_mod {
5..13,
),
name: "test_mod",
kind: Module,
kind: Some(
Module,
),
container_name: None,
description: None,
docs: None,
@ -720,7 +734,9 @@ mod test_mod {
35..44,
),
name: "test_foo1",
kind: Function,
kind: Some(
Function,
),
container_name: None,
description: None,
docs: None,
@ -780,7 +796,9 @@ mod root_tests {
26..40,
),
name: "nested_tests_0",
kind: Module,
kind: Some(
Module,
),
container_name: None,
description: None,
docs: None,
@ -800,7 +818,9 @@ mod root_tests {
55..69,
),
name: "nested_tests_1",
kind: Module,
kind: Some(
Module,
),
container_name: None,
description: None,
docs: None,
@ -820,7 +840,9 @@ mod root_tests {
107..121,
),
name: "nested_test_11",
kind: Function,
kind: Some(
Function,
),
container_name: None,
description: None,
docs: None,
@ -845,7 +867,9 @@ mod root_tests {
163..177,
),
name: "nested_test_12",
kind: Function,
kind: Some(
Function,
),
container_name: None,
description: None,
docs: None,
@ -870,7 +894,9 @@ mod root_tests {
206..220,
),
name: "nested_tests_2",
kind: Module,
kind: Some(
Module,
),
container_name: None,
description: None,
docs: None,
@ -890,7 +916,9 @@ mod root_tests {
258..271,
),
name: "nested_test_2",
kind: Function,
kind: Some(
Function,
),
container_name: None,
description: None,
docs: None,
@ -933,7 +961,9 @@ fn test_foo1() {}
36..45,
),
name: "test_foo1",
kind: Function,
kind: Some(
Function,
),
container_name: None,
description: None,
docs: None,
@ -983,7 +1013,9 @@ fn test_foo1() {}
58..67,
),
name: "test_foo1",
kind: Function,
kind: Some(
Function,
),
container_name: None,
description: None,
docs: None,

View File

@ -385,7 +385,10 @@ pub(crate) fn handle_workspace_symbol(
#[allow(deprecated)]
let info = SymbolInformation {
name: nav.name.to_string(),
kind: to_proto::symbol_kind(nav.kind),
kind: nav
.kind
.map(to_proto::symbol_kind)
.unwrap_or(lsp_types::SymbolKind::Variable),
tags: None,
location: to_proto::location_from_nav(snap, nav)?,
container_name,
@ -1263,7 +1266,7 @@ pub(crate) fn handle_call_hierarchy_prepare(
let RangeInfo { range: _, info: navs } = nav_info;
let res = navs
.into_iter()
.filter(|it| it.kind == SymbolKind::Function)
.filter(|it| it.kind == Some(SymbolKind::Function))
.map(|it| to_proto::call_hierarchy_item(&snap, it))
.collect::<Result<Vec<_>>>()?;

View File

@ -43,10 +43,9 @@ pub(crate) fn symbol_kind(symbol_kind: SymbolKind) -> lsp_types::SymbolKind {
SymbolKind::Static => lsp_types::SymbolKind::Constant,
SymbolKind::Const => lsp_types::SymbolKind::Constant,
SymbolKind::Impl => lsp_types::SymbolKind::Object,
SymbolKind::Local
| SymbolKind::SelfParam
| SymbolKind::LifetimeParam
| SymbolKind::DocTest => lsp_types::SymbolKind::Variable,
SymbolKind::Local | SymbolKind::SelfParam | SymbolKind::LifetimeParam => {
lsp_types::SymbolKind::Variable
}
SymbolKind::Union => lsp_types::SymbolKind::Struct,
}
}
@ -722,7 +721,7 @@ pub(crate) fn call_hierarchy_item(
) -> Result<lsp_types::CallHierarchyItem> {
let name = target.name.to_string();
let detail = target.description.clone();
let kind = symbol_kind(target.kind);
let kind = target.kind.map(symbol_kind).unwrap_or(lsp_types::SymbolKind::Function);
let (uri, range, selection_range) = location_info(snap, target)?;
Ok(lsp_types::CallHierarchyItem {
name,