From 4fd5248749202e0578d719bc5480171a85358836 Mon Sep 17 00:00:00 2001 From: Aramis Razzaghipour Date: Sun, 23 May 2021 23:45:26 +1000 Subject: Add highlighting of items from other crates --- crates/hir/src/lib.rs | 17 +++++ crates/ide/src/syntax_highlighting.rs | 3 + crates/ide/src/syntax_highlighting/highlight.rs | 89 +++++++++++++++++++++---- crates/ide/src/syntax_highlighting/tags.rs | 4 ++ crates/rust-analyzer/src/semantic_tokens.rs | 1 + 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 { pub fn module(self, db: &dyn HirDatabase) -> Module { self.parent.module(db) } + + pub fn krate(self, db: &dyn HirDatabase) -> Crate { + self.module(db).krate() + } + pub fn parent_enum(self, _db: &dyn HirDatabase) -> Enum { self.parent } @@ -767,6 +772,10 @@ impl VariantDef { } } + pub fn krate(self, db: &dyn HirDatabase) -> Crate { + self.module(db).krate() + } + pub fn name(&self, db: &dyn HirDatabase) -> Name { match self { VariantDef::Struct(s) => s.name(db), @@ -1074,6 +1083,10 @@ impl Trait { Module { id: self.id.lookup(db.upcast()).container } } + pub fn krate(self, db: &dyn HirDatabase) -> Crate { + self.module(db).krate() + } + pub fn name(self, db: &dyn HirDatabase) -> Name { db.trait_data(self.id).name.clone() } @@ -1178,6 +1191,10 @@ impl MacroDef { Some(Module { id: def_map.module_id(module_id) }) } + pub fn krate(self, db: &dyn HirDatabase) -> Option { + self.module(db).map(Module::krate) + } + /// XXX: this parses the file pub fn name(self, db: &dyn HirDatabase) -> Option { 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( &mut hl, &sema, InFile::new(file_id.into(), &root), + sema.scope(&root).krate(), range_to_highlight, syntactic_name_ref_highlighting, ); @@ -90,6 +91,7 @@ fn traverse( hl: &mut Highlights, sema: &Semantics, root: InFile<&SyntaxNode>, + krate: Option, range_to_highlight: TextRange, syntactic_name_ref_highlighting: bool, ) { @@ -209,6 +211,7 @@ fn traverse( if let Some((mut highlight, binding_hash)) = highlight::element( &sema, + krate, &mut bindings_shadow_count, syntactic_name_ref_highlighting, 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::{ pub(super) fn element( sema: &Semantics, + krate: Option, bindings_shadow_count: &mut FxHashMap, syntactic_name_ref_highlighting: bool, element: SyntaxElement, @@ -46,8 +47,10 @@ pub(super) fn element( match name_kind { Some(NameClass::ExternCrate(_)) => SymbolKind::Module.into(), - Some(NameClass::Definition(def)) => highlight_def(db, def) | HlMod::Definition, - Some(NameClass::ConstReference(def)) => highlight_def(db, def), + Some(NameClass::Definition(def)) => { + highlight_def(db, krate, def) | HlMod::Definition + } + Some(NameClass::ConstReference(def)) => highlight_def(db, krate, def), Some(NameClass::PatFieldShorthand { field_ref, .. }) => { let mut h = HlTag::Symbol(SymbolKind::Field).into(); if let Definition::Field(field) = field_ref { @@ -82,7 +85,7 @@ pub(super) fn element( } }; - let mut h = highlight_def(db, def); + let mut h = highlight_def(db, krate, def); if let Definition::Local(local) = &def { if is_consumed_lvalue(name_ref.syntax().clone().into(), local, db) { @@ -136,9 +139,11 @@ pub(super) fn element( let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap(); match NameClass::classify_lifetime(sema, &lifetime) { - Some(NameClass::Definition(def)) => highlight_def(db, def) | HlMod::Definition, + Some(NameClass::Definition(def)) => { + highlight_def(db, krate, def) | HlMod::Definition + } None => match NameRefClass::classify_lifetime(sema, &lifetime) { - Some(NameRefClass::Definition(def)) => highlight_def(db, def), + Some(NameRefClass::Definition(def)) => highlight_def(db, krate, def), _ => SymbolKind::LifetimeParam.into(), }, _ => Highlight::from(SymbolKind::LifetimeParam) | HlMod::Definition, @@ -277,10 +282,26 @@ pub(super) fn element( hash((name, shadow_count)) } } -fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { +fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) -> Highlight { match def { - Definition::Macro(_) => HlTag::Symbol(SymbolKind::Macro), - Definition::Field(_) => HlTag::Symbol(SymbolKind::Field), + Definition::Macro(m) => { + let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Macro)); + + if m.krate(db) != krate { + h |= HlMod::Foreign; + } + + return h; + } + Definition::Field(field) => { + let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Field)); + + if Some(field.parent_def(db).krate(db)) != krate { + h |= HlMod::Foreign; + } + + return h; + } Definition::ModuleDef(def) => match def { hir::ModuleDef::Module(_) => HlTag::Symbol(SymbolKind::Module), hir::ModuleDef::Function(func) => { @@ -314,14 +335,37 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { if func.is_async(db) { h |= HlMod::Async; } + if func.krate(db) != krate { + h |= HlMod::Foreign; + } + return h; + } + hir::ModuleDef::Adt(adt) => { + let h = match adt { + hir::Adt::Struct(_) => HlTag::Symbol(SymbolKind::Struct), + hir::Adt::Enum(_) => HlTag::Symbol(SymbolKind::Enum), + hir::Adt::Union(_) => HlTag::Symbol(SymbolKind::Union), + }; + let mut h = Highlight::new(h); + + if Some(adt.krate(db)) != krate { + h |= HlMod::Foreign; + } + + return h; + } + hir::ModuleDef::Variant(variant) => { + let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Variant)); + + if Some(variant.krate(db)) != krate { + h |= HlMod::Foreign; + } + return h; } - hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HlTag::Symbol(SymbolKind::Struct), - hir::ModuleDef::Adt(hir::Adt::Enum(_)) => HlTag::Symbol(SymbolKind::Enum), - hir::ModuleDef::Adt(hir::Adt::Union(_)) => HlTag::Symbol(SymbolKind::Union), - hir::ModuleDef::Variant(_) => HlTag::Symbol(SymbolKind::Variant), hir::ModuleDef::Const(konst) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Const)); + if let Some(item) = konst.as_assoc_item(db) { h |= HlMod::Associated; match item.container(db) { @@ -336,6 +380,10 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { } } + if konst.krate(db) != krate { + h |= HlMod::Foreign; + } + return h; } hir::ModuleDef::Trait(trait_) => { @@ -344,10 +392,16 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { if trait_.is_unsafe(db) { h |= HlMod::Unsafe; } + + if Some(trait_.krate(db)) != krate { + h |= HlMod::Foreign; + } + return h; } hir::ModuleDef::TypeAlias(type_) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::TypeAlias)); + if let Some(item) = type_.as_assoc_item(db) { h |= HlMod::Associated; match item.container(db) { @@ -361,15 +415,26 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { } } } + + if Some(type_.krate(db)) != krate { + h |= HlMod::Foreign; + } + return h; } hir::ModuleDef::BuiltinType(_) => HlTag::BuiltinType, hir::ModuleDef::Static(s) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Static)); + if s.is_mut(db) { h |= HlMod::Mutable; h |= HlMod::Unsafe; } + + if s.krate(db) != krate { + h |= HlMod::Foreign; + } + return h; } }, 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 { Trait, /// Used with keywords like `async` and `await`. Async, + /// Used for items from other crates. + Foreign, // Keep this last! /// Used for unsafe functions, unsafe traits, mutable statics, union accesses and unsafe operations. Unsafe, @@ -189,6 +191,7 @@ impl HlMod { HlMod::Static, HlMod::Trait, HlMod::Async, + HlMod::Foreign, HlMod::Unsafe, ]; @@ -207,6 +210,7 @@ impl HlMod { HlMod::Static => "static", HlMod::Trait => "trait", HlMod::Async => "async", + HlMod::Foreign => "foreign", HlMod::Unsafe => "unsafe", } } 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![ (MUTABLE, "mutable"), (CONSUMING, "consuming"), (ASYNC, "async"), + (FOREIGN, "foreign"), (UNSAFE, "unsafe"), (ATTRIBUTE_MODIFIER, "attribute"), (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( HlMod::Mutable => semantic_tokens::MUTABLE, HlMod::Consuming => semantic_tokens::CONSUMING, HlMod::Async => semantic_tokens::ASYNC, + HlMod::Foreign => semantic_tokens::FOREIGN, HlMod::Unsafe => semantic_tokens::UNSAFE, HlMod::Callable => semantic_tokens::CALLABLE, HlMod::Static => lsp_types::SemanticTokenModifier::STATIC, -- cgit v1.2.3 From c32428571c3fbf12bd4a4187c797488355416a18 Mon Sep 17 00:00:00 2001 From: Aramis Razzaghipour Date: Mon, 24 May 2021 09:42:06 +1000 Subject: Remove hir krate methods --- crates/hir/src/lib.rs | 48 ------------------------- crates/ide/src/inlay_hints.rs | 2 +- crates/ide/src/runnables.rs | 2 +- crates/ide/src/syntax_highlighting/highlight.rs | 18 +++++----- 4 files changed, 11 insertions(+), 59 deletions(-) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 6522a924b..ec5f0c53d 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -552,10 +552,6 @@ impl Struct { Module { id: self.id.lookup(db.upcast()).container } } - pub fn krate(self, db: &dyn HirDatabase) -> Option { - Some(self.module(db).krate()) - } - pub fn name(self, db: &dyn HirDatabase) -> Name { db.struct_data(self.id).name.clone() } @@ -640,10 +636,6 @@ impl Enum { Module { id: self.id.lookup(db.upcast()).container } } - pub fn krate(self, db: &dyn HirDatabase) -> Option { - Some(self.module(db).krate()) - } - pub fn name(self, db: &dyn HirDatabase) -> Name { db.enum_data(self.id).name.clone() } @@ -674,10 +666,6 @@ impl Variant { self.parent.module(db) } - pub fn krate(self, db: &dyn HirDatabase) -> Crate { - self.module(db).krate() - } - pub fn parent_enum(self, _db: &dyn HirDatabase) -> Enum { self.parent } @@ -734,10 +722,6 @@ impl Adt { } } - pub fn krate(self, db: &dyn HirDatabase) -> Crate { - self.module(db).krate() - } - pub fn name(self, db: &dyn HirDatabase) -> Name { match self { Adt::Struct(s) => s.name(db), @@ -772,10 +756,6 @@ impl VariantDef { } } - pub fn krate(self, db: &dyn HirDatabase) -> Crate { - self.module(db).krate() - } - pub fn name(&self, db: &dyn HirDatabase) -> Name { match self { VariantDef::Struct(s) => s.name(db), @@ -830,10 +810,6 @@ impl Function { self.id.lookup(db.upcast()).module(db.upcast()).into() } - pub fn krate(self, db: &dyn HirDatabase) -> Option { - Some(self.module(db).krate()) - } - pub fn name(self, db: &dyn HirDatabase) -> Name { db.function_data(self.id).name.clone() } @@ -1023,10 +999,6 @@ impl Const { Module { id: self.id.lookup(db.upcast()).module(db.upcast()) } } - pub fn krate(self, db: &dyn HirDatabase) -> Option { - Some(self.module(db).krate()) - } - pub fn name(self, db: &dyn HirDatabase) -> Option { db.const_data(self.id).name.clone() } @@ -1054,10 +1026,6 @@ impl Static { Module { id: self.id.lookup(db.upcast()).module(db.upcast()) } } - pub fn krate(self, db: &dyn HirDatabase) -> Option { - Some(self.module(db).krate()) - } - pub fn name(self, db: &dyn HirDatabase) -> Option { db.static_data(self.id).name.clone() } @@ -1083,10 +1051,6 @@ impl Trait { Module { id: self.id.lookup(db.upcast()).container } } - pub fn krate(self, db: &dyn HirDatabase) -> Crate { - self.module(db).krate() - } - pub fn name(self, db: &dyn HirDatabase) -> Name { db.trait_data(self.id).name.clone() } @@ -1125,10 +1089,6 @@ impl TypeAlias { Module { id: self.id.lookup(db.upcast()).module(db.upcast()) } } - pub fn krate(self, db: &dyn HirDatabase) -> Crate { - self.module(db).krate() - } - pub fn type_ref(self, db: &dyn HirDatabase) -> Option { db.type_alias_data(self.id).type_ref.as_deref().cloned() } @@ -1191,10 +1151,6 @@ impl MacroDef { Some(Module { id: def_map.module_id(module_id) }) } - pub fn krate(self, db: &dyn HirDatabase) -> Option { - self.module(db).map(Module::krate) - } - /// XXX: this parses the file pub fn name(self, db: &dyn HirDatabase) -> Option { match self.source(db)?.value { @@ -1684,10 +1640,6 @@ impl Impl { self.id.lookup(db.upcast()).container.into() } - pub fn krate(self, db: &dyn HirDatabase) -> Crate { - Crate { id: self.module(db).id.krate() } - } - pub fn is_builtin_derive(self, db: &dyn HirDatabase) -> Option> { let src = self.source(db)?; let item = src.file_id.is_builtin_derive(db.upcast())?; diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 960d169f4..0ada3f92b 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -219,7 +219,7 @@ fn hint_iterator( ) -> Option { let db = sema.db; let strukt = ty.strip_references().as_adt()?; - let krate = strukt.krate(db); + let krate = strukt.module(db).krate(); if krate != famous_defs.core()? { return None; } diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs index f76715d84..ce1c76f37 100644 --- a/crates/ide/src/runnables.rs +++ b/crates/ide/src/runnables.rs @@ -227,7 +227,7 @@ pub(crate) fn runnable_fn(sema: &Semantics, def: hir::Function) -> let func = def.source(sema.db)?; let name_string = def.name(sema.db).to_string(); - let root = def.krate(sema.db)?.root_module(sema.db); + let root = def.module(sema.db).krate().root_module(sema.db); let kind = if name_string == "main" && def.module(sema.db) == root { RunnableKind::Bin diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 2d6d8d100..574a14429 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -287,7 +287,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) Definition::Macro(m) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Macro)); - if m.krate(db) != krate { + if m.module(db).map(hir::Module::krate) != krate { h |= HlMod::Foreign; } @@ -296,7 +296,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) Definition::Field(field) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Field)); - if Some(field.parent_def(db).krate(db)) != krate { + if Some(field.parent_def(db).module(db).krate()) != krate { h |= HlMod::Foreign; } @@ -335,7 +335,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) if func.is_async(db) { h |= HlMod::Async; } - if func.krate(db) != krate { + if Some(func.module(db).krate()) != krate { h |= HlMod::Foreign; } return h; @@ -348,7 +348,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) }; let mut h = Highlight::new(h); - if Some(adt.krate(db)) != krate { + if Some(adt.module(db).krate()) != krate { h |= HlMod::Foreign; } @@ -357,7 +357,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) hir::ModuleDef::Variant(variant) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Variant)); - if Some(variant.krate(db)) != krate { + if Some(variant.module(db).krate()) != krate { h |= HlMod::Foreign; } @@ -380,7 +380,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) } } - if konst.krate(db) != krate { + if Some(konst.module(db).krate()) != krate { h |= HlMod::Foreign; } @@ -393,7 +393,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) h |= HlMod::Unsafe; } - if Some(trait_.krate(db)) != krate { + if Some(trait_.module(db).krate()) != krate { h |= HlMod::Foreign; } @@ -416,7 +416,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) } } - if Some(type_.krate(db)) != krate { + if Some(type_.module(db).krate()) != krate { h |= HlMod::Foreign; } @@ -431,7 +431,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) h |= HlMod::Unsafe; } - if s.krate(db) != krate { + if Some(s.module(db).krate()) != krate { h |= HlMod::Foreign; } -- cgit v1.2.3 From b4cddc0705665f549b7feaf9899d91cd6ced4931 Mon Sep 17 00:00:00 2001 From: Aramis Razzaghipour Date: Mon, 24 May 2021 13:15:54 +1000 Subject: Highlight foreign modules as such --- crates/ide/src/syntax_highlighting/highlight.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 574a14429..141536dcc 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -303,7 +303,15 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) return h; } Definition::ModuleDef(def) => match def { - hir::ModuleDef::Module(_) => HlTag::Symbol(SymbolKind::Module), + hir::ModuleDef::Module(module) => { + let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Module)); + + if Some(module.krate()) != krate { + h |= HlMod::Foreign; + } + + return h; + } hir::ModuleDef::Function(func) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Function)); if let Some(item) = func.as_assoc_item(db) { -- cgit v1.2.3 From fa0d0bfb7fc402baf00ea6c6a0d87fb22157a3fb Mon Sep 17 00:00:00 2001 From: Aramis Razzaghipour Date: Mon, 24 May 2021 13:16:08 +1000 Subject: Add testing of foreign item highlighting --- .../test_data/highlighting.html | 18 +++++++++- crates/ide/src/syntax_highlighting/tests.rs | 38 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html index 878431b56..e65dd3ff9 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html @@ -248,4 +248,20 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd } unsafe trait Dangerous {} -impl Dangerous for () {} \ No newline at end of file +impl Dangerous for () {} + +fn use_foo_items() { + let bob = foo::Person { + name: "Bob", + age: foo::consts::NUMBER, + }; + + let control_flow = foo::identity(foo::ControlFlow::Continue); + + if let foo::ControlFlow::Die = control_flow { + foo::die!(); + } +} + + + \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index 9ce26e930..be4447ebb 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -10,6 +10,7 @@ use crate::{fixture, FileRange, HlTag, TextRange}; fn test_highlighting() { check_highlighting( r#" +//- /main.rs crate:main deps:foo use inner::{self as inner_mod}; mod inner {} @@ -222,6 +223,43 @@ async fn async_main() { unsafe trait Dangerous {} impl Dangerous for () {} + +fn use_foo_items() { + let bob = foo::Person { + name: "Bob", + age: foo::consts::NUMBER, + }; + + let control_flow = foo::identity(foo::ControlFlow::Continue); + + if let foo::ControlFlow::Die = control_flow { + foo::die!(); + } +} + + +//- /foo.rs crate:foo +pub struct Person { + pub name: &'static str, + pub age: u8, +} + +pub enum ControlFlow { + Continue, + Die, +} + +pub fn identity(x: T) -> T { x } + +pub mod consts { + pub const NUMBER: i64 = 92; +} + +macro_rules! die { + () => { + panic!(); + }; +} "# .trim(), expect_file!["./test_data/highlighting.html"], -- cgit v1.2.3 From e51188cd09d0a78de20d1fdde866fa5a4a31e938 Mon Sep 17 00:00:00 2001 From: Aramis Razzaghipour Date: Mon, 24 May 2021 14:48:42 +1000 Subject: =?UTF-8?q?Rename=20=E2=80=98foreign=E2=80=99=20semantic=20token?= =?UTF-8?q?=20modifier=20to=20=E2=80=98library=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/ide/src/syntax_highlighting/highlight.rs | 20 ++++++++++---------- crates/ide/src/syntax_highlighting/tags.rs | 6 +++--- .../syntax_highlighting/test_data/highlighting.html | 10 +++++----- crates/rust-analyzer/src/semantic_tokens.rs | 2 +- crates/rust-analyzer/src/to_proto.rs | 2 +- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 141536dcc..f070601fd 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -288,7 +288,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Macro)); if m.module(db).map(hir::Module::krate) != krate { - h |= HlMod::Foreign; + h |= HlMod::Library; } return h; @@ -297,7 +297,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Field)); if Some(field.parent_def(db).module(db).krate()) != krate { - h |= HlMod::Foreign; + h |= HlMod::Library; } return h; @@ -307,7 +307,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Module)); if Some(module.krate()) != krate { - h |= HlMod::Foreign; + h |= HlMod::Library; } return h; @@ -344,7 +344,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) h |= HlMod::Async; } if Some(func.module(db).krate()) != krate { - h |= HlMod::Foreign; + h |= HlMod::Library; } return h; } @@ -357,7 +357,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) let mut h = Highlight::new(h); if Some(adt.module(db).krate()) != krate { - h |= HlMod::Foreign; + h |= HlMod::Library; } return h; @@ -366,7 +366,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Variant)); if Some(variant.module(db).krate()) != krate { - h |= HlMod::Foreign; + h |= HlMod::Library; } return h; @@ -389,7 +389,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) } if Some(konst.module(db).krate()) != krate { - h |= HlMod::Foreign; + h |= HlMod::Library; } return h; @@ -402,7 +402,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) } if Some(trait_.module(db).krate()) != krate { - h |= HlMod::Foreign; + h |= HlMod::Library; } return h; @@ -425,7 +425,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) } if Some(type_.module(db).krate()) != krate { - h |= HlMod::Foreign; + h |= HlMod::Library; } return h; @@ -440,7 +440,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) } if Some(s.module(db).krate()) != krate { - h |= HlMod::Foreign; + h |= HlMod::Library; } return h; diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index 755599cba..e94f17cd9 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -68,7 +68,7 @@ pub enum HlMod { /// Used with keywords like `async` and `await`. Async, /// Used for items from other crates. - Foreign, + Library, // Keep this last! /// Used for unsafe functions, unsafe traits, mutable statics, union accesses and unsafe operations. Unsafe, @@ -191,7 +191,7 @@ impl HlMod { HlMod::Static, HlMod::Trait, HlMod::Async, - HlMod::Foreign, + HlMod::Library, HlMod::Unsafe, ]; @@ -210,7 +210,7 @@ impl HlMod { HlMod::Static => "static", HlMod::Trait => "trait", HlMod::Async => "async", - HlMod::Foreign => "foreign", + HlMod::Library => "library", HlMod::Unsafe => "unsafe", } } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html index e65dd3ff9..055d21109 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html @@ -251,14 +251,14 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd impl Dangerous for () {} fn use_foo_items() { - let bob = foo::Person { - name: "Bob", - age: foo::consts::NUMBER, + let bob = foo::Person { + name: "Bob", + age: foo::consts::NUMBER, }; - let control_flow = foo::identity(foo::ControlFlow::Continue); + let control_flow = foo::identity(foo::ControlFlow::Continue); - if let foo::ControlFlow::Die = control_flow { + if let foo::ControlFlow::Die = control_flow { foo::die!(); } } diff --git a/crates/rust-analyzer/src/semantic_tokens.rs b/crates/rust-analyzer/src/semantic_tokens.rs index 9ebe000b5..6129af95f 100644 --- a/crates/rust-analyzer/src/semantic_tokens.rs +++ b/crates/rust-analyzer/src/semantic_tokens.rs @@ -92,7 +92,7 @@ define_semantic_token_modifiers![ (MUTABLE, "mutable"), (CONSUMING, "consuming"), (ASYNC, "async"), - (FOREIGN, "foreign"), + (LIBRARY, "library"), (UNSAFE, "unsafe"), (ATTRIBUTE_MODIFIER, "attribute"), (TRAIT_MODIFIER, "trait"), diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 6368ba413..af5152b9c 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -504,7 +504,7 @@ fn semantic_token_type_and_modifiers( HlMod::Mutable => semantic_tokens::MUTABLE, HlMod::Consuming => semantic_tokens::CONSUMING, HlMod::Async => semantic_tokens::ASYNC, - HlMod::Foreign => semantic_tokens::FOREIGN, + HlMod::Library => semantic_tokens::LIBRARY, HlMod::Unsafe => semantic_tokens::UNSAFE, HlMod::Callable => semantic_tokens::CALLABLE, HlMod::Static => lsp_types::SemanticTokenModifier::STATIC, -- cgit v1.2.3 From eca4b764e4d8653f3862129586f47cb60be4bf3f Mon Sep 17 00:00:00 2001 From: Aramis Razzaghipour Date: Mon, 24 May 2021 14:53:15 +1000 Subject: Remove superfluous early-returns --- crates/ide/src/syntax_highlighting/highlight.rs | 40 ++++++++++++++----------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index f070601fd..14d68d3f3 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -291,7 +291,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) h |= HlMod::Library; } - return h; + h } Definition::Field(field) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Field)); @@ -300,7 +300,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) h |= HlMod::Library; } - return h; + h } Definition::ModuleDef(def) => match def { hir::ModuleDef::Module(module) => { @@ -310,7 +310,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) h |= HlMod::Library; } - return h; + h } hir::ModuleDef::Function(func) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Function)); @@ -346,7 +346,8 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) if Some(func.module(db).krate()) != krate { h |= HlMod::Library; } - return h; + + h } hir::ModuleDef::Adt(adt) => { let h = match adt { @@ -360,7 +361,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) h |= HlMod::Library; } - return h; + h } hir::ModuleDef::Variant(variant) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Variant)); @@ -369,7 +370,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) h |= HlMod::Library; } - return h; + h } hir::ModuleDef::Const(konst) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Const)); @@ -392,7 +393,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) h |= HlMod::Library; } - return h; + h } hir::ModuleDef::Trait(trait_) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Trait)); @@ -405,7 +406,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) h |= HlMod::Library; } - return h; + h } hir::ModuleDef::TypeAlias(type_) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::TypeAlias)); @@ -428,9 +429,9 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) h |= HlMod::Library; } - return h; + h } - hir::ModuleDef::BuiltinType(_) => HlTag::BuiltinType, + hir::ModuleDef::BuiltinType(_) => Highlight::new(HlTag::BuiltinType), hir::ModuleDef::Static(s) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Static)); @@ -443,14 +444,18 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) h |= HlMod::Library; } - return h; + h } }, - Definition::SelfType(_) => HlTag::Symbol(SymbolKind::Impl), + Definition::SelfType(_) => Highlight::new(HlTag::Symbol(SymbolKind::Impl)), Definition::GenericParam(it) => match it { - hir::GenericParam::TypeParam(_) => HlTag::Symbol(SymbolKind::TypeParam), - hir::GenericParam::ConstParam(_) => HlTag::Symbol(SymbolKind::ConstParam), - hir::GenericParam::LifetimeParam(_) => HlTag::Symbol(SymbolKind::LifetimeParam), + hir::GenericParam::TypeParam(_) => Highlight::new(HlTag::Symbol(SymbolKind::TypeParam)), + hir::GenericParam::ConstParam(_) => { + Highlight::new(HlTag::Symbol(SymbolKind::ConstParam)) + } + hir::GenericParam::LifetimeParam(_) => { + Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam)) + } }, Definition::Local(local) => { let tag = if local.is_self(db) { @@ -468,11 +473,10 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) if ty.as_callable(db).is_some() || ty.impls_fnonce(db) { h |= HlMod::Callable; } - return h; + h } - Definition::Label(_) => HlTag::Symbol(SymbolKind::Label), + Definition::Label(_) => Highlight::new(HlTag::Symbol(SymbolKind::Label)), } - .into() } fn highlight_func_by_name_ref( -- cgit v1.2.3 From d75277b66b07e3fa4fed5088b92c6f65b2de8fe9 Mon Sep 17 00:00:00 2001 From: Aramis Razzaghipour Date: Tue, 25 May 2021 08:50:54 +1000 Subject: Refactor application of `library` semantic token modifier --- crates/ide/src/syntax_highlighting/highlight.rs | 77 +++++-------------------- 1 file changed, 15 insertions(+), 62 deletions(-) diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 14d68d3f3..b4a3d39c9 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -283,35 +283,11 @@ pub(super) fn element( } } fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) -> Highlight { - match def { - Definition::Macro(m) => { - let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Macro)); - - if m.module(db).map(hir::Module::krate) != krate { - h |= HlMod::Library; - } - - h - } - Definition::Field(field) => { - let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Field)); - - if Some(field.parent_def(db).module(db).krate()) != krate { - h |= HlMod::Library; - } - - h - } + let mut h = match def { + Definition::Macro(_) => Highlight::new(HlTag::Symbol(SymbolKind::Macro)), + Definition::Field(_) => Highlight::new(HlTag::Symbol(SymbolKind::Field)), Definition::ModuleDef(def) => match def { - hir::ModuleDef::Module(module) => { - let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Module)); - - if Some(module.krate()) != krate { - h |= HlMod::Library; - } - - h - } + hir::ModuleDef::Module(_) => Highlight::new(HlTag::Symbol(SymbolKind::Module)), hir::ModuleDef::Function(func) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Function)); if let Some(item) = func.as_assoc_item(db) { @@ -343,9 +319,6 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) if func.is_async(db) { h |= HlMod::Async; } - if Some(func.module(db).krate()) != krate { - h |= HlMod::Library; - } h } @@ -355,23 +328,10 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) hir::Adt::Enum(_) => HlTag::Symbol(SymbolKind::Enum), hir::Adt::Union(_) => HlTag::Symbol(SymbolKind::Union), }; - let mut h = Highlight::new(h); - - if Some(adt.module(db).krate()) != krate { - h |= HlMod::Library; - } - h - } - hir::ModuleDef::Variant(variant) => { - let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Variant)); - - if Some(variant.module(db).krate()) != krate { - h |= HlMod::Library; - } - - h + Highlight::new(h) } + hir::ModuleDef::Variant(_) => Highlight::new(HlTag::Symbol(SymbolKind::Variant)), hir::ModuleDef::Const(konst) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Const)); @@ -389,10 +349,6 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) } } - if Some(konst.module(db).krate()) != krate { - h |= HlMod::Library; - } - h } hir::ModuleDef::Trait(trait_) => { @@ -402,10 +358,6 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) h |= HlMod::Unsafe; } - if Some(trait_.module(db).krate()) != krate { - h |= HlMod::Library; - } - h } hir::ModuleDef::TypeAlias(type_) => { @@ -425,10 +377,6 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) } } - if Some(type_.module(db).krate()) != krate { - h |= HlMod::Library; - } - h } hir::ModuleDef::BuiltinType(_) => Highlight::new(HlTag::BuiltinType), @@ -440,10 +388,6 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) h |= HlMod::Unsafe; } - if Some(s.module(db).krate()) != krate { - h |= HlMod::Library; - } - h } }, @@ -476,7 +420,16 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) h } Definition::Label(_) => Highlight::new(HlTag::Symbol(SymbolKind::Label)), + }; + + let is_from_other_crate = def.module(db).map(hir::Module::krate) != krate; + let is_builtin_type = matches!(def, Definition::ModuleDef(hir::ModuleDef::BuiltinType(_))); + + if is_from_other_crate && !is_builtin_type { + h |= HlMod::Library; } + + h } fn highlight_func_by_name_ref( -- cgit v1.2.3