Skip to content

Commit 800c06e

Browse files
committed
add semantic tokens for deprecated items
1 parent 8b45966 commit 800c06e

36 files changed

+211
-35
lines changed

crates/ide/src/syntax_highlighting/highlight.rs

Lines changed: 66 additions & 35 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},
@@ -483,20 +483,25 @@ pub(super) fn highlight_def(
483483
is_ref: bool,
484484
) -> Highlight {
485485
let db = sema.db;
486-
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))
486+
let (mut h, attrs) = match def {
487+
Definition::Macro(m) => {
488+
(Highlight::new(HlTag::Symbol(m.kind(sema.db).into())), Some(m.attrs(sema.db)))
490489
}
491-
Definition::Crate(_) => {
492-
Highlight::new(HlTag::Symbol(SymbolKind::Module)) | HlMod::CrateRoot
490+
Definition::Field(field) => {
491+
(Highlight::new(HlTag::Symbol(SymbolKind::Field)), Some(field.attrs(sema.db)))
493492
}
493+
Definition::TupleField(_) => (Highlight::new(HlTag::Symbol(SymbolKind::Field)), None),
494+
Definition::Crate(krate) => (
495+
Highlight::new(HlTag::Symbol(SymbolKind::Module)) | HlMod::CrateRoot,
496+
Some(krate.attrs(sema.db)),
497+
),
494498
Definition::Module(module) => {
495499
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Module));
496500
if module.is_crate_root() {
497501
h |= HlMod::CrateRoot;
498502
}
499-
h
503+
504+
(h, Some(module.attrs(sema.db)))
500505
}
501506
Definition::Function(func) => {
502507
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Function));
@@ -544,7 +549,7 @@ pub(super) fn highlight_def(
544549
h |= HlMod::Const;
545550
}
546551

547-
h
552+
(h, Some(func.attrs(sema.db)))
548553
}
549554
Definition::Adt(adt) => {
550555
let h = match adt {
@@ -553,9 +558,11 @@ pub(super) fn highlight_def(
553558
hir::Adt::Union(_) => HlTag::Symbol(SymbolKind::Union),
554559
};
555560

556-
Highlight::new(h)
561+
(Highlight::new(h), Some(adt.attrs(sema.db)))
562+
}
563+
Definition::Variant(variant) => {
564+
(Highlight::new(HlTag::Symbol(SymbolKind::Variant)), Some(variant.attrs(sema.db)))
557565
}
558-
Definition::Variant(_) => Highlight::new(HlTag::Symbol(SymbolKind::Variant)),
559566
Definition::Const(konst) => {
560567
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Const)) | HlMod::Const;
561568
if let Some(item) = konst.as_assoc_item(db) {
@@ -573,9 +580,11 @@ pub(super) fn highlight_def(
573580
}
574581
}
575582

576-
h
583+
(h, Some(konst.attrs(sema.db)))
584+
}
585+
Definition::Trait(trait_) => {
586+
(Highlight::new(HlTag::Symbol(SymbolKind::Trait)), Some(trait_.attrs(sema.db)))
577587
}
578-
Definition::Trait(_) => Highlight::new(HlTag::Symbol(SymbolKind::Trait)),
579588
Definition::TypeAlias(type_) => {
580589
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::TypeAlias));
581590

@@ -594,10 +603,12 @@ pub(super) fn highlight_def(
594603
}
595604
}
596605

597-
h
606+
(h, Some(type_.attrs(sema.db)))
607+
}
608+
Definition::BuiltinType(_) => (Highlight::new(HlTag::BuiltinType), None),
609+
Definition::BuiltinLifetime(_) => {
610+
(Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam)), None)
598611
}
599-
Definition::BuiltinType(_) => Highlight::new(HlTag::BuiltinType),
600-
Definition::BuiltinLifetime(_) => Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam)),
601612
Definition::Static(s) => {
602613
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Static));
603614

@@ -608,18 +619,23 @@ pub(super) fn highlight_def(
608619
}
609620
}
610621

