Skip to content

Commit 1e88f5e

Browse files
committed
Simplify highlight token match guards
1 parent ef6f596 commit 1e88f5e

File tree

2 files changed

+92
-102
lines changed

2 files changed

+92
-102
lines changed

crates/ide/src/syntax_highlighting/highlight.rs

Lines changed: 74 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Computes color for a single element.
22
3-
use hir::{AsAssocItem, AssocItemContainer, Semantics, VariantDef};
3+
use hir::{AsAssocItem, Semantics};
44
use ide_db::{
55
defs::{Definition, NameClass, NameRefClass},
66
RootDatabase, SymbolKind,
@@ -45,36 +45,34 @@ pub(super) fn element(
4545
};
4646

4747
match name_kind {
48-
Some(NameClass::ExternCrate(_)) => HlTag::Symbol(SymbolKind::Module).into(),
48+
Some(NameClass::ExternCrate(_)) => SymbolKind::Module.into(),
4949
Some(NameClass::Definition(def)) => highlight_def(db, def) | HlMod::Definition,
5050
Some(NameClass::ConstReference(def)) => highlight_def(db, def),
5151
Some(NameClass::PatFieldShorthand { field_ref, .. }) => {
5252
let mut h = HlTag::Symbol(SymbolKind::Field).into();
5353
if let Definition::Field(field) = field_ref {
54-
if let VariantDef::Union(_) = field.parent_def(db) {
54+
if let hir::VariantDef::Union(_) = field.parent_def(db) {
5555
h |= HlMod::Unsafe;
5656
}
5757
}
58-
5958
h
6059
}
6160
None => highlight_name_by_syntax(name) | HlMod::Definition,
6261
}
6362
}
64-
6563
// Highlight references like the definitions they resolve to
6664
NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => {
6765
// even though we track whether we are in an attribute or not we still need this special case
6866
// as otherwise we would emit unresolved references for name refs inside attributes
69-
Highlight::from(HlTag::Symbol(SymbolKind::Function))
67+
SymbolKind::Function.into()
7068
}
7169
NAME_REF => {
7270
let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
7371
highlight_func_by_name_ref(sema, &name_ref).unwrap_or_else(|| {
7472
let is_self = name_ref.self_token().is_some();
7573
let h = match NameRefClass::classify(sema, &name_ref) {
7674
Some(name_kind) => match name_kind {
77-
NameRefClass::ExternCrate(_) => HlTag::Symbol(SymbolKind::Module).into(),
75+
NameRefClass::ExternCrate(_) => SymbolKind::Module.into(),
7876
NameRefClass::Definition(def) => {
7977
if let Definition::Local(local) = &def {
8078
if let Some(name) = local.name(db) {
@@ -95,7 +93,7 @@ pub(super) fn element(
9593
if let Some(parent) = name_ref.syntax().parent() {
9694
if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) {
9795
if let Definition::Field(field) = def {
98-
if let VariantDef::Union(_) = field.parent_def(db) {
96+
if let hir::VariantDef::Union(_) = field.parent_def(db) {
9997
h |= HlMod::Unsafe;
10098
}
10199
}
@@ -104,17 +102,15 @@ pub(super) fn element(
104102

105103
h
106104
}
107-
NameRefClass::FieldShorthand { .. } => {
108-
HlTag::Symbol(SymbolKind::Field).into()
109-
}
105+
NameRefClass::FieldShorthand { .. } => SymbolKind::Field.into(),
110106
},
111107
None if syntactic_name_ref_highlighting => {
112108
highlight_name_ref_by_syntax(name_ref, sema)
113109
}
114110
None => HlTag::UnresolvedReference.into(),
115111
};
116112
if h.tag == HlTag::Symbol(SymbolKind::Module) && is_self {
117-
HlTag::Symbol(SymbolKind::SelfParam).into()
113+
SymbolKind::SelfParam.into()
118114
} else {
119115
h
120116
}
@@ -135,65 +131,52 @@ pub(super) fn element(
135131
INT_NUMBER | FLOAT_NUMBER => HlTag::NumericLiteral.into(),
136132
BYTE => HlTag::ByteLiteral.into(),
137133
CHAR => HlTag::CharLiteral.into(),
138-
QUESTION => Highlight::new(HlTag::Operator(HlOperator::Other)) | HlMod::ControlFlow,
134+
QUESTION => HlTag::Operator(HlOperator::Other) | HlMod::ControlFlow,
139135
LIFETIME => {
140136
let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap();
141137

142138
match NameClass::classify_lifetime(sema, &lifetime) {
143139
Some(NameClass::Definition(def)) => highlight_def(db, def) | HlMod::Definition,
144140
None => match NameRefClass::classify_lifetime(sema, &lifetime) {
145141
Some(NameRefClass::Definition(def)) => highlight_def(db, def),
146-
_ => Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam)),
142+
_ => SymbolKind::LifetimeParam.into(),
147143
},
148-
_ => Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam)) | HlMod::Definition,
144+
_ => Highlight::from(SymbolKind::LifetimeParam) | HlMod::Definition,
149145
}
150146
}
151147
p if p.is_punct() => match p {
152-
T![&] if element.parent().and_then(ast::BinExpr::cast).is_some() => {
153-
HlTag::Operator(HlOperator::Bitwise).into()
154-
}
148+
T![&] if parent_matches::<ast::BinExpr>(&element) => HlOperator::Bitwise.into(),
155149
T![&] => {
156150
let h = HlTag::Operator(HlOperator::Other).into();
157151
let is_unsafe = element
158152
.parent()
159153
.and_then(ast::RefExpr::cast)
160-
.map(|ref_expr| sema.is_unsafe_ref_expr(&ref_expr))
161-
.unwrap_or(false);
154+
.map_or(false, |ref_expr| sema.is_unsafe_ref_expr(&ref_expr));
162155
if is_unsafe {
163156
h | HlMod::Unsafe
164157
} else {
165158
h
166159
}
167160
}
168-
T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.] => {
169-
HlTag::Operator(HlOperator::Other).into()
170-
}
171-
T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => {
172-
HlTag::Symbol(SymbolKind::Macro).into()
173-
}
174-
T![!] if element.parent().and_then(ast::NeverType::cast).is_some() => {
175-
HlTag::BuiltinType.into()
176-
}
177-
T![!] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
178-
HlTag::Operator(HlOperator::Logical).into()
179-
}
180-
T![*] if element.parent().and_then(ast::PtrType::cast).is_some() => {
181-
HlTag::Keyword.into()
182-
}
183-
T![*] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
161+
T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.] => HlOperator::Other.into(),
162+
T![!] if parent_matches::<ast::MacroCall>(&element) => SymbolKind::Macro.into(),
163+
T![!] if parent_matches::<ast::NeverType>(&element) => HlTag::BuiltinType.into(),
164+
T![!] if parent_matches::<ast::PrefixExpr>(&element) => HlOperator::Logical.into(),
165+
T![*] if parent_matches::<ast::PtrType>(&element) => HlTag::Keyword.into(),
166+
T![*] if parent_matches::<ast::PrefixExpr>(&element) => {
184167
let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
185168

186169
let expr = prefix_expr.expr()?;
187170
let ty = sema.type_of_expr(&expr)?;
188171
if ty.is_raw_ptr() {
189172
HlTag::Operator(HlOperator::Other) | HlMod::Unsafe
190173
} else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() {
191-
HlTag::Operator(HlOperator::Other).into()
174+
HlOperator::Other.into()
192175
} else {
193-
HlTag::Punctuation(HlPunct::Other).into()
176+
HlPunct::Other.into()
194177
}
195178
}
196-
T![-] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
179+
T![-] if parent_matches::<ast::PrefixExpr>(&element) => {
197180
let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
198181

199182
let expr = prefix_expr.expr()?;
@@ -203,41 +186,31 @@ pub(super) fn element(
203186
}
204187
.into()
205188
}
206-
_ if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
207-
HlTag::Operator(HlOperator::Other).into()
208-
}
189+
_ if parent_matches::<ast::PrefixExpr>(&element) => HlOperator::Other.into(),
209190
T![+] | T![-] | T![*] | T![/] | T![+=] | T![-=] | T![*=] | T![/=]
210-
if element.parent().and_then(ast::BinExpr::cast).is_some() =>
191+
if parent_matches::<ast::BinExpr>(&element) =>
211192
{
212-
HlTag::Operator(HlOperator::Arithmetic).into()
193+
HlOperator::Arithmetic.into()
213194
}
214195
T![|] | T![&] | T![!] | T![^] | T![|=] | T![&=] | T![^=]
215-
if element.parent().and_then(ast::BinExpr::cast).is_some() =>
196+
if parent_matches::<ast::BinExpr>(&element) =>
216197
{
217-
HlTag::Operator(HlOperator::Bitwise).into()
198+
HlOperator::Bitwise.into()
218199
}
219-
T![&&] | T![||] if element.parent().and_then(ast::BinExpr::cast).is_some() => {
220-
HlTag::Operator(HlOperator::Logical).into()
200+
T![&&] | T![||] if parent_matches::<ast::BinExpr>(&element) => {
201+
HlOperator::Logical.into()
221202
}
222203
T![>] | T![<] | T![==] | T![>=] | T![<=] | T![!=]
223-
if element.parent().and_then(ast::BinExpr::cast).is_some() =>
204+
if parent_matches::<ast::BinExpr>(&element) =>
224205
{
225-
HlTag::Operator(HlOperator::Comparison).into()
226-
}
227-
_ if element.parent().and_then(ast::BinExpr::cast).is_some() => {
228-
HlTag::Operator(HlOperator::Other).into()
206+
HlOperator::Comparison.into()
229207
}
230-
_ if element.parent().and_then(ast::RangeExpr::cast).is_some() => {
231-
HlTag::Operator(HlOperator::Other).into()
232-
}
233-
_ if element.parent().and_then(ast::RangePat::cast).is_some() => {
234-
HlTag::Operator(HlOperator::Other).into()
235-
}
236-
_ if element.parent().and_then(ast::RestPat::cast).is_some() => {
237-
HlTag::Operator(HlOperator::Other).into()
238-
}
239-
_ if element.parent().and_then(ast::Attr::cast).is_some() => HlTag::Attribute.into(),
240-
kind => HlTag::Punctuation(match kind {
208+
_ if parent_matches::<ast::BinExpr>(&element) => HlOperator::Other.into(),
209+
_ if parent_matches::<ast::RangeExpr>(&element) => HlOperator::Other.into(),
210+
_ if parent_matches::<ast::RangePat>(&element) => HlOperator::Other.into(),
211+
_ if parent_matches::<ast::RestPat>(&element) => HlOperator::Other.into(),
212+
_ if parent_matches::<ast::Attr>(&element) => HlTag::Attribute.into(),
213+
kind => match kind {
241214
T!['['] | T![']'] => HlPunct::Bracket,
242215
T!['{'] | T!['}'] => HlPunct::Brace,
243216
T!['('] | T![')'] => HlPunct::Parenthesis,
@@ -247,7 +220,7 @@ pub(super) fn element(
247220
T![;] => HlPunct::Semi,
248221
T![.] => HlPunct::Dot,
249222
_ => HlPunct::Other,
250-
})
223+
}
251224
.into(),
252225
},
253226

@@ -303,7 +276,6 @@ pub(super) fn element(
303276
hash((name, shadow_count))
304277
}
305278
}
306-
307279
fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
308280
match def {
309281
Definition::Macro(_) => HlTag::Symbol(SymbolKind::Macro),
@@ -319,12 +291,12 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
319291
}
320292

321293
match item.container(db) {
322-
AssocItemContainer::Impl(i) => {
294+
hir::AssocItemContainer::Impl(i) => {
323295
if i.trait_(db).is_some() {
324296
h |= HlMod::Trait;
325297
}
326298
}
327-
AssocItemContainer::Trait(_t) => {
299+
hir::AssocItemContainer::Trait(_t) => {
328300
h |= HlMod::Trait;
329301
}
330302
}
@@ -344,12 +316,12 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
344316
if let Some(item) = konst.as_assoc_item(db) {
345317
h |= HlMod::Associated;
346318
match item.container(db) {
347-
AssocItemContainer::Impl(i) => {
319+
hir::AssocItemContainer::Impl(i) => {
348320
if i.trait_(db).is_some() {
349321
h |= HlMod::Trait;
350322
}
351323
}
352-
AssocItemContainer::Trait(_t) => {
324+
hir::AssocItemContainer::Trait(_t) => {
353325
h |= HlMod::Trait;
354326
}
355327
}
@@ -363,12 +335,12 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
363335
if let Some(item) = type_.as_assoc_item(db) {
364336
h |= HlMod::Associated;
365337
match item.container(db) {
366-
AssocItemContainer::Impl(i) => {
338+
hir::AssocItemContainer::Impl(i) => {
367339
if i.trait_(db).is_some() {
368340
h |= HlMod::Trait;
369341
}
370342
}
371-
AssocItemContainer::Trait(_t) => {
343+
hir::AssocItemContainer::Trait(_t) => {
372344
h |= HlMod::Trait;
373345
}
374346
}
@@ -427,7 +399,7 @@ fn highlight_method_call(
427399
method_call: &ast::MethodCallExpr,
428400
) -> Option<Highlight> {
429401
let func = sema.resolve_method_call(&method_call)?;
430-
let mut h = HlTag::Symbol(SymbolKind::Function).into();
402+
let mut h = SymbolKind::Function.into();
431403
h |= HlMod::Associated;
432404
if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) {
433405
h |= HlMod::Unsafe;
@@ -463,20 +435,20 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
463435
};
464436

465437
let tag = match parent.kind() {
466-
STRUCT => HlTag::Symbol(SymbolKind::Struct),
467-
ENUM => HlTag::Symbol(SymbolKind::Enum),
468-
VARIANT => HlTag::Symbol(SymbolKind::Variant),
469-
UNION => HlTag::Symbol(SymbolKind::Union),
470-
TRAIT => HlTag::Symbol(SymbolKind::Trait),
471-
TYPE_ALIAS => HlTag::Symbol(SymbolKind::TypeAlias),
472-
TYPE_PARAM => HlTag::Symbol(SymbolKind::TypeParam),
473-
RECORD_FIELD => HlTag::Symbol(SymbolKind::Field),
474-
MODULE => HlTag::Symbol(SymbolKind::Module),
475-
FN => HlTag::Symbol(SymbolKind::Function),
476-
CONST => HlTag::Symbol(SymbolKind::Const),
477-
STATIC => HlTag::Symbol(SymbolKind::Static),
478-
IDENT_PAT => HlTag::Symbol(SymbolKind::Local),
479-
_ => default,
438+
STRUCT => SymbolKind::Struct,
439+
ENUM => SymbolKind::Enum,
440+
VARIANT => SymbolKind::Variant,
441+
UNION => SymbolKind::Union,
442+
TRAIT => SymbolKind::Trait,
443+
TYPE_ALIAS => SymbolKind::TypeAlias,
444+
TYPE_PARAM => SymbolKind::TypeParam,
445+
RECORD_FIELD => SymbolKind::Field,
446+
MODULE => SymbolKind::Module,
447+
FN => SymbolKind::Function,
448+
CONST => SymbolKind::Const,
449+
STATIC => SymbolKind::Static,
450+
IDENT_PAT => SymbolKind::Local,
451+
_ => return default.into(),
480452
};
481453

482454
tag.into()
@@ -494,20 +466,15 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabas
494466
METHOD_CALL_EXPR => {
495467
return ast::MethodCallExpr::cast(parent)
496468
.and_then(|it| highlight_method_call(sema, &it))
497-
.unwrap_or_else(|| HlTag::Symbol(SymbolKind::Function).into());
469+
.unwrap_or_else(|| SymbolKind::Function.into());
498470
}
499471
FIELD_EXPR => {
500472
let h = HlTag::Symbol(SymbolKind::Field);
501473
let is_union = ast::FieldExpr::cast(parent)
502-
.and_then(|field_expr| {
503-
let field = sema.resolve_field(&field_expr)?;
504-
Some(if let VariantDef::Union(_) = field.parent_def(sema.db) {
505-
true
506-
} else {
507-
false
508-
})
509-
})
510-
.unwrap_or(false);
474+
.and_then(|field_expr| sema.resolve_field(&field_expr))
475+
.map_or(false, |field| {
476+
matches!(field.parent_def(sema.db), hir::VariantDef::Union(_))
477+
});
511478
if is_union {
512479
h | HlMod::Unsafe
513480
} else {
@@ -524,9 +491,9 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabas
524491
_ => {
525492
// within path, decide whether it is module or adt by checking for uppercase name
526493
return if name.text().chars().next().unwrap_or_default().is_uppercase() {
527-
HlTag::Symbol(SymbolKind::Struct)
494+
SymbolKind::Struct
528495
} else {
529-
HlTag::Symbol(SymbolKind::Module)
496+
SymbolKind::Module
530497
}
531498
.into();
532499
}
@@ -537,11 +504,11 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabas
537504
};
538505

539506
match parent.kind() {
540-
CALL_EXPR => HlTag::Symbol(SymbolKind::Function).into(),
507+
CALL_EXPR => SymbolKind::Function.into(),
541508
_ => if name.text().chars().next().unwrap_or_default().is_uppercase() {
542-
HlTag::Symbol(SymbolKind::Struct)
509+
SymbolKind::Struct
543510
} else {
544-
HlTag::Symbol(SymbolKind::Const)
511+
SymbolKind::Const
545512
}
546513
.into(),
547514
}
@@ -576,6 +543,11 @@ fn parents_match(mut node: NodeOrToken<SyntaxNode, SyntaxToken>, mut kinds: &[Sy
576543
kinds.len() == 0
577544
}
578545

546+
#[inline]
547+
fn parent_matches<N: AstNode>(element: &SyntaxElement) -> bool {
548+
element.parent().map_or(false, |it| N::can_cast(it.kind()))
549+
}
550+
579551
fn is_child_of_impl(element: &SyntaxElement) -> bool {
580552
match element.parent() {
581553
Some(e) => e.kind() == IMPL,

0 commit comments

Comments
 (0)