Simplify highlight token match guards

This commit is contained in:
Lukas Wirth 2021-04-23 17:19:46 +02:00
parent ef6f596b4b
commit 1e88f5ec8e
2 changed files with 92 additions and 102 deletions

View file

@ -1,6 +1,6 @@
//! Computes color for a single element. //! Computes color for a single element.
use hir::{AsAssocItem, AssocItemContainer, Semantics, VariantDef}; use hir::{AsAssocItem, Semantics};
use ide_db::{ use ide_db::{
defs::{Definition, NameClass, NameRefClass}, defs::{Definition, NameClass, NameRefClass},
RootDatabase, SymbolKind, RootDatabase, SymbolKind,
@ -45,28 +45,26 @@ pub(super) fn element(
}; };
match name_kind { match name_kind {
Some(NameClass::ExternCrate(_)) => HlTag::Symbol(SymbolKind::Module).into(), Some(NameClass::ExternCrate(_)) => SymbolKind::Module.into(),
Some(NameClass::Definition(def)) => highlight_def(db, def) | HlMod::Definition, Some(NameClass::Definition(def)) => highlight_def(db, def) | HlMod::Definition,
Some(NameClass::ConstReference(def)) => highlight_def(db, def), Some(NameClass::ConstReference(def)) => highlight_def(db, def),
Some(NameClass::PatFieldShorthand { field_ref, .. }) => { Some(NameClass::PatFieldShorthand { field_ref, .. }) => {
let mut h = HlTag::Symbol(SymbolKind::Field).into(); let mut h = HlTag::Symbol(SymbolKind::Field).into();
if let Definition::Field(field) = field_ref { if let Definition::Field(field) = field_ref {
if let VariantDef::Union(_) = field.parent_def(db) { if let hir::VariantDef::Union(_) = field.parent_def(db) {
h |= HlMod::Unsafe; h |= HlMod::Unsafe;
} }
} }
h h
} }
None => highlight_name_by_syntax(name) | HlMod::Definition, None => highlight_name_by_syntax(name) | HlMod::Definition,
} }
} }
// Highlight references like the definitions they resolve to // Highlight references like the definitions they resolve to
NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => { NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => {
// even though we track whether we are in an attribute or not we still need this special case // even though we track whether we are in an attribute or not we still need this special case
// as otherwise we would emit unresolved references for name refs inside attributes // as otherwise we would emit unresolved references for name refs inside attributes
Highlight::from(HlTag::Symbol(SymbolKind::Function)) SymbolKind::Function.into()
} }
NAME_REF => { NAME_REF => {
let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap(); let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
@ -74,7 +72,7 @@ pub(super) fn element(
let is_self = name_ref.self_token().is_some(); let is_self = name_ref.self_token().is_some();
let h = match NameRefClass::classify(sema, &name_ref) { let h = match NameRefClass::classify(sema, &name_ref) {
Some(name_kind) => match name_kind { Some(name_kind) => match name_kind {
NameRefClass::ExternCrate(_) => HlTag::Symbol(SymbolKind::Module).into(), NameRefClass::ExternCrate(_) => SymbolKind::Module.into(),
NameRefClass::Definition(def) => { NameRefClass::Definition(def) => {
if let Definition::Local(local) = &def { if let Definition::Local(local) = &def {
if let Some(name) = local.name(db) { if let Some(name) = local.name(db) {
@ -95,7 +93,7 @@ pub(super) fn element(
if let Some(parent) = name_ref.syntax().parent() { if let Some(parent) = name_ref.syntax().parent() {
if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) { if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) {
if let Definition::Field(field) = def { if let Definition::Field(field) = def {
if let VariantDef::Union(_) = field.parent_def(db) { if let hir::VariantDef::Union(_) = field.parent_def(db) {
h |= HlMod::Unsafe; h |= HlMod::Unsafe;
} }
} }
@ -104,9 +102,7 @@ pub(super) fn element(
h h
} }
NameRefClass::FieldShorthand { .. } => { NameRefClass::FieldShorthand { .. } => SymbolKind::Field.into(),
HlTag::Symbol(SymbolKind::Field).into()
}
}, },
None if syntactic_name_ref_highlighting => { None if syntactic_name_ref_highlighting => {
highlight_name_ref_by_syntax(name_ref, sema) highlight_name_ref_by_syntax(name_ref, sema)
@ -114,7 +110,7 @@ pub(super) fn element(
None => HlTag::UnresolvedReference.into(), None => HlTag::UnresolvedReference.into(),
}; };
if h.tag == HlTag::Symbol(SymbolKind::Module) && is_self { if h.tag == HlTag::Symbol(SymbolKind::Module) && is_self {
HlTag::Symbol(SymbolKind::SelfParam).into() SymbolKind::SelfParam.into()
} else { } else {
h h
} }
@ -135,7 +131,7 @@ pub(super) fn element(
INT_NUMBER | FLOAT_NUMBER => HlTag::NumericLiteral.into(), INT_NUMBER | FLOAT_NUMBER => HlTag::NumericLiteral.into(),
BYTE => HlTag::ByteLiteral.into(), BYTE => HlTag::ByteLiteral.into(),
CHAR => HlTag::CharLiteral.into(), CHAR => HlTag::CharLiteral.into(),
QUESTION => Highlight::new(HlTag::Operator(HlOperator::Other)) | HlMod::ControlFlow, QUESTION => HlTag::Operator(HlOperator::Other) | HlMod::ControlFlow,
LIFETIME => { LIFETIME => {
let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap(); let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap();
@ -143,44 +139,31 @@ pub(super) fn element(
Some(NameClass::Definition(def)) => highlight_def(db, def) | HlMod::Definition, Some(NameClass::Definition(def)) => highlight_def(db, def) | HlMod::Definition,
None => match NameRefClass::classify_lifetime(sema, &lifetime) { None => match NameRefClass::classify_lifetime(sema, &lifetime) {
Some(NameRefClass::Definition(def)) => highlight_def(db, def), Some(NameRefClass::Definition(def)) => highlight_def(db, def),
_ => Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam)), _ => SymbolKind::LifetimeParam.into(),
}, },
_ => Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam)) | HlMod::Definition, _ => Highlight::from(SymbolKind::LifetimeParam) | HlMod::Definition,
} }
} }
p if p.is_punct() => match p { p if p.is_punct() => match p {
T![&] if element.parent().and_then(ast::BinExpr::cast).is_some() => { T![&] if parent_matches::<ast::BinExpr>(&element) => HlOperator::Bitwise.into(),
HlTag::Operator(HlOperator::Bitwise).into()
}
T![&] => { T![&] => {
let h = HlTag::Operator(HlOperator::Other).into(); let h = HlTag::Operator(HlOperator::Other).into();
let is_unsafe = element let is_unsafe = element
.parent() .parent()
.and_then(ast::RefExpr::cast) .and_then(ast::RefExpr::cast)
.map(|ref_expr| sema.is_unsafe_ref_expr(&ref_expr)) .map_or(false, |ref_expr| sema.is_unsafe_ref_expr(&ref_expr));
.unwrap_or(false);
if is_unsafe { if is_unsafe {
h | HlMod::Unsafe h | HlMod::Unsafe
} else { } else {
h h
} }
} }
T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.] => { T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.] => HlOperator::Other.into(),
HlTag::Operator(HlOperator::Other).into() T![!] if parent_matches::<ast::MacroCall>(&element) => SymbolKind::Macro.into(),
} T![!] if parent_matches::<ast::NeverType>(&element) => HlTag::BuiltinType.into(),
T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => { T![!] if parent_matches::<ast::PrefixExpr>(&element) => HlOperator::Logical.into(),
HlTag::Symbol(SymbolKind::Macro).into() T![*] if parent_matches::<ast::PtrType>(&element) => HlTag::Keyword.into(),
} T![*] if parent_matches::<ast::PrefixExpr>(&element) => {
T![!] if element.parent().and_then(ast::NeverType::cast).is_some() => {
HlTag::BuiltinType.into()
}
T![!] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
HlTag::Operator(HlOperator::Logical).into()
}
T![*] if element.parent().and_then(ast::PtrType::cast).is_some() => {
HlTag::Keyword.into()
}
T![*] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?; let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
let expr = prefix_expr.expr()?; let expr = prefix_expr.expr()?;
@ -188,12 +171,12 @@ pub(super) fn element(
if ty.is_raw_ptr() { if ty.is_raw_ptr() {
HlTag::Operator(HlOperator::Other) | HlMod::Unsafe HlTag::Operator(HlOperator::Other) | HlMod::Unsafe
} else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() { } else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() {
HlTag::Operator(HlOperator::Other).into() HlOperator::Other.into()
} else { } else {
HlTag::Punctuation(HlPunct::Other).into() HlPunct::Other.into()
} }
} }
T![-] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { T![-] if parent_matches::<ast::PrefixExpr>(&element) => {
let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?; let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
let expr = prefix_expr.expr()?; let expr = prefix_expr.expr()?;
@ -203,41 +186,31 @@ pub(super) fn element(
} }
.into() .into()
} }
_ if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { _ if parent_matches::<ast::PrefixExpr>(&element) => HlOperator::Other.into(),
HlTag::Operator(HlOperator::Other).into()
}
T![+] | T![-] | T![*] | T![/] | T![+=] | T![-=] | T![*=] | T![/=] T![+] | T![-] | T![*] | T![/] | T![+=] | T![-=] | T![*=] | T![/=]
if element.parent().and_then(ast::BinExpr::cast).is_some() => if parent_matches::<ast::BinExpr>(&element) =>
{ {
HlTag::Operator(HlOperator::Arithmetic).into() HlOperator::Arithmetic.into()
} }
T![|] | T![&] | T![!] | T![^] | T![|=] | T![&=] | T![^=] T![|] | T![&] | T![!] | T![^] | T![|=] | T![&=] | T![^=]
if element.parent().and_then(ast::BinExpr::cast).is_some() => if parent_matches::<ast::BinExpr>(&element) =>
{ {
HlTag::Operator(HlOperator::Bitwise).into() HlOperator::Bitwise.into()
} }
T![&&] | T![||] if element.parent().and_then(ast::BinExpr::cast).is_some() => { T![&&] | T![||] if parent_matches::<ast::BinExpr>(&element) => {
HlTag::Operator(HlOperator::Logical).into() HlOperator::Logical.into()
} }
T![>] | T![<] | T![==] | T![>=] | T![<=] | T![!=] T![>] | T![<] | T![==] | T![>=] | T![<=] | T![!=]
if element.parent().and_then(ast::BinExpr::cast).is_some() => if parent_matches::<ast::BinExpr>(&element) =>
{ {
HlTag::Operator(HlOperator::Comparison).into() HlOperator::Comparison.into()
} }
_ if element.parent().and_then(ast::BinExpr::cast).is_some() => { _ if parent_matches::<ast::BinExpr>(&element) => HlOperator::Other.into(),
HlTag::Operator(HlOperator::Other).into() _ if parent_matches::<ast::RangeExpr>(&element) => HlOperator::Other.into(),
} _ if parent_matches::<ast::RangePat>(&element) => HlOperator::Other.into(),
_ if element.parent().and_then(ast::RangeExpr::cast).is_some() => { _ if parent_matches::<ast::RestPat>(&element) => HlOperator::Other.into(),
HlTag::Operator(HlOperator::Other).into() _ if parent_matches::<ast::Attr>(&element) => HlTag::Attribute.into(),
} kind => match kind {
_ if element.parent().and_then(ast::RangePat::cast).is_some() => {
HlTag::Operator(HlOperator::Other).into()
}
_ if element.parent().and_then(ast::RestPat::cast).is_some() => {
HlTag::Operator(HlOperator::Other).into()
}
_ if element.parent().and_then(ast::Attr::cast).is_some() => HlTag::Attribute.into(),
kind => HlTag::Punctuation(match kind {
T!['['] | T![']'] => HlPunct::Bracket, T!['['] | T![']'] => HlPunct::Bracket,
T!['{'] | T!['}'] => HlPunct::Brace, T!['{'] | T!['}'] => HlPunct::Brace,
T!['('] | T![')'] => HlPunct::Parenthesis, T!['('] | T![')'] => HlPunct::Parenthesis,
@ -247,7 +220,7 @@ pub(super) fn element(
T![;] => HlPunct::Semi, T![;] => HlPunct::Semi,
T![.] => HlPunct::Dot, T![.] => HlPunct::Dot,
_ => HlPunct::Other, _ => HlPunct::Other,
}) }
.into(), .into(),
}, },
@ -303,7 +276,6 @@ pub(super) fn element(
hash((name, shadow_count)) hash((name, shadow_count))
} }
} }
fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
match def { match def {
Definition::Macro(_) => HlTag::Symbol(SymbolKind::Macro), Definition::Macro(_) => HlTag::Symbol(SymbolKind::Macro),
@ -319,12 +291,12 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
} }
match item.container(db) { match item.container(db) {
AssocItemContainer::Impl(i) => { hir::AssocItemContainer::Impl(i) => {
if i.trait_(db).is_some() { if i.trait_(db).is_some() {
h |= HlMod::Trait; h |= HlMod::Trait;
} }
} }
AssocItemContainer::Trait(_t) => { hir::AssocItemContainer::Trait(_t) => {
h |= HlMod::Trait; h |= HlMod::Trait;
} }
} }
@ -344,12 +316,12 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
if let Some(item) = konst.as_assoc_item(db) { if let Some(item) = konst.as_assoc_item(db) {
h |= HlMod::Associated; h |= HlMod::Associated;
match item.container(db) { match item.container(db) {
AssocItemContainer::Impl(i) => { hir::AssocItemContainer::Impl(i) => {
if i.trait_(db).is_some() { if i.trait_(db).is_some() {
h |= HlMod::Trait; h |= HlMod::Trait;
} }
} }
AssocItemContainer::Trait(_t) => { hir::AssocItemContainer::Trait(_t) => {
h |= HlMod::Trait; h |= HlMod::Trait;
} }
} }
@ -363,12 +335,12 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
if let Some(item) = type_.as_assoc_item(db) { if let Some(item) = type_.as_assoc_item(db) {
h |= HlMod::Associated; h |= HlMod::Associated;
match item.container(db) { match item.container(db) {
AssocItemContainer::Impl(i) => { hir::AssocItemContainer::Impl(i) => {
if i.trait_(db).is_some() { if i.trait_(db).is_some() {
h |= HlMod::Trait; h |= HlMod::Trait;
} }
} }
AssocItemContainer::Trait(_t) => { hir::AssocItemContainer::Trait(_t) => {
h |= HlMod::Trait; h |= HlMod::Trait;
} }
} }
@ -427,7 +399,7 @@ fn highlight_method_call(
method_call: &ast::MethodCallExpr, method_call: &ast::MethodCallExpr,
) -> Option<Highlight> { ) -> Option<Highlight> {
let func = sema.resolve_method_call(&method_call)?; let func = sema.resolve_method_call(&method_call)?;
let mut h = HlTag::Symbol(SymbolKind::Function).into(); let mut h = SymbolKind::Function.into();
h |= HlMod::Associated; h |= HlMod::Associated;
if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) { if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) {
h |= HlMod::Unsafe; h |= HlMod::Unsafe;
@ -463,20 +435,20 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
}; };
let tag = match parent.kind() { let tag = match parent.kind() {
STRUCT => HlTag::Symbol(SymbolKind::Struct), STRUCT => SymbolKind::Struct,
ENUM => HlTag::Symbol(SymbolKind::Enum), ENUM => SymbolKind::Enum,
VARIANT => HlTag::Symbol(SymbolKind::Variant), VARIANT => SymbolKind::Variant,
UNION => HlTag::Symbol(SymbolKind::Union), UNION => SymbolKind::Union,
TRAIT => HlTag::Symbol(SymbolKind::Trait), TRAIT => SymbolKind::Trait,
TYPE_ALIAS => HlTag::Symbol(SymbolKind::TypeAlias), TYPE_ALIAS => SymbolKind::TypeAlias,
TYPE_PARAM => HlTag::Symbol(SymbolKind::TypeParam), TYPE_PARAM => SymbolKind::TypeParam,
RECORD_FIELD => HlTag::Symbol(SymbolKind::Field), RECORD_FIELD => SymbolKind::Field,
MODULE => HlTag::Symbol(SymbolKind::Module), MODULE => SymbolKind::Module,
FN => HlTag::Symbol(SymbolKind::Function), FN => SymbolKind::Function,
CONST => HlTag::Symbol(SymbolKind::Const), CONST => SymbolKind::Const,
STATIC => HlTag::Symbol(SymbolKind::Static), STATIC => SymbolKind::Static,
IDENT_PAT => HlTag::Symbol(SymbolKind::Local), IDENT_PAT => SymbolKind::Local,
_ => default, _ => return default.into(),
}; };
tag.into() tag.into()
@ -494,20 +466,15 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabas
METHOD_CALL_EXPR => { METHOD_CALL_EXPR => {
return ast::MethodCallExpr::cast(parent) return ast::MethodCallExpr::cast(parent)
.and_then(|it| highlight_method_call(sema, &it)) .and_then(|it| highlight_method_call(sema, &it))
.unwrap_or_else(|| HlTag::Symbol(SymbolKind::Function).into()); .unwrap_or_else(|| SymbolKind::Function.into());
} }
FIELD_EXPR => { FIELD_EXPR => {
let h = HlTag::Symbol(SymbolKind::Field); let h = HlTag::Symbol(SymbolKind::Field);
let is_union = ast::FieldExpr::cast(parent) let is_union = ast::FieldExpr::cast(parent)
.and_then(|field_expr| { .and_then(|field_expr| sema.resolve_field(&field_expr))
let field = sema.resolve_field(&field_expr)?; .map_or(false, |field| {
Some(if let VariantDef::Union(_) = field.parent_def(sema.db) { matches!(field.parent_def(sema.db), hir::VariantDef::Union(_))
true });
} else {
false
})
})
.unwrap_or(false);
if is_union { if is_union {
h | HlMod::Unsafe h | HlMod::Unsafe
} else { } else {
@ -524,9 +491,9 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabas
_ => { _ => {
// within path, decide whether it is module or adt by checking for uppercase name // within path, decide whether it is module or adt by checking for uppercase name
return if name.text().chars().next().unwrap_or_default().is_uppercase() { return if name.text().chars().next().unwrap_or_default().is_uppercase() {
HlTag::Symbol(SymbolKind::Struct) SymbolKind::Struct
} else { } else {
HlTag::Symbol(SymbolKind::Module) SymbolKind::Module
} }
.into(); .into();
} }
@ -537,11 +504,11 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabas
}; };
match parent.kind() { match parent.kind() {
CALL_EXPR => HlTag::Symbol(SymbolKind::Function).into(), CALL_EXPR => SymbolKind::Function.into(),
_ => if name.text().chars().next().unwrap_or_default().is_uppercase() { _ => if name.text().chars().next().unwrap_or_default().is_uppercase() {
HlTag::Symbol(SymbolKind::Struct) SymbolKind::Struct
} else { } else {
HlTag::Symbol(SymbolKind::Const) SymbolKind::Const
} }
.into(), .into(),
} }
@ -576,6 +543,11 @@ fn parents_match(mut node: NodeOrToken<SyntaxNode, SyntaxToken>, mut kinds: &[Sy
kinds.len() == 0 kinds.len() == 0
} }
#[inline]
fn parent_matches<N: AstNode>(element: &SyntaxElement) -> bool {
element.parent().map_or(false, |it| N::can_cast(it.kind()))
}
fn is_child_of_impl(element: &SyntaxElement) -> bool { fn is_child_of_impl(element: &SyntaxElement) -> bool {
match element.parent() { match element.parent() {
Some(e) => e.kind() == IMPL, Some(e) => e.kind() == IMPL,

View file

@ -234,6 +234,24 @@ impl From<HlTag> for Highlight {
} }
} }
impl From<HlOperator> for Highlight {
fn from(op: HlOperator) -> Highlight {
Highlight::new(HlTag::Operator(op))
}
}
impl From<HlPunct> for Highlight {
fn from(punct: HlPunct) -> Highlight {
Highlight::new(HlTag::Punctuation(punct))
}
}
impl From<SymbolKind> for Highlight {
fn from(sym: SymbolKind) -> Highlight {
Highlight::new(HlTag::Symbol(sym))
}
}
impl Highlight { impl Highlight {
pub(crate) fn new(tag: HlTag) -> Highlight { pub(crate) fn new(tag: HlTag) -> Highlight {
Highlight { tag, mods: HlMods::default() } Highlight { tag, mods: HlMods::default() }