From 1735b3ef6a7c7f7b3f5cdecdbf204c85991bcd63 Mon Sep 17 00:00:00 2001 From: Chetan Khilosiya Date: Tue, 6 Apr 2021 00:09:17 +0530 Subject: 8279: Added initial implementation for Operator semantic highlighting. --- crates/ide/src/syntax_highlighting/highlight.rs | 66 ++++++++++++++++++++----- 1 file changed, 54 insertions(+), 12 deletions(-) (limited to 'crates/ide/src/syntax_highlighting/highlight.rs') diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 5ccb84714..7734ea301 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -12,7 +12,10 @@ use syntax::{ SyntaxNode, SyntaxToken, T, }; -use crate::{syntax_highlighting::tags::HlPunct, Highlight, HlMod, HlTag}; +use crate::{ + syntax_highlighting::tags::{HlOperator, HlPunct}, + Highlight, HlMod, HlTag, +}; pub(super) fn element( sema: &Semantics, @@ -132,7 +135,7 @@ pub(super) fn element( INT_NUMBER | FLOAT_NUMBER => HlTag::NumericLiteral.into(), BYTE => HlTag::ByteLiteral.into(), CHAR => HlTag::CharLiteral.into(), - QUESTION => Highlight::new(HlTag::Operator) | HlMod::ControlFlow, + QUESTION => Highlight::new(HlTag::Operator(HlOperator::Other)) | HlMod::ControlFlow, LIFETIME => { let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap(); @@ -146,8 +149,11 @@ pub(super) fn element( } } p if p.is_punct() => match p { + T![&] if element.parent().and_then(ast::BinExpr::cast).is_some() => { + HlTag::Operator(HlOperator::Bitwise).into() + } T![&] => { - let h = HlTag::Operator.into(); + let h = HlTag::Operator(HlOperator::Other).into(); let is_unsafe = element .parent() .and_then(ast::RefExpr::cast) @@ -159,13 +165,21 @@ pub(super) fn element( h } } - T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.] => HlTag::Operator.into(), + T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.] => { + HlTag::Operator(HlOperator::Other).into() + } T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => { + eprintln!("in macro call: {}", element); HlTag::Symbol(SymbolKind::Macro).into() } T![!] if element.parent().and_then(ast::NeverType::cast).is_some() => { + eprintln!("in never type : {}", element); HlTag::BuiltinType.into() } + T![!] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { + eprintln!("pre expr for : {}", element); + HlTag::Operator(HlOperator::Bitwise).into() + } T![*] if element.parent().and_then(ast::PtrType::cast).is_some() => { HlTag::Keyword.into() } @@ -175,32 +189,60 @@ pub(super) fn element( let expr = prefix_expr.expr()?; let ty = sema.type_of_expr(&expr)?; if ty.is_raw_ptr() { - HlTag::Operator | HlMod::Unsafe + HlTag::Operator(HlOperator::Other) | HlMod::Unsafe } else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() { - HlTag::Operator.into() + HlTag::Operator(HlOperator::Other).into() } else { HlTag::Punctuation(HlPunct::Other).into() } } T![-] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { + eprintln!("the - operator: {}", element); let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?; let expr = prefix_expr.expr()?; match expr { ast::Expr::Literal(_) => HlTag::NumericLiteral, - _ => HlTag::Operator, + _ => HlTag::Operator(HlOperator::Other), } .into() } _ if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { - HlTag::Operator.into() + eprintln!("the prefix expr block: {}", element); + HlTag::Operator(HlOperator::Other).into() + } + T![+] | T![-] | T![*] | T![/] | T![+=] | T![-=] | T![*=] | T![/=] + if element.parent().and_then(ast::BinExpr::cast).is_some() => + { + HlTag::Operator(HlOperator::Arithmetic).into() + } + T![|] | T![&] | T![!] | T![^] | T![|=] | T![&=] | T![^=] + if element.parent().and_then(ast::BinExpr::cast).is_some() => + { + HlTag::Operator(HlOperator::Bitwise).into() + } + T![&&] | T![||] if element.parent().and_then(ast::BinExpr::cast).is_some() => { + HlTag::Operator(HlOperator::Logical).into() + } + T![>] | T![<] | T![==] | T![>=] | T![<=] | T![!=] + if element.parent().and_then(ast::BinExpr::cast).is_some() => + { + HlTag::Operator(HlOperator::Comparision).into() + } + _ if element.parent().and_then(ast::BinExpr::cast).is_some() => { + eprintln!("the bin expr : {}", element); + HlTag::Operator(HlOperator::Other).into() } - _ if element.parent().and_then(ast::BinExpr::cast).is_some() => HlTag::Operator.into(), _ if element.parent().and_then(ast::RangeExpr::cast).is_some() => { - HlTag::Operator.into() + eprintln!("the range expr block: {}", element); + HlTag::Operator(HlOperator::Other).into() + } + _ 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::RangePat::cast).is_some() => HlTag::Operator.into(), - _ if element.parent().and_then(ast::RestPat::cast).is_some() => HlTag::Operator.into(), _ if element.parent().and_then(ast::Attr::cast).is_some() => HlTag::Attribute.into(), kind => HlTag::Punctuation(match kind { T!['['] | T![']'] => HlPunct::Bracket, -- cgit v1.2.3 From b232549653fa3477dec0585e7bdfe637d7aaf31e Mon Sep 17 00:00:00 2001 From: Chetan Khilosiya Date: Tue, 6 Apr 2021 00:52:00 +0530 Subject: 8279: Fix the not operator use and test case fix. --- crates/ide/src/syntax_highlighting/highlight.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'crates/ide/src/syntax_highlighting/highlight.rs') diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 7734ea301..8dd5d801b 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -169,16 +169,13 @@ pub(super) fn element( HlTag::Operator(HlOperator::Other).into() } T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => { - eprintln!("in macro call: {}", element); HlTag::Symbol(SymbolKind::Macro).into() } T![!] if element.parent().and_then(ast::NeverType::cast).is_some() => { - eprintln!("in never type : {}", element); HlTag::BuiltinType.into() } T![!] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { - eprintln!("pre expr for : {}", element); - HlTag::Operator(HlOperator::Bitwise).into() + HlTag::Operator(HlOperator::Logical).into() } T![*] if element.parent().and_then(ast::PtrType::cast).is_some() => { HlTag::Keyword.into() @@ -197,7 +194,6 @@ pub(super) fn element( } } T![-] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { - eprintln!("the - operator: {}", element); let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?; let expr = prefix_expr.expr()?; @@ -208,7 +204,6 @@ pub(super) fn element( .into() } _ if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { - eprintln!("the prefix expr block: {}", element); HlTag::Operator(HlOperator::Other).into() } T![+] | T![-] | T![*] | T![/] | T![+=] | T![-=] | T![*=] | T![/=] @@ -230,11 +225,9 @@ pub(super) fn element( HlTag::Operator(HlOperator::Comparision).into() } _ if element.parent().and_then(ast::BinExpr::cast).is_some() => { - eprintln!("the bin expr : {}", element); HlTag::Operator(HlOperator::Other).into() } _ if element.parent().and_then(ast::RangeExpr::cast).is_some() => { - eprintln!("the range expr block: {}", element); HlTag::Operator(HlOperator::Other).into() } _ if element.parent().and_then(ast::RangePat::cast).is_some() => { -- cgit v1.2.3