mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 22:01:37 +00:00
Refactor highlighting to use classify_name
This commit is contained in:
parent
4deba88c33
commit
ef70925f5f
1 changed files with 72 additions and 71 deletions
|
@ -9,12 +9,15 @@ use ra_syntax::{
|
||||||
ast::{self, NameOwner},
|
ast::{self, NameOwner},
|
||||||
AstNode, Direction, SmolStr, SyntaxElement, SyntaxKind,
|
AstNode, Direction, SmolStr, SyntaxElement, SyntaxKind,
|
||||||
SyntaxKind::*,
|
SyntaxKind::*,
|
||||||
TextRange, T,
|
SyntaxNode, TextRange, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::RootDatabase,
|
db::RootDatabase,
|
||||||
references::{classify_name_ref, NameKind::*},
|
references::{
|
||||||
|
classify_name, classify_name_ref,
|
||||||
|
NameKind::{self, *},
|
||||||
|
},
|
||||||
FileId,
|
FileId,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -100,81 +103,43 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
|
||||||
if node.ancestors().any(|it| it.kind() == ATTR) {
|
if node.ancestors().any(|it| it.kind() == ATTR) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if let Some(name_ref) = node.as_node().cloned().and_then(ast::NameRef::cast) {
|
|
||||||
|
let name_ref = node.as_node().cloned().and_then(ast::NameRef::cast).unwrap();
|
||||||
let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind);
|
let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind);
|
||||||
match name_kind {
|
|
||||||
Some(Macro(_)) => "macro",
|
if let Some(Pat((_, ptr))) = &name_kind {
|
||||||
Some(Field(_)) => "field",
|
|
||||||
Some(AssocItem(hir::AssocItem::Function(_))) => "function",
|
|
||||||
Some(AssocItem(hir::AssocItem::Const(_))) => "constant",
|
|
||||||
Some(AssocItem(hir::AssocItem::TypeAlias(_))) => "type",
|
|
||||||
Some(Def(hir::ModuleDef::Module(_))) => "module",
|
|
||||||
Some(Def(hir::ModuleDef::Function(_))) => "function",
|
|
||||||
Some(Def(hir::ModuleDef::Adt(_))) => "type",
|
|
||||||
Some(Def(hir::ModuleDef::EnumVariant(_))) => "constant",
|
|
||||||
Some(Def(hir::ModuleDef::Const(_))) => "constant",
|
|
||||||
Some(Def(hir::ModuleDef::Static(_))) => "constant",
|
|
||||||
Some(Def(hir::ModuleDef::Trait(_))) => "type",
|
|
||||||
Some(Def(hir::ModuleDef::TypeAlias(_))) => "type",
|
|
||||||
Some(Def(hir::ModuleDef::BuiltinType(_))) => "type",
|
|
||||||
Some(SelfType(_)) => "type",
|
|
||||||
Some(Pat((_, ptr))) => {
|
|
||||||
let pat = ptr.to_node(&root);
|
let pat = ptr.to_node(&root);
|
||||||
if let Some(name) = pat.name() {
|
if let Some(name) = pat.name() {
|
||||||
let text = name.text();
|
let text = name.text();
|
||||||
let shadow_count =
|
let shadow_count = bindings_shadow_count.entry(text.clone()).or_default();
|
||||||
bindings_shadow_count.entry(text.clone()).or_default();
|
binding_hash = Some(calc_binding_hash(file_id, &text, *shadow_count))
|
||||||
binding_hash =
|
|
||||||
Some(calc_binding_hash(file_id, &text, *shadow_count))
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let analyzer =
|
name_kind
|
||||||
hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
|
.map_or("text", |it| highlight_name(db, file_id, name_ref.syntax(), &root, it))
|
||||||
if is_variable_mutable(db, &analyzer, ptr.to_node(&root)) {
|
|
||||||
"variable.mut"
|
|
||||||
} else {
|
|
||||||
"variable"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(SelfParam(_)) => "type",
|
|
||||||
Some(GenericParam(_)) => "type",
|
|
||||||
None => "text",
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
"text"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
NAME => {
|
NAME => {
|
||||||
if let Some(name) = node.as_node().cloned().and_then(ast::Name::cast) {
|
let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap();
|
||||||
let analyzer = hir::SourceAnalyzer::new(db, file_id, name.syntax(), None);
|
let name_kind = classify_name(db, file_id, &name).map(|d| d.kind);
|
||||||
if let Some(pat) = name.syntax().ancestors().find_map(ast::BindPat::cast) {
|
|
||||||
|
if let Some(Pat((_, ptr))) = &name_kind {
|
||||||
|
let pat = ptr.to_node(&root);
|
||||||
if let Some(name) = pat.name() {
|
if let Some(name) = pat.name() {
|
||||||
let text = name.text();
|
let text = name.text();
|
||||||
let shadow_count =
|
let shadow_count = bindings_shadow_count.entry(text.clone()).or_default();
|
||||||
bindings_shadow_count.entry(text.clone()).or_default();
|
|
||||||
*shadow_count += 1;
|
*shadow_count += 1;
|
||||||
binding_hash = Some(calc_binding_hash(file_id, &text, *shadow_count))
|
binding_hash = Some(calc_binding_hash(file_id, &text, *shadow_count))
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if is_variable_mutable(db, &analyzer, pat) {
|
match name_kind {
|
||||||
"variable.mut"
|
Some(name_kind) => highlight_name(db, file_id, name.syntax(), &root, name_kind),
|
||||||
} else {
|
None => name.syntax().parent().map_or("function", |x| match x.kind() {
|
||||||
"variable"
|
TYPE_PARAM | STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => "type",
|
||||||
}
|
|
||||||
} else {
|
|
||||||
name.syntax()
|
|
||||||
.parent()
|
|
||||||
.map(|x| match x.kind() {
|
|
||||||
TYPE_PARAM | STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => {
|
|
||||||
"type"
|
|
||||||
}
|
|
||||||
RECORD_FIELD_DEF => "field",
|
RECORD_FIELD_DEF => "field",
|
||||||
_ => "function",
|
_ => "function",
|
||||||
})
|
}),
|
||||||
.unwrap_or("function")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
"text"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal",
|
INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal",
|
||||||
|
@ -272,6 +237,42 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
|
||||||
buf
|
buf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn highlight_name(
|
||||||
|
db: &RootDatabase,
|
||||||
|
file_id: FileId,
|
||||||
|
node: &SyntaxNode,
|
||||||
|
root: &SyntaxNode,
|
||||||
|
name_kind: NameKind,
|
||||||
|
) -> &'static str {
|
||||||
|
match name_kind {
|
||||||
|
Macro(_) => "macro",
|
||||||
|
Field(_) => "field",
|
||||||
|
AssocItem(hir::AssocItem::Function(_)) => "function",
|
||||||
|
AssocItem(hir::AssocItem::Const(_)) => "constant",
|
||||||
|
AssocItem(hir::AssocItem::TypeAlias(_)) => "type",
|
||||||
|
Def(hir::ModuleDef::Module(_)) => "module",
|
||||||
|
Def(hir::ModuleDef::Function(_)) => "function",
|
||||||
|
Def(hir::ModuleDef::Adt(_)) => "type",
|
||||||
|
Def(hir::ModuleDef::EnumVariant(_)) => "constant",
|
||||||
|
Def(hir::ModuleDef::Const(_)) => "constant",
|
||||||
|
Def(hir::ModuleDef::Static(_)) => "constant",
|
||||||
|
Def(hir::ModuleDef::Trait(_)) => "type",
|
||||||
|
Def(hir::ModuleDef::TypeAlias(_)) => "type",
|
||||||
|
Def(hir::ModuleDef::BuiltinType(_)) => "type",
|
||||||
|
SelfType(_) => "type",
|
||||||
|
SelfParam(_) => "type",
|
||||||
|
GenericParam(_) => "type",
|
||||||
|
Pat((_, ptr)) => {
|
||||||
|
let analyzer = hir::SourceAnalyzer::new(db, file_id, node, None);
|
||||||
|
if is_variable_mutable(db, &analyzer, ptr.to_node(&root)) {
|
||||||
|
"variable.mut"
|
||||||
|
} else {
|
||||||
|
"variable"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//FIXME: like, real html escaping
|
//FIXME: like, real html escaping
|
||||||
fn html_escape(text: &str) -> String {
|
fn html_escape(text: &str) -> String {
|
||||||
text.replace("<", "<").replace(">", ">")
|
text.replace("<", "<").replace(">", ">")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue