aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src
diff options
context:
space:
mode:
authorEkaterina Babshukova <[email protected]>2019-10-14 12:59:02 +0100
committerEkaterina Babshukova <[email protected]>2019-10-22 21:47:31 +0100
commit88ff88d3189de9dd9b0d88bdda3da769254c2b8e (patch)
tree080616bb7e00e028e8d561c828e48eb51956b635 /crates/ra_ide_api/src
parent19fbf2c16b5c1f39e23c720a2655cfdb49c25135 (diff)
use Lazy, some fixes
Diffstat (limited to 'crates/ra_ide_api/src')
-rw-r--r--crates/ra_ide_api/src/goto_definition.rs2
-rw-r--r--crates/ra_ide_api/src/hover.rs3
-rw-r--r--crates/ra_ide_api/src/references.rs13
-rw-r--r--crates/ra_ide_api/src/references/classify.rs34
-rw-r--r--crates/ra_ide_api/src/references/name_definition.rs20
-rw-r--r--crates/ra_ide_api/src/references/rename.rs2
-rw-r--r--crates/ra_ide_api/src/references/search_scope.rs16
-rw-r--r--crates/ra_ide_api/src/syntax_highlighting.rs2
8 files changed, 49 insertions, 43 deletions
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs
index 582617286..d14908b25 100644
--- a/crates/ra_ide_api/src/goto_definition.rs
+++ b/crates/ra_ide_api/src/goto_definition.rs
@@ -54,7 +54,7 @@ pub(crate) fn reference_definition(
54) -> ReferenceResult { 54) -> ReferenceResult {
55 use self::ReferenceResult::*; 55 use self::ReferenceResult::*;
56 56
57 let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.item); 57 let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind);
58 match name_kind { 58 match name_kind {
59 Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)), 59 Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)),
60 Some(Field(field)) => return Exact(NavigationTarget::from_field(db, field)), 60 Some(Field(field)) => return Exact(NavigationTarget::from_field(db, field)),
diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs
index 318708df3..ba328efa1 100644
--- a/crates/ra_ide_api/src/hover.rs
+++ b/crates/ra_ide_api/src/hover.rs
@@ -100,8 +100,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
100 let mut range = None; 100 let mut range = None;
101 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset) { 101 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset) {
102 let mut no_fallback = false; 102 let mut no_fallback = false;
103 let name_kind = 103 let name_kind = classify_name_ref(db, position.file_id, &name_ref).map(|d| d.kind);
104 classify_name_ref(db, position.file_id, &name_ref).and_then(|d| Some(d.item));
105 match name_kind { 104 match name_kind {
106 Some(Macro(it)) => { 105 Some(Macro(it)) => {
107 let src = it.source(db); 106 let src = it.source(db);
diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs
index 3761fd6e7..3d282d48a 100644
--- a/crates/ra_ide_api/src/references.rs
+++ b/crates/ra_ide_api/src/references.rs
@@ -5,6 +5,7 @@ mod name_definition;
5mod rename; 5mod rename;
6mod search_scope; 6mod search_scope;
7 7
8use once_cell::unsync::Lazy;
8use ra_db::{SourceDatabase, SourceDatabaseExt}; 9use ra_db::{SourceDatabase, SourceDatabaseExt};
9use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, SyntaxNode, TextUnit}; 10use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, SyntaxNode, TextUnit};
10 11
@@ -61,7 +62,7 @@ pub(crate) fn find_all_refs(
61 let syntax = parse.tree().syntax().clone(); 62 let syntax = parse.tree().syntax().clone();
62 let RangeInfo { range, info: (name, def) } = find_name(db, &syntax, position)?; 63 let RangeInfo { range, info: (name, def) } = find_name(db, &syntax, position)?;
63 64
64 let declaration = match def.item { 65 let declaration = match def.kind {
65 NameKind::Macro(mac) => NavigationTarget::from_macro_def(db, mac), 66 NameKind::Macro(mac) => NavigationTarget::from_macro_def(db, mac),
66 NameKind::Field(field) => NavigationTarget::from_field(db, field), 67 NameKind::Field(field) => NavigationTarget::from_field(db, field),
67 NameKind::AssocItem(assoc) => NavigationTarget::from_assoc_item(db, assoc), 68 NameKind::AssocItem(assoc) => NavigationTarget::from_assoc_item(db, assoc),
@@ -98,7 +99,7 @@ fn find_name<'a>(
98 99
99fn process_definition(db: &RootDatabase, def: NameDefinition, name: String) -> Vec<FileRange> { 100fn process_definition(db: &RootDatabase, def: NameDefinition, name: String) -> Vec<FileRange> {
100 let pat = name.as_str(); 101 let pat = name.as_str();
101 let scope = def.scope(db).scope; 102 let scope = def.scope(db).files;
102 let mut refs = vec![]; 103 let mut refs = vec![];
103 104
104 let is_match = |file_id: FileId, name_ref: &ast::NameRef| -> bool { 105 let is_match = |file_id: FileId, name_ref: &ast::NameRef| -> bool {
@@ -112,12 +113,14 @@ fn process_definition(db: &RootDatabase, def: NameDefinition, name: String) -> V
112 113
113 for (file_id, text_range) in scope { 114 for (file_id, text_range) in scope {
114 let text = db.file_text(file_id); 115 let text = db.file_text(file_id);
115 let parse = SourceFile::parse(&text); 116 let parse = Lazy::new(|| SourceFile::parse(&text));
116 let syntax = parse.tree().syntax().clone();
117 117
118 for (idx, _) in text.match_indices(pat) { 118 for (idx, _) in text.match_indices(pat) {
119 let offset = TextUnit::from_usize(idx); 119 let offset = TextUnit::from_usize(idx);
120 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(&syntax, offset) { 120
121 if let Some(name_ref) =
122 find_node_at_offset::<ast::NameRef>(parse.tree().syntax(), offset)
123 {
121 let range = name_ref.syntax().text_range(); 124 let range = name_ref.syntax().text_range();
122 125
123 if let Some(text_range) = text_range { 126 if let Some(text_range) = text_range {
diff --git a/crates/ra_ide_api/src/references/classify.rs b/crates/ra_ide_api/src/references/classify.rs
index 5cb194c0e..93e079ccc 100644
--- a/crates/ra_ide_api/src/references/classify.rs
+++ b/crates/ra_ide_api/src/references/classify.rs
@@ -1,3 +1,5 @@
1//! FIXME: write short doc here
2
1use hir::{Either, FromSource, Module, ModuleSource, Path, PathResolution, Source, SourceAnalyzer}; 3use hir::{Either, FromSource, Module, ModuleSource, Path, PathResolution, Source, SourceAnalyzer};
2use ra_db::FileId; 4use ra_db::FileId;
3use ra_syntax::{ast, match_ast, AstNode, AstPtr}; 5use ra_syntax::{ast, match_ast, AstNode, AstPtr};
@@ -102,8 +104,9 @@ pub(crate) fn classify_name_ref(
102 let analyzer = SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); 104 let analyzer = SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
103 105
104 if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { 106 if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) {
105 let func = analyzer.resolve_method_call(&method_call)?; 107 if let Some(func) = analyzer.resolve_method_call(&method_call) {
106 return Some(from_assoc_item(db, func.into())); 108 return Some(from_assoc_item(db, func.into()));
109 }
107 } 110 }
108 111
109 if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { 112 if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) {
@@ -128,15 +131,10 @@ pub(crate) fn classify_name_ref(
128 let container = Module::from_definition(db, Source { file_id, ast })?; 131 let container = Module::from_definition(db, Source { file_id, ast })?;
129 let visibility = None; 132 let visibility = None;
130 133
131 if let Some(macro_call) = 134 if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {
132 parent.parent().and_then(|node| node.parent()).and_then(ast::MacroCall::cast)
133 {
134 if let Some(macro_def) = analyzer.resolve_macro_call(db, &macro_call) { 135 if let Some(macro_def) = analyzer.resolve_macro_call(db, &macro_call) {
135 return Some(NameDefinition { 136 let kind = NameKind::Macro(macro_def);
136 item: NameKind::Macro(macro_def), 137 return Some(NameDefinition { kind, container, visibility });
137 container,
138 visibility,
139 });
140 } 138 }
141 } 139 }
142 140
@@ -147,23 +145,23 @@ pub(crate) fn classify_name_ref(
147 AssocItem(item) => Some(from_assoc_item(db, item)), 145 AssocItem(item) => Some(from_assoc_item(db, item)),
148 LocalBinding(Either::A(pat)) => from_pat(db, file_id, pat), 146 LocalBinding(Either::A(pat)) => from_pat(db, file_id, pat),
149 LocalBinding(Either::B(par)) => { 147 LocalBinding(Either::B(par)) => {
150 let item = NameKind::SelfParam(par); 148 let kind = NameKind::SelfParam(par);
151 Some(NameDefinition { item, container, visibility }) 149 Some(NameDefinition { kind, container, visibility })
152 } 150 }
153 GenericParam(par) => { 151 GenericParam(par) => {
154 // FIXME: get generic param def 152 // FIXME: get generic param def
155 let item = NameKind::GenericParam(par); 153 let kind = NameKind::GenericParam(par);
156 Some(NameDefinition { item, container, visibility }) 154 Some(NameDefinition { kind, container, visibility })
157 } 155 }
158 Macro(def) => { 156 Macro(def) => {
159 let item = NameKind::Macro(def); 157 let kind = NameKind::Macro(def);
160 Some(NameDefinition { item, container, visibility }) 158 Some(NameDefinition { kind, container, visibility })
161 } 159 }
162 SelfType(impl_block) => { 160 SelfType(impl_block) => {
163 let ty = impl_block.target_ty(db); 161 let ty = impl_block.target_ty(db);
164 let item = NameKind::SelfType(ty); 162 let kind = NameKind::SelfType(ty);
165 let container = impl_block.module(); 163 let container = impl_block.module();
166 Some(NameDefinition { item, container, visibility }) 164 Some(NameDefinition { kind, container, visibility })
167 } 165 }
168 } 166 }
169} 167}
diff --git a/crates/ra_ide_api/src/references/name_definition.rs b/crates/ra_ide_api/src/references/name_definition.rs
index 58baf3686..723d97237 100644
--- a/crates/ra_ide_api/src/references/name_definition.rs
+++ b/crates/ra_ide_api/src/references/name_definition.rs
@@ -1,3 +1,5 @@
1//! FIXME: write short doc here
2
1use hir::{ 3use hir::{
2 db::AstDatabase, Adt, AssocItem, DefWithBody, FromSource, HasSource, HirFileId, MacroDef, 4 db::AstDatabase, Adt, AssocItem, DefWithBody, FromSource, HasSource, HirFileId, MacroDef,
3 Module, ModuleDef, StructField, Ty, VariantDef, 5 Module, ModuleDef, StructField, Ty, VariantDef,
@@ -22,7 +24,7 @@ pub enum NameKind {
22pub(crate) struct NameDefinition { 24pub(crate) struct NameDefinition {
23 pub visibility: Option<ast::Visibility>, 25 pub visibility: Option<ast::Visibility>,
24 pub container: Module, 26 pub container: Module,
25 pub item: NameKind, 27 pub kind: NameKind,
26} 28}
27 29
28pub(super) fn from_pat( 30pub(super) fn from_pat(
@@ -50,9 +52,9 @@ pub(super) fn from_pat(
50 } 52 }
51 } 53 }
52 })?; 54 })?;
53 let item = NameKind::Pat((def, pat)); 55 let kind = NameKind::Pat((def, pat));
54 let container = def.module(db); 56 let container = def.module(db);
55 Some(NameDefinition { item, container, visibility: None }) 57 Some(NameDefinition { kind, container, visibility: None })
56} 58}
57 59
58pub(super) fn from_assoc_item(db: &RootDatabase, item: AssocItem) -> NameDefinition { 60pub(super) fn from_assoc_item(db: &RootDatabase, item: AssocItem) -> NameDefinition {
@@ -62,19 +64,19 @@ pub(super) fn from_assoc_item(db: &RootDatabase, item: AssocItem) -> NameDefinit
62 AssocItem::Const(c) => c.source(db).ast.visibility(), 64 AssocItem::Const(c) => c.source(db).ast.visibility(),
63 AssocItem::TypeAlias(a) => a.source(db).ast.visibility(), 65 AssocItem::TypeAlias(a) => a.source(db).ast.visibility(),
64 }; 66 };
65 let item = NameKind::AssocItem(item); 67 let kind = NameKind::AssocItem(item);
66 NameDefinition { item, container, visibility } 68 NameDefinition { kind, container, visibility }
67} 69}
68 70
69pub(super) fn from_struct_field(db: &RootDatabase, field: StructField) -> NameDefinition { 71pub(super) fn from_struct_field(db: &RootDatabase, field: StructField) -> NameDefinition {
70 let item = NameKind::Field(field); 72 let kind = NameKind::Field(field);
71 let parent = field.parent_def(db); 73 let parent = field.parent_def(db);
72 let container = parent.module(db); 74 let container = parent.module(db);
73 let visibility = match parent { 75 let visibility = match parent {
74 VariantDef::Struct(s) => s.source(db).ast.visibility(), 76 VariantDef::Struct(s) => s.source(db).ast.visibility(),
75 VariantDef::EnumVariant(e) => e.source(db).ast.parent_enum().visibility(), 77 VariantDef::EnumVariant(e) => e.source(db).ast.parent_enum().visibility(),
76 }; 78 };
77 NameDefinition { item, container, visibility } 79 NameDefinition { kind, container, visibility }
78} 80}
79 81
80pub(super) fn from_module_def( 82pub(super) fn from_module_def(
@@ -82,7 +84,7 @@ pub(super) fn from_module_def(
82 def: ModuleDef, 84 def: ModuleDef,
83 module: Option<Module>, 85 module: Option<Module>,
84) -> NameDefinition { 86) -> NameDefinition {
85 let item = NameKind::Def(def); 87 let kind = NameKind::Def(def);
86 let (container, visibility) = match def { 88 let (container, visibility) = match def {
87 ModuleDef::Module(it) => { 89 ModuleDef::Module(it) => {
88 let container = it.parent(db).or_else(|| Some(it)).unwrap(); 90 let container = it.parent(db).or_else(|| Some(it)).unwrap();
@@ -104,5 +106,5 @@ pub(super) fn from_module_def(
104 ModuleDef::Adt(Adt::Enum(it)) => (it.module(db), it.source(db).ast.visibility()), 106 ModuleDef::Adt(Adt::Enum(it)) => (it.module(db), it.source(db).ast.visibility()),
105 ModuleDef::BuiltinType(..) => (module.unwrap(), None), 107 ModuleDef::BuiltinType(..) => (module.unwrap(), None),
106 }; 108 };
107 NameDefinition { item, container, visibility } 109 NameDefinition { kind, container, visibility }
108} 110}
diff --git a/crates/ra_ide_api/src/references/rename.rs b/crates/ra_ide_api/src/references/rename.rs
index 7e564a40e..c91dada46 100644
--- a/crates/ra_ide_api/src/references/rename.rs
+++ b/crates/ra_ide_api/src/references/rename.rs
@@ -1,3 +1,5 @@
1//! FIXME: write short doc here
2
1use hir::ModuleSource; 3use hir::ModuleSource;
2use ra_db::SourceDatabase; 4use ra_db::SourceDatabase;
3use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SyntaxNode}; 5use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SyntaxNode};
diff --git a/crates/ra_ide_api/src/references/search_scope.rs b/crates/ra_ide_api/src/references/search_scope.rs
index 346815d31..aae9db13b 100644
--- a/crates/ra_ide_api/src/references/search_scope.rs
+++ b/crates/ra_ide_api/src/references/search_scope.rs
@@ -1,3 +1,5 @@
1//! FIXME: write short doc here
2
1use hir::{DefWithBody, HasSource, ModuleSource}; 3use hir::{DefWithBody, HasSource, ModuleSource};
2use ra_db::{FileId, SourceDatabase}; 4use ra_db::{FileId, SourceDatabase};
3use ra_syntax::{AstNode, TextRange}; 5use ra_syntax::{AstNode, TextRange};
@@ -7,21 +9,21 @@ use crate::db::RootDatabase;
7use super::{NameDefinition, NameKind}; 9use super::{NameDefinition, NameKind};
8 10
9pub(crate) struct SearchScope { 11pub(crate) struct SearchScope {
10 pub scope: Vec<(FileId, Option<TextRange>)>, 12 pub files: Vec<(FileId, Option<TextRange>)>,
11} 13}
12 14
13impl NameDefinition { 15impl NameDefinition {
14 pub fn scope(&self, db: &RootDatabase) -> SearchScope { 16 pub(crate) fn scope(&self, db: &RootDatabase) -> SearchScope {
15 let module_src = self.container.definition_source(db); 17 let module_src = self.container.definition_source(db);
16 let file_id = module_src.file_id.original_file(db); 18 let file_id = module_src.file_id.original_file(db);
17 19
18 if let NameKind::Pat((def, _)) = self.item { 20 if let NameKind::Pat((def, _)) = self.kind {
19 let range = match def { 21 let range = match def {
20 DefWithBody::Function(f) => f.source(db).ast.syntax().text_range(), 22 DefWithBody::Function(f) => f.source(db).ast.syntax().text_range(),
21 DefWithBody::Const(c) => c.source(db).ast.syntax().text_range(), 23 DefWithBody::Const(c) => c.source(db).ast.syntax().text_range(),
22 DefWithBody::Static(s) => s.source(db).ast.syntax().text_range(), 24 DefWithBody::Static(s) => s.source(db).ast.syntax().text_range(),
23 }; 25 };
24 return SearchScope { scope: vec![(file_id, Some(range))] }; 26 return SearchScope { files: vec![(file_id, Some(range))] };
25 } 27 }
26 28
27 if let Some(ref vis) = self.visibility { 29 if let Some(ref vis) = self.visibility {
@@ -30,7 +32,7 @@ impl NameDefinition {
30 let mut files = source_root.walk().map(|id| (id.into(), None)).collect::<Vec<_>>(); 32 let mut files = source_root.walk().map(|id| (id.into(), None)).collect::<Vec<_>>();
31 33
32 if vis.syntax().to_string().as_str() == "pub(crate)" { 34 if vis.syntax().to_string().as_str() == "pub(crate)" {
33 return SearchScope { scope: files }; 35 return SearchScope { files };
34 } 36 }
35 if vis.syntax().to_string().as_str() == "pub" { 37 if vis.syntax().to_string().as_str() == "pub" {
36 let krate = self.container.krate(db).unwrap(); 38 let krate = self.container.krate(db).unwrap();
@@ -47,7 +49,7 @@ impl NameDefinition {
47 } 49 }
48 } 50 }
49 51
50 return SearchScope { scope: files }; 52 return SearchScope { files };
51 } 53 }
52 // FIXME: "pub(super)", "pub(in path)" 54 // FIXME: "pub(super)", "pub(in path)"
53 } 55 }
@@ -56,6 +58,6 @@ impl NameDefinition {
56 ModuleSource::Module(m) => Some(m.syntax().text_range()), 58 ModuleSource::Module(m) => Some(m.syntax().text_range()),
57 ModuleSource::SourceFile(_) => None, 59 ModuleSource::SourceFile(_) => None,
58 }; 60 };
59 SearchScope { scope: vec![(file_id, range)] } 61 SearchScope { files: vec![(file_id, range)] }
60 } 62 }
61} 63}
diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs
index 9254327f6..33f3caceb 100644
--- a/crates/ra_ide_api/src/syntax_highlighting.rs
+++ b/crates/ra_ide_api/src/syntax_highlighting.rs
@@ -101,7 +101,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
101 continue; 101 continue;
102 } 102 }
103 if let Some(name_ref) = node.as_node().cloned().and_then(ast::NameRef::cast) { 103 if let Some(name_ref) = node.as_node().cloned().and_then(ast::NameRef::cast) {
104 let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.item); 104 let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind);
105 match name_kind { 105 match name_kind {
106 Some(Macro(_)) => "macro", 106 Some(Macro(_)) => "macro",
107 Some(Field(_)) => "field", 107 Some(Field(_)) => "field",