aboutsummaryrefslogtreecommitdiff
path: root/crates/ide
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide')
-rw-r--r--crates/ide/src/display/navigation_target.rs19
-rw-r--r--crates/ide/src/doc_links.rs1
-rw-r--r--crates/ide/src/hover.rs47
-rw-r--r--crates/ide/src/references.rs16
-rw-r--r--crates/ide/src/runnables.rs2
-rw-r--r--crates/ide/src/syntax_highlighting.rs1
-rw-r--r--crates/ide/src/syntax_highlighting/injection.rs13
-rw-r--r--crates/ide/src/syntax_highlighting/tags.rs1
-rw-r--r--crates/ide/src/syntax_highlighting/test_data/highlighting.html4
-rw-r--r--crates/ide/src/syntax_highlighting/tests.rs4
10 files changed, 101 insertions, 7 deletions
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
490impl 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..98c7bfbe5 100644
--- a/crates/ide/src/hover.rs
+++ b/crates/ide/src/hover.rs
@@ -109,6 +109,8 @@ pub(crate) fn hover(
109 match node { 109 match node {
110 ast::Name(name) => NameClass::classify(&sema, &name).and_then(|d| d.defined(sema.db)), 110 ast::Name(name) => NameClass::classify(&sema, &name).and_then(|d| d.defined(sema.db)),
111 ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)), 111 ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)),
112 ast::Lifetime(lifetime) => NameClass::classify_lifetime(&sema, &lifetime)
113 .map_or_else(|| NameRefClass::classify_lifetime(&sema, &lifetime).map(|d| d.referenced(sema.db)), |d| d.defined(sema.db)),
112 _ => None, 114 _ => None,
113 } 115 }
114 }; 116 };
@@ -360,9 +362,9 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
360 ModuleDef::Static(it) => from_def_source(db, it, mod_path), 362 ModuleDef::Static(it) => from_def_source(db, it, mod_path),
361 ModuleDef::Trait(it) => from_def_source(db, it, mod_path), 363 ModuleDef::Trait(it) => from_def_source(db, it, mod_path),
362 ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path), 364 ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path),
363 ModuleDef::BuiltinType(it) => return Some(it.to_string().into()), 365 ModuleDef::BuiltinType(it) => Some(Markup::fenced_block(&it)),
364 }, 366 },
365 Definition::Local(it) => return Some(Markup::fenced_block(&it.ty(db).display(db))), 367 Definition::Local(it) => Some(Markup::fenced_block(&it.ty(db).display(db))),
366 Definition::SelfType(impl_def) => { 368 Definition::SelfType(impl_def) => {
367 impl_def.target_ty(db).as_adt().and_then(|adt| match adt { 369 impl_def.target_ty(db).as_adt().and_then(|adt| match adt {
368 Adt::Struct(it) => from_def_source(db, it, mod_path), 370 Adt::Struct(it) => from_def_source(db, it, mod_path),
@@ -370,7 +372,9 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
370 Adt::Enum(it) => from_def_source(db, it, mod_path), 372 Adt::Enum(it) => from_def_source(db, it, mod_path),
371 }) 373 })
372 } 374 }
373 Definition::TypeParam(_) | Definition::LifetimeParam(_) | Definition::Label(_) => { 375 Definition::Label(it) => Some(Markup::fenced_block(&it.name(db))),
376 Definition::LifetimeParam(it) => Some(Markup::fenced_block(&it.name(db))),
377 Definition::TypeParam(_) | Definition::ConstParam(_) => {
374 // FIXME: Hover for generic param 378 // FIXME: Hover for generic param
375 None 379 None
376 } 380 }
@@ -403,7 +407,7 @@ fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
403 return tokens.max_by_key(priority); 407 return tokens.max_by_key(priority);
404 fn priority(n: &SyntaxToken) -> usize { 408 fn priority(n: &SyntaxToken) -> usize {
405 match n.kind() { 409 match n.kind() {
406 IDENT | INT_NUMBER => 3, 410 IDENT | INT_NUMBER | LIFETIME_IDENT => 3,
407 T!['('] | T![')'] => 2, 411 T!['('] | T![')'] => 2,
408 kind if kind.is_trivia() => 0, 412 kind if kind.is_trivia() => 0,
409 _ => 1, 413 _ => 1,
@@ -1169,7 +1173,10 @@ fn f() { fo<|>o!(); }
1169 r#"struct TS(String, i32<|>);"#, 1173 r#"struct TS(String, i32<|>);"#,
1170 expect![[r#" 1174 expect![[r#"
1171 *i32* 1175 *i32*
1176
1177 ```rust
1172 i32 1178 i32
1179 ```
1173 "#]], 1180 "#]],
1174 ) 1181 )
1175 } 1182 }
@@ -3221,4 +3228,36 @@ fn no_hover() {
3221"#, 3228"#,
3222 ); 3229 );
3223 } 3230 }
3231
3232 #[test]
3233 fn hover_label() {
3234 check(
3235 r#"
3236fn foo() {
3237 'label<|>: loop {}
3238}
3239"#,
3240 expect![[r#"
3241 *'label*
3242
3243 ```rust
3244 'label
3245 ```
3246 "#]],
3247 );
3248 }
3249
3250 #[test]
3251 fn hover_lifetime() {
3252 check(
3253 r#"fn foo<'lifetime>(_: &'lifetime<|> ()) {}"#,
3254 expect![[r#"
3255 *'lifetime*
3256
3257 ```rust
3258 'lifetime
3259 ```
3260 "#]],
3261 );
3262 }
3224} 3263}
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#"
1152fn 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/runnables.rs b/crates/ide/src/runnables.rs
index 891183266..c893afc7c 100644
--- a/crates/ide/src/runnables.rs
+++ b/crates/ide/src/runnables.rs
@@ -230,7 +230,7 @@ impl TestAttr {
230 230
231const RUSTDOC_FENCE: &str = "```"; 231const RUSTDOC_FENCE: &str = "```";
232const RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUNNABLE: &[&str] = 232const RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUNNABLE: &[&str] =
233 &["", "rust", "should_panic", "edition2015", "edition2018"]; 233 &["", "rust", "should_panic", "edition2015", "edition2018", "edition2021"];
234 234
235fn has_runnable_doc_test(attrs: &hir::Attrs) -> bool { 235fn has_runnable_doc_test(attrs: &hir::Attrs) -> bool {
236 attrs.docs().map_or(false, |doc| { 236 attrs.docs().map_or(false, |doc| {
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/injection.rs b/crates/ide/src/syntax_highlighting/injection.rs
index 9eb184c74..6cbd683c6 100644
--- a/crates/ide/src/syntax_highlighting/injection.rs
+++ b/crates/ide/src/syntax_highlighting/injection.rs
@@ -54,8 +54,17 @@ pub(super) fn highlight_injection(
54type RangesMap = BTreeMap<TextSize, TextSize>; 54type RangesMap = BTreeMap<TextSize, TextSize>;
55 55
56const RUSTDOC_FENCE: &'static str = "```"; 56const RUSTDOC_FENCE: &'static str = "```";
57const RUSTDOC_FENCE_TOKENS: &[&'static str] = 57const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[
58 &["", "rust", "should_panic", "ignore", "no_run", "compile_fail", "edition2015", "edition2018"]; 58 "",
59 "rust",
60 "should_panic",
61 "ignore",
62 "no_run",
63 "compile_fail",
64 "edition2015",
65 "edition2018",
66 "edition2021",
67];
59 68
60/// Extracts Rust code from documentation comments as well as a mapping from 69/// Extracts Rust code from documentation comments as well as a mapping from
61/// the extracted source code back to the original source ranges. 70/// the extracted source code back to the original source ranges.
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">&lt;</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">&gt;</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="operator">-&gt;</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">&lt;</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">-&gt;</span> <span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">&gt;</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">&lt;</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">-&gt;</span> <span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">&gt;</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
94fn const_param<const FOO: usize>() -> usize {
95 FOO
96}
97
94use ops::Fn; 98use ops::Fn;
95fn baz<F: Fn() -> ()>(f: F) { 99fn baz<F: Fn() -> ()>(f: F) {
96 f() 100 f()