Introduce SymbolKind::Derive

This commit is contained in:
Lukas Wirth 2021-12-04 18:18:09 +01:00
parent 642c1eb720
commit e4f2d0e3a8
12 changed files with 125 additions and 105 deletions

View file

@ -363,7 +363,13 @@ impl TryToNav for hir::MacroDef {
let mut res = NavigationTarget::from_named( let mut res = NavigationTarget::from_named(
db, db,
src.as_ref().with_value(name_owner), src.as_ref().with_value(name_owner),
SymbolKind::Macro, match self.kind() {
hir::MacroKind::Declarative
| hir::MacroKind::BuiltIn
| hir::MacroKind::ProcMacro => SymbolKind::Macro,
hir::MacroKind::Derive => SymbolKind::Derive,
hir::MacroKind::Attr => SymbolKind::Attribute,
},
); );
res.docs = self.docs(db); res.docs = self.docs(db);
Some(res) Some(res)

View file

@ -374,7 +374,13 @@ fn highlight_def(
) -> Highlight { ) -> Highlight {
let db = sema.db; let db = sema.db;
let mut h = match def { let mut h = match def {
Definition::Macro(_) => Highlight::new(HlTag::Symbol(SymbolKind::Macro)), Definition::Macro(m) => Highlight::new(HlTag::Symbol(match m.kind() {
hir::MacroKind::Declarative | hir::MacroKind::BuiltIn | hir::MacroKind::ProcMacro => {
SymbolKind::Macro
}
hir::MacroKind::Derive => SymbolKind::Derive,
hir::MacroKind::Attr => SymbolKind::Attribute,
})),
Definition::Field(_) => Highlight::new(HlTag::Symbol(SymbolKind::Field)), Definition::Field(_) => Highlight::new(HlTag::Symbol(SymbolKind::Field)),
Definition::Module(module) => { Definition::Module(module) => {
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Module)); let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Module));

View file

@ -128,6 +128,7 @@ impl HlTag {
SymbolKind::BuiltinAttr => "builtin_attr", SymbolKind::BuiltinAttr => "builtin_attr",
SymbolKind::Const => "constant", SymbolKind::Const => "constant",
SymbolKind::ConstParam => "const_param", SymbolKind::ConstParam => "const_param",
SymbolKind::Derive => "derive",
SymbolKind::Enum => "enum", SymbolKind::Enum => "enum",
SymbolKind::Field => "field", SymbolKind::Field => "field",
SymbolKind::Function => "function", SymbolKind::Function => "function",

View file

@ -44,7 +44,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="keyword">mod</span> <span class="module declaration">inner</span> <span class="brace">{</span><span class="brace">}</span> <span class="keyword">mod</span> <span class="module declaration">inner</span> <span class="brace">{</span><span class="brace">}</span>
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute library">allow</span><span class="parenthesis attribute">(</span><span class="parenthesis attribute">)</span><span class="attribute_bracket attribute">]</span> <span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute library">allow</span><span class="parenthesis attribute">(</span><span class="parenthesis attribute">)</span><span class="attribute_bracket attribute">]</span>
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="module attribute crate_root library">proc_macros</span><span class="operator attribute">::</span><span class="macro attribute library">identity</span><span class="attribute_bracket attribute">]</span> <span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="module attribute crate_root library">proc_macros</span><span class="operator attribute">::</span><span class="attribute attribute library">identity</span><span class="attribute_bracket attribute">]</span>
<span class="keyword">pub</span> <span class="keyword">mod</span> <span class="module declaration public">ops</span> <span class="brace">{</span> <span class="keyword">pub</span> <span class="keyword">mod</span> <span class="module declaration public">ops</span> <span class="brace">{</span>
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute library">lang</span> <span class="operator attribute">=</span> <span class="string_literal attribute">"fn_once"</span><span class="attribute_bracket attribute">]</span> <span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute library">lang</span> <span class="operator attribute">=</span> <span class="string_literal attribute">"fn_once"</span><span class="attribute_bracket attribute">]</span>
<span class="keyword">pub</span> <span class="keyword">trait</span> <span class="trait declaration public">FnOnce</span><span class="angle">&lt;</span><span class="type_param declaration">Args</span><span class="angle">&gt;</span> <span class="brace">{</span><span class="brace">}</span> <span class="keyword">pub</span> <span class="keyword">trait</span> <span class="trait declaration public">FnOnce</span><span class="angle">&lt;</span><span class="type_param declaration">Args</span><span class="angle">&gt;</span> <span class="brace">{</span><span class="brace">}</span>
@ -87,7 +87,7 @@ proc_macros::<span class="macro">mirror!</span> <span class="brace">{</span>
<span class="brace">}</span> <span class="brace">}</span>
<span class="brace">}</span> <span class="brace">}</span>
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="macro attribute default_library library">derive</span><span class="parenthesis attribute">(</span><span class="macro attribute default_library library">Copy</span><span class="parenthesis attribute">)</span><span class="attribute_bracket attribute">]</span> <span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="attribute attribute default_library library">derive</span><span class="parenthesis attribute">(</span><span class="derive attribute default_library library">Copy</span><span class="parenthesis attribute">)</span><span class="attribute_bracket attribute">]</span>
<span class="keyword">struct</span> <span class="struct declaration">FooCopy</span> <span class="brace">{</span> <span class="keyword">struct</span> <span class="struct declaration">FooCopy</span> <span class="brace">{</span>
<span class="field declaration">x</span><span class="colon">:</span> <span class="builtin_type">u32</span><span class="comma">,</span> <span class="field declaration">x</span><span class="colon">:</span> <span class="builtin_type">u32</span><span class="comma">,</span>
<span class="brace">}</span> <span class="brace">}</span>

