aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/imp.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src/imp.rs')
-rw-r--r--crates/ra_ide_api/src/imp.rs76
1 files changed, 30 insertions, 46 deletions
diff --git a/crates/ra_ide_api/src/imp.rs b/crates/ra_ide_api/src/imp.rs
index ba4aa0fd5..61771ed40 100644
--- a/crates/ra_ide_api/src/imp.rs
+++ b/crates/ra_ide_api/src/imp.rs
@@ -15,7 +15,6 @@ use ra_syntax::{
15 15
16use crate::{ 16use crate::{
17 AnalysisChange, 17 AnalysisChange,
18 Cancelable,
19 CrateId, db, Diagnostic, FileId, FilePosition, FileRange, FileSystemEdit, 18 CrateId, db, Diagnostic, FileId, FilePosition, FileRange, FileSystemEdit,
20 Query, RootChange, SourceChange, SourceFileEdit, 19 Query, RootChange, SourceChange, SourceFileEdit,
21 symbol_index::{LibrarySymbolsQuery, FileSymbol}, 20 symbol_index::{LibrarySymbolsQuery, FileSymbol},
@@ -99,25 +98,22 @@ impl db::RootDatabase {
99 98
100impl db::RootDatabase { 99impl db::RootDatabase {
101 /// Returns `Vec` for the same reason as `parent_module` 100 /// Returns `Vec` for the same reason as `parent_module`
102 pub(crate) fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> { 101 pub(crate) fn crate_for(&self, file_id: FileId) -> Vec<CrateId> {
103 let module = match source_binder::module_from_file_id(self, file_id)? { 102 let module = match source_binder::module_from_file_id(self, file_id) {
104 Some(it) => it, 103 Some(it) => it,
105 None => return Ok(Vec::new()), 104 None => return Vec::new(),
106 }; 105 };
107 let krate = match module.krate(self)? { 106 let krate = match module.krate(self) {
108 Some(it) => it, 107 Some(it) => it,
109 None => return Ok(Vec::new()), 108 None => return Vec::new(),
110 }; 109 };
111 Ok(vec![krate.crate_id()]) 110 vec![krate.crate_id()]
112 } 111 }
113 pub(crate) fn find_all_refs( 112 pub(crate) fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> {
114 &self,
115 position: FilePosition,
116 ) -> Cancelable<Vec<(FileId, TextRange)>> {
117 let file = self.source_file(position.file_id); 113 let file = self.source_file(position.file_id);
118 // Find the binding associated with the offset 114 // Find the binding associated with the offset
119 let (binding, descr) = match find_binding(self, &file, position)? { 115 let (binding, descr) = match find_binding(self, &file, position) {
120 None => return Ok(Vec::new()), 116 None => return Vec::new(),
121 Some(it) => it, 117 Some(it) => it,
122 }; 118 };
123 119
@@ -128,46 +124,40 @@ impl db::RootDatabase {
128 .collect::<Vec<_>>(); 124 .collect::<Vec<_>>();
129 ret.extend( 125 ret.extend(
130 descr 126 descr
131 .scopes(self)? 127 .scopes(self)
132 .find_all_refs(binding) 128 .find_all_refs(binding)
133 .into_iter() 129 .into_iter()
134 .map(|ref_desc| (position.file_id, ref_desc.range)), 130 .map(|ref_desc| (position.file_id, ref_desc.range)),
135 ); 131 );
136 132
137 return Ok(ret); 133 return ret;
138 134
139 fn find_binding<'a>( 135 fn find_binding<'a>(
140 db: &db::RootDatabase, 136 db: &db::RootDatabase,
141 source_file: &'a SourceFile, 137 source_file: &'a SourceFile,
142 position: FilePosition, 138 position: FilePosition,
143 ) -> Cancelable<Option<(&'a ast::BindPat, hir::Function)>> { 139 ) -> Option<(&'a ast::BindPat, hir::Function)> {
144 let syntax = source_file.syntax(); 140 let syntax = source_file.syntax();
145 if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) { 141 if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) {
146 let descr = ctry!(source_binder::function_from_child_node( 142 let descr = source_binder::function_from_child_node(
147 db, 143 db,
148 position.file_id, 144 position.file_id,
149 binding.syntax(), 145 binding.syntax(),
150 )?); 146 )?;
151 return Ok(Some((binding, descr))); 147 return Some((binding, descr));
152 }; 148 };
153 let name_ref = ctry!(find_node_at_offset::<ast::NameRef>(syntax, position.offset)); 149 let name_ref = find_node_at_offset::<ast::NameRef>(syntax, position.offset)?;
154 let descr = ctry!(source_binder::function_from_child_node( 150 let descr =
155 db, 151 source_binder::function_from_child_node(db, position.file_id, name_ref.syntax())?;
156 position.file_id, 152 let scope = descr.scopes(db);
157 name_ref.syntax(), 153 let resolved = scope.resolve_local_name(name_ref)?;
158 )?);
159 let scope = descr.scopes(db)?;
160 let resolved = ctry!(scope.resolve_local_name(name_ref));
161 let resolved = resolved.ptr().resolve(source_file); 154 let resolved = resolved.ptr().resolve(source_file);
162 let binding = ctry!(find_node_at_offset::<ast::BindPat>( 155 let binding = find_node_at_offset::<ast::BindPat>(syntax, resolved.range().end())?;
163 syntax, 156 Some((binding, descr))
164 resolved.range().end()
165 ));
166 Ok(Some((binding, descr)))
167 } 157 }
168 } 158 }
169 159
170 pub(crate) fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> { 160 pub(crate) fn diagnostics(&self, file_id: FileId) -> Vec<Diagnostic> {
171 let syntax = self.source_file(file_id); 161 let syntax = self.source_file(file_id);
172 162
173 let mut res = ra_ide_api_light::diagnostics(&syntax) 163 let mut res = ra_ide_api_light::diagnostics(&syntax)
@@ -179,8 +169,8 @@ impl db::RootDatabase {
179 fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)), 169 fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)),
180 }) 170 })
181 .collect::<Vec<_>>(); 171 .collect::<Vec<_>>();
182 if let Some(m) = source_binder::module_from_file_id(self, file_id)? { 172 if let Some(m) = source_binder::module_from_file_id(self, file_id) {
183 for (name_node, problem) in m.problems(self)? { 173 for (name_node, problem) in m.problems(self) {
184 let source_root = self.file_source_root(file_id); 174 let source_root = self.file_source_root(file_id);
185 let diag = match problem { 175 let diag = match problem {
186 Problem::UnresolvedModule { candidate } => { 176 Problem::UnresolvedModule { candidate } => {
@@ -228,7 +218,7 @@ impl db::RootDatabase {
228 res.push(diag) 218 res.push(diag)
229 } 219 }
230 }; 220 };
231 Ok(res) 221 res
232 } 222 }
233 223
234 pub(crate) fn assists(&self, frange: FileRange) -> Vec<SourceChange> { 224 pub(crate) fn assists(&self, frange: FileRange) -> Vec<SourceChange> {
@@ -239,13 +229,8 @@ impl db::RootDatabase {
239 .collect() 229 .collect()
240 } 230 }
241 231
242 pub(crate) fn rename( 232 pub(crate) fn rename(&self, position: FilePosition, new_name: &str) -> Vec<SourceFileEdit> {
243 &self, 233 self.find_all_refs(position)
244 position: FilePosition,
245 new_name: &str,
246 ) -> Cancelable<Vec<SourceFileEdit>> {
247 let res = self
248 .find_all_refs(position)?
249 .iter() 234 .iter()
250 .map(|(file_id, text_range)| SourceFileEdit { 235 .map(|(file_id, text_range)| SourceFileEdit {
251 file_id: *file_id, 236 file_id: *file_id,
@@ -255,10 +240,9 @@ impl db::RootDatabase {
255 builder.finish() 240 builder.finish()
256 }, 241 },
257 }) 242 })
258 .collect::<Vec<_>>(); 243 .collect::<Vec<_>>()
259 Ok(res)
260 } 244 }
261 pub(crate) fn index_resolve(&self, name_ref: &ast::NameRef) -> Cancelable<Vec<FileSymbol>> { 245 pub(crate) fn index_resolve(&self, name_ref: &ast::NameRef) -> Vec<FileSymbol> {
262 let name = name_ref.text(); 246 let name = name_ref.text();
263 let mut query = Query::new(name.to_string()); 247 let mut query = Query::new(name.to_string());
264 query.exact(); 248 query.exact();