aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/syntax_highlighting
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/syntax_highlighting')
-rw-r--r--crates/ide/src/syntax_highlighting/highlight.rs89
-rw-r--r--crates/ide/src/syntax_highlighting/tags.rs4
2 files changed, 81 insertions, 12 deletions
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
20pub(super) fn element( 20pub(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}
280fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { 285fn 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 }