From 3e7472f76c70c3b0a31bb72f6f318c1aa1aba83d Mon Sep 17 00:00:00 2001 From: Aramis Razzaghipour Date: Fri, 28 May 2021 09:25:32 +1000 Subject: Add `public` semantic token modifier for public items --- crates/ide/src/syntax_highlighting.rs | 1 + crates/ide/src/syntax_highlighting/highlight.rs | 17 ++++++--- crates/ide/src/syntax_highlighting/tags.rs | 4 +++ .../test_data/highlight_assoc_functions.html | 8 ++--- .../test_data/highlight_doctest.html | 18 +++++----- .../test_data/highlighting.html | 42 +++++++++++++--------- crates/ide/src/syntax_highlighting/tests.rs | 10 +++++- crates/rust-analyzer/src/semantic_tokens.rs | 1 + crates/rust-analyzer/src/to_proto.rs | 1 + 9 files changed, 67 insertions(+), 35 deletions(-) diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index d44a1b45f..5259d86d2 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -137,6 +137,7 @@ pub struct HlRange { // injected:: Emitted for doc-string injected highlighting like rust source blocks in documentation. // intraDocLink:: Emitted for intra doc links in doc-strings. // library:: Emitted for items that are defined outside of the current crate. +// public:: Emitted for items that are from the current crate and are `pub`. // mutable:: Emitted for mutable locals and statics. // static:: Emitted for "static" functions, also known as functions that do not take a `self` param, as well as statics and consts. // trait:: Emitted for associated trait items. diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 6834fe11a..82e16a51b 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -1,6 +1,6 @@ //! Computes color for a single element. -use hir::{AsAssocItem, Semantics}; +use hir::{AsAssocItem, HasVisibility, Semantics}; use ide_db::{ defs::{Definition, NameClass, NameRefClass}, RootDatabase, SymbolKind, @@ -439,9 +439,12 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) let is_from_other_crate = def.module(db).map(hir::Module::krate) != krate; let is_builtin_type = matches!(def, Definition::ModuleDef(hir::ModuleDef::BuiltinType(_))); + let is_public = def.visibility(db) == Some(hir::Visibility::Public); - if is_from_other_crate && !is_builtin_type { - h |= HlMod::Library; + match (is_from_other_crate, is_builtin_type, is_public) { + (true, false, _) => h |= HlMod::Library, + (false, _, true) => h |= HlMod::Public, + _ => {} } h @@ -475,8 +478,14 @@ fn highlight_method_call( if func.as_assoc_item(sema.db).and_then(|it| it.containing_trait(sema.db)).is_some() { h |= HlMod::Trait; } - if Some(func.module(sema.db).krate()) != krate { + + let is_from_other_crate = Some(func.module(sema.db).krate()) != krate; + let is_public = func.visibility(sema.db) == hir::Visibility::Public; + + if is_from_other_crate { h |= HlMod::Library; + } else if is_public { + h |= HlMod::Public; } if let Some(self_param) = func.self_param(sema.db) { diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index 9d481deae..b4d59d00b 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -71,6 +71,8 @@ pub enum HlMod { Async, /// Used for items from other crates. Library, + /// Used for public items. + Public, // Keep this last! /// Used for unsafe functions, unsafe traits, mutable statics, union accesses and unsafe operations. Unsafe, @@ -194,6 +196,7 @@ impl HlMod { HlMod::Trait, HlMod::Async, HlMod::Library, + HlMod::Public, HlMod::Unsafe, ]; @@ -213,6 +216,7 @@ impl HlMod { HlMod::Trait => "trait", HlMod::Async => "async", HlMod::Library => "library", + HlMod::Public => "public", HlMod::Unsafe => "unsafe", } } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html b/crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html index 4e85f7c0b..9326d9d45 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html @@ -43,8 +43,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd struct foo {} impl foo { - pub fn is_static() {} - pub fn is_not_static(&self) {} + pub fn is_static() {} + pub fn is_not_static(&self) {} } trait t { @@ -53,7 +53,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd } impl t for foo { - pub fn is_static() {} - pub fn is_not_static(&self) {} + pub fn is_static() {} + pub fn is_not_static(&self) {} } \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html index 79a285107..1f2f83a08 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html @@ -63,7 +63,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd // KILLER WHALE /// Ishmael."; /// ``` - pub const bar: bool = true; + pub const bar: bool = true; /// Constructs a new `Foo`. /// @@ -73,7 +73,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd /// # #![allow(unused_mut)] /// let mut foo: Foo = Foo::new(); /// ``` - pub const fn new() -> Foo { + pub const fn new() -> Foo { Foo { bar: true } } @@ -107,7 +107,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd /// ```sh /// echo 1 /// ``` - pub fn foo(&self) -> bool { + pub fn foo(&self) -> bool { true } } @@ -119,10 +119,10 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd /// /// [`Item`]: module::Item /// [mix_and_match]: ThisShouldntResolve -pub fn all_the_links() {} +pub fn all_the_links() {} -pub mod module { - pub struct Item; +pub mod module { + pub struct Item; } /// ``` @@ -148,7 +148,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd #[cfg_attr(not(feature = "alloc"), doc = "```ignore")] /// let _ = example(&alloc::vec![1, 2, 3]); /// ``` -pub fn mix_and_match() {} +pub fn mix_and_match() {} /** It is beyond me why you'd use these when you got /// @@ -157,7 +157,7 @@ It is beyond me why you'd use these when you got /// ``` [`block_comments2`] tests these with indentation */ -pub fn block_comments() {} +pub fn block_comments() {} /** Really, I don't get it @@ -166,4 +166,4 @@ It is beyond me why you'd use these when you got /// ``` [`block_comments`] tests these without indentation */ -pub fn block_comments2() {} \ No newline at end of file +pub fn block_comments2() {} \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html index 9232cf905..63daf25c6 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html @@ -45,26 +45,26 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd macro Copy {} // Needed for function consuming vs normal -pub mod marker { +pub mod marker { #[lang = "copy"] - pub trait Copy {} + pub trait Copy {} } -pub mod ops { +pub mod ops { #[lang = "fn_once"] - pub trait FnOnce<Args> {} + pub trait FnOnce<Args> {} #[lang = "fn_mut"] - pub trait FnMut<Args>: FnOnce<Args> {} + pub trait FnMut<Args>: FnOnce<Args> {} #[lang = "fn"] - pub trait Fn<Args>: FnMut<Args> {} + pub trait Fn<Args>: FnMut<Args> {} } struct Foo { - pub x: i32, - pub y: i32, + pub x: i32, + pub y: i32, } trait Bar where Self: { @@ -73,7 +73,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd impl Bar for Foo where Self: { fn bar(&self) -> i32 { - self.x + self.x } } @@ -83,11 +83,11 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd } fn qux(&mut self) { - self.x = 0; + self.x = 0; } fn quop(&self) -> i32 { - self.x + self.x } } @@ -128,8 +128,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd FOO } -use ops::Fn; -fn baz<F: Fn() -> ()>(f: F) { +use ops::Fn; +fn baz<F: Fn() -> ()>(f: F) { f() } @@ -181,7 +181,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd let mut vec = Vec::new(); if true { let x = 92; - vec.push(Foo { x, y: 1 }); + vec.push(Foo { x, y: 1 }); } unsafe { vec.set_len(0); @@ -198,12 +198,12 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd let y = &mut x; let z = &y; - let Foo { x: z, y } = Foo { x: z, y }; + let Foo { x: z, y } = Foo { x: z, y }; y; - let mut foo = Foo { x, y: x }; - let foo2 = Foo { x, y: x }; + let mut foo = Foo { x, y: x }; + let foo2 = Foo { x, y: x }; foo.quop(); foo.qux(); foo.baz(foo2); @@ -269,5 +269,13 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd } } +pub enum Bool { True, False } + +impl Bool { + pub const fn to_primitive(self) -> bool { + matches!(self, Self::True) + } +} +const USAGE_OF_BOOL:bool = Bool::True.to_primitive(); \ 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 4f0b1ce85..502a88af2 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -242,6 +242,14 @@ fn use_foo_items() { } } +pub enum Bool { True, False } + +impl Bool { + pub const fn to_primitive(self) -> bool { + matches!(self, Self::True) + } +} +const USAGE_OF_BOOL:bool = Bool::True.to_primitive(); //- /foo.rs crate:foo pub struct Person { @@ -390,7 +398,7 @@ struct Foo { .highlight_range(FileRange { file_id, range: TextRange::at(45.into(), 1.into()) }) .unwrap(); - assert_eq!(&highlights[0].highlight.to_string(), "field.declaration"); + assert_eq!(&highlights[0].highlight.to_string(), "field.declaration.public"); } #[test] diff --git a/crates/rust-analyzer/src/semantic_tokens.rs b/crates/rust-analyzer/src/semantic_tokens.rs index c20642231..3f6c52e91 100644 --- a/crates/rust-analyzer/src/semantic_tokens.rs +++ b/crates/rust-analyzer/src/semantic_tokens.rs @@ -93,6 +93,7 @@ define_semantic_token_modifiers![ (CONSUMING, "consuming"), (ASYNC, "async"), (LIBRARY, "library"), + (PUBLIC, "public"), (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 310d8c6d2..f9497f76f 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -505,6 +505,7 @@ fn semantic_token_type_and_modifiers( HlMod::Consuming => semantic_tokens::CONSUMING, HlMod::Async => semantic_tokens::ASYNC, HlMod::Library => semantic_tokens::LIBRARY, + HlMod::Public => semantic_tokens::PUBLIC, HlMod::Unsafe => semantic_tokens::UNSAFE, HlMod::Callable => semantic_tokens::CALLABLE, HlMod::Static => lsp_types::SemanticTokenModifier::STATIC, -- cgit v1.2.3