diff options
author | Aleksey Kladov <[email protected]> | 2019-11-09 21:32:00 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-11-11 11:42:08 +0000 |
commit | 8b7f853cc19d0940ec542e10bc23aa78455bbb3b (patch) | |
tree | eb418bb0df37516dcf24ffc754a862ffea1ab789 /crates/ra_ide_api/src | |
parent | 5ac4ffbc121c8231fe3ea5c2bb918f7aae60f197 (diff) |
Add hir::Local
Diffstat (limited to 'crates/ra_ide_api/src')
-rw-r--r-- | crates/ra_ide_api/src/display/navigation_target.rs | 70 | ||||
-rw-r--r-- | crates/ra_ide_api/src/goto_definition.rs | 3 | ||||
-rw-r--r-- | crates/ra_ide_api/src/hover.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide_api/src/references.rs | 3 | ||||
-rw-r--r-- | crates/ra_ide_api/src/references/classify.rs | 22 | ||||
-rw-r--r-- | crates/ra_ide_api/src/references/name_definition.rs | 38 | ||||
-rw-r--r-- | crates/ra_ide_api/src/references/search_scope.rs | 6 | ||||
-rw-r--r-- | crates/ra_ide_api/src/snapshots/rainbow_highlighting.html | 12 | ||||
-rw-r--r-- | crates/ra_ide_api/src/syntax_highlighting.rs | 79 |
9 files changed, 81 insertions, 154 deletions
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 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use hir::{AssocItem, FieldSource, HasSource, ModuleSource}; | 3 | use hir::{AssocItem, Either, FieldSource, HasSource, ModuleSource}; |
4 | use ra_db::{FileId, SourceDatabase}; | 4 | use ra_db::{FileId, SourceDatabase}; |
5 | use ra_syntax::{ | 5 | use ra_syntax::{ |
6 | ast::{self, DocCommentsOwner}, | 6 | ast::{self, DocCommentsOwner, NameOwner}, |
7 | match_ast, AstNode, AstPtr, SmolStr, | 7 | match_ast, AstNode, SmolStr, |
8 | SyntaxKind::{self, NAME}, | 8 | SyntaxKind::{self, BIND_PAT}, |
9 | SyntaxNode, TextRange, | 9 | SyntaxNode, TextRange, |
10 | }; | 10 | }; |
11 | 11 | ||
@@ -76,42 +76,6 @@ impl NavigationTarget { | |||
76 | self.focus_range | 76 | self.focus_range |
77 | } | 77 | } |
78 | 78 | ||
79 | pub(crate) fn from_bind_pat( | ||
80 | db: &RootDatabase, | ||
81 | file_id: FileId, | ||
82 | pat: &ast::BindPat, | ||
83 | ) -> NavigationTarget { | ||
84 | NavigationTarget::from_named(db, file_id.into(), pat, None, None) | ||
85 | } | ||
86 | |||
87 | pub(crate) fn from_pat( | ||
88 | db: &RootDatabase, | ||
89 | file_id: FileId, | ||
90 | pat: AstPtr<ast::BindPat>, | ||
91 | ) -> NavigationTarget { | ||
92 | let parse = db.parse(file_id); | ||
93 | let pat = pat.to_node(parse.tree().syntax()); | ||
94 | NavigationTarget::from_bind_pat(db, file_id, &pat) | ||
95 | } | ||
96 | |||
97 | pub(crate) fn from_self_param( | ||
98 | file_id: FileId, | ||
99 | par: AstPtr<ast::SelfParam>, | ||
100 | ) -> NavigationTarget { | ||
101 | let (name, full_range) = ("self".into(), par.syntax_node_ptr().range()); | ||
102 | |||
103 | NavigationTarget { | ||
104 | file_id, | ||
105 | name, | ||
106 | full_range, | ||
107 | focus_range: None, | ||
108 | kind: NAME, | ||
109 | container_name: None, | ||
110 | description: None, //< No document node for SelfParam | ||
111 | docs: None, //< No document node for SelfParam | ||
112 | } | ||
113 | } | ||
114 | |||
115 | pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget { | 79 | pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget { |
116 | let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); | 80 | let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); |
117 | if let Some(src) = module.declaration_source(db) { | 81 | if let Some(src) = module.declaration_source(db) { |
@@ -370,6 +334,32 @@ impl ToNav for hir::AssocItem { | |||
370 | } | 334 | } |
371 | } | 335 | } |
372 | 336 | ||
337 | impl ToNav for hir::Local { | ||
338 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | ||
339 | let src = self.source(db); | ||
340 | let (full_range, focus_range) = match src.ast { | ||
341 | Either::A(it) => { | ||
342 | (it.syntax().text_range(), it.name().map(|it| it.syntax().text_range())) | ||
343 | } | ||
344 | Either::B(it) => (it.syntax().text_range(), Some(it.self_kw_token().text_range())), | ||
345 | }; | ||
346 | let name = match self.name(db) { | ||
347 | Some(it) => it.to_string().into(), | ||
348 | None => "".into(), | ||
349 | }; | ||
350 | NavigationTarget { | ||
351 | file_id: src.file_id.original_file(db), | ||
352 | name, | ||
353 | kind: BIND_PAT, | ||
354 | full_range, | ||
355 | focus_range, | ||
356 | container_name: None, | ||
357 | description: None, | ||
358 | docs: None, | ||
359 | } | ||
360 | } | ||
361 | } | ||
362 | |||
373 | fn find_range_from_node( | 363 | fn find_range_from_node( |
374 | db: &RootDatabase, | 364 | db: &RootDatabase, |
375 | src: hir::HirFileId, | 365 | 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( | |||
68 | return Exact(adt.to_nav(db)); | 68 | return Exact(adt.to_nav(db)); |
69 | } | 69 | } |
70 | } | 70 | } |
71 | Some(Pat((_, pat))) => return Exact(NavigationTarget::from_pat(db, file_id, pat)), | 71 | Some(Local(local)) => return Exact(local.to_nav(db)), |
72 | Some(SelfParam(par)) => return Exact(NavigationTarget::from_self_param(file_id, par)), | ||
73 | Some(GenericParam(_)) => { | 72 | Some(GenericParam(_)) => { |
74 | // FIXME: go to the generic param def | 73 | // FIXME: go to the generic param def |
75 | } | 74 | } |
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<RangeIn | |||
143 | }) | 143 | }) |
144 | } | 144 | } |
145 | } | 145 | } |
146 | Some(Pat(_)) | Some(SelfParam(_)) => { | 146 | Some(Local(_)) => { |
147 | // Hover for these shows type names | 147 | // Hover for these shows type names |
148 | no_fallback = true; | 148 | no_fallback = true; |
149 | } | 149 | } |
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( | |||
86 | Some((adt, _)) => adt.to_nav(db), | 86 | Some((adt, _)) => adt.to_nav(db), |
87 | None => return None, | 87 | None => return None, |
88 | }, | 88 | }, |
89 | NameKind::Pat((_, pat)) => NavigationTarget::from_pat(db, position.file_id, pat), | 89 | NameKind::Local(local) => local.to_nav(db), |
90 | NameKind::SelfParam(par) => NavigationTarget::from_self_param(position.file_id, par), | ||
91 | NameKind::GenericParam(_) => return None, | 90 | NameKind::GenericParam(_) => return None, |
92 | }; | 91 | }; |
93 | 92 | ||
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 @@ | |||
1 | //! Functions that are used to classify an element from its definition or reference. | 1 | //! Functions that are used to classify an element from its definition or reference. |
2 | 2 | ||
3 | use hir::{Either, FromSource, Module, ModuleSource, Path, PathResolution, Source, SourceAnalyzer}; | 3 | use hir::{FromSource, Module, ModuleSource, Path, PathResolution, Source, SourceAnalyzer}; |
4 | use ra_db::FileId; | 4 | use ra_db::FileId; |
5 | use ra_prof::profile; | 5 | use ra_prof::profile; |
6 | use ra_syntax::{ast, match_ast, AstNode, AstPtr}; | 6 | use ra_syntax::{ast, match_ast, AstNode}; |
7 | use test_utils::tested_by; | 7 | use test_utils::tested_by; |
8 | 8 | ||
9 | use super::{ | 9 | use super::{ |
10 | name_definition::{from_assoc_item, from_module_def, from_pat, from_struct_field}, | 10 | name_definition::{from_assoc_item, from_module_def, from_struct_field}, |
11 | NameDefinition, NameKind, | 11 | NameDefinition, NameKind, |
12 | }; | 12 | }; |
13 | use crate::db::RootDatabase; | 13 | use crate::db::RootDatabase; |
@@ -25,7 +25,13 @@ pub(crate) fn classify_name( | |||
25 | match_ast! { | 25 | match_ast! { |
26 | match parent { | 26 | match parent { |
27 | ast::BindPat(it) => { | 27 | ast::BindPat(it) => { |
28 | from_pat(db, file_id, AstPtr::new(&it)) | 28 | let src = hir::Source { file_id, ast: it }; |
29 | let local = hir::Local::from_source(db, src)?; | ||
30 | Some(NameDefinition { | ||
31 | visibility: None, | ||
32 | container: local.module(db), | ||
33 | kind: NameKind::Local(local), | ||
34 | }) | ||
29 | }, | 35 | }, |
30 | ast::RecordFieldDef(it) => { | 36 | ast::RecordFieldDef(it) => { |
31 | let ast = hir::FieldSource::Named(it); | 37 | let ast = hir::FieldSource::Named(it); |
@@ -159,10 +165,10 @@ pub(crate) fn classify_name_ref( | |||
159 | match resolved { | 165 | match resolved { |
160 | Def(def) => Some(from_module_def(db, def, Some(container))), | 166 | Def(def) => Some(from_module_def(db, def, Some(container))), |
161 | AssocItem(item) => Some(from_assoc_item(db, item)), | 167 | AssocItem(item) => Some(from_assoc_item(db, item)), |
162 | LocalBinding(Either::A(pat)) => from_pat(db, file_id, pat), | 168 | Local(local) => { |
163 | LocalBinding(Either::B(par)) => { | 169 | let container = local.module(db); |
164 | let kind = NameKind::SelfParam(par); | 170 | let kind = NameKind::Local(local); |
165 | Some(NameDefinition { kind, container, visibility }) | 171 | Some(NameDefinition { kind, container, visibility: None }) |
166 | } | 172 | } |
167 | GenericParam(par) => { | 173 | GenericParam(par) => { |
168 | // FIXME: get generic param def | 174 | // 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 @@ | |||
4 | //! Note that the reference search is possible for not all of the classified items. | 4 | //! Note that the reference search is possible for not all of the classified items. |
5 | 5 | ||
6 | use hir::{ | 6 | use hir::{ |
7 | db::AstDatabase, Adt, AssocItem, DefWithBody, FromSource, HasSource, HirFileId, MacroDef, | 7 | Adt, AssocItem, HasSource, Local, MacroDef, Module, ModuleDef, StructField, Ty, VariantDef, |
8 | Module, ModuleDef, StructField, Ty, VariantDef, | ||
9 | }; | 8 | }; |
10 | use ra_syntax::{ast, ast::VisibilityOwner, match_ast, AstNode, AstPtr}; | 9 | use ra_syntax::{ast, ast::VisibilityOwner}; |
11 | 10 | ||
12 | use crate::db::RootDatabase; | 11 | use crate::db::RootDatabase; |
13 | 12 | ||
@@ -18,8 +17,7 @@ pub enum NameKind { | |||
18 | AssocItem(AssocItem), | 17 | AssocItem(AssocItem), |
19 | Def(ModuleDef), | 18 | Def(ModuleDef), |
20 | SelfType(Ty), | 19 | SelfType(Ty), |
21 | Pat((DefWithBody, AstPtr<ast::BindPat>)), | 20 | Local(Local), |
22 | SelfParam(AstPtr<ast::SelfParam>), | ||
23 | GenericParam(u32), | 21 | GenericParam(u32), |
24 | } | 22 | } |
25 | 23 | ||
@@ -30,36 +28,6 @@ pub(crate) struct NameDefinition { | |||
30 | pub kind: NameKind, | 28 | pub kind: NameKind, |
31 | } | 29 | } |
32 | 30 | ||
33 | pub(super) fn from_pat( | ||
34 | db: &RootDatabase, | ||
35 | file_id: HirFileId, | ||
36 | pat: AstPtr<ast::BindPat>, | ||
37 | ) -> Option<NameDefinition> { | ||
38 | let root = db.parse_or_expand(file_id)?; | ||
39 | let def = pat.to_node(&root).syntax().ancestors().find_map(|node| { | ||
40 | match_ast! { | ||
41 | match node { | ||
42 | ast::FnDef(it) => { | ||
43 | let src = hir::Source { file_id, ast: it }; | ||
44 | Some(hir::Function::from_source(db, src)?.into()) | ||
45 | }, | ||
46 | ast::ConstDef(it) => { | ||
47 | let src = hir::Source { file_id, ast: it }; | ||
48 | Some(hir::Const::from_source(db, src)?.into()) | ||
49 | }, | ||
50 | ast::StaticDef(it) => { | ||
51 | let src = hir::Source { file_id, ast: it }; | ||
52 | Some(hir::Static::from_source(db, src)?.into()) | ||
53 | }, | ||
54 | _ => None, | ||
55 | } | ||
56 | } | ||
57 | })?; | ||
58 | let kind = NameKind::Pat((def, pat)); | ||
59 | let container = def.module(db); | ||
60 | Some(NameDefinition { kind, container, visibility: None }) | ||
61 | } | ||
62 | |||
63 | pub(super) fn from_assoc_item(db: &RootDatabase, item: AssocItem) -> NameDefinition { | 31 | pub(super) fn from_assoc_item(db: &RootDatabase, item: AssocItem) -> NameDefinition { |
64 | let container = item.module(db); | 32 | let container = item.module(db); |
65 | let visibility = match item { | 33 | 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 { | |||
71 | let module_src = self.container.definition_source(db); | 71 | let module_src = self.container.definition_source(db); |
72 | let file_id = module_src.file_id.original_file(db); | 72 | let file_id = module_src.file_id.original_file(db); |
73 | 73 | ||
74 | if let NameKind::Pat((def, _)) = self.kind { | 74 | if let NameKind::Local(var) = self.kind { |
75 | let mut res = FxHashMap::default(); | 75 | let range = match var.parent(db) { |
76 | let range = match def { | ||
77 | DefWithBody::Function(f) => f.source(db).ast.syntax().text_range(), | 76 | DefWithBody::Function(f) => f.source(db).ast.syntax().text_range(), |
78 | DefWithBody::Const(c) => c.source(db).ast.syntax().text_range(), | 77 | DefWithBody::Const(c) => c.source(db).ast.syntax().text_range(), |
79 | DefWithBody::Static(s) => s.source(db).ast.syntax().text_range(), | 78 | DefWithBody::Static(s) => s.source(db).ast.syntax().text_range(), |
80 | }; | 79 | }; |
80 | let mut res = FxHashMap::default(); | ||
81 | res.insert(file_id, Some(range)); | 81 | res.insert(file_id, Some(range)); |
82 | return SearchScope::new(res); | 82 | return SearchScope::new(res); |
83 | } | 83 | } |
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 | |||
20 | .keyword\.control { color: #F0DFAF; font-weight: bold; } | 20 | .keyword\.control { color: #F0DFAF; font-weight: bold; } |
21 | </style> | 21 | </style> |
22 | <pre><code><span class="keyword">fn</span> <span class="function">main</span>() { | 22 | <pre><code><span class="keyword">fn</span> <span class="function">main</span>() { |
23 | <span class="keyword">let</span> <span class="variable" data-binding-hash="3888301305669440875" style="color: hsl(242,59%,59%);">hello</span> = <span class="string">"hello"</span>; | 23 | <span class="keyword">let</span> <span class="variable" data-binding-hash="8723171760279909834" style="color: hsl(307,91%,75%);">hello</span> = <span class="string">"hello"</span>; |
24 | <span class="keyword">let</span> <span class="variable" data-binding-hash="5695551762718493399" style="color: hsl(272,48%,45%);">x</span> = <span class="variable" data-binding-hash="3888301305669440875" style="color: hsl(242,59%,59%);">hello</span>.<span class="text">to_string</span>(); | 24 | <span class="keyword">let</span> <span class="variable" data-binding-hash="14702933417323009544" style="color: hsl(108,90%,49%);">x</span> = <span class="variable" data-binding-hash="8723171760279909834" style="color: hsl(307,91%,75%);">hello</span>.<span class="text">to_string</span>(); |
25 | <span class="keyword">let</span> <span class="variable" data-binding-hash="5435401749617022797" style="color: hsl(353,77%,74%);">y</span> = <span class="variable" data-binding-hash="3888301305669440875" style="color: hsl(242,59%,59%);">hello</span>.<span class="text">to_string</span>(); | 25 | <span class="keyword">let</span> <span class="variable" data-binding-hash="5443150872754369068" style="color: hsl(215,43%,43%);">y</span> = <span class="variable" data-binding-hash="8723171760279909834" style="color: hsl(307,91%,75%);">hello</span>.<span class="text">to_string</span>(); |
26 | 26 | ||
27 | <span class="keyword">let</span> <span class="variable" data-binding-hash="1903207544374197704" style="color: hsl(58,61%,61%);">x</span> = <span class="string">"other color please!"</span>; | 27 | <span class="keyword">let</span> <span class="variable" data-binding-hash="17358108296605513516" style="color: hsl(331,46%,60%);">x</span> = <span class="string">"other color please!"</span>; |
28 | <span class="keyword">let</span> <span class="variable" data-binding-hash="14878783531007968800" style="color: hsl(265,73%,83%);">y</span> = <span class="variable" data-binding-hash="1903207544374197704" style="color: hsl(58,61%,61%);">x</span>.<span class="text">to_string</span>(); | 28 | <span class="keyword">let</span> <span class="variable" data-binding-hash="2073121142529774969" style="color: hsl(320,43%,74%);">y</span> = <span class="variable" data-binding-hash="17358108296605513516" style="color: hsl(331,46%,60%);">x</span>.<span class="text">to_string</span>(); |
29 | } | 29 | } |
30 | 30 | ||
31 | <span class="keyword">fn</span> <span class="function">bar</span>() { | 31 | <span class="keyword">fn</span> <span class="function">bar</span>() { |
32 | <span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable.mut" data-binding-hash="3888301305669440875" style="color: hsl(242,59%,59%);">hello</span> = <span class="string">"hello"</span>; | 32 | <span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable.mut" data-binding-hash="8723171760279909834" style="color: hsl(307,91%,75%);">hello</span> = <span class="string">"hello"</span>; |
33 | }</code></pre> \ No newline at end of file | 33 | }</code></pre> \ 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 @@ | |||
2 | 2 | ||
3 | use rustc_hash::{FxHashMap, FxHashSet}; | 3 | use rustc_hash::{FxHashMap, FxHashSet}; |
4 | 4 | ||
5 | use hir::{Mutability, Ty}; | 5 | use hir::{Mutability, Name}; |
6 | use ra_db::SourceDatabase; | 6 | use ra_db::SourceDatabase; |
7 | use ra_prof::profile; | 7 | use ra_prof::profile; |
8 | use ra_syntax::{ | 8 | use ra_syntax::{ast, AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxKind::*, TextRange, T}; |
9 | ast::{self, NameOwner}, | ||
10 | AstNode, Direction, SmolStr, SyntaxElement, SyntaxKind, | ||
11 | SyntaxKind::*, | ||
12 | SyntaxNode, TextRange, T, | ||
13 | }; | ||
14 | 9 | ||
15 | use crate::{ | 10 | use crate::{ |
16 | db::RootDatabase, | 11 | db::RootDatabase, |
@@ -43,32 +38,12 @@ fn is_control_keyword(kind: SyntaxKind) -> bool { | |||
43 | } | 38 | } |
44 | } | 39 | } |
45 | 40 | ||
46 | fn is_variable_mutable( | ||
47 | db: &RootDatabase, | ||
48 | analyzer: &hir::SourceAnalyzer, | ||
49 | pat: ast::BindPat, | ||
50 | ) -> bool { | ||
51 | if pat.is_mutable() { | ||
52 | return true; | ||
53 | } | ||
54 | |||
55 | let ty = analyzer.type_of_pat(db, &pat.into()).unwrap_or(Ty::Unknown); | ||
56 | if let Some((_, mutability)) = ty.as_reference() { | ||
57 | match mutability { | ||
58 | Mutability::Shared => false, | ||
59 | Mutability::Mut => true, | ||
60 | } | ||
61 | } else { | ||
62 | false | ||
63 | } | ||
64 | } | ||
65 | |||
66 | pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> { | 41 | pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> { |
67 | let _p = profile("highlight"); | 42 | let _p = profile("highlight"); |
68 | let parse = db.parse(file_id); | 43 | let parse = db.parse(file_id); |
69 | let root = parse.tree().syntax().clone(); | 44 | let root = parse.tree().syntax().clone(); |
70 | 45 | ||
71 | fn calc_binding_hash(file_id: FileId, text: &SmolStr, shadow_count: u32) -> u64 { | 46 | fn calc_binding_hash(file_id: FileId, name: &Name, shadow_count: u32) -> u64 { |
72 | fn hash<T: std::hash::Hash + std::fmt::Debug>(x: T) -> u64 { | 47 | fn hash<T: std::hash::Hash + std::fmt::Debug>(x: T) -> u64 { |
73 | use std::{collections::hash_map::DefaultHasher, hash::Hasher}; | 48 | use std::{collections::hash_map::DefaultHasher, hash::Hasher}; |
74 | 49 | ||
@@ -77,13 +52,13 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa | |||
77 | hasher.finish() | 52 | hasher.finish() |
78 | } | 53 | } |
79 | 54 | ||
80 | hash((file_id, text, shadow_count)) | 55 | hash((file_id, name, shadow_count)) |
81 | } | 56 | } |
82 | 57 | ||
83 | // Visited nodes to handle highlighting priorities | 58 | // Visited nodes to handle highlighting priorities |
84 | // FIXME: retain only ranges here | 59 | // FIXME: retain only ranges here |
85 | let mut highlighted: FxHashSet<SyntaxElement> = FxHashSet::default(); | 60 | let mut highlighted: FxHashSet<SyntaxElement> = FxHashSet::default(); |
86 | let mut bindings_shadow_count: FxHashMap<SmolStr, u32> = FxHashMap::default(); | 61 | let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default(); |
87 | 62 | ||
88 | let mut res = Vec::new(); | 63 | let mut res = Vec::new(); |
89 | for node in root.descendants_with_tokens() { | 64 | for node in root.descendants_with_tokens() { |
@@ -107,34 +82,29 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa | |||
107 | let name_ref = node.as_node().cloned().and_then(ast::NameRef::cast).unwrap(); | 82 | let name_ref = node.as_node().cloned().and_then(ast::NameRef::cast).unwrap(); |
108 | let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind); | 83 | let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind); |
109 | 84 | ||
110 | if let Some(Pat((_, ptr))) = &name_kind { | 85 | if let Some(Local(local)) = &name_kind { |
111 | let pat = ptr.to_node(&root); | 86 | if let Some(name) = local.name(db) { |
112 | if let Some(name) = pat.name() { | 87 | let shadow_count = bindings_shadow_count.entry(name.clone()).or_default(); |
113 | let text = name.text(); | 88 | binding_hash = Some(calc_binding_hash(file_id, &name, *shadow_count)) |
114 | let shadow_count = bindings_shadow_count.entry(text.clone()).or_default(); | ||
115 | binding_hash = Some(calc_binding_hash(file_id, &text, *shadow_count)) | ||
116 | } | 89 | } |
117 | }; | 90 | }; |
118 | 91 | ||
119 | name_kind | 92 | name_kind.map_or("text", |it| highlight_name(db, it)) |
120 | .map_or("text", |it| highlight_name(db, file_id, name_ref.syntax(), &root, it)) | ||
121 | } | 93 | } |
122 | NAME => { | 94 | NAME => { |
123 | let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap(); | 95 | let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap(); |
124 | let name_kind = classify_name(db, file_id, &name).map(|d| d.kind); | 96 | let name_kind = classify_name(db, file_id, &name).map(|d| d.kind); |
125 | 97 | ||
126 | if let Some(Pat((_, ptr))) = &name_kind { | 98 | if let Some(Local(local)) = &name_kind { |
127 | let pat = ptr.to_node(&root); | 99 | if let Some(name) = local.name(db) { |
128 | if let Some(name) = pat.name() { | 100 | let shadow_count = bindings_shadow_count.entry(name.clone()).or_default(); |
129 | let text = name.text(); | ||
130 | let shadow_count = bindings_shadow_count.entry(text.clone()).or_default(); | ||
131 | *shadow_count += 1; | 101 | *shadow_count += 1; |
132 | binding_hash = Some(calc_binding_hash(file_id, &text, *shadow_count)) | 102 | binding_hash = Some(calc_binding_hash(file_id, &name, *shadow_count)) |
133 | } | 103 | } |
134 | }; | 104 | }; |
135 | 105 | ||
136 | match name_kind { | 106 | match name_kind { |
137 | Some(name_kind) => highlight_name(db, file_id, name.syntax(), &root, name_kind), | 107 | Some(name_kind) => highlight_name(db, name_kind), |
138 | None => name.syntax().parent().map_or("function", |x| match x.kind() { | 108 | None => name.syntax().parent().map_or("function", |x| match x.kind() { |
139 | TYPE_PARAM | STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => "type", | 109 | TYPE_PARAM | STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => "type", |
140 | RECORD_FIELD_DEF => "field", | 110 | RECORD_FIELD_DEF => "field", |
@@ -237,13 +207,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo | |||
237 | buf | 207 | buf |
238 | } | 208 | } |
239 | 209 | ||
240 | fn highlight_name( | 210 | fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str { |
241 | db: &RootDatabase, | ||
242 | file_id: FileId, | ||
243 | node: &SyntaxNode, | ||
244 | root: &SyntaxNode, | ||
245 | name_kind: NameKind, | ||
246 | ) -> &'static str { | ||
247 | match name_kind { | 211 | match name_kind { |
248 | Macro(_) => "macro", | 212 | Macro(_) => "macro", |
249 | Field(_) => "field", | 213 | Field(_) => "field", |
@@ -260,14 +224,15 @@ fn highlight_name( | |||
260 | Def(hir::ModuleDef::TypeAlias(_)) => "type", | 224 | Def(hir::ModuleDef::TypeAlias(_)) => "type", |
261 | Def(hir::ModuleDef::BuiltinType(_)) => "type", | 225 | Def(hir::ModuleDef::BuiltinType(_)) => "type", |
262 | SelfType(_) => "type", | 226 | SelfType(_) => "type", |
263 | SelfParam(_) => "type", | ||
264 | GenericParam(_) => "type", | 227 | GenericParam(_) => "type", |
265 | Pat((_, ptr)) => { | 228 | Local(local) => { |
266 | let analyzer = hir::SourceAnalyzer::new(db, file_id, node, None); | 229 | if local.is_mut(db) { |
267 | if is_variable_mutable(db, &analyzer, ptr.to_node(&root)) { | ||
268 | "variable.mut" | 230 | "variable.mut" |
269 | } else { | 231 | } else { |
270 | "variable" | 232 | match local.ty(db).as_reference() { |
233 | Some((_, Mutability::Mut)) => "variable.mut", | ||
234 | _ => "variable", | ||
235 | } | ||
271 | } | 236 | } |
272 | } | 237 | } |
273 | } | 238 | } |