Skip to content

Commit 06a4acb

Browse files
committed
add semantic tokens for deprecated items
1 parent 8b45966 commit 06a4acb

File tree

7 files changed

+184
-11
lines changed

7 files changed

+184
-11
lines changed

crates/ide/src/syntax_highlighting/highlight.rs

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::ops::ControlFlow;
44

55
use either::Either;
6-
use hir::{AsAssocItem, HasVisibility, Semantics};
6+
use hir::{AsAssocItem, HasAttrs, HasVisibility, Semantics, sym};
77
use ide_db::{
88
FxHashMap, RootDatabase, SymbolKind,
99
defs::{Definition, IdentClass, NameClass, NameRefClass},
@@ -484,18 +484,38 @@ pub(super) fn highlight_def(
484484
) -> Highlight {
485485
let db = sema.db;
486486
let mut h = match def {
487-
Definition::Macro(m) => Highlight::new(HlTag::Symbol(m.kind(sema.db).into())),
488-
Definition::Field(_) | Definition::TupleField(_) => {
489-
Highlight::new(HlTag::Symbol(SymbolKind::Field))
487+
Definition::Macro(m) => {
488+
let mut h = Highlight::new(HlTag::Symbol(m.kind(sema.db).into()));
489+
if m.attrs(sema.db).by_key(sym::deprecated).exists() {
490+
h |= HlMod::Deprecated;
491+
}
492+
h
490493
}
491-
Definition::Crate(_) => {
492-
Highlight::new(HlTag::Symbol(SymbolKind::Module)) | HlMod::CrateRoot
494+
Definition::Field(field) => {
495+
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Field));
496+
if field.attrs(sema.db).by_key(sym::deprecated).exists() {
497+
h |= HlMod::Deprecated;
498+
}
499+
h
500+
}
501+
Definition::TupleField(_) => Highlight::new(HlTag::Symbol(SymbolKind::Field)),
502+
Definition::Crate(krate) => {
503+
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Module)) | HlMod::CrateRoot;
504+
if krate.attrs(sema.db).by_key(sym::deprecated).exists() {
505+
h |= HlMod::Deprecated;
506+
}
507+
508+
h
493509
}
494510
Definition::Module(module) => {
495511
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Module));
496512
if module.is_crate_root() {
497513
h |= HlMod::CrateRoot;
498514
}
515+
if module.attrs(sema.db).by_key(sym::deprecated).exists() {
516+
h |= HlMod::Deprecated;
517+
}
518+
499519
h
500520
}
501521
Definition::Function(func) => {
@@ -543,19 +563,32 @@ pub(super) fn highlight_def(
543563
if func.is_const(db) {
544564
h |= HlMod::Const;
545565
}
566+
if func.attrs(sema.db).by_key(sym::deprecated).exists() {
567+
h |= HlMod::Deprecated;
568+
}
546569

547570
h
548571
}
549572
Definition::Adt(adt) => {
550-
let h = match adt {
573+
let mut h = Highlight::new(match adt {
551574
hir::Adt::Struct(_) => HlTag::Symbol(SymbolKind::Struct),
552575
hir::Adt::Enum(_) => HlTag::Symbol(SymbolKind::Enum),
553576
hir::Adt::Union(_) => HlTag::Symbol(SymbolKind::Union),
554-
};
577+
});
578+
if adt.attrs(sema.db).by_key(sym::deprecated).exists() {
579+
h |= HlMod::Deprecated;
580+
}
581+
582+
h
583+
}
584+
Definition::Variant(variant) => {
585+
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Variant));
586+
if variant.attrs(sema.db).by_key(sym::deprecated).exists() {
587+
h |= HlMod::Deprecated;
588+
}
555589

556-
Highlight::new(h)
590+
h
557591
}
558-
Definition::Variant(_) => Highlight::new(HlTag::Symbol(SymbolKind::Variant)),
559592
Definition::Const(konst) => {
560593
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Const)) | HlMod::Const;
561594
if let Some(item) = konst.as_assoc_item(db) {
@@ -572,10 +605,20 @@ pub(super) fn highlight_def(
572605
}
573606
}
574607
}
608+
if konst.attrs(sema.db).by_key(sym::deprecated).exists() {
609+
h |= HlMod::Deprecated;
610+
}
611+
612+
h
613+
}
614+
Definition::Trait(trait_) => {
615+
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Trait));
616+
if trait_.attrs(sema.db).by_key(sym::deprecated).exists() {
617+
h |= HlMod::Deprecated;
618+
}
575619

576620
h
577621
}
578-
Definition::Trait(_) => Highlight::new(HlTag::Symbol(SymbolKind::Trait)),
579622
Definition::TypeAlias(type_) => {
580623
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::TypeAlias));
581624

@@ -593,6 +636,9 @@ pub(super) fn highlight_def(
593636
}
594637
}
595638
}
639+
if type_.attrs(sema.db).by_key(sym::deprecated).exists() {
640+
h |= HlMod::Deprecated;
641+
}
596642

