Use dedicated semantic highlight tag for parameters

closes #5106
This commit is contained in:
Aleksey Kladov 2020-07-11 14:50:00 +02:00
parent 87ddcba05f
commit e1d6b7f7c4
11 changed files with 44 additions and 20 deletions

View File

@ -33,7 +33,10 @@
};
use ra_db::{CrateId, Edition, FileId};
use ra_prof::profile;
use ra_syntax::ast::{self, AttrsOwner, NameOwner};
use ra_syntax::{
ast::{self, AttrsOwner, NameOwner},
AstNode,
};
use rustc_hash::FxHashSet;
use crate::{
@ -955,6 +958,16 @@ pub struct Local {
}
impl Local {
pub fn is_param(self, db: &dyn HirDatabase) -> bool {
let src = self.source(db);
match src.value {
Either::Left(bind_pat) => {
bind_pat.syntax().ancestors().any(|it| ast::Param::can_cast(it.kind()))
}
Either::Right(_self_param) => true,
}
}
// FIXME: why is this an option? It shouldn't be?
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
let body = db.body(self.parent.into());

View File

@ -630,9 +630,10 @@ fn highlight_name(db: &RootDatabase, def: Definition) -> Highlight {
},
Definition::SelfType(_) => HighlightTag::SelfType,
Definition::TypeParam(_) => HighlightTag::TypeParam,
// FIXME: distinguish between locals and parameters
Definition::Local(local) => {
let mut h = Highlight::new(HighlightTag::Local);
let tag =
if local.is_param(db) { HighlightTag::ValueParam } else { HighlightTag::Local };
let mut h = Highlight::new(tag);
if local.is_mut(db) || local.ty(db).is_mutable_reference() {
h |= HighlightModifier::Mutable;
}

View File

@ -83,14 +83,15 @@ fn html_escape(text: &str) -> String {
.bool_literal { color: #BFE6EB; }
.macro { color: #94BFF3; }
.module { color: #AFD8AF; }
.value_param { color: #DCDCCC; }
.variable { color: #DCDCCC; }
.format_specifier { color: #CC696B; }
.mutable { text-decoration: underline; }
.unresolved_reference { color: #FC5555; }
.escape_sequence { color: #94BFF3; }
.keyword { color: #F0DFAF; font-weight: bold; }
.keyword.unsafe { color: #BC8383; font-weight: bold; }
.control { font-style: italic; }
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
</style>
";

View File

@ -41,6 +41,7 @@ pub enum HighlightTag {
TypeAlias,
TypeParam,
Union,
ValueParam,
Local,
UnresolvedReference,
FormatSpecifier,
@ -95,6 +96,7 @@ fn as_str(self) -> &'static str {
HighlightTag::TypeAlias => "type_alias",
HighlightTag::TypeParam => "type_param",
HighlightTag::Union => "union",
HighlightTag::ValueParam => "value_param",
HighlightTag::Local => "variable",
HighlightTag::UnresolvedReference => "unresolved_reference",
}

View File

@ -24,15 +24,16 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
.bool_literal { color: #BFE6EB; }
.macro { color: #94BFF3; }
.module { color: #AFD8AF; }
.value_param { color: #DCDCCC; }
.variable { color: #DCDCCC; }
.format_specifier { color: #CC696B; }
.mutable { text-decoration: underline; }
.unresolved_reference { color: #FC5555; }
.escape_sequence { color: #94BFF3; }
.keyword { color: #F0DFAF; font-weight: bold; }
.keyword.unsafe { color: #BC8383; font-weight: bold; }
.control { font-style: italic; }
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
</style>
<pre><code><span class="comment documentation">/// ```</span>
<span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="generic injected"> _ = </span><span class="string_literal injected">"early doctests should not go boom"</span><span class="generic injected">;</span>

View File

@ -24,17 +24,18 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
.bool_literal { color: #BFE6EB; }
.macro { color: #94BFF3; }
.module { color: #AFD8AF; }
.value_param { color: #DCDCCC; }
.variable { color: #DCDCCC; }
.format_specifier { color: #CC696B; }
.mutable { text-decoration: underline; }
.unresolved_reference { color: #FC5555; }
.escape_sequence { color: #94BFF3; }
.keyword { color: #F0DFAF; font-weight: bold; }
.keyword.unsafe { color: #BC8383; font-weight: bold; }
.control { font-style: italic; }
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
</style>
<pre><code><span class="keyword">fn</span> <span class="function declaration">fixture</span>(<span class="variable declaration">ra_fixture</span>: &<span class="builtin_type">str</span>) {}
<pre><code><span class="keyword">fn</span> <span class="function declaration">fixture</span>(<span class="value_param declaration">ra_fixture</span>: &<span class="builtin_type">str</span>) {}
<span class="keyword">fn</span> <span class="function declaration">main</span>() {
<span class="function">fixture</span>(<span class="string_literal">r#"</span>

View File

@ -24,15 +24,16 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
.bool_literal { color: #BFE6EB; }
.macro { color: #94BFF3; }
.module { color: #AFD8AF; }
.value_param { color: #DCDCCC; }
.variable { color: #DCDCCC; }
.format_specifier { color: #CC696B; }
.mutable { text-decoration: underline; }
.unresolved_reference { color: #FC5555; }
.escape_sequence { color: #94BFF3; }
.keyword { color: #F0DFAF; font-weight: bold; }
.keyword.unsafe { color: #BC8383; font-weight: bold; }
.control { font-style: italic; }
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
</style>
<pre><code><span class="macro">macro_rules!</span> <span class="macro declaration">println</span> {
($($arg:tt)*) =&gt; ({

View File

@ -24,15 +24,16 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
.bool_literal { color: #BFE6EB; }
.macro { color: #94BFF3; }
.module { color: #AFD8AF; }
.value_param { color: #DCDCCC; }
.variable { color: #DCDCCC; }
.format_specifier { color: #CC696B; }
.mutable { text-decoration: underline; }
.unresolved_reference { color: #FC5555; }
.escape_sequence { color: #94BFF3; }
.keyword { color: #F0DFAF; font-weight: bold; }
.keyword.unsafe { color: #BC8383; font-weight: bold; }
.control { font-style: italic; }
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
</style>
<pre><code><span class="keyword unsafe">unsafe</span> <span class="keyword">fn</span> <span class="function declaration unsafe">unsafe_fn</span>() {}

View File

@ -24,15 +24,16 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
.bool_literal { color: #BFE6EB; }
.macro { color: #94BFF3; }
.module { color: #AFD8AF; }
.value_param { color: #DCDCCC; }
.variable { color: #DCDCCC; }
.format_specifier { color: #CC696B; }
.mutable { text-decoration: underline; }
.unresolved_reference { color: #FC5555; }
.escape_sequence { color: #94BFF3; }
.keyword { color: #F0DFAF; font-weight: bold; }
.keyword.unsafe { color: #BC8383; font-weight: bold; }
.control { font-style: italic; }
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
</style>
<pre><code><span class="attribute">#[</span><span class="function attribute">derive</span><span class="attribute">(Clone, Debug)]</span>
<span class="keyword">struct</span> <span class="struct declaration">Foo</span> {
@ -108,8 +109,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="keyword">use</span> <span class="enum">Option</span>::*;
<span class="keyword">impl</span>&lt;<span class="type_param declaration">T</span>&gt; <span class="enum">Option</span>&lt;<span class="type_param">T</span>&gt; {
<span class="keyword">fn</span> <span class="function declaration">and</span>&lt;<span class="type_param declaration">U</span>&gt;(<span class="self_keyword">self</span>, <span class="variable declaration">other</span>: <span class="enum">Option</span>&lt;<span class="type_param">U</span>&gt;) -&gt; <span class="enum">Option</span>&lt;(<span class="type_param">T</span>, <span class="type_param">U</span>)&gt; {
<span class="keyword control">match</span> <span class="variable">other</span> {
<span class="keyword">fn</span> <span class="function declaration">and</span>&lt;<span class="type_param declaration">U</span>&gt;(<span class="self_keyword">self</span>, <span class="value_param declaration">other</span>: <span class="enum">Option</span>&lt;<span class="type_param">U</span>&gt;) -&gt; <span class="enum">Option</span>&lt;(<span class="type_param">T</span>, <span class="type_param">U</span>)&gt; {
<span class="keyword control">match</span> <span class="value_param">other</span> {
<span class="enum_variant">None</span> =&gt; <span class="macro">unimplemented!</span>(),
<span class="variable declaration">Nope</span> =&gt; <span class="variable">Nope</span>,
}

View File

@ -24,15 +24,16 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
.bool_literal { color: #BFE6EB; }
.macro { color: #94BFF3; }
.module { color: #AFD8AF; }
.value_param { color: #DCDCCC; }
.variable { color: #DCDCCC; }
.format_specifier { color: #CC696B; }
.mutable { text-decoration: underline; }
.unresolved_reference { color: #FC5555; }
.escape_sequence { color: #94BFF3; }
.keyword { color: #F0DFAF; font-weight: bold; }
.keyword.unsafe { color: #BC8383; font-weight: bold; }
.control { font-style: italic; }
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
</style>
<pre><code><span class="keyword">fn</span> <span class="function declaration">main</span>() {
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="8121853618659664005" style="color: hsl(261,57%,61%);">hello</span> = <span class="string_literal">"hello"</span>;

View File

@ -309,6 +309,7 @@ fn semantic_token_type_and_modifiers(
}
HighlightTag::EnumVariant => semantic_tokens::ENUM_MEMBER,
HighlightTag::Macro => lsp_types::SemanticTokenType::MACRO,
HighlightTag::ValueParam => lsp_types::SemanticTokenType::PARAMETER,
HighlightTag::Local => lsp_types::SemanticTokenType::VARIABLE,
HighlightTag::TypeParam => lsp_types::SemanticTokenType::TYPE_PARAMETER,
HighlightTag::Lifetime => semantic_tokens::LIFETIME,