diff options
Diffstat (limited to 'crates/ra_analysis/src/lib.rs')
-rw-r--r-- | crates/ra_analysis/src/lib.rs | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs new file mode 100644 index 000000000..4da55ab26 --- /dev/null +++ b/crates/ra_analysis/src/lib.rs | |||
@@ -0,0 +1,240 @@ | |||
1 | extern crate parking_lot; | ||
2 | #[macro_use] | ||
3 | extern crate log; | ||
4 | extern crate once_cell; | ||
5 | extern crate ra_syntax; | ||
6 | extern crate ra_editor; | ||
7 | extern crate fst; | ||
8 | extern crate rayon; | ||
9 | extern crate relative_path; | ||
10 | #[macro_use] | ||
11 | extern crate crossbeam_channel; | ||
12 | extern crate im; | ||
13 | extern crate salsa; | ||
14 | |||
15 | mod symbol_index; | ||
16 | mod module_map; | ||
17 | mod imp; | ||
18 | mod job; | ||
19 | mod roots; | ||
20 | mod db; | ||
21 | mod queries; | ||
22 | mod descriptors; | ||
23 | |||
24 | use std::{ | ||
25 | sync::Arc, | ||
26 | collections::HashMap, | ||
27 | fmt::Debug, | ||
28 | }; | ||
29 | |||
30 | use relative_path::{RelativePath, RelativePathBuf}; | ||
31 | use ra_syntax::{File, TextRange, TextUnit, AtomEdit}; | ||
32 | use imp::{AnalysisImpl, AnalysisHostImpl, FileResolverImp}; | ||
33 | |||
34 | pub use ra_editor::{ | ||
35 | StructureNode, LineIndex, FileSymbol, | ||
36 | Runnable, RunnableKind, HighlightedRange, CompletionItem, | ||
37 | }; | ||
38 | pub use job::{JobToken, JobHandle}; | ||
39 | |||
40 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
41 | pub struct FileId(pub u32); | ||
42 | |||
43 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
44 | pub struct CrateId(pub u32); | ||
45 | |||
46 | #[derive(Debug, Clone, Default)] | ||
47 | pub struct CrateGraph { | ||
48 | pub crate_roots: HashMap<CrateId, FileId>, | ||
49 | } | ||
50 | |||
51 | pub trait FileResolver: Debug + Send + Sync + 'static { | ||
52 | fn file_stem(&self, file_id: FileId) -> String; | ||
53 | fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId>; | ||
54 | } | ||
55 | |||
56 | #[derive(Debug)] | ||
57 | pub struct AnalysisHost { | ||
58 | imp: AnalysisHostImpl | ||
59 | } | ||
60 | |||
61 | impl AnalysisHost { | ||
62 | pub fn new() -> AnalysisHost { | ||
63 | AnalysisHost { imp: AnalysisHostImpl::new() } | ||
64 | } | ||
65 | pub fn analysis(&self) -> Analysis { | ||
66 | Analysis { imp: self.imp.analysis() } | ||
67 | } | ||
68 | pub fn change_file(&mut self, file_id: FileId, text: Option<String>) { | ||
69 | self.change_files(::std::iter::once((file_id, text))); | ||
70 | } | ||
71 | pub fn change_files(&mut self, mut changes: impl Iterator<Item=(FileId, Option<String>)>) { | ||
72 | self.imp.change_files(&mut changes) | ||
73 | } | ||
74 | pub fn set_file_resolver(&mut self, resolver: Arc<FileResolver>) { | ||
75 | self.imp.set_file_resolver(FileResolverImp::new(resolver)); | ||
76 | } | ||
77 | pub fn set_crate_graph(&mut self, graph: CrateGraph) { | ||
78 | self.imp.set_crate_graph(graph) | ||
79 | } | ||
80 | pub fn add_library(&mut self, data: LibraryData) { | ||
81 | self.imp.add_library(data.root) | ||
82 | } | ||
83 | } | ||
84 | |||
85 | #[derive(Debug)] | ||
86 | pub struct SourceChange { | ||
87 | pub label: String, | ||
88 | pub source_file_edits: Vec<SourceFileEdit>, | ||
89 | pub file_system_edits: Vec<FileSystemEdit>, | ||
90 | pub cursor_position: Option<Position>, | ||
91 | } | ||
92 | |||
93 | #[derive(Debug)] | ||
94 | pub struct Position { | ||
95 | pub file_id: FileId, | ||
96 | pub offset: TextUnit, | ||
97 | } | ||
98 | |||
99 | #[derive(Debug)] | ||
100 | pub struct SourceFileEdit { | ||
101 | pub file_id: FileId, | ||
102 | pub edits: Vec<AtomEdit>, | ||
103 | } | ||
104 | |||
105 | #[derive(Debug)] | ||
106 | pub enum FileSystemEdit { | ||
107 | CreateFile { | ||
108 | anchor: FileId, | ||
109 | path: RelativePathBuf, | ||
110 | }, | ||
111 | MoveFile { | ||
112 | file: FileId, | ||
113 | path: RelativePathBuf, | ||
114 | } | ||
115 | } | ||
116 | |||
117 | #[derive(Debug)] | ||
118 | pub struct Diagnostic { | ||
119 | pub message: String, | ||
120 | pub range: TextRange, | ||
121 | pub fix: Option<SourceChange>, | ||
122 | } | ||
123 | |||
124 | #[derive(Debug)] | ||
125 | pub struct Query { | ||
126 | query: String, | ||
127 | lowercased: String, | ||
128 | only_types: bool, | ||
129 | libs: bool, | ||
130 | exact: bool, | ||
131 | limit: usize, | ||
132 | } | ||
133 | |||
134 | impl Query { | ||
135 | pub fn new(query: String) -> Query { | ||
136 | let lowercased = query.to_lowercase(); | ||
137 | Query { | ||
138 | query, | ||
139 | lowercased, | ||
140 | only_types: false, | ||
141 | libs: false, | ||
142 | exact: false, | ||
143 | limit: usize::max_value() | ||
144 | } | ||
145 | } | ||
146 | pub fn only_types(&mut self) { | ||
147 | self.only_types = true; | ||
148 | } | ||
149 | pub fn libs(&mut self) { | ||
150 | self.libs = true; | ||
151 | } | ||
152 | pub fn exact(&mut self) { | ||
153 | self.exact = true; | ||
154 | } | ||
155 | pub fn limit(&mut self, limit: usize) { | ||
156 | self.limit = limit | ||
157 | } | ||
158 | } | ||
159 | |||
160 | #[derive(Clone, Debug)] | ||
161 | pub struct Analysis { | ||
162 | imp: AnalysisImpl | ||
163 | } | ||
164 | |||
165 | impl Analysis { | ||
166 | pub fn file_syntax(&self, file_id: FileId) -> File { | ||
167 | self.imp.file_syntax(file_id).clone() | ||
168 | } | ||
169 | pub fn file_line_index(&self, file_id: FileId) -> Arc<LineIndex> { | ||
170 | self.imp.file_line_index(file_id) | ||
171 | } | ||
172 | pub fn extend_selection(&self, file: &File, range: TextRange) -> TextRange { | ||
173 | ra_editor::extend_selection(file, range).unwrap_or(range) | ||
174 | } | ||
175 | pub fn matching_brace(&self, file: &File, offset: TextUnit) -> Option<TextUnit> { | ||
176 | ra_editor::matching_brace(file, offset) | ||
177 | } | ||
178 | pub fn syntax_tree(&self, file_id: FileId) -> String { | ||
179 | let file = self.imp.file_syntax(file_id); | ||
180 | ra_editor::syntax_tree(&file) | ||
181 | } | ||
182 | pub fn join_lines(&self, file_id: FileId, range: TextRange) -> SourceChange { | ||
183 | let file = self.imp.file_syntax(file_id); | ||
184 | SourceChange::from_local_edit(file_id, "join lines", ra_editor::join_lines(&file, range)) | ||
185 | } | ||
186 | pub fn on_eq_typed(&self, file_id: FileId, offset: TextUnit) -> Option<SourceChange> { | ||
187 | let file = self.imp.file_syntax(file_id); | ||
188 | Some(SourceChange::from_local_edit(file_id, "add semicolon", ra_editor::on_eq_typed(&file, offset)?)) | ||
189 | } | ||
190 | pub fn file_structure(&self, file_id: FileId) -> Vec<StructureNode> { | ||
191 | let file = self.imp.file_syntax(file_id); | ||
192 | ra_editor::file_structure(&file) | ||
193 | } | ||
194 | pub fn symbol_search(&self, query: Query, token: &JobToken) -> Vec<(FileId, FileSymbol)> { | ||
195 | self.imp.world_symbols(query, token) | ||
196 | } | ||
197 | pub fn approximately_resolve_symbol(&self, file_id: FileId, offset: TextUnit, token: &JobToken) -> Vec<(FileId, FileSymbol)> { | ||
198 | self.imp.approximately_resolve_symbol(file_id, offset, token) | ||
199 | } | ||
200 | pub fn parent_module(&self, file_id: FileId) -> Vec<(FileId, FileSymbol)> { | ||
201 | self.imp.parent_module(file_id) | ||
202 | } | ||
203 | pub fn crate_for(&self, file_id: FileId) -> Vec<CrateId> { | ||
204 | self.imp.crate_for(file_id) | ||
205 | } | ||
206 | pub fn crate_root(&self, crate_id: CrateId) -> FileId { | ||
207 | self.imp.crate_root(crate_id) | ||
208 | } | ||
209 | pub fn runnables(&self, file_id: FileId) -> Vec<Runnable> { | ||
210 | let file = self.imp.file_syntax(file_id); | ||
211 | ra_editor::runnables(&file) | ||
212 | } | ||
213 | pub fn highlight(&self, file_id: FileId) -> Vec<HighlightedRange> { | ||
214 | let file = self.imp.file_syntax(file_id); | ||
215 | ra_editor::highlight(&file) | ||
216 | } | ||
217 | pub fn completions(&self, file_id: FileId, offset: TextUnit) -> Option<Vec<CompletionItem>> { | ||
218 | let file = self.imp.file_syntax(file_id); | ||
219 | ra_editor::scope_completion(&file, offset) | ||
220 | } | ||
221 | pub fn assists(&self, file_id: FileId, range: TextRange) -> Vec<SourceChange> { | ||
222 | self.imp.assists(file_id, range) | ||
223 | } | ||
224 | pub fn diagnostics(&self, file_id: FileId) -> Vec<Diagnostic> { | ||
225 | self.imp.diagnostics(file_id) | ||
226 | } | ||
227 | } | ||
228 | |||
229 | #[derive(Debug)] | ||
230 | pub struct LibraryData { | ||
231 | root: roots::ReadonlySourceRoot | ||
232 | } | ||
233 | |||
234 | impl LibraryData { | ||
235 | pub fn prepare(files: Vec<(FileId, String)>, file_resolver: Arc<FileResolver>) -> LibraryData { | ||
236 | let file_resolver = FileResolverImp::new(file_resolver); | ||
237 | let root = roots::ReadonlySourceRoot::new(files, file_resolver); | ||
238 | LibraryData { root } | ||
239 | } | ||
240 | } | ||