From 18bf2e5af5875f036b321bcf9e07e9904c02510e Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 1 Jan 2021 10:07:01 +0100 Subject: Add ConstParams to the ide layer --- crates/ide/src/display/navigation_target.rs | 19 +++++++++++++++++++ crates/ide/src/doc_links.rs | 1 + crates/ide/src/hover.rs | 5 ++++- crates/ide/src/references.rs | 16 ++++++++++++++++ crates/ide/src/syntax_highlighting.rs | 1 + crates/ide/src/syntax_highlighting/tags.rs | 1 + .../syntax_highlighting/test_data/highlighting.html | 4 ++++ crates/ide/src/syntax_highlighting/tests.rs | 4 ++++ 8 files changed, 50 insertions(+), 1 deletion(-) (limited to 'crates/ide') diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index 6431e7d6d..bcde2b6f1 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs @@ -24,6 +24,7 @@ pub enum SymbolKind { Impl, Field, TypeParam, + ConstParam, LifetimeParam, ValueParam, SelfParam, @@ -225,6 +226,7 @@ impl TryToNav for Definition { Definition::TypeParam(it) => Some(it.to_nav(db)), Definition::LifetimeParam(it) => Some(it.to_nav(db)), Definition::Label(it) => Some(it.to_nav(db)), + Definition::ConstParam(it) => Some(it.to_nav(db)), } } } @@ -485,6 +487,23 @@ impl ToNav for hir::LifetimeParam { } } +impl ToNav for hir::ConstParam { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + let src = self.source(db); + let full_range = src.value.syntax().text_range(); + NavigationTarget { + file_id: src.file_id.original_file(db), + name: self.name(db).to_string().into(), + kind: Some(SymbolKind::ConstParam), + full_range, + focus_range: src.value.name().map(|n| n.syntax().text_range()), + container_name: None, + description: None, + docs: None, + } + } +} + /// Get a description of a symbol. /// /// e.g. `struct Name`, `enum Name`, `fn Name` diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs index e10516f43..367fac05e 100644 --- a/crates/ide/src/doc_links.rs +++ b/crates/ide/src/doc_links.rs @@ -193,6 +193,7 @@ fn rewrite_intra_doc_link( Definition::SelfType(_) | Definition::Local(_) | Definition::TypeParam(_) + | Definition::ConstParam(_) | Definition::LifetimeParam(_) | Definition::Label(_) => return None, }?; diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 73245fbe7..49eb8caae 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -370,7 +370,10 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option { Adt::Enum(it) => from_def_source(db, it, mod_path), }) } - Definition::TypeParam(_) | Definition::LifetimeParam(_) | Definition::Label(_) => { + Definition::TypeParam(_) + | Definition::LifetimeParam(_) + | Definition::ConstParam(_) + | Definition::Label(_) => { // FIXME: Hover for generic param None } diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index 21b2d7ca1..fa58fc319 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs @@ -1144,4 +1144,20 @@ fn foo<'a>() -> &'a () { "#]], ); } + + #[test] + fn test_find_const_param() { + check( + r#" +fn foo: usize>() -> usize { + FOO +} +"#, + expect![[r#" + FOO ConstParam FileId(0) 7..23 13..16 Other + + FileId(0) 42..45 Other + "#]], + ); + } } diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 5ad96581b..ba0085244 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -819,6 +819,7 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { }, Definition::SelfType(_) => HighlightTag::Symbol(SymbolKind::Impl), Definition::TypeParam(_) => HighlightTag::Symbol(SymbolKind::TypeParam), + Definition::ConstParam(_) => HighlightTag::Symbol(SymbolKind::ConstParam), Definition::Local(local) => { let tag = if local.is_param(db) { HighlightTag::Symbol(SymbolKind::ValueParam) diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index 2a6cc0cab..8b8867079 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -77,6 +77,7 @@ impl HighlightTag { SymbolKind::Function => "function", SymbolKind::TypeAlias => "type_alias", SymbolKind::TypeParam => "type_param", + SymbolKind::ConstParam => "const_param", SymbolKind::LifetimeParam => "lifetime", SymbolKind::Macro => "macro", SymbolKind::Local => "variable", diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html index 72ff9dd40..02270b077 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html @@ -118,6 +118,10 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd loop {} } +fn const_param<const FOO: usize>() -> usize { + FOO +} + use ops::Fn; fn baz<F: Fn() -> ()>(f: F) { f() diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index e0df0d2b5..30b5b648e 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -91,6 +91,10 @@ fn never() -> ! { loop {} } +fn const_param() -> usize { + FOO +} + use ops::Fn; fn baz ()>(f: F) { f() -- cgit v1.2.3