597643
h
598644
}
@@ -607,6 +653,9 @@ pub(super) fn highlight_def(
607653
h |= HlMod::Unsafe;
608654
}
609655
}
656+
if s.attrs(sema.db).by_key(sym::deprecated).exists() {
657+
h |= HlMod::Deprecated;
658+
}
610659

611660
h
612661
}
@@ -647,6 +696,9 @@ pub(super) fn highlight_def(
647696
if extern_crate.alias(db).is_none() {
648697
highlight |= HlMod::Library;
649698
}
699+
if extern_crate.attrs(db).by_key(sym::deprecated).exists() {
700+
highlight |= HlMod::Deprecated;
701+
}
650702
highlight
651703
}
652704
Definition::Label(_) => Highlight::new(HlTag::Symbol(SymbolKind::Label)),
@@ -721,6 +773,7 @@ fn highlight_method_call(
721773
let is_from_other_crate = krate.as_ref().map_or(false, |krate| def_crate != *krate);
722774
let is_from_builtin_crate = def_crate.is_builtin(sema.db);
723775
let is_public = func.visibility(sema.db) == hir::Visibility::Public;
776+
let is_deprecated = func.attrs(sema.db).by_key(sym::deprecated).exists();
724777

725778
if is_from_other_crate {
726779
h |= HlMod::Library;
@@ -732,6 +785,10 @@ fn highlight_method_call(
732785
h |= HlMod::DefaultLibrary;
733786
}
734787

788+
if is_deprecated {
789+
h |= HlMod::Deprecated;
790+
}
791+
735792
if let Some(self_param) = func.self_param(sema.db) {
736793
match self_param.access(sema.db) {
737794
hir::Access::Shared => h |= HlMod::Reference,

crates/ide/src/syntax_highlighting/html.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
121121
.reference { font-style: italic; font-weight: bold; }
122122
.const { font-weight: bolder; }
123123
.unsafe { color: #BC8383; }
124+
.deprecated { text-decoration: line-through; }
124125
125126
.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
126127
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }

crates/ide/src/syntax_highlighting/tags.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ pub enum HlMod {
6767
/// `foo` in `fn foo(x: i32)` is a definition, `foo` in `foo(90 + 2)` is
6868
/// not.
6969
Definition,
70+
/// Used for things with the `#[deprecated]` attribute.
71+
Deprecated,
7072
/// Doc-strings like this one.
7173
Documentation,
7274
/// Highlighting injection like rust code in doc strings or ra_fixture.
@@ -224,6 +226,7 @@ impl HlMod {
224226
HlMod::CrateRoot,
225227
HlMod::DefaultLibrary,
226228
HlMod::Definition,
229+
HlMod::Deprecated,
227230
HlMod::Documentation,
228231
HlMod::Injected,
229232
HlMod::IntraDocLink,
@@ -250,6 +253,7 @@ impl HlMod {
250253
HlMod::CrateRoot => "crate_root",
251254
HlMod::DefaultLibrary => "default_library",
252255
HlMod::Definition => "declaration",
256+
HlMod::Deprecated => "deprecated",
253257
HlMod::Documentation => "documentation",
254258
HlMod::Injected => "injected",
255259
HlMod::IntraDocLink => "intra_doc_link",
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
<style>
3+
body { margin: 0; }
4+
pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padding: 0.4em; }
5+
6+
.lifetime { color: #DFAF8F; font-style: italic; }
7+
.label { color: #DFAF8F; font-style: italic; }
8+
.comment { color: #7F9F7F; }
9+
.documentation { color: #629755; }
10+
.intra_doc_link { font-style: italic; }
11+
.injected { opacity: 0.65 ; }
12+
.struct, .enum { color: #7CB8BB; }
13+
.enum_variant { color: #BDE0F3; }
14+
.string_literal { color: #CC9393; }
15+
.field { color: #94BFF3; }
16+
.function { color: #93E0E3; }
17+
.parameter { color: #94BFF3; }
18+
.text { color: #DCDCCC; }
19+
.type { color: #7CB8BB; }
20+
.builtin_type { color: #8CD0D3; }
21+
.type_param { color: #DFAF8F; }
22+
.attribute { color: #94BFF3; }
23+
.numeric_literal { color: #BFEBBF; }
24+
.bool_literal { color: #BFE6EB; }
25+
.macro { color: #94BFF3; }
26+
.proc_macro { color: #94BFF3; text-decoration: underline; }
27+
.derive { color: #94BFF3; font-style: italic; }
28+
.module { color: #AFD8AF; }
29+
.value_param { color: #DCDCCC; }
30+
.variable { color: #DCDCCC; }
31+
.format_specifier { color: #CC696B; }
32+
.mutable { text-decoration: underline; }
33+
.escape_sequence { color: #94BFF3; }
34+
.keyword { color: #F0DFAF; font-weight: bold; }
35+
.control { font-style: italic; }
36+
.reference { font-style: italic; font-weight: bold; }
37+
.const { font-weight: bolder; }
38+
.unsafe { color: #BC8383; }
39+
.deprecated { text-decoration: line-through; }
40+
41+
.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
42+
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
43+
</style>
44+
<pre><code><span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">!</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute">deprecated</span><span class="attribute_bracket attribute">]</span>
45+
<span class="keyword">use</span> <span class="keyword crate_root deprecated public">crate</span> <span class="keyword">as</span> <span class="punctuation">_</span><span class="semicolon">;</span>
46+
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute">deprecated</span><span class="attribute_bracket attribute">]</span>
47+
<span class="keyword">macro_rules</span><span class="macro_bang">!</span> <span class="macro declaration deprecated public">macro_</span> <span class="brace">{</span>
48+
<span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="operator">=</span><span class="operator">&gt;</span> <span class="brace">{</span><span class="brace">}</span><span class="semicolon">;</span>
49+
<span class="brace">}</span>
50+
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute">deprecated</span><span class="attribute_bracket attribute">]</span>
51+
<span class="keyword">mod</span> <span class="module declaration deprecated">mod_</span> <span class="brace">{</span><span class="brace">}</span>
52+
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute">deprecated</span><span class="attribute_bracket attribute">]</span>
53+
<span class="keyword">fn</span> <span class="function declaration deprecated">func</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
54+
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute">deprecated</span><span class="attribute_bracket attribute">]</span>
55+
<span class="keyword">struct</span> <span class="struct declaration deprecated">Struct</span> <span class="brace">{</span>
56+
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute">deprecated</span><span class="attribute_bracket attribute">]</span>
57+
<span class="field declaration deprecated">field</span><span class="colon">:</span> <span class="builtin_type">u32</span>
58+
<span class="brace">}</span>
59+
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute">deprecated</span><span class="attribute_bracket attribute">]</span>
60+
<span class="keyword">enum</span> <span class="enum declaration deprecated">Enum</span> <span class="brace">{</span>
61+
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute">deprecated</span><span class="attribute_bracket attribute">]</span>
62+
<span class="enum_variant declaration deprecated">Variant</span>
63+
<span class="brace">}</span>
64+
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute">deprecated</span><span class="attribute_bracket attribute">]</span>
65+
<span class="keyword const">const</span> <span class="constant const declaration deprecated">CONST</span><span class="colon">:</span> <span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="operator">=</span> <span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
66+
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute">deprecated</span><span class="attribute_bracket attribute">]</span>
67+
<span class="keyword">trait</span> <span class="trait declaration deprecated">Trait</span> <span class="brace">{</span><span class="brace">}</span>
68+
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute">deprecated</span><span class="attribute_bracket attribute">]</span>
69+
<span class="keyword">type</span> <span class="type_alias declaration deprecated">Alias</span> <span class="operator">=</span> <span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
70+
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute">deprecated</span><span class="attribute_bracket attribute">]</span>
71+
<span class="keyword">static</span> <span class="static declaration deprecated">STATIC</span><span class="colon">:</span> <span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="operator">=</span> <span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span></code></pre>

crates/ide/src/syntax_highlighting/tests.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,3 +1511,41 @@ fn main() {
15111511
false,
15121512
);
15131513
}
1514+
1515+
#[test]
1516+
fn test_deprecated_highlighting() {
1517+
check_highlighting(
1518+
r#"
1519+
#![deprecated]
1520+
use crate as _;
1521+
#[deprecated]
1522+
macro_rules! macro_ {
1523+
() => {};
1524+
}
1525+
#[deprecated]
1526+
mod mod_ {}
1527+
#[deprecated]
1528+
fn func() {}
1529+
#[deprecated]
1530+
struct Struct {
1531+
#[deprecated]
1532+
field: u32
1533+
}
1534+
#[deprecated]
1535+
enum Enum {
1536+
#[deprecated]
1537+
Variant
1538+
}
1539+
#[deprecated]
1540+
const CONST: () = ();
1541+
#[deprecated]
1542+
trait Trait {}
1543+
#[deprecated]
1544+
type Alias = ();
1545+
#[deprecated]
1546+
static STATIC: () = ();
1547+
"#,
1548+
expect_file!["./test_data/highlight_deprecated.html"],
1549+
false,
1550+
);
1551+
}

crates/rust-analyzer/src/lsp/semantic_tokens.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ define_semantic_token_modifiers![
143143
DECLARATION,
144144
STATIC,
145145
DEFAULT_LIBRARY,
146+
DEPRECATED,
146147
}
147148
custom {
148149
(ASSOCIATED, "associated"),

crates/rust-analyzer/src/lsp/to_proto.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ fn semantic_token_type_and_modifiers(
882882
HlMod::ControlFlow => mods::CONTROL_FLOW,
883883
HlMod::CrateRoot => mods::CRATE_ROOT,
884884
HlMod::DefaultLibrary => mods::DEFAULT_LIBRARY,
885+
HlMod::Deprecated => mods::DEPRECATED,
885886
HlMod::Definition => mods::DECLARATION,
886887
HlMod::Documentation => mods::DOCUMENTATION,
887888
HlMod::Injected => mods::INJECTED,

0 commit comments

Comments
 (0)