From 3188c1451a6f1237859dcb813debef9fa537195c Mon Sep 17 00:00:00 2001 From: Jeremy Kolb Date: Sun, 10 Nov 2019 13:59:39 -0500 Subject: Hover for builtins --- crates/ra_ide_api/src/hover.rs | 50 ++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 19 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index ba328efa1..244c65814 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use hir::{Adt, HasSource, HirDisplay}; +use hir::{Adt, BuiltinType, HasSource, HirDisplay}; use ra_db::SourceDatabase; use ra_syntax::{ algo::{ancestors_at_offset, find_covering_element, find_node_at_offset}, @@ -117,27 +117,27 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option from_def_source(db, it), hir::AssocItem::TypeAlias(it) => from_def_source(db, it), }), - Some(Def(it)) => { - match it { - hir::ModuleDef::Module(it) => { - if let hir::ModuleSource::Module(it) = it.definition_source(db).ast { - res.extend(hover_text(it.doc_comment_text(), it.short_label())) - } + Some(Def(it)) => match it { + hir::ModuleDef::Module(it) => { + if let hir::ModuleSource::Module(it) = it.definition_source(db).ast { + res.extend(hover_text(it.doc_comment_text(), it.short_label())) } - hir::ModuleDef::Function(it) => res.extend(from_def_source(db, it)), - hir::ModuleDef::Adt(Adt::Struct(it)) => res.extend(from_def_source(db, it)), - hir::ModuleDef::Adt(Adt::Union(it)) => res.extend(from_def_source(db, it)), - hir::ModuleDef::Adt(Adt::Enum(it)) => res.extend(from_def_source(db, it)), - hir::ModuleDef::EnumVariant(it) => res.extend(from_def_source(db, it)), - hir::ModuleDef::Const(it) => res.extend(from_def_source(db, it)), - hir::ModuleDef::Static(it) => res.extend(from_def_source(db, it)), - hir::ModuleDef::Trait(it) => res.extend(from_def_source(db, it)), - hir::ModuleDef::TypeAlias(it) => res.extend(from_def_source(db, it)), - hir::ModuleDef::BuiltinType(_) => { - // FIXME: hover for builtin Type ? + } + hir::ModuleDef::Function(it) => res.extend(from_def_source(db, it)), + hir::ModuleDef::Adt(Adt::Struct(it)) => res.extend(from_def_source(db, it)), + hir::ModuleDef::Adt(Adt::Union(it)) => res.extend(from_def_source(db, it)), + hir::ModuleDef::Adt(Adt::Enum(it)) => res.extend(from_def_source(db, it)), + hir::ModuleDef::EnumVariant(it) => res.extend(from_def_source(db, it)), + hir::ModuleDef::Const(it) => res.extend(from_def_source(db, it)), + hir::ModuleDef::Static(it) => res.extend(from_def_source(db, it)), + hir::ModuleDef::Trait(it) => res.extend(from_def_source(db, it)), + hir::ModuleDef::TypeAlias(it) => res.extend(from_def_source(db, it)), + hir::ModuleDef::BuiltinType(it) => { + if let Some(b) = BuiltinType::ALL.iter().find(|(_, ty)| *ty == it) { + res.extend(Some(b.0.to_string())) } } - } + }, Some(SelfType(ty)) => { if let Some((adt_def, _)) = ty.as_adt() { res.extend(match adt_def { @@ -722,4 +722,16 @@ fn func(foo: i32) { if true { <|>foo; }; } assert_eq!(trim_markup_opt(hover.info.first()), Some("macro_rules! foo")); assert_eq!(hover.info.is_exact(), true); } + + #[test] + fn test_hover_tuple_field() { + let (analysis, position) = single_file_with_position( + " + struct TS(String, i32<|>); + ", + ); + let hover = analysis.hover(position).unwrap().unwrap(); + assert_eq!(trim_markup_opt(hover.info.first()), Some("i32")); + assert_eq!(hover.info.is_exact(), true); + } } -- cgit v1.2.3 From 5bb92c2d1af5f3045617a665d4e5c676700eb3c1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 11 Nov 2019 11:59:19 +0300 Subject: impl fmt::Display for BuiltinType --- crates/ra_ide_api/src/hover.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index 244c65814..cc41390b2 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use hir::{Adt, BuiltinType, HasSource, HirDisplay}; +use hir::{Adt, HasSource, HirDisplay}; use ra_db::SourceDatabase; use ra_syntax::{ algo::{ancestors_at_offset, find_covering_element, find_node_at_offset}, @@ -132,11 +132,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option res.extend(from_def_source(db, it)), hir::ModuleDef::Trait(it) => res.extend(from_def_source(db, it)), hir::ModuleDef::TypeAlias(it) => res.extend(from_def_source(db, it)), - hir::ModuleDef::BuiltinType(it) => { - if let Some(b) = BuiltinType::ALL.iter().find(|(_, ty)| *ty == it) { - res.extend(Some(b.0.to_string())) - } - } + hir::ModuleDef::BuiltinType(it) => res.extend(Some(it.to_string())), }, Some(SelfType(ty)) => { if let Some((adt_def, _)) = ty.as_adt() { -- cgit v1.2.3 From 4deba88c33c470f084c531fa979fe5684d37f757 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 11 Nov 2019 11:15:19 +0300 Subject: Introduce ToNav trait --- crates/ra_ide_api/src/display.rs | 2 +- crates/ra_ide_api/src/display/navigation_target.rs | 328 +++++++++++---------- crates/ra_ide_api/src/goto_definition.rs | 16 +- crates/ra_ide_api/src/goto_type_definition.rs | 4 +- crates/ra_ide_api/src/impls.rs | 11 +- crates/ra_ide_api/src/lib.rs | 4 +- crates/ra_ide_api/src/references.rs | 12 +- 7 files changed, 200 insertions(+), 177 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/display.rs b/crates/ra_ide_api/src/display.rs index a980c56bc..30617412a 100644 --- a/crates/ra_ide_api/src/display.rs +++ b/crates/ra_ide_api/src/display.rs @@ -15,7 +15,7 @@ pub use function_signature::FunctionSignature; pub use navigation_target::NavigationTarget; pub use structure::{file_structure, StructureNode}; -pub(crate) use navigation_target::{description_from_symbol, docs_from_symbol}; +pub(crate) use navigation_target::{description_from_symbol, docs_from_symbol, ToNav}; pub(crate) use short_label::ShortLabel; pub(crate) fn function_label(node: &ast::FnDef) -> String { diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index 1bf81e7d5..41d467564 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -29,19 +29,8 @@ pub struct NavigationTarget { docs: Option, } -fn find_range_from_node( - db: &RootDatabase, - src: hir::HirFileId, - node: &SyntaxNode, -) -> (FileId, TextRange) { - let text_range = node.text_range(); - let (file_id, text_range) = src - .expansion_info(db) - .and_then(|expansion_info| expansion_info.find_range(text_range)) - .unwrap_or((src, text_range)); - - // FIXME: handle recursive macro generated macro - (file_id.original_file(db), text_range) +pub(crate) trait ToNav { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget; } impl NavigationTarget { @@ -95,19 +84,6 @@ impl NavigationTarget { NavigationTarget::from_named(db, file_id.into(), pat, None, None) } - pub(crate) fn from_symbol(db: &RootDatabase, symbol: FileSymbol) -> NavigationTarget { - NavigationTarget { - file_id: symbol.file_id, - name: symbol.name.clone(), - kind: symbol.ptr.kind(), - full_range: symbol.ptr.range(), - focus_range: symbol.name_range, - container_name: symbol.container_name.clone(), - description: description_from_symbol(db, &symbol), - docs: docs_from_symbol(db, &symbol), - } - } - pub(crate) fn from_pat( db: &RootDatabase, file_id: FileId, @@ -136,39 +112,6 @@ impl NavigationTarget { } } - pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget { - let src = module.definition_source(db); - let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); - match src.ast { - ModuleSource::SourceFile(node) => { - let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax()); - - NavigationTarget::from_syntax( - file_id, - name, - None, - text_range, - node.syntax(), - None, - None, - ) - } - ModuleSource::Module(node) => { - let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax()); - - NavigationTarget::from_syntax( - file_id, - name, - None, - text_range, - node.syntax(), - node.doc_comment_text(), - node.short_label(), - ) - } - } - } - pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget { let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); if let Some(src) = module.declaration_source(db) { @@ -183,55 +126,7 @@ impl NavigationTarget { src.ast.short_label(), ); } - NavigationTarget::from_module(db, module) - } - - pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget { - let src = field.source(db); - match src.ast { - FieldSource::Named(it) => NavigationTarget::from_named( - db, - src.file_id, - &it, - it.doc_comment_text(), - it.short_label(), - ), - FieldSource::Pos(it) => { - let (file_id, text_range) = find_range_from_node(db, src.file_id, it.syntax()); - NavigationTarget::from_syntax( - file_id, - "".into(), - None, - text_range, - it.syntax(), - None, - None, - ) - } - } - } - - pub(crate) fn from_def_source(db: &RootDatabase, def: D) -> NavigationTarget - where - D: HasSource, - A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel, - { - let src = def.source(db); - NavigationTarget::from_named( - db, - src.file_id, - &src.ast, - src.ast.doc_comment_text(), - src.ast.short_label(), - ) - } - - pub(crate) fn from_adt_def(db: &RootDatabase, adt_def: hir::Adt) -> NavigationTarget { - match adt_def { - hir::Adt::Struct(it) => NavigationTarget::from_def_source(db, it), - hir::Adt::Union(it) => NavigationTarget::from_def_source(db, it), - hir::Adt::Enum(it) => NavigationTarget::from_def_source(db, it), - } + module.to_nav(db) } pub(crate) fn from_def( @@ -239,14 +134,14 @@ impl NavigationTarget { module_def: hir::ModuleDef, ) -> Option { let nav = match module_def { - hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module), - hir::ModuleDef::Function(func) => NavigationTarget::from_def_source(db, func), - hir::ModuleDef::Adt(it) => NavigationTarget::from_adt_def(db, it), - hir::ModuleDef::Const(it) => NavigationTarget::from_def_source(db, it), - hir::ModuleDef::Static(it) => NavigationTarget::from_def_source(db, it), - hir::ModuleDef::EnumVariant(it) => NavigationTarget::from_def_source(db, it), - hir::ModuleDef::Trait(it) => NavigationTarget::from_def_source(db, it), - hir::ModuleDef::TypeAlias(it) => NavigationTarget::from_def_source(db, it), + hir::ModuleDef::Module(module) => module.to_nav(db), + hir::ModuleDef::Function(it) => it.to_nav(db), + hir::ModuleDef::Adt(it) => it.to_nav(db), + hir::ModuleDef::Const(it) => it.to_nav(db), + hir::ModuleDef::Static(it) => it.to_nav(db), + hir::ModuleDef::EnumVariant(it) => it.to_nav(db), + hir::ModuleDef::Trait(it) => it.to_nav(db), + hir::ModuleDef::TypeAlias(it) => it.to_nav(db), hir::ModuleDef::BuiltinType(..) => { return None; } @@ -254,41 +149,6 @@ impl NavigationTarget { Some(nav) } - pub(crate) fn from_impl_block( - db: &RootDatabase, - impl_block: hir::ImplBlock, - ) -> NavigationTarget { - let src = impl_block.source(db); - let (file_id, text_range) = find_range_from_node(db, src.file_id, src.ast.syntax()); - - NavigationTarget::from_syntax( - file_id, - "impl".into(), - None, - text_range, - src.ast.syntax(), - None, - None, - ) - } - - pub(crate) fn from_assoc_item( - db: &RootDatabase, - assoc_item: hir::AssocItem, - ) -> NavigationTarget { - match assoc_item { - AssocItem::Function(it) => NavigationTarget::from_def_source(db, it), - AssocItem::Const(it) => NavigationTarget::from_def_source(db, it), - AssocItem::TypeAlias(it) => NavigationTarget::from_def_source(db, it), - } - } - - pub(crate) fn from_macro_def(db: &RootDatabase, macro_call: hir::MacroDef) -> NavigationTarget { - let src = macro_call.source(db); - log::debug!("nav target {:#?}", src.ast.syntax()); - NavigationTarget::from_named(db, src.file_id, &src.ast, src.ast.doc_comment_text(), None) - } - #[cfg(test)] pub(crate) fn assert_match(&self, expected: &str) { let actual = self.debug_render(); @@ -359,6 +219,172 @@ impl NavigationTarget { } } +impl ToNav for FileSymbol { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + NavigationTarget { + file_id: self.file_id, + name: self.name.clone(), + kind: self.ptr.kind(), + full_range: self.ptr.range(), + focus_range: self.name_range, + container_name: self.container_name.clone(), + description: description_from_symbol(db, self), + docs: docs_from_symbol(db, self), + } + } +} + +pub(crate) trait ToNavFromAst {} +impl ToNavFromAst for hir::Function {} +impl ToNavFromAst for hir::Const {} +impl ToNavFromAst for hir::Static {} +impl ToNavFromAst for hir::Struct {} +impl ToNavFromAst for hir::Enum {} +impl ToNavFromAst for hir::EnumVariant {} +impl ToNavFromAst for hir::Union {} +impl ToNavFromAst for hir::TypeAlias {} +impl ToNavFromAst for hir::Trait {} + +impl ToNav for D +where + D: HasSource + ToNavFromAst + Copy, + D::Ast: ast::DocCommentsOwner + ast::NameOwner + ShortLabel, +{ + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + let src = self.source(db); + NavigationTarget::from_named( + db, + src.file_id, + &src.ast, + src.ast.doc_comment_text(), + src.ast.short_label(), + ) + } +} + +impl ToNav for hir::Module { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + let src = self.definition_source(db); + let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default(); + match src.ast { + ModuleSource::SourceFile(node) => { + let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax()); + + NavigationTarget::from_syntax( + file_id, + name, + None, + text_range, + node.syntax(), + None, + None, + ) + } + ModuleSource::Module(node) => { + let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax()); + + NavigationTarget::from_syntax( + file_id, + name, + None, + text_range, + node.syntax(), + node.doc_comment_text(), + node.short_label(), + ) + } + } + } +} + +impl ToNav for hir::ImplBlock { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + let src = self.source(db); + let (file_id, text_range) = find_range_from_node(db, src.file_id, src.ast.syntax()); + + NavigationTarget::from_syntax( + file_id, + "impl".into(), + None, + text_range, + src.ast.syntax(), + None, + None, + ) + } +} + +impl ToNav for hir::StructField { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + let src = self.source(db); + + match src.ast { + FieldSource::Named(it) => NavigationTarget::from_named( + db, + src.file_id, + &it, + it.doc_comment_text(), + it.short_label(), + ), + FieldSource::Pos(it) => { + let (file_id, text_range) = find_range_from_node(db, src.file_id, it.syntax()); + NavigationTarget::from_syntax( + file_id, + "".into(), + None, + text_range, + it.syntax(), + None, + None, + ) + } + } + } +} + +impl ToNav for hir::MacroDef { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + let src = self.source(db); + log::debug!("nav target {:#?}", src.ast.syntax()); + NavigationTarget::from_named(db, src.file_id, &src.ast, src.ast.doc_comment_text(), None) + } +} + +impl ToNav for hir::Adt { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + match self { + hir::Adt::Struct(it) => it.to_nav(db), + hir::Adt::Union(it) => it.to_nav(db), + hir::Adt::Enum(it) => it.to_nav(db), + } + } +} + +impl ToNav for hir::AssocItem { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + match self { + AssocItem::Function(it) => it.to_nav(db), + AssocItem::Const(it) => it.to_nav(db), + AssocItem::TypeAlias(it) => it.to_nav(db), + } + } +} + +fn find_range_from_node( + db: &RootDatabase, + src: hir::HirFileId, + node: &SyntaxNode, +) -> (FileId, TextRange) { + let text_range = node.text_range(); + let (file_id, text_range) = src + .expansion_info(db) + .and_then(|expansion_info| expansion_info.find_range(text_range)) + .unwrap_or((src, text_range)); + + // FIXME: handle recursive macro generated macro + (file_id.original_file(db), text_range) +} + pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option { let parse = db.parse(symbol.file_id); let node = symbol.ptr.to_node(parse.tree().syntax()); diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index afa59cbe3..713b61d5e 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -9,7 +9,7 @@ use ra_syntax::{ use crate::{ db::RootDatabase, - display::ShortLabel, + display::{ShortLabel, ToNav}, references::{classify_name_ref, NameKind::*}, FilePosition, NavigationTarget, RangeInfo, }; @@ -56,16 +56,16 @@ pub(crate) fn reference_definition( let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind); match name_kind { - Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)), - Some(Field(field)) => return Exact(NavigationTarget::from_field(db, field)), - Some(AssocItem(assoc)) => return Exact(NavigationTarget::from_assoc_item(db, assoc)), + Some(Macro(mac)) => return Exact(mac.to_nav(db)), + Some(Field(field)) => return Exact(field.to_nav(db)), + Some(AssocItem(assoc)) => return Exact(assoc.to_nav(db)), Some(Def(def)) => match NavigationTarget::from_def(db, def) { Some(nav) => return Exact(nav), None => return Approximate(vec![]), }, Some(SelfType(ty)) => { - if let Some((def_id, _)) = ty.as_adt() { - return Exact(NavigationTarget::from_adt_def(db, def_id)); + if let Some((adt, _)) = ty.as_adt() { + return Exact(adt.to_nav(db)); } } Some(Pat((_, pat))) => return Exact(NavigationTarget::from_pat(db, file_id, pat)), @@ -79,7 +79,7 @@ pub(crate) fn reference_definition( // Fallback index based approach: let navs = crate::symbol_index::index_resolve(db, name_ref) .into_iter() - .map(|s| NavigationTarget::from_symbol(db, s)) + .map(|s| s.to_nav(db)) .collect(); Approximate(navs) } @@ -95,7 +95,7 @@ pub(crate) fn name_definition( if module.has_semi() { let src = hir::Source { file_id: file_id.into(), ast: module }; if let Some(child_module) = hir::Module::from_declaration(db, src) { - let nav = NavigationTarget::from_module(db, child_module); + let nav = child_module.to_nav(db); return Some(vec![nav]); } } diff --git a/crates/ra_ide_api/src/goto_type_definition.rs b/crates/ra_ide_api/src/goto_type_definition.rs index 059d80524..71146591d 100644 --- a/crates/ra_ide_api/src/goto_type_definition.rs +++ b/crates/ra_ide_api/src/goto_type_definition.rs @@ -3,7 +3,7 @@ use ra_db::SourceDatabase; use ra_syntax::{ast, AstNode}; -use crate::{db::RootDatabase, FilePosition, NavigationTarget, RangeInfo}; +use crate::{db::RootDatabase, display::ToNav, FilePosition, NavigationTarget, RangeInfo}; pub(crate) fn goto_type_definition( db: &RootDatabase, @@ -33,7 +33,7 @@ pub(crate) fn goto_type_definition( let adt_def = analyzer.autoderef(db, ty).find_map(|ty| ty.as_adt().map(|adt| adt.0))?; - let nav = NavigationTarget::from_adt_def(db, adt_def); + let nav = adt_def.to_nav(db); Some(RangeInfo::new(node.text_range(), vec![nav])) } diff --git a/crates/ra_ide_api/src/impls.rs b/crates/ra_ide_api/src/impls.rs index b899ed3a5..bc9b66550 100644 --- a/crates/ra_ide_api/src/impls.rs +++ b/crates/ra_ide_api/src/impls.rs @@ -4,7 +4,7 @@ use hir::{db::HirDatabase, ApplicationTy, FromSource, Ty, TypeCtor}; use ra_db::SourceDatabase; use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; -use crate::{db::RootDatabase, FilePosition, NavigationTarget, RangeInfo}; +use crate::{db::RootDatabase, display::ToNav, FilePosition, NavigationTarget, RangeInfo}; pub(crate) fn goto_implementation( db: &RootDatabase, @@ -58,7 +58,7 @@ fn impls_for_def( impls .all_impls() .filter(|impl_block| is_equal_for_find_impls(&ty, &impl_block.target_ty(db))) - .map(|imp| NavigationTarget::from_impl_block(db, imp)) + .map(|imp| imp.to_nav(db)) .collect(), ) } @@ -75,12 +75,7 @@ fn impls_for_trait( let krate = module.krate(); let impls = db.impls_in_crate(krate); - Some( - impls - .lookup_impl_blocks_for_trait(tr) - .map(|imp| NavigationTarget::from_impl_block(db, imp)) - .collect(), - ) + Some(impls.lookup_impl_blocks_for_trait(tr).map(|imp| imp.to_nav(db)).collect()) } fn is_equal_for_find_impls(original_ty: &Ty, impl_ty: &Ty) -> bool { diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index d0188da44..484fbcc82 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -56,7 +56,7 @@ use ra_db::{ }; use ra_syntax::{SourceFile, TextRange, TextUnit}; -use crate::{db::LineIndexDatabase, symbol_index::FileSymbol}; +use crate::{db::LineIndexDatabase, display::ToNav, symbol_index::FileSymbol}; pub use crate::{ assists::{Assist, AssistId}, @@ -351,7 +351,7 @@ impl Analysis { self.with_db(|db| { symbol_index::world_symbols(db, query) .into_iter() - .map(|s| NavigationTarget::from_symbol(db, s)) + .map(|s| s.to_nav(db)) .collect::>() }) } diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index b5b1c9a16..0a76bf484 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs @@ -19,7 +19,9 @@ use ra_db::{SourceDatabase, SourceDatabaseExt}; use ra_prof::profile; use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, SyntaxNode, TextUnit}; -use crate::{db::RootDatabase, FilePosition, FileRange, NavigationTarget, RangeInfo}; +use crate::{ + db::RootDatabase, display::ToNav, FilePosition, FileRange, NavigationTarget, RangeInfo, +}; pub(crate) use self::{ classify::{classify_name, classify_name_ref}, @@ -76,12 +78,12 @@ pub(crate) fn find_all_refs( let RangeInfo { range, info: (name, def) } = find_name(db, &syntax, position)?; let declaration = match def.kind { - NameKind::Macro(mac) => NavigationTarget::from_macro_def(db, mac), - NameKind::Field(field) => NavigationTarget::from_field(db, field), - NameKind::AssocItem(assoc) => NavigationTarget::from_assoc_item(db, assoc), + NameKind::Macro(mac) => mac.to_nav(db), + NameKind::Field(field) => field.to_nav(db), + NameKind::AssocItem(assoc) => assoc.to_nav(db), NameKind::Def(def) => NavigationTarget::from_def(db, def)?, NameKind::SelfType(ref ty) => match ty.as_adt() { - Some((def_id, _)) => NavigationTarget::from_adt_def(db, def_id), + Some((adt, _)) => adt.to_nav(db), None => return None, }, NameKind::Pat((_, pat)) => NavigationTarget::from_pat(db, position.file_id, pat), -- cgit v1.2.3 From ef70925f5f4f89f56f113c11bca44fd1a7764d50 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 11 Nov 2019 12:52:14 +0300 Subject: Refactor highlighting to use classify_name --- crates/ra_ide_api/src/syntax_highlighting.rs | 145 ++++++++++++++------------- 1 file changed, 73 insertions(+), 72 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs index 33f3caceb..1ee68abe2 100644 --- a/crates/ra_ide_api/src/syntax_highlighting.rs +++ b/crates/ra_ide_api/src/syntax_highlighting.rs @@ -9,12 +9,15 @@ use ra_syntax::{ ast::{self, NameOwner}, AstNode, Direction, SmolStr, SyntaxElement, SyntaxKind, SyntaxKind::*, - TextRange, T, + SyntaxNode, TextRange, T, }; use crate::{ db::RootDatabase, - references::{classify_name_ref, NameKind::*}, + references::{ + classify_name, classify_name_ref, + NameKind::{self, *}, + }, FileId, }; @@ -100,81 +103,43 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec "macro", - 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); - if let Some(name) = pat.name() { - let text = name.text(); - let shadow_count = - bindings_shadow_count.entry(text.clone()).or_default(); - binding_hash = - Some(calc_binding_hash(file_id, &text, *shadow_count)) - } - let analyzer = - hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); - if is_variable_mutable(db, &analyzer, ptr.to_node(&root)) { - "variable.mut" - } else { - "variable" - } - } - Some(SelfParam(_)) => "type", - Some(GenericParam(_)) => "type", - None => "text", + 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); + + 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(); + binding_hash = Some(calc_binding_hash(file_id, &text, *shadow_count)) } - } else { - "text" - } + }; + + name_kind + .map_or("text", |it| highlight_name(db, file_id, name_ref.syntax(), &root, it)) } NAME => { - if let Some(name) = node.as_node().cloned().and_then(ast::Name::cast) { - let analyzer = hir::SourceAnalyzer::new(db, file_id, name.syntax(), None); - if let Some(pat) = name.syntax().ancestors().find_map(ast::BindPat::cast) { - if let Some(name) = pat.name() { - let text = name.text(); - let shadow_count = - bindings_shadow_count.entry(text.clone()).or_default(); - *shadow_count += 1; - binding_hash = Some(calc_binding_hash(file_id, &text, *shadow_count)) - } - - if is_variable_mutable(db, &analyzer, pat) { - "variable.mut" - } else { - "variable" - } - } 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", - _ => "function", - }) - .unwrap_or("function") + 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(); + *shadow_count += 1; + binding_hash = Some(calc_binding_hash(file_id, &text, *shadow_count)) } - } else { - "text" + }; + + match name_kind { + Some(name_kind) => highlight_name(db, file_id, name.syntax(), &root, 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", + _ => "function", + }), } } 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 } +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 fn html_escape(text: &str) -> String { text.replace("<", "<").replace(">", ">") -- cgit v1.2.3 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/display/navigation_target.rs | 70 ++++++++----------- crates/ra_ide_api/src/goto_definition.rs | 3 +- crates/ra_ide_api/src/hover.rs | 2 +- crates/ra_ide_api/src/references.rs | 3 +- crates/ra_ide_api/src/references/classify.rs | 22 +++--- .../ra_ide_api/src/references/name_definition.rs | 38 +---------- crates/ra_ide_api/src/references/search_scope.rs | 6 +- .../src/snapshots/rainbow_highlighting.html | 12 ++-- crates/ra_ide_api/src/syntax_highlighting.rs | 79 ++++++---------------- 9 files changed, 81 insertions(+), 154 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index 41d467564..f7ad08515 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -1,11 +1,11 @@ //! FIXME: write short doc here -use hir::{AssocItem, FieldSource, HasSource, ModuleSource}; +use hir::{AssocItem, Either, FieldSource, HasSource, ModuleSource}; use ra_db::{FileId, SourceDatabase}; use ra_syntax::{ - ast::{self, DocCommentsOwner}, - match_ast, AstNode, AstPtr, SmolStr, - SyntaxKind::{self, NAME}, + ast::{self, DocCommentsOwner, NameOwner}, + match_ast, AstNode, SmolStr, + SyntaxKind::{self, BIND_PAT}, SyntaxNode, TextRange, }; @@ -76,42 +76,6 @@ impl NavigationTarget { self.focus_range } - pub(crate) fn from_bind_pat( - db: &RootDatabase, - file_id: FileId, - pat: &ast::BindPat, - ) -> NavigationTarget { - NavigationTarget::from_named(db, file_id.into(), pat, None, None) - } - - pub(crate) fn from_pat( - db: &RootDatabase, - file_id: FileId, - pat: AstPtr, - ) -> NavigationTarget { - let parse = db.parse(file_id); - let pat = pat.to_node(parse.tree().syntax()); - NavigationTarget::from_bind_pat(db, file_id, &pat) - } - - pub(crate) fn from_self_param( - file_id: FileId, - par: AstPtr, - ) -> NavigationTarget { - let (name, full_range) = ("self".into(), par.syntax_node_ptr().range()); - - NavigationTarget { - file_id, - name, - full_range, - focus_range: None, - kind: NAME, - container_name: None, - description: None, //< No document node for SelfParam - docs: None, //< No document node for SelfParam - } - } - pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget { let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); if let Some(src) = module.declaration_source(db) { @@ -370,6 +334,32 @@ impl ToNav for hir::AssocItem { } } +impl ToNav for hir::Local { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + let src = self.source(db); + let (full_range, focus_range) = match src.ast { + Either::A(it) => { + (it.syntax().text_range(), it.name().map(|it| it.syntax().text_range())) + } + Either::B(it) => (it.syntax().text_range(), Some(it.self_kw_token().text_range())), + }; + let name = match self.name(db) { + Some(it) => it.to_string().into(), + None => "".into(), + }; + NavigationTarget { + file_id: src.file_id.original_file(db), + name, + kind: BIND_PAT, + full_range, + focus_range, + container_name: None, + description: None, + docs: None, + } + } +} + fn find_range_from_node( db: &RootDatabase, src: hir::HirFileId, diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 713b61d5e..6c8387f6c 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -68,8 +68,7 @@ pub(crate) fn reference_definition( return Exact(adt.to_nav(db)); } } - Some(Pat((_, pat))) => return Exact(NavigationTarget::from_pat(db, file_id, pat)), - Some(SelfParam(par)) => return Exact(NavigationTarget::from_self_param(file_id, par)), + Some(Local(local)) => return Exact(local.to_nav(db)), Some(GenericParam(_)) => { // FIXME: go to the generic param def } diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index cc41390b2..086e6dec3 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -143,7 +143,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option { + Some(Local(_)) => { // Hover for these shows type names no_fallback = true; } diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index 0a76bf484..9cb9433e7 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs @@ -86,8 +86,7 @@ pub(crate) fn find_all_refs( Some((adt, _)) => adt.to_nav(db), None => return None, }, - NameKind::Pat((_, pat)) => NavigationTarget::from_pat(db, position.file_id, pat), - NameKind::SelfParam(par) => NavigationTarget::from_self_param(position.file_id, par), + NameKind::Local(local) => local.to_nav(db), NameKind::GenericParam(_) => return None, }; diff --git a/crates/ra_ide_api/src/references/classify.rs b/crates/ra_ide_api/src/references/classify.rs index 153082d5b..217f9951e 100644 --- a/crates/ra_ide_api/src/references/classify.rs +++ b/crates/ra_ide_api/src/references/classify.rs @@ -1,13 +1,13 @@ //! Functions that are used to classify an element from its definition or reference. -use hir::{Either, FromSource, Module, ModuleSource, Path, PathResolution, Source, SourceAnalyzer}; +use hir::{FromSource, Module, ModuleSource, Path, PathResolution, Source, SourceAnalyzer}; use ra_db::FileId; use ra_prof::profile; -use ra_syntax::{ast, match_ast, AstNode, AstPtr}; +use ra_syntax::{ast, match_ast, AstNode}; use test_utils::tested_by; use super::{ - name_definition::{from_assoc_item, from_module_def, from_pat, from_struct_field}, + name_definition::{from_assoc_item, from_module_def, from_struct_field}, NameDefinition, NameKind, }; use crate::db::RootDatabase; @@ -25,7 +25,13 @@ pub(crate) fn classify_name( match_ast! { match parent { ast::BindPat(it) => { - from_pat(db, file_id, AstPtr::new(&it)) + let src = hir::Source { file_id, ast: it }; + let local = hir::Local::from_source(db, src)?; + Some(NameDefinition { + visibility: None, + container: local.module(db), + kind: NameKind::Local(local), + }) }, ast::RecordFieldDef(it) => { let ast = hir::FieldSource::Named(it); @@ -159,10 +165,10 @@ pub(crate) fn classify_name_ref( match resolved { Def(def) => Some(from_module_def(db, def, Some(container))), AssocItem(item) => Some(from_assoc_item(db, item)), - LocalBinding(Either::A(pat)) => from_pat(db, file_id, pat), - LocalBinding(Either::B(par)) => { - let kind = NameKind::SelfParam(par); - Some(NameDefinition { kind, container, visibility }) + Local(local) => { + let container = local.module(db); + let kind = NameKind::Local(local); + Some(NameDefinition { kind, container, visibility: None }) } GenericParam(par) => { // FIXME: get generic param def diff --git a/crates/ra_ide_api/src/references/name_definition.rs b/crates/ra_ide_api/src/references/name_definition.rs index 4580bc789..450f7ea9b 100644 --- a/crates/ra_ide_api/src/references/name_definition.rs +++ b/crates/ra_ide_api/src/references/name_definition.rs @@ -4,10 +4,9 @@ //! Note that the reference search is possible for not all of the classified items. use hir::{ - db::AstDatabase, Adt, AssocItem, DefWithBody, FromSource, HasSource, HirFileId, MacroDef, - Module, ModuleDef, StructField, Ty, VariantDef, + Adt, AssocItem, HasSource, Local, MacroDef, Module, ModuleDef, StructField, Ty, VariantDef, }; -use ra_syntax::{ast, ast::VisibilityOwner, match_ast, AstNode, AstPtr}; +use ra_syntax::{ast, ast::VisibilityOwner}; use crate::db::RootDatabase; @@ -18,8 +17,7 @@ pub enum NameKind { AssocItem(AssocItem), Def(ModuleDef), SelfType(Ty), - Pat((DefWithBody, AstPtr)), - SelfParam(AstPtr), + Local(Local), GenericParam(u32), } @@ -30,36 +28,6 @@ pub(crate) struct NameDefinition { pub kind: NameKind, } -pub(super) fn from_pat( - db: &RootDatabase, - file_id: HirFileId, - pat: AstPtr, -) -> Option { - let root = db.parse_or_expand(file_id)?; - let def = pat.to_node(&root).syntax().ancestors().find_map(|node| { - match_ast! { - match node { - ast::FnDef(it) => { - let src = hir::Source { file_id, ast: it }; - Some(hir::Function::from_source(db, src)?.into()) - }, - ast::ConstDef(it) => { - let src = hir::Source { file_id, ast: it }; - Some(hir::Const::from_source(db, src)?.into()) - }, - ast::StaticDef(it) => { - let src = hir::Source { file_id, ast: it }; - Some(hir::Static::from_source(db, src)?.into()) - }, - _ => None, - } - } - })?; - let kind = NameKind::Pat((def, pat)); - let container = def.module(db); - Some(NameDefinition { kind, container, visibility: None }) -} - pub(super) fn from_assoc_item(db: &RootDatabase, item: AssocItem) -> NameDefinition { let container = item.module(db); let visibility = match item { diff --git a/crates/ra_ide_api/src/references/search_scope.rs b/crates/ra_ide_api/src/references/search_scope.rs index f2789e0b2..2907787c2 100644 --- a/crates/ra_ide_api/src/references/search_scope.rs +++ b/crates/ra_ide_api/src/references/search_scope.rs @@ -71,13 +71,13 @@ impl NameDefinition { let module_src = self.container.definition_source(db); let file_id = module_src.file_id.original_file(db); - if let NameKind::Pat((def, _)) = self.kind { - let mut res = FxHashMap::default(); - let range = match def { + if let NameKind::Local(var) = self.kind { + let range = match var.parent(db) { DefWithBody::Function(f) => f.source(db).ast.syntax().text_range(), DefWithBody::Const(c) => c.source(db).ast.syntax().text_range(), DefWithBody::Static(s) => s.source(db).ast.syntax().text_range(), }; + let mut res = FxHashMap::default(); res.insert(file_id, Some(range)); return SearchScope::new(res); } diff --git a/crates/ra_ide_api/src/snapshots/rainbow_highlighting.html b/crates/ra_ide_api/src/snapshots/rainbow_highlighting.html index ed664817e..79f11ea80 100644 --- a/crates/ra_ide_api/src/snapshots/rainbow_highlighting.html +++ b/crates/ra_ide_api/src/snapshots/rainbow_highlighting.html @@ -20,14 +20,14 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .keyword\.control { color: #F0DFAF; font-weight: bold; }
fn main() {
-    let hello = "hello";
-    let x = hello.to_string();
-    let y = hello.to_string();
+    let hello = "hello";
+    let x = hello.to_string();
+    let y = hello.to_string();
 
-    let x = "other color please!";
-    let y = x.to_string();
+    let x = "other color please!";
+    let y = x.to_string();
 }
 
 fn bar() {
-    let mut hello = "hello";
+    let mut hello = "hello";
 }
\ No newline at end of file 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