View file

@ -9,7 +9,7 @@ use crate::{completions::Completions, context::CompletionContext, CompletionItem
pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) { pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) {
let add_completion = |item: &str| { let add_completion = |item: &str| {
let mut completion = CompletionItem::new(SymbolKind::Attribute, ctx.source_range(), item); let mut completion = CompletionItem::new(SymbolKind::BuiltinAttr, ctx.source_range(), item);
completion.insert_text(format!(r#""{}""#, item)); completion.insert_text(format!(r#""{}""#, item));
acc.add(completion.build()); acc.add(completion.build());
}; };
@ -32,7 +32,7 @@ pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) {
krate.potential_cfg(ctx.db).get_cfg_values(&name).cloned().for_each(|s| { krate.potential_cfg(ctx.db).get_cfg_values(&name).cloned().for_each(|s| {
let insert_text = format!(r#""{}""#, s); let insert_text = format!(r#""{}""#, s);
let mut item = let mut item =
CompletionItem::new(SymbolKind::Attribute, ctx.source_range(), s); CompletionItem::new(SymbolKind::BuiltinAttr, ctx.source_range(), s);
item.insert_text(insert_text); item.insert_text(insert_text);
acc.add(item.build()); acc.add(item.build());
@ -42,7 +42,7 @@ pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) {
None => { None => {
if let Some(krate) = ctx.krate { if let Some(krate) = ctx.krate {
krate.potential_cfg(ctx.db).get_cfg_keys().cloned().for_each(|s| { krate.potential_cfg(ctx.db).get_cfg_keys().cloned().for_each(|s| {
let item = CompletionItem::new(SymbolKind::Attribute, ctx.source_range(), s); let item = CompletionItem::new(SymbolKind::BuiltinAttr, ctx.source_range(), s);
acc.add(item.build()); acc.add(item.build());
}) })
} }

View file

@ -57,7 +57,7 @@ pub(super) fn complete_derive(
_ => (name, None), _ => (name, None),
}; };
let mut item = CompletionItem::new(SymbolKind::Attribute, ctx.source_range(), label); let mut item = CompletionItem::new(SymbolKind::Derive, ctx.source_range(), label);
if let Some(docs) = mac.docs(ctx.db) { if let Some(docs) = mac.docs(ctx.db) {
item.documentation(docs); item.documentation(docs);
} }
@ -67,7 +67,7 @@ pub(super) fn complete_derive(
item.add_to(acc); item.add_to(acc);
} }
flyimport_attribute(acc, ctx); flyimport_derive(acc, ctx);
} }
fn get_derives_in_scope(ctx: &CompletionContext) -> Vec<(hir::Name, MacroDef)> { fn get_derives_in_scope(ctx: &CompletionContext) -> Vec<(hir::Name, MacroDef)> {
@ -82,7 +82,7 @@ fn get_derives_in_scope(ctx: &CompletionContext) -> Vec<(hir::Name, MacroDef)> {
result result
} }
fn flyimport_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { fn flyimport_derive(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
if ctx.token.kind() != SyntaxKind::IDENT { if ctx.token.kind() != SyntaxKind::IDENT {
return None; return None;
}; };
@ -106,13 +106,14 @@ fn flyimport_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option
hir::ItemInNs::Macros(mac) => Some((import, mac)), hir::ItemInNs::Macros(mac) => Some((import, mac)),
_ => None, _ => None,
}) })
.filter(|&(_, mac)| mac.kind() == MacroKind::Derive)
.filter(|&(_, mac)| !ctx.is_item_hidden(&hir::ItemInNs::Macros(mac))) .filter(|&(_, mac)| !ctx.is_item_hidden(&hir::ItemInNs::Macros(mac)))
.sorted_by_key(|(import, _)| { .sorted_by_key(|(import, _)| {
compute_fuzzy_completion_order_key(&import.import_path, &user_input_lowercased) compute_fuzzy_completion_order_key(&import.import_path, &user_input_lowercased)
}) })
.filter_map(|(import, mac)| { .filter_map(|(import, mac)| {
let mut item = CompletionItem::new( let mut item = CompletionItem::new(
SymbolKind::Attribute, SymbolKind::Derive,
ctx.source_range(), ctx.source_range(),
mac.name(ctx.db)?.to_smol_str(), mac.name(ctx.db)?.to_smol_str(),
); );

View file

@ -26,7 +26,7 @@ pub(super) fn complete_repr(acc: &mut Completions, ctx: &CompletionContext, inpu
continue; continue;
} }
let mut item = CompletionItem::new(SymbolKind::Attribute, ctx.source_range(), label); let mut item = CompletionItem::new(SymbolKind::BuiltinAttr, ctx.source_range(), label);
if let Some(lookup) = lookup { if let Some(lookup) = lookup {
item.lookup_by(lookup); item.lookup_by(lookup);
} }

View file

@ -235,6 +235,7 @@ impl CompletionItemKind {
SymbolKind::BuiltinAttr => "ba", SymbolKind::BuiltinAttr => "ba",
SymbolKind::Const => "ct", SymbolKind::Const => "ct",
SymbolKind::ConstParam => "cp", SymbolKind::ConstParam => "cp",
SymbolKind::Derive => "de",
SymbolKind::Enum => "en", SymbolKind::Enum => "en",
SymbolKind::Field => "fd", SymbolKind::Field => "fd",
SymbolKind::Function => "fn", SymbolKind::Function => "fn",

View file

@ -560,9 +560,9 @@ mod cfg {
check( check(
r#"#[cfg(target_endian = $0"#, r#"#[cfg(target_endian = $0"#,
expect![[r#" expect![[r#"
at little ba little
at big ba big
"#]], "#]],
); );
} }
} }
@ -594,13 +594,13 @@ mod derive {
#[derive($0)] struct Test; #[derive($0)] struct Test;
"#, "#,
expect![[r#" expect![[r#"
at Default de Default
at Clone, Copy de Clone, Copy
at PartialEq de PartialEq
at PartialEq, Eq de PartialEq, Eq
at PartialEq, Eq, PartialOrd, Ord de PartialEq, Eq, PartialOrd, Ord
at Clone de Clone
at PartialEq, PartialOrd de PartialEq, PartialOrd
"#]], "#]],
); );
} }
@ -613,12 +613,12 @@ mod derive {
#[derive(serde::Serialize, PartialEq, $0)] struct Test; #[derive(serde::Serialize, PartialEq, $0)] struct Test;
"#, "#,
expect![[r#" expect![[r#"
at Default de Default
at Clone, Copy de Clone, Copy
at Eq de Eq
at Eq, PartialOrd, Ord de Eq, PartialOrd, Ord
at Clone de Clone
at PartialOrd de PartialOrd
"#]], "#]],
) )
} }
@ -631,12 +631,12 @@ mod derive {
#[derive($0 serde::Serialize, PartialEq)] struct Test; #[derive($0 serde::Serialize, PartialEq)] struct Test;
"#, "#,
expect![[r#" expect![[r#"
at Default de Default
at Clone, Copy de Clone, Copy
at Eq de Eq
at Eq, PartialOrd, Ord de Eq, PartialOrd, Ord
at Clone de Clone
at PartialOrd de PartialOrd
"#]], "#]],
) )
} }
@ -649,7 +649,7 @@ mod derive {
#[derive(der$0)] struct Test; #[derive(der$0)] struct Test;
"#, "#,
expect![[r#" expect![[r#"
at DeriveIdentity (use proc_macros::DeriveIdentity) de DeriveIdentity (use proc_macros::DeriveIdentity)
"#]], "#]],
); );
check_derive( check_derive(
@ -659,7 +659,7 @@ use proc_macros::DeriveIdentity;
#[derive(der$0)] struct Test; #[derive(der$0)] struct Test;
"#, "#,
expect![[r#" expect![[r#"
at DeriveIdentity de DeriveIdentity
"#]], "#]],
); );
} }
@ -775,22 +775,22 @@ mod repr {
check_repr( check_repr(
r#"#[repr($0)] struct Test;"#, r#"#[repr($0)] struct Test;"#,
expect![[r#" expect![[r#"
at align($0) ba align($0)
at packed ba packed
at transparent ba transparent
at C ba C
at u8 ba u8
at u16 ba u16
at u32 ba u32
at u64 ba u64
at u128 ba u128
at usize ba usize
at i8 ba i8
at i16 ba i16
at i32 ba i32
at i64 ba i64
at i28 ba i28
at isize ba isize
"#]], "#]],
); );
} }
@ -805,20 +805,20 @@ mod repr {
check_repr( check_repr(
r#"#[repr(align(1), $0)] struct Test;"#, r#"#[repr(align(1), $0)] struct Test;"#,
expect![[r#" expect![[r#"
at transparent ba transparent
at C ba C
at u8 ba u8
at u16 ba u16
at u32 ba u32
at u64 ba u64
at u128 ba u128
at usize ba usize
at i8 ba i8
at i16 ba i16
at i32 ba i32
at i64 ba i64
at i28 ba i28
at isize ba isize
"#]], "#]],
); );
} }
@ -828,20 +828,20 @@ mod repr {
check_repr( check_repr(
r#"#[repr(packed, $0)] struct Test;"#, r#"#[repr(packed, $0)] struct Test;"#,
expect![[r#" expect![[r#"
at transparent ba transparent
at C ba C
at u8 ba u8
at u16 ba u16
at u32 ba u32
at u64 ba u64
at u128 ba u128
at usize ba usize
at i8 ba i8
at i16 ba i16
at i32 ba i32
at i64 ba i64
at i28 ba i28
at isize ba isize
"#]], "#]],
); );
} }
@ -851,20 +851,20 @@ mod repr {
check_repr( check_repr(
r#"#[repr(C, $0)] struct Test;"#, r#"#[repr(C, $0)] struct Test;"#,
expect![[r#" expect![[r#"
at align($0) ba align($0)
at packed ba packed
at u8 ba u8
at u16 ba u16
at u32 ba u32
at u64 ba u64
at u128 ba u128
at usize ba usize
at i8 ba i8
at i16 ba i16
at i32 ba i32
at i64 ba i64
at i28 ba i28
at isize ba isize
"#]], "#]],
); );
} }
@ -874,9 +874,9 @@ mod repr {
check_repr( check_repr(
r#"#[repr(usize, $0)] struct Test;"#, r#"#[repr(usize, $0)] struct Test;"#,
expect![[r#" expect![[r#"
at align($0) ba align($0)
at packed ba packed
at C ba C
"#]], "#]],
); );
} }

View file

@ -149,6 +149,7 @@ pub enum SymbolKind {
BuiltinAttr, BuiltinAttr,
Const, Const,
ConstParam, ConstParam,
Derive,
Enum, Enum,
Field, Field,
Function, Function,

View file

@ -53,6 +53,7 @@ define_semantic_token_types![
(COMMA, "comma"), (COMMA, "comma"),
(COMPARISON, "comparison"), (COMPARISON, "comparison"),
(CONST_PARAMETER, "constParameter"), (CONST_PARAMETER, "constParameter"),
(DERIVE, "derive"),
(DOT, "dot"), (DOT, "dot"),
(ESCAPE_SEQUENCE, "escapeSequence"), (ESCAPE_SEQUENCE, "escapeSequence"),
(FORMAT_SPECIFIER, "formatSpecifier"), (FORMAT_SPECIFIER, "formatSpecifier"),

View file

@ -50,9 +50,10 @@ pub(crate) fn symbol_kind(symbol_kind: SymbolKind) -> lsp_types::SymbolKind {
SymbolKind::Enum => lsp_types::SymbolKind::ENUM, SymbolKind::Enum => lsp_types::SymbolKind::ENUM,
SymbolKind::Variant => lsp_types::SymbolKind::ENUM_MEMBER, SymbolKind::Variant => lsp_types::SymbolKind::ENUM_MEMBER,
SymbolKind::Trait => lsp_types::SymbolKind::INTERFACE, SymbolKind::Trait => lsp_types::SymbolKind::INTERFACE,
SymbolKind::Macro | SymbolKind::BuiltinAttr | SymbolKind::Attribute => { SymbolKind::Macro
lsp_types::SymbolKind::FUNCTION | SymbolKind::BuiltinAttr
} | SymbolKind::Attribute
| SymbolKind::Derive => lsp_types::SymbolKind::FUNCTION,
SymbolKind::Module | SymbolKind::ToolModule => lsp_types::SymbolKind::MODULE, SymbolKind::Module | SymbolKind::ToolModule => lsp_types::SymbolKind::MODULE,
SymbolKind::TypeAlias | SymbolKind::TypeParam => lsp_types::SymbolKind::TYPE_PARAMETER, SymbolKind::TypeAlias | SymbolKind::TypeParam => lsp_types::SymbolKind::TYPE_PARAMETER,
SymbolKind::Field => lsp_types::SymbolKind::FIELD, SymbolKind::Field => lsp_types::SymbolKind::FIELD,
@ -112,6 +113,7 @@ pub(crate) fn completion_item_kind(
SymbolKind::Attribute => lsp_types::CompletionItemKind::FUNCTION, SymbolKind::Attribute => lsp_types::CompletionItemKind::FUNCTION,
SymbolKind::Const => lsp_types::CompletionItemKind::CONSTANT, SymbolKind::Const => lsp_types::CompletionItemKind::CONSTANT,
SymbolKind::ConstParam => lsp_types::CompletionItemKind::TYPE_PARAMETER, SymbolKind::ConstParam => lsp_types::CompletionItemKind::TYPE_PARAMETER,
SymbolKind::Derive => lsp_types::CompletionItemKind::FUNCTION,
SymbolKind::Enum => lsp_types::CompletionItemKind::ENUM, SymbolKind::Enum => lsp_types::CompletionItemKind::ENUM,
SymbolKind::Field => lsp_types::CompletionItemKind::FIELD, SymbolKind::Field => lsp_types::CompletionItemKind::FIELD,
SymbolKind::Function => lsp_types::CompletionItemKind::FUNCTION, SymbolKind::Function => lsp_types::CompletionItemKind::FUNCTION,
@ -471,6 +473,7 @@ fn semantic_token_type_and_modifiers(
let type_ = match highlight.tag { let type_ = match highlight.tag {
HlTag::Symbol(symbol) => match symbol { HlTag::Symbol(symbol) => match symbol {
SymbolKind::Attribute => semantic_tokens::ATTRIBUTE, SymbolKind::Attribute => semantic_tokens::ATTRIBUTE,
SymbolKind::Derive => semantic_tokens::DERIVE,
SymbolKind::Module => lsp_types::SemanticTokenType::NAMESPACE, SymbolKind::Module => lsp_types::SemanticTokenType::NAMESPACE,
SymbolKind::Impl => semantic_tokens::TYPE_ALIAS, SymbolKind::Impl => semantic_tokens::TYPE_ALIAS,
SymbolKind::Field => lsp_types::SemanticTokenType::PROPERTY, SymbolKind::Field => lsp_types::SemanticTokenType::PROPERTY,