Merge pull request #4474 from georgewfraser/color_attrs

Color attribute functions
This commit is contained in:
Aleksey Kladov 2020-05-24 15:32:31 +02:00 committed by GitHub
commit ce7144a93d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 24 additions and 4 deletions

View file

@ -27,7 +27,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
.keyword.unsafe { color: #BC8383; font-weight: bold; }
.control { font-style: italic; }
</style>
<pre><code><span class="attribute">#[derive(Clone, Debug)]</span>
<pre><code><span class="attribute">#[</span><span class="function attribute">derive</span><span class="attribute">(Clone, Debug)]</span>
<span class="keyword">struct</span> <span class="struct declaration">Foo</span> {
<span class="keyword">pub</span> <span class="field declaration">x</span>: <span class="builtin_type">i32</span>,
<span class="keyword">pub</span> <span class="field declaration">y</span>: <span class="builtin_type">i32</span>,

View file

@ -361,7 +361,9 @@ fn highlight_element(
}
// Highlight references like the definitions they resolve to
NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => return None,
NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => {
Highlight::from(HighlightTag::Function) | HighlightModifier::Attribute
}
NAME_REF => {
let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
match classify_name_ref(sema, &name_ref) {

View file

@ -45,8 +45,10 @@ pub enum HighlightTag {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(u8)]
pub enum HighlightModifier {
/// Used to differentiate individual elements within attributes.
Attribute = 0,
/// Used with keywords like `if` and `break`.
ControlFlow = 0,
ControlFlow,
/// `foo` in `fn foo(x: i32)` is a definition, `foo` in `foo(90 + 2)` is
/// not.
Definition,
@ -95,6 +97,7 @@ impl fmt::Display for HighlightTag {
impl HighlightModifier {
const ALL: &'static [HighlightModifier] = &[
HighlightModifier::Attribute,
HighlightModifier::ControlFlow,
HighlightModifier::Definition,
HighlightModifier::Mutable,
@ -103,6 +106,7 @@ impl HighlightModifier {
fn as_str(self) -> &'static str {
match self {
HighlightModifier::Attribute => "attribute",
HighlightModifier::ControlFlow => "control",
HighlightModifier::Definition => "declaration",
HighlightModifier::Mutable => "mutable",

View file

@ -67,6 +67,7 @@ define_semantic_token_modifiers![
(CONTROL_FLOW, "controlFlow"),
(MUTABLE, "mutable"),
(UNSAFE, "unsafe"),
(ATTRIBUTE_MODIFIER, "attribute"),
];
#[derive(Default)]

View file

@ -307,6 +307,7 @@ fn semantic_token_type_and_modifiers(
for modifier in highlight.modifiers.iter() {
let modifier = match modifier {
HighlightModifier::Attribute => semantic_tokens::ATTRIBUTE_MODIFIER,
HighlightModifier::Definition => lsp_types::SemanticTokenModifier::DECLARATION,
HighlightModifier::ControlFlow => semantic_tokens::CONTROL_FLOW,
HighlightModifier::Mutable => semantic_tokens::MUTABLE,

View file

@ -610,6 +610,10 @@
}
],
"semanticTokenModifiers": [
{
"id": "attribute",
"description": "Style for elements within attributes"
},
{
"id": "constant",
"description": "Style for compile-time constants"
@ -637,6 +641,9 @@
"attribute": [
"meta.attribute.rust"
],
"function.attribute": [
"entity.name.function.attribute.rust"
],
"builtinType": [
"support.type.primitive.rust"
],

View file

@ -75,8 +75,13 @@
{
"comment": "Attribute",
"name": "meta.attribute.rust",
"begin": "#\\!?\\[",
"begin": "#\\!?\\[(\\w*)",
"end": "\\]",
"captures": {
"1": {
"name": "entity.name.function.attribute.rust"
}
},
"patterns": [
{
"include": "#string_literal"