aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src/lib.rs')
-rw-r--r--crates/ra_ide_api/src/lib.rs182
1 files changed, 24 insertions, 158 deletions
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}