611-
h
622+
(h, Some(s.attrs(sema.db)))
612623
}
613-
Definition::SelfType(_) => Highlight::new(HlTag::Symbol(SymbolKind::Impl)),
614-
Definition::GenericParam(it) => match it {
615-
hir::GenericParam::TypeParam(_) => Highlight::new(HlTag::Symbol(SymbolKind::TypeParam)),
616-
hir::GenericParam::ConstParam(_) => {
617-
Highlight::new(HlTag::Symbol(SymbolKind::ConstParam)) | HlMod::Const
618-
}
619-
hir::GenericParam::LifetimeParam(_) => {
620-
Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam))
621-
}
622-
},
624+
Definition::SelfType(_) => (Highlight::new(HlTag::Symbol(SymbolKind::Impl)), None),
625+
Definition::GenericParam(it) => (
626+
match it {
627+
hir::GenericParam::TypeParam(_) => {
628+
Highlight::new(HlTag::Symbol(SymbolKind::TypeParam))
629+
}
630+
hir::GenericParam::ConstParam(_) => {
631+
Highlight::new(HlTag::Symbol(SymbolKind::ConstParam)) | HlMod::Const
632+
}
633+
hir::GenericParam::LifetimeParam(_) => {
634+
Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam))
635+
}
636+
},
637+
None,
638+
),
623639
Definition::Local(local) => {
624640
let tag = if local.is_self(db) {
625641
HlTag::Symbol(SymbolKind::SelfParam)
@@ -639,24 +655,28 @@ pub(super) fn highlight_def(
639655
if ty.as_callable(db).is_some() || ty.impls_fnonce(db) {
640656
h |= HlMod::Callable;
641657
}
642-
h
658+
(h, None)
643659
}
644660
Definition::ExternCrateDecl(extern_crate) => {
645661
let mut highlight =
646662
Highlight::new(HlTag::Symbol(SymbolKind::Module)) | HlMod::CrateRoot;
647663
if extern_crate.alias(db).is_none() {
648664
highlight |= HlMod::Library;
649665
}
650-
highlight
666+
(highlight, Some(extern_crate.attrs(sema.db)))
667+
}
668+
Definition::Label(_) => (Highlight::new(HlTag::Symbol(SymbolKind::Label)), None),
669+
Definition::BuiltinAttr(_) => {
670+
(Highlight::new(HlTag::Symbol(SymbolKind::BuiltinAttr)), None)
671+
}
672+
Definition::ToolModule(_) => (Highlight::new(HlTag::Symbol(SymbolKind::ToolModule)), None),
673+
Definition::DeriveHelper(_) => {
674+
(Highlight::new(HlTag::Symbol(SymbolKind::DeriveHelper)), None)
651675
}
652-
Definition::Label(_) => Highlight::new(HlTag::Symbol(SymbolKind::Label)),
653-
Definition::BuiltinAttr(_) => Highlight::new(HlTag::Symbol(SymbolKind::BuiltinAttr)),
654-
Definition::ToolModule(_) => Highlight::new(HlTag::Symbol(SymbolKind::ToolModule)),
655-
Definition::DeriveHelper(_) => Highlight::new(HlTag::Symbol(SymbolKind::DeriveHelper)),
656676
Definition::InlineAsmRegOrRegClass(_) => {
657-
Highlight::new(HlTag::Symbol(SymbolKind::InlineAsmRegOrRegClass))
677+
(Highlight::new(HlTag::Symbol(SymbolKind::InlineAsmRegOrRegClass)), None)
658678
}
659-
Definition::InlineAsmOperand(_) => Highlight::new(HlTag::Symbol(SymbolKind::Local)),
679+
Definition::InlineAsmOperand(_) => (Highlight::new(HlTag::Symbol(SymbolKind::Local)), None),
660680
};
661681

662682
let def_crate = def.krate(db);
@@ -676,6 +696,12 @@ pub(super) fn highlight_def(
676696
h |= HlMod::DefaultLibrary;
677697
}
678698

699+
if let Some(attrs) = attrs
700+
&& attrs.by_key(sym::deprecated).exists()
701+
{
702+
h |= HlMod::Deprecated;
703+
}
704+
679705
h
680706
}
681707

@@ -721,6 +747,7 @@ fn highlight_method_call(
721747
let is_from_other_crate = krate.as_ref().map_or(false, |krate| def_crate != *krate);
722748
let is_from_builtin_crate = def_crate.is_builtin(sema.db);
723749
let is_public = func.visibility(sema.db) == hir::Visibility::Public;
750+
let is_deprecated = func.attrs(sema.db).by_key(sym::deprecated).exists();
724751

725752
if is_from_other_crate {
726753
h |= HlMod::Library;
@@ -732,6 +759,10 @@ fn highlight_method_call(
732759
h |= HlMod::DefaultLibrary;
733760
}
734761

762+
if is_deprecated {
763+
h |= HlMod::Deprecated;
764+
}
765+
735766
if let Some(self_param) = func.self_param(sema.db) {
736767
match self_param.access(sema.db) {
737768
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)