Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split punctuation semantic highlighting up into more tags #6238

Merged
merged 1 commit into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub use crate::{
references::{rename::RenameError, Declaration, ReferenceSearchResult},
runnables::{Runnable, RunnableKind, TestId},
syntax_highlighting::{
tags::{Highlight, HlMod, HlMods, HlTag},
tags::{Highlight, HlMod, HlMods, HlPunct, HlTag},
HlRange,
},
};
Expand Down
17 changes: 14 additions & 3 deletions crates/ide/src/syntax_highlighting/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use syntax::{
SyntaxNode, SyntaxToken, T,
};

use crate::{Highlight, HlMod, HlTag, SymbolKind};
use crate::{syntax_highlighting::tags::HlPunct, Highlight, HlMod, HlTag, SymbolKind};

pub(super) fn element(
sema: &Semantics<RootDatabase>,
Expand Down Expand Up @@ -173,7 +173,7 @@ pub(super) fn element(
} else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() {
HlTag::Operator.into()
} else {
HlTag::Punctuation.into()
HlTag::Punctuation(HlPunct::Other).into()
}
}
T![-] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
Expand All @@ -196,7 +196,18 @@ pub(super) fn element(
_ 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(),
_ => HlTag::Punctuation.into(),
kind => HlTag::Punctuation(match kind {
T!['['] | T![']'] => HlPunct::Bracket,
T!['{'] | T!['}'] => HlPunct::Brace,
T!['('] | T![')'] => HlPunct::Parenthesis,
T![<] | T![>] => HlPunct::Angle,
T![,] => HlPunct::Comma,
T![:] => HlPunct::Colon,
T![;] => HlPunct::Semi,
T![.] => HlPunct::Dot,
_ => HlPunct::Other,
})
.into(),
},

k if k.is_keyword() => {
Expand Down
36 changes: 34 additions & 2 deletions crates/ide/src/syntax_highlighting/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub enum HlTag {
EscapeSequence,
FormatSpecifier,
Keyword,
Punctuation,
Punctuation(HlPunct),
Operator,
UnresolvedReference,

Expand Down Expand Up @@ -61,6 +61,28 @@ pub enum HlMod {
Unsafe,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum HlPunct {
/// []
Bracket,
/// {}
Brace,
/// ()
Parenthesis,
/// <>
Angle,
/// ,
Comma,
/// .
Dot,
/// :
Colon,
/// ;
Semi,
///
Other,
}

impl HlTag {
fn as_str(self) -> &'static str {
match self {
Expand Down Expand Up @@ -95,7 +117,17 @@ impl HlTag {
HlTag::EscapeSequence => "escape_sequence",
HlTag::FormatSpecifier => "format_specifier",
HlTag::Keyword => "keyword",
HlTag::Punctuation => "punctuation",
HlTag::Punctuation(punct) => match punct {
HlPunct::Bracket => "bracket",
HlPunct::Brace => "brace",
HlPunct::Parenthesis => "parentheses",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.

This is the plural, I think.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always mix those up

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof, how do I run the HTML highlighting tests? Even with RUN_SLOW_TEST=1 cargo test nothing fails after changing that line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, saving the changed files is a good idea 😄.

HlPunct::Angle => "angle",
HlPunct::Comma => "comma",
HlPunct::Dot => "dot",
HlPunct::Colon => "colon",
HlPunct::Semi => "semicolon",
HlPunct::Other => "punctuation",
},
HlTag::NumericLiteral => "numeric_literal",
HlTag::Operator => "operator",
HlTag::StringLiteral => "string_literal",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@

.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
</style>
<pre><code><span class="keyword">fn</span> <span class="function declaration">not_static</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span><span class="punctuation">}</span>
<pre><code><span class="keyword">fn</span> <span class="function declaration">not_static</span><span class="parentheses">(</span><span class="parentheses">)</span> <span class="brace">{</span><span class="brace">}</span>

<span class="keyword">struct</span> <span class="struct declaration">foo</span> <span class="punctuation">{</span><span class="punctuation">}</span>
<span class="keyword">struct</span> <span class="struct declaration">foo</span> <span class="brace">{</span><span class="brace">}</span>

<span class="keyword">impl</span> <span class="struct">foo</span> <span class="punctuation">{</span>
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function declaration static associated">is_static</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span><span class="punctuation">}</span>
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function declaration associated">is_not_static</span><span class="punctuation">(</span><span class="operator">&</span><span class="self_keyword">self</span><span class="punctuation">)</span> <span class="punctuation">{</span><span class="punctuation">}</span>
<span class="punctuation">}</span>
<span class="keyword">impl</span> <span class="struct">foo</span> <span class="brace">{</span>
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function declaration static associated">is_static</span><span class="parentheses">(</span><span class="parentheses">)</span> <span class="brace">{</span><span class="brace">}</span>
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function declaration associated">is_not_static</span><span class="parentheses">(</span><span class="operator">&</span><span class="self_keyword">self</span><span class="parentheses">)</span> <span class="brace">{</span><span class="brace">}</span>
<span class="brace">}</span>

<span class="keyword">trait</span> <span class="trait declaration">t</span> <span class="punctuation">{</span>
<span class="keyword">fn</span> <span class="function declaration static associated">t_is_static</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span><span class="punctuation">}</span>
<span class="keyword">fn</span> <span class="function declaration associated">t_is_not_static</span><span class="punctuation">(</span><span class="operator">&</span><span class="self_keyword">self</span><span class="punctuation">)</span> <span class="punctuation">{</span><span class="punctuation">}</span>
<span class="punctuation">}</span>
<span class="keyword">trait</span> <span class="trait declaration">t</span> <span class="brace">{</span>
<span class="keyword">fn</span> <span class="function declaration static associated">t_is_static</span><span class="parentheses">(</span><span class="parentheses">)</span> <span class="brace">{</span><span class="brace">}</span>
<span class="keyword">fn</span> <span class="function declaration associated">t_is_not_static</span><span class="parentheses">(</span><span class="operator">&</span><span class="self_keyword">self</span><span class="parentheses">)</span> <span class="brace">{</span><span class="brace">}</span>
<span class="brace">}</span>

<span class="keyword">impl</span> <span class="trait">t</span> <span class="keyword">for</span> <span class="struct">foo</span> <span class="punctuation">{</span>
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function declaration static associated">is_static</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span><span class="punctuation">}</span>
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function declaration associated">is_not_static</span><span class="punctuation">(</span><span class="operator">&</span><span class="self_keyword">self</span><span class="punctuation">)</span> <span class="punctuation">{</span><span class="punctuation">}</span>
<span class="punctuation">}</span>
<span class="keyword">impl</span> <span class="trait">t</span> <span class="keyword">for</span> <span class="struct">foo</span> <span class="brace">{</span>
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function declaration static associated">is_static</span><span class="parentheses">(</span><span class="parentheses">)</span> <span class="brace">{</span><span class="brace">}</span>
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function declaration associated">is_not_static</span><span class="parentheses">(</span><span class="operator">&</span><span class="self_keyword">self</span><span class="parentheses">)</span> <span class="brace">{</span><span class="brace">}</span>
<span class="brace">}</span>
</code></pre>
Loading