1344: Highlight primitive types r=matklad a=lnicola

This is a hack to get better highlighting of primitive types until #1340.

Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
This commit is contained in:
bors[bot] 2019-05-29 19:02:06 +00:00
commit 7e9c5e32e5
2 changed files with 55 additions and 42 deletions

View file

@ -19,13 +19,13 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padding: 0.4e
</style> </style>
<pre><code><span class="attribute">#</span><span class="attribute">[</span><span class="attribute">derive</span><span class="attribute">(</span><span class="attribute">Clone</span><span class="attribute">,</span><span class="attribute"> </span><span class="attribute">Debug</span><span class="attribute">)</span><span class="attribute">]</span> <pre><code><span class="attribute">#</span><span class="attribute">[</span><span class="attribute">derive</span><span class="attribute">(</span><span class="attribute">Clone</span><span class="attribute">,</span><span class="attribute"> </span><span class="attribute">Debug</span><span class="attribute">)</span><span class="attribute">]</span>
<span class="keyword">struct</span> <span class="type">Foo</span> { <span class="keyword">struct</span> <span class="type">Foo</span> {
<span class="keyword">pub</span> <span class="function">x</span>: <span class="text">i32</span>, <span class="keyword">pub</span> <span class="function">x</span>: <span class="type">i32</span>,
<span class="keyword">pub</span> <span class="function">y</span>: <span class="text">i32</span>, <span class="keyword">pub</span> <span class="function">y</span>: <span class="type">i32</span>,
} }
<span class="keyword">fn</span> <span class="function">foo</span>&lt;<span class="type type">T</span>&gt;() -&gt; <span class="type">T</span> { <span class="keyword">fn</span> <span class="function">foo</span>&lt;<span class="type">T</span>&gt;() -&gt; <span class="type">T</span> {
<span class="macro">unimplemented</span><span class="macro">!</span>(); <span class="macro">unimplemented</span><span class="macro">!</span>();
<span class="function">foo</span>::&lt;<span class="type text">i32</span>&gt;(); <span class="function">foo</span>::&lt;<span class="type">i32</span>&gt;();
} }
<span class="comment">// comment</span> <span class="comment">// comment</span>

View file

@ -30,6 +30,14 @@ fn is_control_keyword(kind: SyntaxKind) -> bool {
} }
} }
fn is_prim_type(node: &ast::NameRef) -> bool {
match node.text().as_str() {
"u8" | "i8" | "u16" | "i16" | "u32" | "i32" | "u64" | "i64" | "u128" | "i128" | "usize"
| "isize" | "f32" | "f64" | "bool" | "char" | "str" => true,
_ => false,
}
}
pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> { pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> {
let _p = profile("highlight"); let _p = profile("highlight");
let source_file = db.parse(file_id).tree; let source_file = db.parse(file_id).tree;
@ -62,46 +70,52 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
ATTR => "attribute", ATTR => "attribute",
NAME_REF => { NAME_REF => {
if let Some(name_ref) = node.as_node().and_then(ast::NameRef::cast) { if let Some(name_ref) = node.as_node().and_then(ast::NameRef::cast) {
use crate::name_ref_kind::{classify_name_ref, NameRefKind::*}; // FIXME: revisit this after #1340
use hir::{ModuleDef, ImplItem}; if is_prim_type(name_ref) {
"type"
} else {
use crate::name_ref_kind::{classify_name_ref, NameRefKind::*};
use hir::{ModuleDef, ImplItem};
// FIXME: try to reuse the SourceAnalyzers // FIXME: try to reuse the SourceAnalyzers
let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); let analyzer =
match classify_name_ref(db, &analyzer, name_ref) { hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
Some(Method(_)) => "function", match classify_name_ref(db, &analyzer, name_ref) {
Some(Macro(_)) => "macro", Some(Method(_)) => "function",
Some(FieldAccess(_)) => "field", Some(Macro(_)) => "macro",
Some(AssocItem(ImplItem::Method(_))) => "function", Some(FieldAccess(_)) => "field",
Some(AssocItem(ImplItem::Const(_))) => "constant", Some(AssocItem(ImplItem::Method(_))) => "function",
Some(AssocItem(ImplItem::TypeAlias(_))) => "type", Some(AssocItem(ImplItem::Const(_))) => "constant",
Some(Def(ModuleDef::Module(_))) => "module", Some(AssocItem(ImplItem::TypeAlias(_))) => "type",
Some(Def(ModuleDef::Function(_))) => "function", Some(Def(ModuleDef::Module(_))) => "module",
Some(Def(ModuleDef::Struct(_))) => "type", Some(Def(ModuleDef::Function(_))) => "function",
Some(Def(ModuleDef::Union(_))) => "type", Some(Def(ModuleDef::Struct(_))) => "type",
Some(Def(ModuleDef::Enum(_))) => "type", Some(Def(ModuleDef::Union(_))) => "type",
Some(Def(ModuleDef::EnumVariant(_))) => "constant", Some(Def(ModuleDef::Enum(_))) => "type",
Some(Def(ModuleDef::Const(_))) => "constant", Some(Def(ModuleDef::EnumVariant(_))) => "constant",
Some(Def(ModuleDef::Static(_))) => "constant", Some(Def(ModuleDef::Const(_))) => "constant",
Some(Def(ModuleDef::Trait(_))) => "type", Some(Def(ModuleDef::Static(_))) => "constant",
Some(Def(ModuleDef::TypeAlias(_))) => "type", Some(Def(ModuleDef::Trait(_))) => "type",
Some(SelfType(_)) => "type", Some(Def(ModuleDef::TypeAlias(_))) => "type",
Some(Pat(ptr)) => { Some(SelfType(_)) => "type",
binding_hash = Some({ Some(Pat(ptr)) => {
let text = ptr binding_hash = Some({
.syntax_node_ptr() let text = ptr
.to_node(&source_file.syntax()) .syntax_node_ptr()
.text() .to_node(&source_file.syntax())
.to_smol_string(); .text()
let shadow_count = .to_smol_string();
bindings_shadow_count.entry(text.clone()).or_default(); let shadow_count =
calc_binding_hash(file_id, &text, *shadow_count) bindings_shadow_count.entry(text.clone()).or_default();
}); calc_binding_hash(file_id, &text, *shadow_count)
});
"variable" "variable"
}
Some(SelfParam(_)) => "type",
Some(GenericParam(_)) => "type",
None => "text",
} }
Some(SelfParam(_)) => "type",
Some(GenericParam(_)) => "type",
None => "text",
} }
} else { } else {
"text" "text"
@ -138,7 +152,6 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
"text" "text"
} }
} }
TYPE_ALIAS_DEF | TYPE_ARG | TYPE_PARAM => "type",
INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal", INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal",
LIFETIME => "parameter", LIFETIME => "parameter",
T![unsafe] => "keyword.unsafe", T![unsafe] => "keyword.unsafe",