diff options
author | Aramis Razzaghipour <[email protected]> | 2021-05-23 14:45:26 +0100 |
---|---|---|
committer | Aramis Razzaghipour <[email protected]> | 2021-05-24 05:53:48 +0100 |
commit | 4fd5248749202e0578d719bc5480171a85358836 (patch) | |
tree | 69d1e6928bb9b7fa527835703890c4ee5343e5ca /crates | |
parent | 064ff633f6055ed35022222c89af01d3f9943e3c (diff) |
Add highlighting of items from other crates
Diffstat (limited to 'crates')
-rw-r--r-- | crates/hir/src/lib.rs | 17 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting.rs | 3 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/highlight.rs | 89 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/tags.rs | 4 | ||||
-rw-r--r-- | crates/rust-analyzer/src/semantic_tokens.rs | 1 | ||||
-rw-r--r-- | crates/rust-analyzer/src/to_proto.rs | 1 |
6 files changed, 103 insertions, 12 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index a7c42ca1e..6522a924b 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs | |||
@@ -673,6 +673,11 @@ impl Variant { | |||
673 | pub fn module(self, db: &dyn HirDatabase) -> Module { | 673 | pub fn module(self, db: &dyn HirDatabase) -> Module { |
674 | self.parent.module(db) | 674 | self.parent.module(db) |
675 | } | 675 | } |
676 | |||
677 | pub fn krate(self, db: &dyn HirDatabase) -> Crate { | ||
678 | self.module(db).krate() | ||
679 | } | ||
680 | |||
676 | pub fn parent_enum(self, _db: &dyn HirDatabase) -> Enum { | 681 | pub fn parent_enum(self, _db: &dyn HirDatabase) -> Enum { |
677 | self.parent | 682 | self.parent |
678 | } | 683 | } |
@@ -767,6 +772,10 @@ impl VariantDef { | |||
767 | } | 772 | } |
768 | } | 773 | } |
769 | 774 | ||
775 | pub fn krate(self, db: &dyn HirDatabase) -> Crate { | ||
776 | self.module(db).krate() | ||
777 | } | ||
778 | |||
770 | pub fn name(&self, db: &dyn HirDatabase) -> Name { | 779 | pub fn name(&self, db: &dyn HirDatabase) -> Name { |
771 | match self { | 780 | match self { |
772 | VariantDef::Struct(s) => s.name(db), | 781 | VariantDef::Struct(s) => s.name(db), |
@@ -1074,6 +1083,10 @@ impl Trait { | |||
1074 | Module { id: self.id.lookup(db.upcast()).container } | 1083 | Module { id: self.id.lookup(db.upcast()).container } |
1075 | } | 1084 | } |
1076 | 1085 | ||
1086 | pub fn krate(self, db: &dyn HirDatabase) -> Crate { | ||
1087 | self.module(db).krate() | ||
1088 | } | ||
1089 | |||
1077 | pub fn name(self, db: &dyn HirDatabase) -> Name { | 1090 | pub fn name(self, db: &dyn HirDatabase) -> Name { |
1078 | db.trait_data(self.id).name.clone() | 1091 | db.trait_data(self.id).name.clone() |
1079 | } | 1092 | } |
@@ -1178,6 +1191,10 @@ impl MacroDef { | |||
1178 | Some(Module { id: def_map.module_id(module_id) }) | 1191 | Some(Module { id: def_map.module_id(module_id) }) |
1179 | } | 1192 | } |
1180 | 1193 | ||
1194 | pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> { | ||
1195 | self.module(db).map(Module::krate) | ||
1196 | } | ||
1197 | |||
1181 | /// XXX: this parses the file | 1198 | /// XXX: this parses the file |
1182 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | 1199 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { |
1183 | match self.source(db)?.value { | 1200 | match self.source(db)?.value { |
diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 9df8d21af..cf1a8bad7 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs | |||
@@ -80,6 +80,7 @@ pub(crate) fn highlight( | |||
80 | &mut hl, | 80 | &mut hl, |
81 | &sema, | 81 | &sema, |
82 | InFile::new(file_id.into(), &root), | 82 | InFile::new(file_id.into(), &root), |
83 | sema.scope(&root).krate(), | ||
83 | range_to_highlight, | 84 | range_to_highlight, |
84 | syntactic_name_ref_highlighting, | 85 | syntactic_name_ref_highlighting, |
85 | ); | 86 | ); |
@@ -90,6 +91,7 @@ fn traverse( | |||
90 | hl: &mut Highlights, | 91 | hl: &mut Highlights, |
91 | sema: &Semantics<RootDatabase>, | 92 | sema: &Semantics<RootDatabase>, |
92 | root: InFile<&SyntaxNode>, | 93 | root: InFile<&SyntaxNode>, |
94 | krate: Option<hir::Crate>, | ||
93 | range_to_highlight: TextRange, | 95 | range_to_highlight: TextRange, |
94 | syntactic_name_ref_highlighting: bool, | 96 | syntactic_name_ref_highlighting: bool, |
95 | ) { | 97 | ) { |
@@ -209,6 +211,7 @@ fn traverse( | |||
209 | 211 | ||
210 | if let Some((mut highlight, binding_hash)) = highlight::element( | 212 | if let Some((mut highlight, binding_hash)) = highlight::element( |
211 | &sema, | 213 | &sema, |
214 | krate, | ||
212 | &mut bindings_shadow_count, | 215 | &mut bindings_shadow_count, |
213 | syntactic_name_ref_highlighting, | 216 | syntactic_name_ref_highlighting, |
214 | element_to_highlight.clone(), | 217 | element_to_highlight.clone(), |
diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 058e37ff0..2d6d8d100 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs | |||
@@ -19,6 +19,7 @@ use crate::{ | |||
19 | 19 | ||
20 | pub(super) fn element( | 20 | pub(super) fn element( |
21 | sema: &Semantics<RootDatabase>, | 21 | sema: &Semantics<RootDatabase>, |
22 | krate: Option<hir::Crate>, | ||
22 | bindings_shadow_count: &mut FxHashMap<hir::Name, u32>, | 23 | bindings_shadow_count: &mut FxHashMap<hir::Name, u32>, |
23 | syntactic_name_ref_highlighting: bool, | 24 | syntactic_name_ref_highlighting: bool, |
24 | element: SyntaxElement, | 25 | element: SyntaxElement, |
@@ -46,8 +47,10 @@ pub(super) fn element( | |||
46 | 47 | ||
47 | match name_kind { | 48 | match name_kind { |
48 | Some(NameClass::ExternCrate(_)) => SymbolKind::Module.into(), | 49 | Some(NameClass::ExternCrate(_)) => SymbolKind::Module.into(), |
49 | Some(NameClass::Definition(def)) => highlight_def(db, def) | HlMod::Definition, | 50 | Some(NameClass::Definition(def)) => { |
50 | Some(NameClass::ConstReference(def)) => highlight_def(db, def), | 51 | highlight_def(db, krate, def) | HlMod::Definition |
52 | } | ||
53 | Some(NameClass::ConstReference(def)) => highlight_def(db, krate, def), | ||
51 | Some(NameClass::PatFieldShorthand { field_ref, .. }) => { | 54 | Some(NameClass::PatFieldShorthand { field_ref, .. }) => { |
52 | let mut h = HlTag::Symbol(SymbolKind::Field).into(); | 55 | let mut h = HlTag::Symbol(SymbolKind::Field).into(); |
53 | if let Definition::Field(field) = field_ref { | 56 | if let Definition::Field(field) = field_ref { |
@@ -82,7 +85,7 @@ pub(super) fn element( | |||
82 | } | 85 | } |
83 | }; | 86 | }; |
84 | 87 | ||
85 | let mut h = highlight_def(db, def); | 88 | let mut h = highlight_def(db, krate, def); |
86 | 89 | ||
87 | if let Definition::Local(local) = &def { | 90 | if let Definition::Local(local) = &def { |
88 | if is_consumed_lvalue(name_ref.syntax().clone().into(), local, db) { | 91 | if is_consumed_lvalue(name_ref.syntax().clone().into(), local, db) { |
@@ -136,9 +139,11 @@ pub(super) fn element( | |||
136 | let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap(); | 139 | let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap(); |
137 | 140 | ||
138 | match NameClass::classify_lifetime(sema, &lifetime) { | 141 | match NameClass::classify_lifetime(sema, &lifetime) { |
139 | Some(NameClass::Definition(def)) => highlight_def(db, def) | HlMod::Definition, | 142 | Some(NameClass::Definition(def)) => { |
143 | highlight_def(db, krate, def) | HlMod::Definition | ||
144 | } | ||
140 | None => match NameRefClass::classify_lifetime(sema, &lifetime) { | 145 | None => match NameRefClass::classify_lifetime(sema, &lifetime) { |
141 | Some(NameRefClass::Definition(def)) => highlight_def(db, def), | 146 | Some(NameRefClass::Definition(def)) => highlight_def(db, krate, def), |
142 | _ => SymbolKind::LifetimeParam.into(), | 147 | _ => SymbolKind::LifetimeParam.into(), |
143 | }, | 148 | }, |
144 | _ => Highlight::from(SymbolKind::LifetimeParam) | HlMod::Definition, | 149 | _ => Highlight::from(SymbolKind::LifetimeParam) | HlMod::Definition, |
@@ -277,10 +282,26 @@ pub(super) fn element( | |||
277 | hash((name, shadow_count)) | 282 | hash((name, shadow_count)) |
278 | } | 283 | } |
279 | } | 284 | } |
280 | fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { | 285 | fn highlight_def(db: &RootDatabase, krate: Option<hir::Crate>, def: Definition) -> Highlight { |
281 | match def { | 286 | match def { |
282 | Definition::Macro(_) => HlTag::Symbol(SymbolKind::Macro), | 287 | Definition::Macro(m) => { |
283 | Definition::Field(_) => HlTag::Symbol(SymbolKind::Field), | 288 | let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Macro)); |
289 | |||
290 | if m.krate(db) != krate { | ||
291 | h |= HlMod::Foreign; | ||
292 | } | ||
293 | |||
294 | return h; | ||
295 | } | ||
296 | Definition::Field(field) => { | ||
297 | let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Field)); | ||
298 | |||
299 | if Some(field.parent_def(db).krate(db)) != krate { | ||
300 | h |= HlMod::Foreign; | ||
301 | } | ||
302 | |||
303 | return h; | ||
304 | } | ||
284 | Definition::ModuleDef(def) => match def { | 305 | Definition::ModuleDef(def) => match def { |
285 | hir::ModuleDef::Module(_) => HlTag::Symbol(SymbolKind::Module), | 306 | hir::ModuleDef::Module(_) => HlTag::Symbol(SymbolKind::Module), |
286 | hir::ModuleDef::Function(func) => { | 307 | hir::ModuleDef::Function(func) => { |
@@ -314,14 +335,37 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { | |||
314 | if func.is_async(db) { | 335 | if func.is_async(db) { |
315 | h |= HlMod::Async; | 336 | h |= HlMod::Async; |
316 | } | 337 | } |
338 | if func.krate(db) != krate { | ||
339 | h |= HlMod::Foreign; | ||
340 | } | ||
341 | return h; | ||
342 | } | ||
343 | hir::ModuleDef::Adt(adt) => { | ||
344 | let h = match adt { | ||
345 | hir::Adt::Struct(_) => HlTag::Symbol(SymbolKind::Struct), | ||
346 | hir::Adt::Enum(_) => HlTag::Symbol(SymbolKind::Enum), | ||
347 | hir::Adt::Union(_) => HlTag::Symbol(SymbolKind::Union), | ||
348 | }; | ||
349 | let mut h = Highlight::new(h); | ||
350 | |||
351 | if Some(adt.krate(db)) != krate { | ||
352 | h |= HlMod::Foreign; | ||
353 | } | ||
354 | |||
355 | return h; | ||
356 | } | ||
357 | hir::ModuleDef::Variant(variant) => { | ||
358 | let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Variant)); | ||
359 | |||
360 | if Some(variant.krate(db)) != krate { | ||
361 | h |= HlMod::Foreign; | ||
362 | } | ||
363 | |||
317 | return h; | 364 | return h; |
318 | } | 365 | } |
319 | hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HlTag::Symbol(SymbolKind::Struct), | ||
320 | hir::ModuleDef::Adt(hir::Adt::Enum(_)) => HlTag::Symbol(SymbolKind::Enum), | ||
321 | hir::ModuleDef::Adt(hir::Adt::Union(_)) => HlTag::Symbol(SymbolKind::Union), | ||
322 | hir::ModuleDef::Variant(_) => HlTag::Symbol(SymbolKind::Variant), | ||
323 | hir::ModuleDef::Const(konst) => { | 366 | hir::ModuleDef::Const(konst) => { |
324 | let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Const)); | 367 | let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Const)); |
368 | |||
325 | if let Some(item) = konst.as_assoc_item(db) { | 369 | if let Some(item) = konst.as_assoc_item(db) { |
326 | h |= HlMod::Associated; | 370 | h |= HlMod::Associated; |
327 | match item.container(db) { | 371 | match item.container(db) { |
@@ -336,6 +380,10 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { | |||
336 | } | 380 | } |
337 | } | 381 | } |
338 | 382 | ||
383 | if konst.krate(db) != krate { | ||
384 | h |= HlMod::Foreign; | ||
385 | } | ||
386 | |||
339 | return h; | 387 | return h; |
340 | } | 388 | } |
341 | hir::ModuleDef::Trait(trait_) => { | 389 | hir::ModuleDef::Trait(trait_) => { |
@@ -344,10 +392,16 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { | |||
344 | if trait_.is_unsafe(db) { | 392 | if trait_.is_unsafe(db) { |
345 | h |= HlMod::Unsafe; | 393 | h |= HlMod::Unsafe; |
346 | } | 394 | } |
395 | |||
396 | if Some(trait_.krate(db)) != krate { | ||
397 | h |= HlMod::Foreign; | ||
398 | } | ||
399 | |||
347 | return h; | 400 | return h; |
348 | } | 401 | } |
349 | hir::ModuleDef::TypeAlias(type_) => { | 402 | hir::ModuleDef::TypeAlias(type_) => { |
350 | let mut h = Highlight::new(HlTag::Symbol(SymbolKind::TypeAlias)); | 403 | let mut h = Highlight::new(HlTag::Symbol(SymbolKind::TypeAlias)); |
404 | |||
351 | if let Some(item) = type_.as_assoc_item(db) { | 405 | if let Some(item) = type_.as_assoc_item(db) { |
352 | h |= HlMod::Associated; | 406 | h |= HlMod::Associated; |
353 | match item.container(db) { | 407 | match item.container(db) { |
@@ -361,15 +415,26 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { | |||
361 | } | 415 | } |
362 | } | 416 | } |
363 | } | 417 | } |
418 | |||
419 | if Some(type_.krate(db)) != krate { | ||
420 | h |= HlMod::Foreign; | ||
421 | } | ||
422 | |||
364 | return h; | 423 | return h; |
365 | } | 424 | } |
366 | hir::ModuleDef::BuiltinType(_) => HlTag::BuiltinType, | 425 | hir::ModuleDef::BuiltinType(_) => HlTag::BuiltinType, |
367 | hir::ModuleDef::Static(s) => { | 426 | hir::ModuleDef::Static(s) => { |
368 | let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Static)); | 427 | let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Static)); |
428 | |||
369 | if s.is_mut(db) { | 429 | if s.is_mut(db) { |
370 | h |= HlMod::Mutable; | 430 | h |= HlMod::Mutable; |
371 | h |= HlMod::Unsafe; | 431 | h |= HlMod::Unsafe; |
372 | } | 432 | } |
433 | |||
434 | if s.krate(db) != krate { | ||
435 | h |= HlMod::Foreign; | ||
436 | } | ||
437 | |||
373 | return h; | 438 | return h; |
374 | } | 439 | } |
375 | }, | 440 | }, |
diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index 27473a2f9..755599cba 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs | |||
@@ -67,6 +67,8 @@ pub enum HlMod { | |||
67 | Trait, | 67 | Trait, |
68 | /// Used with keywords like `async` and `await`. | 68 | /// Used with keywords like `async` and `await`. |
69 | Async, | 69 | Async, |
70 | /// Used for items from other crates. | ||
71 | Foreign, | ||
70 | // Keep this last! | 72 | // Keep this last! |
71 | /// Used for unsafe functions, unsafe traits, mutable statics, union accesses and unsafe operations. | 73 | /// Used for unsafe functions, unsafe traits, mutable statics, union accesses and unsafe operations. |
72 | Unsafe, | 74 | Unsafe, |
@@ -189,6 +191,7 @@ impl HlMod { | |||
189 | HlMod::Static, | 191 | HlMod::Static, |
190 | HlMod::Trait, | 192 | HlMod::Trait, |
191 | HlMod::Async, | 193 | HlMod::Async, |
194 | HlMod::Foreign, | ||
192 | HlMod::Unsafe, | 195 | HlMod::Unsafe, |
193 | ]; | 196 | ]; |
194 | 197 | ||
@@ -207,6 +210,7 @@ impl HlMod { | |||
207 | HlMod::Static => "static", | 210 | HlMod::Static => "static", |
208 | HlMod::Trait => "trait", | 211 | HlMod::Trait => "trait", |
209 | HlMod::Async => "async", | 212 | HlMod::Async => "async", |
213 | HlMod::Foreign => "foreign", | ||
210 | HlMod::Unsafe => "unsafe", | 214 | HlMod::Unsafe => "unsafe", |
211 | } | 215 | } |
212 | } | 216 | } |
diff --git a/crates/rust-analyzer/src/semantic_tokens.rs b/crates/rust-analyzer/src/semantic_tokens.rs index 4fd576adb..9ebe000b5 100644 --- a/crates/rust-analyzer/src/semantic_tokens.rs +++ b/crates/rust-analyzer/src/semantic_tokens.rs | |||
@@ -92,6 +92,7 @@ define_semantic_token_modifiers![ | |||
92 | (MUTABLE, "mutable"), | 92 | (MUTABLE, "mutable"), |
93 | (CONSUMING, "consuming"), | 93 | (CONSUMING, "consuming"), |
94 | (ASYNC, "async"), | 94 | (ASYNC, "async"), |
95 | (FOREIGN, "foreign"), | ||
95 | (UNSAFE, "unsafe"), | 96 | (UNSAFE, "unsafe"), |
96 | (ATTRIBUTE_MODIFIER, "attribute"), | 97 | (ATTRIBUTE_MODIFIER, "attribute"), |
97 | (TRAIT_MODIFIER, "trait"), | 98 | (TRAIT_MODIFIER, "trait"), |
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 0a3a56773..6368ba413 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs | |||
@@ -504,6 +504,7 @@ fn semantic_token_type_and_modifiers( | |||
504 | HlMod::Mutable => semantic_tokens::MUTABLE, | 504 | HlMod::Mutable => semantic_tokens::MUTABLE, |
505 | HlMod::Consuming => semantic_tokens::CONSUMING, | 505 | HlMod::Consuming => semantic_tokens::CONSUMING, |
506 | HlMod::Async => semantic_tokens::ASYNC, | 506 | HlMod::Async => semantic_tokens::ASYNC, |
507 | HlMod::Foreign => semantic_tokens::FOREIGN, | ||
507 | HlMod::Unsafe => semantic_tokens::UNSAFE, | 508 | HlMod::Unsafe => semantic_tokens::UNSAFE, |
508 | HlMod::Callable => semantic_tokens::CALLABLE, | 509 | HlMod::Callable => semantic_tokens::CALLABLE, |
509 | HlMod::Static => lsp_types::SemanticTokenModifier::STATIC, | 510 | HlMod::Static => lsp_types::SemanticTokenModifier::STATIC, |