diff options
author | Lukas Wirth <[email protected]> | 2021-01-01 09:07:01 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-01-01 13:43:16 +0000 |
commit | 18bf2e5af5875f036b321bcf9e07e9904c02510e (patch) | |
tree | 1a82bc1dddc9a0d77515f7e7036c4b5583d359ba | |
parent | 0acdb730769cfb040ffc5e2c87f83b19fd3ce291 (diff) |
Add ConstParams to the ide layer
-rw-r--r-- | crates/assists/src/ast_transform.rs | 3 | ||||
-rw-r--r-- | crates/hir_ty/src/tests/simple.rs | 1 | ||||
-rw-r--r-- | crates/ide/src/display/navigation_target.rs | 19 | ||||
-rw-r--r-- | crates/ide/src/doc_links.rs | 1 | ||||
-rw-r--r-- | crates/ide/src/hover.rs | 5 | ||||
-rw-r--r-- | crates/ide/src/references.rs | 16 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting.rs | 1 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/tags.rs | 1 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/test_data/highlighting.html | 4 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/tests.rs | 4 | ||||
-rw-r--r-- | crates/ide_db/src/defs.rs | 13 | ||||
-rw-r--r-- | crates/rust-analyzer/src/semantic_tokens.rs | 1 | ||||
-rw-r--r-- | crates/rust-analyzer/src/to_proto.rs | 2 |
13 files changed, 66 insertions, 5 deletions
diff --git a/crates/assists/src/ast_transform.rs b/crates/assists/src/ast_transform.rs index da94e9987..4a3ed7783 100644 --- a/crates/assists/src/ast_transform.rs +++ b/crates/assists/src/ast_transform.rs | |||
@@ -204,7 +204,8 @@ impl<'a> AstTransform<'a> for QualifyPaths<'a> { | |||
204 | } | 204 | } |
205 | PathResolution::Local(_) | 205 | PathResolution::Local(_) |
206 | | PathResolution::TypeParam(_) | 206 | | PathResolution::TypeParam(_) |
207 | | PathResolution::SelfType(_) => None, | 207 | | PathResolution::SelfType(_) |
208 | | PathResolution::ConstParam(_) => None, | ||
208 | PathResolution::Macro(_) => None, | 209 | PathResolution::Macro(_) => None, |
209 | PathResolution::AssocItem(_) => None, | 210 | PathResolution::AssocItem(_) => None, |
210 | } | 211 | } |
diff --git a/crates/hir_ty/src/tests/simple.rs b/crates/hir_ty/src/tests/simple.rs index bd6beea99..8d431b920 100644 --- a/crates/hir_ty/src/tests/simple.rs +++ b/crates/hir_ty/src/tests/simple.rs | |||
@@ -2376,7 +2376,6 @@ fn infer_operator_overload() { | |||
2376 | ); | 2376 | ); |
2377 | } | 2377 | } |
2378 | 2378 | ||
2379 | |||
2380 | #[test] | 2379 | #[test] |
2381 | fn infer_const_params() { | 2380 | fn infer_const_params() { |
2382 | check_infer( | 2381 | check_infer( |
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 { | |||
24 | Impl, | 24 | Impl, |
25 | Field, | 25 | Field, |
26 | TypeParam, | 26 | TypeParam, |
27 | ConstParam, | ||
27 | LifetimeParam, | 28 | LifetimeParam, |
28 | ValueParam, | 29 | ValueParam, |
29 | SelfParam, | 30 | SelfParam, |
@@ -225,6 +226,7 @@ impl TryToNav for Definition { | |||
225 | Definition::TypeParam(it) => Some(it.to_nav(db)), | 226 | Definition::TypeParam(it) => Some(it.to_nav(db)), |
226 | Definition::LifetimeParam(it) => Some(it.to_nav(db)), | 227 | Definition::LifetimeParam(it) => Some(it.to_nav(db)), |
227 | Definition::Label(it) => Some(it.to_nav(db)), | 228 | Definition::Label(it) => Some(it.to_nav(db)), |
229 | Definition::ConstParam(it) => Some(it.to_nav(db)), | ||
228 | } | 230 | } |
229 | } | 231 | } |
230 | } | 232 | } |
@@ -485,6 +487,23 @@ impl ToNav for hir::LifetimeParam { | |||
485 | } | 487 | } |
486 | } | 488 | } |
487 | 489 | ||
490 | impl ToNav for hir::ConstParam { | ||
491 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | ||
492 | let src = self.source(db); | ||
493 | let full_range = src.value.syntax().text_range(); | ||
494 | NavigationTarget { | ||
495 | file_id: src.file_id.original_file(db), | ||
496 | name: self.name(db).to_string().into(), | ||
497 | kind: Some(SymbolKind::ConstParam), | ||
498 | full_range, | ||
499 | focus_range: src.value.name().map(|n| n.syntax().text_range()), | ||
500 | container_name: None, | ||
501 | description: None, | ||
502 | docs: None, | ||
503 | } | ||
504 | } | ||
505 | } | ||
506 | |||
488 | /// Get a description of a symbol. | 507 | /// Get a description of a symbol. |
489 | /// | 508 | /// |
490 | /// e.g. `struct Name`, `enum Name`, `fn Name` | 509 | /// 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( | |||
193 | Definition::SelfType(_) | 193 | Definition::SelfType(_) |
194 | | Definition::Local(_) | 194 | | Definition::Local(_) |
195 | | Definition::TypeParam(_) | 195 | | Definition::TypeParam(_) |
196 | | Definition::ConstParam(_) | ||
196 | | Definition::LifetimeParam(_) | 197 | | Definition::LifetimeParam(_) |
197 | | Definition::Label(_) => return None, | 198 | | Definition::Label(_) => return None, |
198 | }?; | 199 | }?; |
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<Markup> { | |||
370 | Adt::Enum(it) => from_def_source(db, it, mod_path), | 370 | Adt::Enum(it) => from_def_source(db, it, mod_path), |
371 | }) | 371 | }) |
372 | } | 372 | } |
373 | Definition::TypeParam(_) | Definition::LifetimeParam(_) | Definition::Label(_) => { | 373 | Definition::TypeParam(_) |
374 | | Definition::LifetimeParam(_) | ||
375 | | Definition::ConstParam(_) | ||
376 | | Definition::Label(_) => { | ||
374 | // FIXME: Hover for generic param | 377 | // FIXME: Hover for generic param |
375 | None | 378 | None |
376 | } | 379 | } |
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 () { | |||
1144 | "#]], | 1144 | "#]], |
1145 | ); | 1145 | ); |
1146 | } | 1146 | } |
1147 | |||
1148 | #[test] | ||
1149 | fn test_find_const_param() { | ||
1150 | check( | ||
1151 | r#" | ||
1152 | fn foo<const FOO<|>: usize>() -> usize { | ||
1153 | FOO | ||
1154 | } | ||
1155 | "#, | ||
1156 | expect![[r#" | ||
1157 | FOO ConstParam FileId(0) 7..23 13..16 Other | ||
1158 | |||
1159 | FileId(0) 42..45 Other | ||
1160 | "#]], | ||
1161 | ); | ||
1162 | } | ||
1147 | } | 1163 | } |
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 { | |||
819 | }, | 819 | }, |
820 | Definition::SelfType(_) => HighlightTag::Symbol(SymbolKind::Impl), | 820 | Definition::SelfType(_) => HighlightTag::Symbol(SymbolKind::Impl), |
821 | Definition::TypeParam(_) => HighlightTag::Symbol(SymbolKind::TypeParam), | 821 | Definition::TypeParam(_) => HighlightTag::Symbol(SymbolKind::TypeParam), |
822 | Definition::ConstParam(_) => HighlightTag::Symbol(SymbolKind::ConstParam), | ||
822 | Definition::Local(local) => { | 823 | Definition::Local(local) => { |
823 | let tag = if local.is_param(db) { | 824 | let tag = if local.is_param(db) { |
824 | HighlightTag::Symbol(SymbolKind::ValueParam) | 825 | 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 { | |||
77 | SymbolKind::Function => "function", | 77 | SymbolKind::Function => "function", |
78 | SymbolKind::TypeAlias => "type_alias", | 78 | SymbolKind::TypeAlias => "type_alias", |
79 | SymbolKind::TypeParam => "type_param", | 79 | SymbolKind::TypeParam => "type_param", |
80 | SymbolKind::ConstParam => "const_param", | ||
80 | SymbolKind::LifetimeParam => "lifetime", | 81 | SymbolKind::LifetimeParam => "lifetime", |
81 | SymbolKind::Macro => "macro", | 82 | SymbolKind::Macro => "macro", |
82 | SymbolKind::Local => "variable", | 83 | 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 | |||
118 | <span class="keyword control">loop</span> <span class="punctuation">{</span><span class="punctuation">}</span> | 118 | <span class="keyword control">loop</span> <span class="punctuation">{</span><span class="punctuation">}</span> |
119 | <span class="punctuation">}</span> | 119 | <span class="punctuation">}</span> |
120 | 120 | ||
121 | <span class="keyword">fn</span> <span class="function declaration">const_param</span><span class="punctuation"><</span><span class="keyword">const</span> <span class="const_param declaration">FOO</span><span class="punctuation">:</span> <span class="builtin_type">usize</span><span class="punctuation">></span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="operator">-></span> <span class="builtin_type">usize</span> <span class="punctuation">{</span> | ||
122 | <span class="const_param">FOO</span> | ||
123 | <span class="punctuation">}</span> | ||
124 | |||
121 | <span class="keyword">use</span> <span class="module">ops</span><span class="operator">::</span><span class="trait">Fn</span><span class="punctuation">;</span> | 125 | <span class="keyword">use</span> <span class="module">ops</span><span class="operator">::</span><span class="trait">Fn</span><span class="punctuation">;</span> |
122 | <span class="keyword">fn</span> <span class="function declaration">baz</span><span class="punctuation"><</span><span class="type_param declaration">F</span><span class="punctuation">:</span> <span class="trait">Fn</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="operator">-></span> <span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">></span><span class="punctuation">(</span><span class="value_param declaration callable">f</span><span class="punctuation">:</span> <span class="type_param">F</span><span class="punctuation">)</span> <span class="punctuation">{</span> | 126 | <span class="keyword">fn</span> <span class="function declaration">baz</span><span class="punctuation"><</span><span class="type_param declaration">F</span><span class="punctuation">:</span> <span class="trait">Fn</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="operator">-></span> <span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">></span><span class="punctuation">(</span><span class="value_param declaration callable">f</span><span class="punctuation">:</span> <span class="type_param">F</span><span class="punctuation">)</span> <span class="punctuation">{</span> |
123 | <span class="value_param callable">f</span><span class="punctuation">(</span><span class="punctuation">)</span> | 127 | <span class="value_param callable">f</span><span class="punctuation">(</span><span class="punctuation">)</span> |
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() -> ! { | |||
91 | loop {} | 91 | loop {} |
92 | } | 92 | } |
93 | 93 | ||
94 | fn const_param<const FOO: usize>() -> usize { | ||
95 | FOO | ||
96 | } | ||
97 | |||
94 | use ops::Fn; | 98 | use ops::Fn; |
95 | fn baz<F: Fn() -> ()>(f: F) { | 99 | fn baz<F: Fn() -> ()>(f: F) { |
96 | f() | 100 | f() |
diff --git a/crates/ide_db/src/defs.rs b/crates/ide_db/src/defs.rs index d33a6cb86..cc5078bf0 100644 --- a/crates/ide_db/src/defs.rs +++ b/crates/ide_db/src/defs.rs | |||
@@ -6,8 +6,8 @@ | |||
6 | // FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). | 6 | // FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). |
7 | 7 | ||
8 | use hir::{ | 8 | use hir::{ |
9 | db::HirDatabase, Crate, Field, HasVisibility, Impl, Label, LifetimeParam, Local, MacroDef, | 9 | db::HirDatabase, ConstParam, Crate, Field, HasVisibility, Impl, Label, LifetimeParam, Local, |
10 | Module, ModuleDef, Name, PathResolution, Semantics, TypeParam, Visibility, | 10 | MacroDef, Module, ModuleDef, Name, PathResolution, Semantics, TypeParam, Visibility, |
11 | }; | 11 | }; |
12 | use syntax::{ | 12 | use syntax::{ |
13 | ast::{self, AstNode}, | 13 | ast::{self, AstNode}, |
@@ -26,6 +26,7 @@ pub enum Definition { | |||
26 | Local(Local), | 26 | Local(Local), |
27 | TypeParam(TypeParam), | 27 | TypeParam(TypeParam), |
28 | LifetimeParam(LifetimeParam), | 28 | LifetimeParam(LifetimeParam), |
29 | ConstParam(ConstParam), | ||
29 | Label(Label), | 30 | Label(Label), |
30 | } | 31 | } |
31 | 32 | ||
@@ -39,6 +40,7 @@ impl Definition { | |||
39 | Definition::Local(it) => Some(it.module(db)), | 40 | Definition::Local(it) => Some(it.module(db)), |
40 | Definition::TypeParam(it) => Some(it.module(db)), | 41 | Definition::TypeParam(it) => Some(it.module(db)), |
41 | Definition::LifetimeParam(it) => Some(it.module(db)), | 42 | Definition::LifetimeParam(it) => Some(it.module(db)), |
43 | Definition::ConstParam(it) => Some(it.module(db)), | ||
42 | Definition::Label(it) => Some(it.module(db)), | 44 | Definition::Label(it) => Some(it.module(db)), |
43 | } | 45 | } |
44 | } | 46 | } |
@@ -52,6 +54,7 @@ impl Definition { | |||
52 | Definition::Local(_) => None, | 54 | Definition::Local(_) => None, |
53 | Definition::TypeParam(_) => None, | 55 | Definition::TypeParam(_) => None, |
54 | Definition::LifetimeParam(_) => None, | 56 | Definition::LifetimeParam(_) => None, |
57 | Definition::ConstParam(_) => None, | ||
55 | Definition::Label(_) => None, | 58 | Definition::Label(_) => None, |
56 | } | 59 | } |
57 | } | 60 | } |
@@ -79,6 +82,7 @@ impl Definition { | |||
79 | Definition::Local(it) => it.name(db)?, | 82 | Definition::Local(it) => it.name(db)?, |
80 | Definition::TypeParam(it) => it.name(db), | 83 | Definition::TypeParam(it) => it.name(db), |
81 | Definition::LifetimeParam(it) => it.name(db), | 84 | Definition::LifetimeParam(it) => it.name(db), |
85 | Definition::ConstParam(it) => it.name(db), | ||
82 | Definition::Label(it) => it.name(db), | 86 | Definition::Label(it) => it.name(db), |
83 | }; | 87 | }; |
84 | Some(name) | 88 | Some(name) |
@@ -233,6 +237,10 @@ impl NameClass { | |||
233 | let def = sema.to_def(&it)?; | 237 | let def = sema.to_def(&it)?; |
234 | Some(NameClass::Definition(Definition::TypeParam(def))) | 238 | Some(NameClass::Definition(Definition::TypeParam(def))) |
235 | }, | 239 | }, |
240 | ast::ConstParam(it) => { | ||
241 | let def = sema.to_def(&it)?; | ||
242 | Some(NameClass::Definition(Definition::ConstParam(def))) | ||
243 | }, | ||
236 | _ => None, | 244 | _ => None, |
237 | } | 245 | } |
238 | } | 246 | } |
@@ -417,6 +425,7 @@ impl From<PathResolution> for Definition { | |||
417 | PathResolution::TypeParam(par) => Definition::TypeParam(par), | 425 | PathResolution::TypeParam(par) => Definition::TypeParam(par), |
418 | PathResolution::Macro(def) => Definition::Macro(def), | 426 | PathResolution::Macro(def) => Definition::Macro(def), |
419 | PathResolution::SelfType(impl_def) => Definition::SelfType(impl_def), | 427 | PathResolution::SelfType(impl_def) => Definition::SelfType(impl_def), |
428 | PathResolution::ConstParam(par) => Definition::ConstParam(par), | ||
420 | } | 429 | } |
421 | } | 430 | } |
422 | } | 431 | } |
diff --git a/crates/rust-analyzer/src/semantic_tokens.rs b/crates/rust-analyzer/src/semantic_tokens.rs index c2f6a655d..5c4366f16 100644 --- a/crates/rust-analyzer/src/semantic_tokens.rs +++ b/crates/rust-analyzer/src/semantic_tokens.rs | |||
@@ -44,6 +44,7 @@ define_semantic_token_types![ | |||
44 | (ESCAPE_SEQUENCE, "escapeSequence"), | 44 | (ESCAPE_SEQUENCE, "escapeSequence"), |
45 | (FORMAT_SPECIFIER, "formatSpecifier"), | 45 | (FORMAT_SPECIFIER, "formatSpecifier"), |
46 | (GENERIC, "generic"), | 46 | (GENERIC, "generic"), |
47 | (CONST_PARAMETER, "constParameter"), | ||
47 | (LIFETIME, "lifetime"), | 48 | (LIFETIME, "lifetime"), |
48 | (LABEL, "label"), | 49 | (LABEL, "label"), |
49 | (PUNCTUATION, "punctuation"), | 50 | (PUNCTUATION, "punctuation"), |
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 1a38e79f0..999b18351 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs | |||
@@ -42,6 +42,7 @@ pub(crate) fn symbol_kind(symbol_kind: SymbolKind) -> lsp_types::SymbolKind { | |||
42 | SymbolKind::Field => lsp_types::SymbolKind::Field, | 42 | SymbolKind::Field => lsp_types::SymbolKind::Field, |
43 | SymbolKind::Static => lsp_types::SymbolKind::Constant, | 43 | SymbolKind::Static => lsp_types::SymbolKind::Constant, |
44 | SymbolKind::Const => lsp_types::SymbolKind::Constant, | 44 | SymbolKind::Const => lsp_types::SymbolKind::Constant, |
45 | SymbolKind::ConstParam => lsp_types::SymbolKind::Constant, | ||
45 | SymbolKind::Impl => lsp_types::SymbolKind::Object, | 46 | SymbolKind::Impl => lsp_types::SymbolKind::Object, |
46 | SymbolKind::Local | 47 | SymbolKind::Local |
47 | | SymbolKind::SelfParam | 48 | | SymbolKind::SelfParam |
@@ -378,6 +379,7 @@ fn semantic_token_type_and_modifiers( | |||
378 | SymbolKind::Impl => lsp_types::SemanticTokenType::TYPE, | 379 | SymbolKind::Impl => lsp_types::SemanticTokenType::TYPE, |
379 | SymbolKind::Field => lsp_types::SemanticTokenType::PROPERTY, | 380 | SymbolKind::Field => lsp_types::SemanticTokenType::PROPERTY, |
380 | SymbolKind::TypeParam => lsp_types::SemanticTokenType::TYPE_PARAMETER, | 381 | SymbolKind::TypeParam => lsp_types::SemanticTokenType::TYPE_PARAMETER, |
382 | SymbolKind::ConstParam => semantic_tokens::CONST_PARAMETER, | ||
381 | SymbolKind::LifetimeParam => semantic_tokens::LIFETIME, | 383 | SymbolKind::LifetimeParam => semantic_tokens::LIFETIME, |
382 | SymbolKind::Label => semantic_tokens::LABEL, | 384 | SymbolKind::Label => semantic_tokens::LABEL, |
383 | SymbolKind::ValueParam => lsp_types::SemanticTokenType::PARAMETER, | 385 | SymbolKind::ValueParam => lsp_types::SemanticTokenType::PARAMETER, |