Skip to content

Commit 31336ff

Browse files
committed
add semantic tokens for deprecated items
1 parent 8b45966 commit 31336ff

36 files changed

+213
-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",

crates/ide/src/syntax_highlighting/test_data/highlight_asm.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
.reference { font-style: italic; font-weight: bold; }
3737
.const { font-weight: bolder; }
3838
.unsafe { color: #BC8383; }
39+
.deprecated { text-decoration: line-through; }
3940

4041
.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
4142
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }

crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
.reference { font-style: italic; font-weight: bold; }
3737
.const { font-weight: bolder; }
3838
.unsafe { color: #BC8383; }
39+
.deprecated { text-decoration: line-through; }
3940

4041
.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
4142
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }

crates/ide/src/syntax_highlighting/test_data/highlight_attributes.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
.reference { font-style: italic; font-weight: bold; }
3737
.const { font-weight: bolder; }
3838
.unsafe { color: #BC8383; }
39+
.deprecated { text-decoration: line-through; }
3940

4041
.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
4142
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }

crates/ide/src/syntax_highlighting/test_data/highlight_block_mod_items.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
.reference { font-style: italic; font-weight: bold; }
3737
.const { font-weight: bolder; }
3838
.unsafe { color: #BC8383; }
39+
.deprecated { text-decoration: line-through; }
3940

4041
.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
4142
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }

crates/ide/src/syntax_highlighting/test_data/highlight_comments_disabled.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
.reference { font-style: italic; font-weight: bold; }
3737
.const { font-weight: bolder; }
3838
.unsafe { color: #BC8383; }
39+
.deprecated { text-decoration: line-through; }
3940

4041
.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
4142
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }

crates/ide/src/syntax_highlighting/test_data/highlight_const.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
.reference { font-style: italic; font-weight: bold; }
3737
.const { font-weight: bolder; }
3838
.unsafe { color: #BC8383; }
39+
.deprecated { text-decoration: line-through; }
3940

4041
.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
4142
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }

crates/ide/src/syntax_highlighting/test_data/highlight_crate_root.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
.reference { font-style: italic; font-weight: bold; }
3737
.const { font-weight: bolder; }
3838
.unsafe { color: #BC8383; }
39+
.deprecated { text-decoration: line-through; }
3940

4041
.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
4142
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }

0 commit comments

Comments
 (0)