From 8b7f853cc19d0940ec542e10bc23aa78455bbb3b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 10 Nov 2019 00:32:00 +0300 Subject: Add hir::Local --- crates/ra_ide_api/src/syntax_highlighting.rs | 79 ++++++++-------------------- 1 file changed, 22 insertions(+), 57 deletions(-) (limited to 'crates/ra_ide_api/src/syntax_highlighting.rs') diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs index 1ee68abe2..d53a759ee 100644 --- a/crates/ra_ide_api/src/syntax_highlighting.rs +++ b/crates/ra_ide_api/src/syntax_highlighting.rs @@ -2,15 +2,10 @@ use rustc_hash::{FxHashMap, FxHashSet}; -use hir::{Mutability, Ty}; +use hir::{Mutability, Name}; use ra_db::SourceDatabase; use ra_prof::profile; -use ra_syntax::{ - ast::{self, NameOwner}, - AstNode, Direction, SmolStr, SyntaxElement, SyntaxKind, - SyntaxKind::*, - SyntaxNode, TextRange, T, -}; +use ra_syntax::{ast, AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxKind::*, TextRange, T}; use crate::{ db::RootDatabase, @@ -43,32 +38,12 @@ fn is_control_keyword(kind: SyntaxKind) -> bool { } } -fn is_variable_mutable( - db: &RootDatabase, - analyzer: &hir::SourceAnalyzer, - pat: ast::BindPat, -) -> bool { - if pat.is_mutable() { - return true; - } - - let ty = analyzer.type_of_pat(db, &pat.into()).unwrap_or(Ty::Unknown); - if let Some((_, mutability)) = ty.as_reference() { - match mutability { - Mutability::Shared => false, - Mutability::Mut => true, - } - } else { - false - } -} - pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec { let _p = profile("highlight"); let parse = db.parse(file_id); let root = parse.tree().syntax().clone(); - fn calc_binding_hash(file_id: FileId, text: &SmolStr, shadow_count: u32) -> u64 { + fn calc_binding_hash(file_id: FileId, name: &Name, shadow_count: u32) -> u64 { fn hash(x: T) -> u64 { use std::{collections::hash_map::DefaultHasher, hash::Hasher}; @@ -77,13 +52,13 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec = FxHashSet::default(); - let mut bindings_shadow_count: FxHashMap = FxHashMap::default(); + let mut bindings_shadow_count: FxHashMap = FxHashMap::default(); let mut res = Vec::new(); for node in root.descendants_with_tokens() { @@ -107,34 +82,29 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec { let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap(); let name_kind = classify_name(db, file_id, &name).map(|d| d.kind); - if let Some(Pat((_, ptr))) = &name_kind { - let pat = ptr.to_node(&root); - if let Some(name) = pat.name() { - let text = name.text(); - let shadow_count = bindings_shadow_count.entry(text.clone()).or_default(); + if let Some(Local(local)) = &name_kind { + if let Some(name) = local.name(db) { + let shadow_count = bindings_shadow_count.entry(name.clone()).or_default(); *shadow_count += 1; - binding_hash = Some(calc_binding_hash(file_id, &text, *shadow_count)) + binding_hash = Some(calc_binding_hash(file_id, &name, *shadow_count)) } }; match name_kind { - Some(name_kind) => highlight_name(db, file_id, name.syntax(), &root, name_kind), + Some(name_kind) => highlight_name(db, name_kind), None => name.syntax().parent().map_or("function", |x| match x.kind() { TYPE_PARAM | STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => "type", RECORD_FIELD_DEF => "field", @@ -237,13 +207,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo buf } -fn highlight_name( - db: &RootDatabase, - file_id: FileId, - node: &SyntaxNode, - root: &SyntaxNode, - name_kind: NameKind, -) -> &'static str { +fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str { match name_kind { Macro(_) => "macro", Field(_) => "field", @@ -260,14 +224,15 @@ fn highlight_name( 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)) { + Local(local) => { + if local.is_mut(db) { "variable.mut" } else { - "variable" + match local.ty(db).as_reference() { + Some((_, Mutability::Mut)) => "variable.mut", + _ => "variable", + } } } } -- cgit v1.2.3