aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide_api/src/call_info.rs2
-rw-r--r--crates/ra_ide_api/src/change.rs255
-rw-r--r--crates/ra_ide_api/src/diagnostics.rs69
-rw-r--r--crates/ra_ide_api/src/goto_definition.rs3
-rw-r--r--crates/ra_ide_api/src/imp.rs263
-rw-r--r--crates/ra_ide_api/src/impls.rs2
-rw-r--r--crates/ra_ide_api/src/lib.rs182
-rw-r--r--crates/ra_ide_api/src/parent_module.rs15
-rw-r--r--crates/ra_ide_api/src/references.rs (renamed from crates/ra_ide_api/src/rename.rs)66
-rw-r--r--crates/ra_ide_api/src/symbol_index.rs8
-rw-r--r--crates/ra_mbe/src/mbe_expander.rs4
11 files changed, 428 insertions, 441 deletions
diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs
index 2eb388e0e..a59ab7853 100644
--- a/crates/ra_ide_api/src/call_info.rs
+++ b/crates/ra_ide_api/src/call_info.rs
@@ -20,7 +20,7 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<Cal
20 let name_ref = calling_node.name_ref()?; 20 let name_ref = calling_node.name_ref()?;
21 21
22 // Resolve the function's NameRef (NOTE: this isn't entirely accurate). 22 // Resolve the function's NameRef (NOTE: this isn't entirely accurate).
23 let file_symbols = db.index_resolve(name_ref); 23 let file_symbols = crate::symbol_index::index_resolve(db, name_ref);
24 let symbol = file_symbols 24 let symbol = file_symbols
25 .into_iter() 25 .into_iter()
26 .find(|it| it.ptr.kind() == FN_DEF)?; 26 .find(|it| it.ptr.kind() == FN_DEF)?;
diff --git a/crates/ra_ide_api/src/change.rs b/crates/ra_ide_api/src/change.rs
new file mode 100644
index 000000000..992955740
--- /dev/null
+++ b/crates/ra_ide_api/src/change.rs
@@ -0,0 +1,255 @@
1use std::{
2 fmt, time,
3 sync::Arc,
4};
5
6use rustc_hash::FxHashMap;
7use ra_db::{
8 SourceRootId, FileId, CrateGraph, SourceDatabase, SourceRoot,
9 salsa::{Database, SweepStrategy},
10};
11use ra_syntax::SourceFile;
12use relative_path::RelativePathBuf;
13use rayon::prelude::*;
14
15use crate::{
16 db::RootDatabase,
17 symbol_index::{SymbolIndex, SymbolsDatabase},
18 status::syntax_tree_stats,
19};
20
21#[derive(Default)]
22pub struct AnalysisChange {
23 new_roots: Vec<(SourceRootId, bool)>,
24 roots_changed: FxHashMap<SourceRootId, RootChange>,
25 files_changed: Vec<(FileId, Arc<String>)>,
26 libraries_added: Vec<LibraryData>,
27 crate_graph: Option<CrateGraph>,
28}
29
30impl fmt::Debug for AnalysisChange {
31 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
32 let mut d = fmt.debug_struct("AnalysisChange");
33 if !self.new_roots.is_empty() {
34 d.field("new_roots", &self.new_roots);
35 }
36 if !self.roots_changed.is_empty() {
37 d.field("roots_changed", &self.roots_changed);
38 }
39 if !self.files_changed.is_empty() {
40 d.field("files_changed", &self.files_changed.len());
41 }
42 if !self.libraries_added.is_empty() {
43 d.field("libraries_added", &self.libraries_added.len());
44 }
45 if !self.crate_graph.is_some() {
46 d.field("crate_graph", &self.crate_graph);
47 }
48 d.finish()
49 }
50}
51
52impl AnalysisChange {
53 pub fn new() -> AnalysisChange {
54 AnalysisChange::default()
55 }
56
57 pub fn add_root(&mut self, root_id: SourceRootId, is_local: bool) {
58 self.new_roots.push((root_id, is_local));
59 }
60
61 pub fn add_file(
62 &mut self,
63 root_id: SourceRootId,
64 file_id: FileId,
65 path: RelativePathBuf,
66 text: Arc<String>,
67 ) {
68 let file = AddFile {
69 file_id,
70 path,
71 text,
72 };
73 self.roots_changed
74 .entry(root_id)
75 .or_default()
76 .added
77 .push(file);
78 }
79
80 pub fn change_file(&mut self, file_id: FileId, new_text: Arc<String>) {
81 self.files_changed.push((file_id, new_text))
82 }
83
84 pub fn remove_file(&mut self, root_id: SourceRootId, file_id: FileId, path: RelativePathBuf) {
85 let file = RemoveFile { file_id, path };
86 self.roots_changed
87 .entry(root_id)
88 .or_default()
89 .removed
90 .push(file);
91 }
92
93 pub fn add_library(&mut self, data: LibraryData) {
94 self.libraries_added.push(data)
95 }
96
97 pub fn set_crate_graph(&mut self, graph: CrateGraph) {
98 self.crate_graph = Some(graph);
99 }
100}
101
102#[derive(Debug)]
103struct AddFile {
104 file_id: FileId,
105 path: RelativePathBuf,
106 text: Arc<String>,
107}
108
109#[derive(Debug)]
110struct RemoveFile {
111 file_id: FileId,
112 path: RelativePathBuf,
113}
114
115#[derive(Default)]
116struct RootChange {
117 added: Vec<AddFile>,
118 removed: Vec<RemoveFile>,
119}
120
121impl fmt::Debug for RootChange {
122 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
123 fmt.debug_struct("AnalysisChange")
124 .field("added", &self.added.len())
125 .field("removed", &self.removed.len())
126 .finish()
127 }
128}
129
130pub struct LibraryData {
131 root_id: SourceRootId,
132 root_change: RootChange,
133 symbol_index: SymbolIndex,
134}
135
136impl fmt::Debug for LibraryData {
137 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
138 f.debug_struct("LibraryData")
139 .field("root_id", &self.root_id)
140 .field("root_change", &self.root_change)
141 .field("n_symbols", &self.symbol_index.len())
142 .finish()
143 }
144}
145
146impl LibraryData {
147 pub fn prepare(
148 root_id: SourceRootId,
149 files: Vec<(FileId, RelativePathBuf, Arc<String>)>,
150 ) -> LibraryData {
151 let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, _, text)| {
152 let file = SourceFile::parse(text);
153 (*file_id, file)
154 }));
155 let mut root_change = RootChange::default();
156 root_change.added = files
157 .into_iter()
158 .map(|(file_id, path, text)| AddFile {
159 file_id,
160 path,
161 text,
162 })
163 .collect();
164 LibraryData {
165 root_id,
166 root_change,
167 symbol_index,
168 }
169 }
170}
171
172const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100);
173
174impl RootDatabase {
175 pub(crate) fn apply_change(&mut self, change: AnalysisChange) {
176 log::info!("apply_change {:?}", change);
177 if !change.new_roots.is_empty() {
178 let mut local_roots = Vec::clone(&self.local_roots());
179 for (root_id, is_local) in change.new_roots {
180 self.set_source_root(root_id, Default::default());
181 if is_local {
182 local_roots.push(root_id);
183 }
184 }
185 self.set_local_roots(Arc::new(local_roots));
186 }
187
188 for (root_id, root_change) in change.roots_changed {
189 self.apply_root_change(root_id, root_change);
190 }
191 for (file_id, text) in change.files_changed {
192 self.set_file_text(file_id, text)
193 }
194 if !change.libraries_added.is_empty() {
195 let mut libraries = Vec::clone(&self.library_roots());
196 for library in change.libraries_added {
197 libraries.push(library.root_id);
198 self.set_source_root(library.root_id, Default::default());
199 self.set_constant_library_symbols(library.root_id, Arc::new(library.symbol_index));
200 self.apply_root_change(library.root_id, library.root_change);
201 }
202 self.set_library_roots(Arc::new(libraries));
203 }
204 if let Some(crate_graph) = change.crate_graph {
205 self.set_crate_graph(Arc::new(crate_graph))
206 }
207 }
208
209 fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) {
210 let mut source_root = SourceRoot::clone(&self.source_root(root_id));
211 for add_file in root_change.added {
212 self.set_file_text(add_file.file_id, add_file.text);
213 self.set_file_relative_path(add_file.file_id, add_file.path.clone());
214 self.set_file_source_root(add_file.file_id, root_id);
215 source_root.files.insert(add_file.path, add_file.file_id);
216 }
217 for remove_file in root_change.removed {
218 self.set_file_text(remove_file.file_id, Default::default());
219 source_root.files.remove(&remove_file.path);
220 }
221 self.set_source_root(root_id, Arc::new(source_root));
222 }
223
224 pub(crate) fn maybe_collect_garbage(&mut self) {
225 if self.last_gc_check.elapsed() > GC_COOLDOWN {
226 self.last_gc_check = time::Instant::now();
227 let retained_trees = syntax_tree_stats(self).retained;
228 if retained_trees > 100 {
229 log::info!(
230 "automatic garbadge collection, {} retained trees",
231 retained_trees
232 );
233 self.collect_garbage();
234 }
235 }
236 }
237
238 pub(crate) fn collect_garbage(&mut self) {
239 self.last_gc = time::Instant::now();
240
241 let sweep = SweepStrategy::default()
242 .discard_values()
243 .sweep_all_revisions();
244
245 self.query(ra_db::ParseQuery).sweep(sweep);
246
247 self.query(hir::db::HirParseQuery).sweep(sweep);
248 self.query(hir::db::FileItemsQuery).sweep(sweep);
249 self.query(hir::db::FileItemQuery).sweep(sweep);
250
251 self.query(hir::db::LowerModuleQuery).sweep(sweep);
252 self.query(hir::db::LowerModuleSourceMapQuery).sweep(sweep);
253 self.query(hir::db::BodySyntaxMappingQuery).sweep(sweep);
254 }
255}
diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs
new file mode 100644
index 000000000..a499ac7c6
--- /dev/null
+++ b/crates/ra_ide_api/src/diagnostics.rs
@@ -0,0 +1,69 @@
1use hir::{Problem, source_binder};
2use ra_ide_api_light::Severity;
3use ra_db::SourceDatabase;
4
5use crate::{Diagnostic, FileId, FileSystemEdit, SourceChange, db::RootDatabase};
6
7pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> {
8 let syntax = db.parse(file_id);
9
10 let mut res = ra_ide_api_light::diagnostics(&syntax)
11 .into_iter()
12 .map(|d| Diagnostic {
13 range: d.range,
14 message: d.msg,
15 severity: d.severity,
16 fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)),
17 })
18 .collect::<Vec<_>>();
19 if let Some(m) = source_binder::module_from_file_id(db, file_id) {
20 for (name_node, problem) in m.problems(db) {
21 let source_root = db.file_source_root(file_id);
22 let diag = match problem {
23 Problem::UnresolvedModule { candidate } => {
24 let create_file = FileSystemEdit::CreateFile {
25 source_root,
26 path: candidate.clone(),
27 };
28 let fix = SourceChange {
29 label: "create module".to_string(),
30 source_file_edits: Vec::new(),
31 file_system_edits: vec![create_file],
32 cursor_position: None,
33 };
34 Diagnostic {
35 range: name_node.range(),
36 message: "unresolved module".to_string(),
37 severity: Severity::Error,
38 fix: Some(fix),
39 }
40 }
41 Problem::NotDirOwner { move_to, candidate } => {
42 let move_file = FileSystemEdit::MoveFile {
43 src: file_id,
44 dst_source_root: source_root,
45 dst_path: move_to.clone(),
46 };
47 let create_file = FileSystemEdit::CreateFile {
48 source_root,
49 path: move_to.join(candidate),
50 };
51 let fix = SourceChange {
52 label: "move file and create module".to_string(),
53 source_file_edits: Vec::new(),
54 file_system_edits: vec![move_file, create_file],
55 cursor_position: None,
56 };
57 Diagnostic {
58 range: name_node.range(),
59 message: "can't declare module at this location".to_string(),
60 severity: Severity::Error,
61 fix: Some(fix),
62 }
63 }
64 };
65 res.push(diag)
66 }
67 };
68 res
69}
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs
index 681f36623..69f2d2bf6 100644
--- a/crates/ra_ide_api/src/goto_definition.rs
+++ b/crates/ra_ide_api/src/goto_definition.rs
@@ -118,8 +118,7 @@ pub(crate) fn reference_definition(
118 } 118 }
119 } 119 }
120 // If that fails try the index based approach. 120 // If that fails try the index based approach.
121 let navs = db 121 let navs = crate::symbol_index::index_resolve(db, name_ref)
122 .index_resolve(name_ref)
123 .into_iter() 122 .into_iter()
124 .map(NavigationTarget::from_symbol) 123 .map(NavigationTarget::from_symbol)
125 .collect(); 124 .collect();
diff --git a/crates/ra_ide_api/src/imp.rs b/crates/ra_ide_api/src/imp.rs
deleted file mode 100644
index b139efabf..000000000
--- a/crates/ra_ide_api/src/imp.rs
+++ /dev/null
@@ -1,263 +0,0 @@
1use std::{
2 sync::Arc,
3 time,
4};
5
6use hir::{
7 self, Problem, source_binder
8};
9use ra_db::{
10 SourceDatabase, SourceRoot, SourceRootId,
11 salsa::{Database, SweepStrategy},
12};
13use ra_ide_api_light::{self, LocalEdit, Severity};
14use ra_syntax::{
15 algo::find_node_at_offset, ast::{self, NameOwner}, AstNode,
16 SourceFile,
17 TextRange,
18};
19
20use crate::{
21 AnalysisChange,
22 CrateId, db, Diagnostic, FileId, FilePosition, FileSystemEdit,
23 Query, RootChange, SourceChange, SourceFileEdit,
24 symbol_index::{FileSymbol, SymbolsDatabase},
25 status::syntax_tree_stats
26};
27
28const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100);
29
30impl db::RootDatabase {
31 pub(crate) fn apply_change(&mut self, change: AnalysisChange) {
32 log::info!("apply_change {:?}", change);
33 if !change.new_roots.is_empty() {
34 let mut local_roots = Vec::clone(&self.local_roots());
35 for (root_id, is_local) in change.new_roots {
36 self.set_source_root(root_id, Default::default());
37 if is_local {
38 local_roots.push(root_id);
39 }
40 }
41 self.set_local_roots(Arc::new(local_roots));
42 }
43
44 for (root_id, root_change) in change.roots_changed {
45 self.apply_root_change(root_id, root_change);
46 }
47 for (file_id, text) in change.files_changed {
48 self.set_file_text(file_id, text)
49 }
50 if !change.libraries_added.is_empty() {
51 let mut libraries = Vec::clone(&self.library_roots());
52 for library in change.libraries_added {
53 libraries.push(library.root_id);
54 self.set_source_root(library.root_id, Default::default());
55 self.set_constant_library_symbols(library.root_id, Arc::new(library.symbol_index));
56 self.apply_root_change(library.root_id, library.root_change);
57 }
58 self.set_library_roots(Arc::new(libraries));
59 }
60 if let Some(crate_graph) = change.crate_graph {
61 self.set_crate_graph(Arc::new(crate_graph))
62 }
63 }
64
65 fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) {
66 let mut source_root = SourceRoot::clone(&self.source_root(root_id));
67 for add_file in root_change.added {
68 self.set_file_text(add_file.file_id, add_file.text);
69 self.set_file_relative_path(add_file.file_id, add_file.path.clone());
70 self.set_file_source_root(add_file.file_id, root_id);
71 source_root.files.insert(add_file.path, add_file.file_id);
72 }
73 for remove_file in root_change.removed {
74 self.set_file_text(remove_file.file_id, Default::default());
75 source_root.files.remove(&remove_file.path);
76 }
77 self.set_source_root(root_id, Arc::new(source_root));
78 }
79
80 pub(crate) fn maybe_collect_garbage(&mut self) {
81 if self.last_gc_check.elapsed() > GC_COOLDOWN {
82 self.last_gc_check = time::Instant::now();
83 let retained_trees = syntax_tree_stats(self).retained;
84 if retained_trees > 100 {
85 log::info!(
86 "automatic garbadge collection, {} retained trees",
87 retained_trees
88 );
89 self.collect_garbage();
90 }
91 }
92 }
93
94 pub(crate) fn collect_garbage(&mut self) {
95 self.last_gc = time::Instant::now();
96
97 let sweep = SweepStrategy::default()
98 .discard_values()
99 .sweep_all_revisions();
100
101 self.query(ra_db::ParseQuery).sweep(sweep);
102
103 self.query(hir::db::HirParseQuery).sweep(sweep);
104 self.query(hir::db::FileItemsQuery).sweep(sweep);
105 self.query(hir::db::FileItemQuery).sweep(sweep);
106
107 self.query(hir::db::LowerModuleQuery).sweep(sweep);
108 self.query(hir::db::LowerModuleSourceMapQuery).sweep(sweep);
109 self.query(hir::db::BodySyntaxMappingQuery).sweep(sweep);
110 }
111}
112
113impl db::RootDatabase {
114 /// Returns `Vec` for the same reason as `parent_module`
115 pub(crate) fn crate_for(&self, file_id: FileId) -> Vec<CrateId> {
116 let module = match source_binder::module_from_file_id(self, file_id) {
117 Some(it) => it,
118 None => return Vec::new(),
119 };
120 let krate = match module.krate(self) {
121 Some(it) => it,
122 None => return Vec::new(),
123 };
124 vec![krate.crate_id()]
125 }
126
127 pub(crate) fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> {
128 let file = self.parse(position.file_id);
129 // Find the binding associated with the offset
130 let (binding, descr) = match find_binding(self, &file, position) {
131 None => return Vec::new(),
132 Some(it) => it,
133 };
134
135 let mut ret = binding
136 .name()
137 .into_iter()
138 .map(|name| (position.file_id, name.syntax().range()))
139 .collect::<Vec<_>>();
140 ret.extend(
141 descr
142 .scopes(self)
143 .find_all_refs(binding)
144 .into_iter()
145 .map(|ref_desc| (position.file_id, ref_desc.range)),
146 );
147
148 return ret;
149
150 fn find_binding<'a>(
151 db: &db::RootDatabase,
152 source_file: &'a SourceFile,
153 position: FilePosition,
154 ) -> Option<(&'a ast::BindPat, hir::Function)> {
155 let syntax = source_file.syntax();
156 if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) {
157 let descr = source_binder::function_from_child_node(
158 db,
159 position.file_id,
160 binding.syntax(),
161 )?;
162 return Some((binding, descr));
163 };
164 let name_ref = find_node_at_offset::<ast::NameRef>(syntax, position.offset)?;
165 let descr =
166 source_binder::function_from_child_node(db, position.file_id, name_ref.syntax())?;
167 let scope = descr.scopes(db);
168 let resolved = scope.resolve_local_name(name_ref)?;
169 let resolved = resolved.ptr().to_node(source_file);
170 let binding = find_node_at_offset::<ast::BindPat>(syntax, resolved.range().end())?;
171 Some((binding, descr))
172 }
173 }
174
175 pub(crate) fn diagnostics(&self, file_id: FileId) -> Vec<Diagnostic> {
176 let syntax = self.parse(file_id);
177
178 let mut res = ra_ide_api_light::diagnostics(&syntax)
179 .into_iter()
180 .map(|d| Diagnostic {
181 range: d.range,
182 message: d.msg,
183 severity: d.severity,
184 fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)),
185 })
186 .collect::<Vec<_>>();
187 if let Some(m) = source_binder::module_from_file_id(self, file_id) {
188 for (name_node, problem) in m.problems(self) {
189 let source_root = self.file_source_root(file_id);
190 let diag = match problem {
191 Problem::UnresolvedModule { candidate } => {
192 let create_file = FileSystemEdit::CreateFile {
193 source_root,
194 path: candidate.clone(),
195 };
196 let fix = SourceChange {
197 label: "create module".to_string(),
198 source_file_edits: Vec::new(),
199 file_system_edits: vec![create_file],
200 cursor_position: None,
201 };
202 Diagnostic {
203 range: name_node.range(),
204 message: "unresolved module".to_string(),
205 severity: Severity::Error,
206 fix: Some(fix),
207 }
208 }
209 Problem::NotDirOwner { move_to, candidate } => {
210 let move_file = FileSystemEdit::MoveFile {
211 src: file_id,
212 dst_source_root: source_root,
213 dst_path: move_to.clone(),
214 };
215 let create_file = FileSystemEdit::CreateFile {
216 source_root,
217 path: move_to.join(candidate),
218 };
219 let fix = SourceChange {
220 label: "move file and create module".to_string(),
221 source_file_edits: Vec::new(),
222 file_system_edits: vec![move_file, create_file],
223 cursor_position: None,
224 };
225 Diagnostic {
226 range: name_node.range(),
227 message: "can't declare module at this location".to_string(),
228 severity: Severity::Error,
229 fix: Some(fix),
230 }
231 }
232 };
233 res.push(diag)
234 }
235 };
236 res
237 }
238
239 pub(crate) fn index_resolve(&self, name_ref: &ast::NameRef) -> Vec<FileSymbol> {
240 let name = name_ref.text();
241 let mut query = Query::new(name.to_string());
242 query.exact();
243 query.limit(4);
244 crate::symbol_index::world_symbols(self, query)
245 }
246}
247
248impl SourceChange {
249 pub(crate) fn from_local_edit(file_id: FileId, edit: LocalEdit) -> SourceChange {
250 let file_edit = SourceFileEdit {
251 file_id,
252 edit: edit.edit,
253 };
254 SourceChange {
255 label: edit.label,
256 source_file_edits: vec![file_edit],
257 file_system_edits: vec![],
258 cursor_position: edit
259 .cursor_position
260 .map(|offset| FilePosition { offset, file_id }),
261 }
262 }
263}
diff --git a/crates/ra_ide_api/src/impls.rs b/crates/ra_ide_api/src/impls.rs
index 91fa41f1f..4fb054139 100644
--- a/crates/ra_ide_api/src/impls.rs
+++ b/crates/ra_ide_api/src/impls.rs
@@ -1,4 +1,4 @@
1use ra_db::{SourceDatabase}; 1use ra_db::SourceDatabase;
2use ra_syntax::{ 2use ra_syntax::{
3 AstNode, ast, 3 AstNode, ast,
4 algo::find_node_at_offset, 4 algo::find_node_at_offset,
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs
index 68d59aae1..1f43b7623 100644
--- a/crates/ra_ide_api/src/lib.rs
+++ b/crates/ra_ide_api/src/lib.rs
@@ -14,10 +14,10 @@
14#![recursion_limit = "128"] 14#![recursion_limit = "128"]
15 15
16mod db; 16mod db;
17mod imp;
18pub mod mock_analysis; 17pub mod mock_analysis;
19mod symbol_index; 18mod symbol_index;
20mod navigation_target; 19mod navigation_target;
20mod change;
21 21
22mod status; 22mod status;
23mod completion; 23mod completion;
@@ -28,14 +28,15 @@ mod hover;
28mod call_info; 28mod call_info;
29mod syntax_highlighting; 29mod syntax_highlighting;
30mod parent_module; 30mod parent_module;
31mod rename; 31mod references;
32mod impls; 32mod impls;
33mod assists; 33mod assists;
34mod diagnostics;
34 35
35#[cfg(test)] 36#[cfg(test)]
36mod marks; 37mod marks;
37 38
38use std::{fmt, sync::Arc}; 39use std::sync::Arc;
39 40
40use ra_syntax::{SourceFile, TreeArc, TextRange, TextUnit}; 41use ra_syntax::{SourceFile, TreeArc, TextRange, TextUnit};
41use ra_text_edit::TextEdit; 42use ra_text_edit::TextEdit;
@@ -43,22 +44,21 @@ use ra_db::{
43 SourceDatabase, CheckCanceled, 44 SourceDatabase, CheckCanceled,
44 salsa::{self, ParallelDatabase}, 45 salsa::{self, ParallelDatabase},
45}; 46};
46use rayon::prelude::*;
47use relative_path::RelativePathBuf; 47use relative_path::RelativePathBuf;
48use rustc_hash::FxHashMap;
49 48
50use crate::{ 49use crate::{
51 symbol_index::{FileSymbol, SymbolIndex}, 50 symbol_index::FileSymbol,
52 db::LineIndexDatabase, 51 db::LineIndexDatabase,
53}; 52};
54 53
55pub use crate::{ 54pub use crate::{
55 change::{AnalysisChange, LibraryData},
56 completion::{CompletionItem, CompletionItemKind, InsertTextFormat}, 56 completion::{CompletionItem, CompletionItemKind, InsertTextFormat},
57 runnables::{Runnable, RunnableKind}, 57 runnables::{Runnable, RunnableKind},
58 navigation_target::NavigationTarget, 58 navigation_target::NavigationTarget,
59}; 59};
60pub use ra_ide_api_light::{ 60pub use ra_ide_api_light::{
61 Fold, FoldKind, HighlightedRange, Severity, StructureNode, 61 Fold, FoldKind, HighlightedRange, Severity, StructureNode, LocalEdit,
62 LineIndex, LineCol, translate_offset_with_edit, 62 LineIndex, LineCol, translate_offset_with_edit,
63}; 63};
64pub use ra_db::{ 64pub use ra_db::{
@@ -74,115 +74,6 @@ static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
74 74
75pub type Cancelable<T> = Result<T, Canceled>; 75pub type Cancelable<T> = Result<T, Canceled>;
76 76
77#[derive(Default)]
78pub struct AnalysisChange {
79 new_roots: Vec<(SourceRootId, bool)>,
80 roots_changed: FxHashMap<SourceRootId, RootChange>,
81 files_changed: Vec<(FileId, Arc<String>)>,
82 libraries_added: Vec<LibraryData>,
83 crate_graph: Option<CrateGraph>,
84}
85
86#[derive(Default)]
87struct RootChange {
88 added: Vec<AddFile>,
89 removed: Vec<RemoveFile>,
90}
91
92#[derive(Debug)]
93struct AddFile {
94 file_id: FileId,
95 path: RelativePathBuf,
96 text: Arc<String>,
97}
98
99#[derive(Debug)]
100struct RemoveFile {
101 file_id: FileId,
102 path: RelativePathBuf,
103}
104
105impl fmt::Debug for AnalysisChange {
106 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
107 let mut d = fmt.debug_struct("AnalysisChange");
108 if !self.new_roots.is_empty() {
109 d.field("new_roots", &self.new_roots);
110 }
111 if !self.roots_changed.is_empty() {
112 d.field("roots_changed", &self.roots_changed);
113 }
114 if !self.files_changed.is_empty() {
115 d.field("files_changed", &self.files_changed.len());
116 }
117 if !self.libraries_added.is_empty() {
118 d.field("libraries_added", &self.libraries_added.len());
119 }
120 if self.crate_graph.is_none() {
121 d.field("crate_graph", &self.crate_graph);
122 }
123 d.finish()
124 }
125}
126
127impl fmt::Debug for RootChange {
128 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
129 fmt.debug_struct("AnalysisChange")
130 .field("added", &self.added.len())
131 .field("removed", &self.removed.len())
132 .finish()
133 }
134}
135
136impl AnalysisChange {
137 pub fn new() -> AnalysisChange {
138 AnalysisChange::default()
139 }
140
141 pub fn add_root(&mut self, root_id: SourceRootId, is_local: bool) {
142 self.new_roots.push((root_id, is_local));
143 }
144
145 pub fn add_file(
146 &mut self,
147 root_id: SourceRootId,
148 file_id: FileId,
149 path: RelativePathBuf,
150 text: Arc<String>,
151 ) {
152 let file = AddFile {
153 file_id,
154 path,
155 text,
156 };
157 self.roots_changed
158 .entry(root_id)
159 .or_default()
160 .added
161 .push(file);
162 }
163
164 pub fn change_file(&mut self, file_id: FileId, new_text: Arc<String>) {
165 self.files_changed.push((file_id, new_text))
166 }
167
168 pub fn remove_file(&mut self, root_id: SourceRootId, file_id: FileId, path: RelativePathBuf) {
169 let file = RemoveFile { file_id, path };
170 self.roots_changed
171 .entry(root_id)
172 .or_default()
173 .removed
174 .push(file);
175 }
176
177 pub fn add_library(&mut self, data: LibraryData) {
178 self.libraries_added.push(data)
179 }
180
181 pub fn set_crate_graph(&mut self, graph: CrateGraph) {
182 self.crate_graph = Some(graph);
183 }
184}
185
186#[derive(Debug)] 77#[derive(Debug)]
187pub struct SourceChange { 78pub struct SourceChange {
188 pub label: String, 79 pub label: String,
@@ -431,7 +322,7 @@ impl Analysis {
431 322
432 /// Finds all usages of the reference at point. 323 /// Finds all usages of the reference at point.
433 pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> { 324 pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
434 self.with_db(|db| db.find_all_refs(position)) 325 self.with_db(|db| references::find_all_refs(db, position))
435 } 326 }
436 327
437 /// Returns a short text descrbing element at position. 328 /// Returns a short text descrbing element at position.
@@ -451,7 +342,7 @@ impl Analysis {
451 342
452 /// Returns crates this file belongs too. 343 /// Returns crates this file belongs too.
453 pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> { 344 pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
454 self.with_db(|db| db.crate_for(file_id)) 345 self.with_db(|db| parent_module::crate_for(db, file_id))
455 } 346 }
456 347
457 /// Returns the root file of the given crate. 348 /// Returns the root file of the given crate.
@@ -482,7 +373,7 @@ impl Analysis {
482 373
483 /// Computes the set of diagnostics for the given file. 374 /// Computes the set of diagnostics for the given file.
484 pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> { 375 pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
485 self.with_db(|db| db.diagnostics(file_id)) 376 self.with_db(|db| diagnostics::diagnostics(db, file_id))
486 } 377 }
487 378
488 /// Computes the type of the expression at the given position. 379 /// Computes the type of the expression at the given position.
@@ -497,7 +388,7 @@ impl Analysis {
497 position: FilePosition, 388 position: FilePosition,
498 new_name: &str, 389 new_name: &str,
499 ) -> Cancelable<Option<SourceChange>> { 390 ) -> Cancelable<Option<SourceChange>> {
500 self.with_db(|db| rename::rename(db, position, new_name)) 391 self.with_db(|db| references::rename(db, position, new_name))
501 } 392 }
502 393
503 fn with_db<F: FnOnce(&db::RootDatabase) -> T + std::panic::UnwindSafe, T>( 394 fn with_db<F: FnOnce(&db::RootDatabase) -> T + std::panic::UnwindSafe, T>(
@@ -508,44 +399,19 @@ impl Analysis {
508 } 399 }
509} 400}
510 401
511pub struct LibraryData { 402impl SourceChange {
512 root_id: SourceRootId, 403 pub(crate) fn from_local_edit(file_id: FileId, edit: LocalEdit) -> SourceChange {
513 root_change: RootChange, 404 let file_edit = SourceFileEdit {
514 symbol_index: SymbolIndex, 405 file_id,
515} 406 edit: edit.edit,
516 407 };
517impl fmt::Debug for LibraryData { 408 SourceChange {
518 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 409 label: edit.label,
519 f.debug_struct("LibraryData") 410 source_file_edits: vec![file_edit],
520 .field("root_id", &self.root_id) 411 file_system_edits: vec![],
521 .field("root_change", &self.root_change) 412 cursor_position: edit
522 .field("n_symbols", &self.symbol_index.len()) 413 .cursor_position
523 .finish() 414 .map(|offset| FilePosition { offset, file_id }),
524 }
525}
526
527impl LibraryData {
528 pub fn prepare(
529 root_id: SourceRootId,
530 files: Vec<(FileId, RelativePathBuf, Arc<String>)>,
531 ) -> LibraryData {
532 let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, _, text)| {
533 let file = SourceFile::parse(text);
534 (*file_id, file)
535 }));
536 let mut root_change = RootChange::default();
537 root_change.added = files
538 .into_iter()
539 .map(|(file_id, path, text)| AddFile {
540 file_id,
541 path,
542 text,
543 })
544 .collect();
545 LibraryData {
546 root_id,
547 root_change,
548 symbol_index,
549 } 415 }
550 } 416 }
551} 417}
diff --git a/crates/ra_ide_api/src/parent_module.rs b/crates/ra_ide_api/src/parent_module.rs
index e94297fe3..603c3db6a 100644
--- a/crates/ra_ide_api/src/parent_module.rs
+++ b/crates/ra_ide_api/src/parent_module.rs
@@ -1,4 +1,4 @@
1use ra_db::FilePosition; 1use ra_db::{FilePosition, FileId, CrateId};
2 2
3use crate::{NavigationTarget, db::RootDatabase}; 3use crate::{NavigationTarget, db::RootDatabase};
4 4
@@ -13,6 +13,19 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<Na
13 vec![nav] 13 vec![nav]
14} 14}
15 15
16/// Returns `Vec` for the same reason as `parent_module`
17pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
18 let module = match hir::source_binder::module_from_file_id(db, file_id) {
19 Some(it) => it,
20 None => return Vec::new(),
21 };
22 let krate = match module.krate(db) {
23 Some(it) => it,
24 None => return Vec::new(),
25 };
26 vec![krate.crate_id()]
27}
28
16#[cfg(test)] 29#[cfg(test)]
17mod tests { 30mod tests {
18 use crate::mock_analysis::analysis_and_position; 31 use crate::mock_analysis::analysis_and_position;
diff --git a/crates/ra_ide_api/src/rename.rs b/crates/ra_ide_api/src/references.rs
index 1c9491a0a..b129f3134 100644
--- a/crates/ra_ide_api/src/rename.rs
+++ b/crates/ra_ide_api/src/references.rs
@@ -1,13 +1,10 @@
1use relative_path::RelativePathBuf; 1use relative_path::{RelativePath, RelativePathBuf};
2 2use hir::{ModuleSource, source_binder};
3use hir::{ 3use ra_db::{FileId, SourceDatabase};
4 self, ModuleSource, source_binder::module_from_declaration,
5};
6use ra_syntax::{ 4use ra_syntax::{
5 AstNode, SyntaxNode, TextRange, SourceFile,
6 ast::{self, NameOwner},
7 algo::find_node_at_offset, 7 algo::find_node_at_offset,
8 ast,
9 AstNode,
10 SyntaxNode
11}; 8};
12 9
13use crate::{ 10use crate::{
@@ -17,8 +14,51 @@ use crate::{
17 SourceChange, 14 SourceChange,
18 SourceFileEdit, 15 SourceFileEdit,
19}; 16};
20use ra_db::SourceDatabase; 17
21use relative_path::RelativePath; 18pub(crate) fn find_all_refs(db: &RootDatabase, position: FilePosition) -> Vec<(FileId, TextRange)> {
19 let file = db.parse(position.file_id);
20 // Find the binding associated with the offset
21 let (binding, descr) = match find_binding(db, &file, position) {
22 None => return Vec::new(),
23 Some(it) => it,
24 };
25
26 let mut ret = binding
27 .name()
28 .into_iter()
29 .map(|name| (position.file_id, name.syntax().range()))
30 .collect::<Vec<_>>();
31 ret.extend(
32 descr
33 .scopes(db)
34 .find_all_refs(binding)
35 .into_iter()
36 .map(|ref_desc| (position.file_id, ref_desc.range)),
37 );
38
39 return ret;
40
41 fn find_binding<'a>(
42 db: &RootDatabase,
43 source_file: &'a SourceFile,
44 position: FilePosition,
45 ) -> Option<(&'a ast::BindPat, hir::Function)> {
46 let syntax = source_file.syntax();
47 if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) {
48 let descr =
49 source_binder::function_from_child_node(db, position.file_id, binding.syntax())?;
50 return Some((binding, descr));
51 };
52 let name_ref = find_node_at_offset::<ast::NameRef>(syntax, position.offset)?;
53 let descr =
54 source_binder::function_from_child_node(db, position.file_id, name_ref.syntax())?;
55 let scope = descr.scopes(db);
56 let resolved = scope.resolve_local_name(name_ref)?;
57 let resolved = resolved.ptr().to_node(source_file);
58 let binding = find_node_at_offset::<ast::BindPat>(syntax, resolved.range().end())?;
59 Some((binding, descr))
60 }
61}
22 62
23pub(crate) fn rename( 63pub(crate) fn rename(
24 db: &RootDatabase, 64 db: &RootDatabase,
@@ -57,7 +97,8 @@ fn rename_mod(
57) -> Option<SourceChange> { 97) -> Option<SourceChange> {
58 let mut source_file_edits = Vec::new(); 98 let mut source_file_edits = Vec::new();
59 let mut file_system_edits = Vec::new(); 99 let mut file_system_edits = Vec::new();
60 if let Some(module) = module_from_declaration(db, position.file_id, &ast_module) { 100 if let Some(module) = source_binder::module_from_declaration(db, position.file_id, &ast_module)
101 {
61 let (file_id, module_source) = module.definition_source(db); 102 let (file_id, module_source) = module.definition_source(db);
62 match module_source { 103 match module_source {
63 ModuleSource::SourceFile(..) => { 104 ModuleSource::SourceFile(..) => {
@@ -108,8 +149,7 @@ fn rename_reference(
108 position: FilePosition, 149 position: FilePosition,
109 new_name: &str, 150 new_name: &str,
110) -> Option<SourceChange> { 151) -> Option<SourceChange> {
111 let edit = db 152 let edit = find_all_refs(db, position)
112 .find_all_refs(position)
113 .iter() 153 .iter()
114 .map(|(file_id, text_range)| SourceFileEdit { 154 .map(|(file_id, text_range)| SourceFileEdit {
115 file_id: *file_id, 155 file_id: *file_id,
diff --git a/crates/ra_ide_api/src/symbol_index.rs b/crates/ra_ide_api/src/symbol_index.rs
index 9f939c650..3d0b2369e 100644
--- a/crates/ra_ide_api/src/symbol_index.rs
+++ b/crates/ra_ide_api/src/symbol_index.rs
@@ -109,6 +109,14 @@ pub(crate) fn world_symbols(db: &RootDatabase, query: Query) -> Vec<FileSymbol>
109 query.search(&buf) 109 query.search(&buf)
110} 110}
111 111
112pub(crate) fn index_resolve(db: &RootDatabase, name_ref: &ast::NameRef) -> Vec<FileSymbol> {
113 let name = name_ref.text();
114 let mut query = Query::new(name.to_string());
115 query.exact();
116 query.limit(4);
117 crate::symbol_index::world_symbols(db, query)
118}
119
112#[derive(Default, Debug)] 120#[derive(Default, Debug)]
113pub(crate) struct SymbolIndex { 121pub(crate) struct SymbolIndex {
114 symbols: Vec<FileSymbol>, 122 symbols: Vec<FileSymbol>,
diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs
index 04b5a4035..fb1066eec 100644
--- a/crates/ra_mbe/src/mbe_expander.rs
+++ b/crates/ra_mbe/src/mbe_expander.rs
@@ -28,7 +28,7 @@ fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Option<tt::Subtree> {
28/// 28///
29/// The tricky bit is dealing with repetitions (`$()*`). Consider this example: 29/// The tricky bit is dealing with repetitions (`$()*`). Consider this example:
30/// 30///
31/// ```ignore 31/// ```not_rust
32/// macro_rules! foo { 32/// macro_rules! foo {
33/// ($($ i:ident $($ e:expr),*);*) => { 33/// ($($ i:ident $($ e:expr),*);*) => {
34/// $(fn $ i() { $($ e);*; })* 34/// $(fn $ i() { $($ e);*; })*
@@ -46,7 +46,7 @@ fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Option<tt::Subtree> {
46/// 46///
47/// For the above example, the bindings would store 47/// For the above example, the bindings would store
48/// 48///
49/// ```ignore 49/// ```not_rust
50/// i -> [foo, bar] 50/// i -> [foo, bar]
51/// e -> [[1, 2, 3], [4, 5, 6]] 51/// e -> [[1, 2, 3], [4, 5, 6]]
52/// ``` 52/// ```