aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/lib.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-08-13 16:42:52 +0100
committerAleksey Kladov <[email protected]>2020-08-13 16:58:27 +0100
commit1b0c7701cc97cd7bef8bb9729011d4cf291a60c5 (patch)
treeb69f0c9947d9cec522ce835d7213b21075fe6dcf /crates/ra_ide/src/lib.rs
parentfc34403018079ea053f26d0a31b7517053c7dd8c (diff)
Rename ra_ide -> ide
Diffstat (limited to 'crates/ra_ide/src/lib.rs')
-rw-r--r--crates/ra_ide/src/lib.rs542
1 files changed, 0 insertions, 542 deletions
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs
deleted file mode 100644
index 4321a6b74..000000000
--- a/crates/ra_ide/src/lib.rs
+++ /dev/null
@@ -1,542 +0,0 @@
1//! ra_ide crate provides "ide-centric" APIs for the rust-analyzer. That is,
2//! it generally operates with files and text ranges, and returns results as
3//! Strings, suitable for displaying to the human.
4//!
5//! What powers this API are the `RootDatabase` struct, which defines a `salsa`
6//! database, and the `hir` crate, where majority of the analysis happens.
7//! However, IDE specific bits of the analysis (most notably completion) happen
8//! in this crate.
9
10// For proving that RootDatabase is RefUnwindSafe.
11#![recursion_limit = "128"]
12
13#[allow(unused)]
14macro_rules! eprintln {
15 ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
16}
17
18pub mod mock_analysis;
19
20mod markup;
21mod prime_caches;
22mod display;
23
24mod call_hierarchy;
25mod call_info;
26mod completion;
27mod diagnostics;
28mod expand_macro;
29mod extend_selection;
30mod file_structure;
31mod folding_ranges;
32mod goto_definition;
33mod goto_implementation;
34mod goto_type_definition;
35mod hover;
36mod inlay_hints;
37mod join_lines;
38mod matching_brace;
39mod parent_module;
40mod references;
41mod runnables;
42mod status;
43mod syntax_highlighting;
44mod syntax_tree;
45mod typing;
46
47use std::sync::Arc;
48
49use base_db::{
50 salsa::{self, ParallelDatabase},
51 CheckCanceled, Env, FileLoader, FileSet, SourceDatabase, VfsPath,
52};
53use cfg::CfgOptions;
54use ide_db::{
55 symbol_index::{self, FileSymbol},
56 LineIndexDatabase,
57};
58use syntax::{SourceFile, TextRange, TextSize};
59
60use crate::display::ToNav;
61
62pub use crate::{
63 call_hierarchy::CallItem,
64 call_info::CallInfo,
65 completion::{
66 CompletionConfig, CompletionItem, CompletionItemKind, CompletionScore, InsertTextFormat,
67 },
68 diagnostics::Severity,
69 display::NavigationTarget,
70 expand_macro::ExpandedMacro,
71 file_structure::StructureNode,
72 folding_ranges::{Fold, FoldKind},
73 hover::{HoverAction, HoverConfig, HoverGotoTypeData, HoverResult},
74 inlay_hints::{InlayHint, InlayHintsConfig, InlayKind},
75 markup::Markup,
76 references::{Declaration, Reference, ReferenceAccess, ReferenceKind, ReferenceSearchResult},
77 runnables::{Runnable, RunnableKind, TestId},
78 syntax_highlighting::{
79 Highlight, HighlightModifier, HighlightModifiers, HighlightTag, HighlightedRange,
80 },
81};
82
83pub use assists::{Assist, AssistConfig, AssistId, AssistKind, ResolvedAssist};
84pub use base_db::{
85 Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRoot,
86 SourceRootId,
87};
88pub use hir::{Documentation, Semantics};
89pub use ide_db::{
90 change::AnalysisChange,
91 line_index::{LineCol, LineIndex},
92 search::SearchScope,
93 source_change::{FileSystemEdit, SourceChange, SourceFileEdit},
94 symbol_index::Query,
95 RootDatabase,
96};
97pub use ssr::SsrError;
98pub use text_edit::{Indel, TextEdit};
99
100pub type Cancelable<T> = Result<T, Canceled>;
101
102#[derive(Debug)]
103pub struct Diagnostic {
104 pub message: String,
105 pub range: TextRange,
106 pub severity: Severity,
107 pub fix: Option<Fix>,
108}
109
110#[derive(Debug)]
111pub struct Fix {
112 pub label: String,
113 pub source_change: SourceChange,
114 /// Allows to trigger the fix only when the caret is in the range given
115 pub fix_trigger_range: TextRange,
116}
117
118impl Fix {
119 pub fn new(
120 label: impl Into<String>,
121 source_change: SourceChange,
122 fix_trigger_range: TextRange,
123 ) -> Self {
124 let label = label.into();
125 assert!(label.starts_with(char::is_uppercase) && !label.ends_with('.'));
126 Self { label, source_change, fix_trigger_range }
127 }
128}
129
130/// Info associated with a text range.
131#[derive(Debug)]
132pub struct RangeInfo<T> {
133 pub range: TextRange,
134 pub info: T,
135}
136
137impl<T> RangeInfo<T> {
138 pub fn new(range: TextRange, info: T) -> RangeInfo<T> {
139 RangeInfo { range, info }
140 }
141}
142
143/// `AnalysisHost` stores the current state of the world.
144#[derive(Debug)]
145pub struct AnalysisHost {
146 db: RootDatabase,
147}
148
149impl AnalysisHost {
150 pub fn new(lru_capacity: Option<usize>) -> AnalysisHost {
151 AnalysisHost { db: RootDatabase::new(lru_capacity) }
152 }
153
154 pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) {
155 self.db.update_lru_capacity(lru_capacity);
156 }
157
158 /// Returns a snapshot of the current state, which you can query for
159 /// semantic information.
160 pub fn analysis(&self) -> Analysis {
161 Analysis { db: self.db.snapshot() }
162 }
163
164 /// Applies changes to the current state of the world. If there are
165 /// outstanding snapshots, they will be canceled.
166 pub fn apply_change(&mut self, change: AnalysisChange) {
167 self.db.apply_change(change)
168 }
169
170 pub fn maybe_collect_garbage(&mut self) {
171 self.db.maybe_collect_garbage();
172 }
173
174 pub fn collect_garbage(&mut self) {
175 self.db.collect_garbage();
176 }
177 /// NB: this clears the database
178 pub fn per_query_memory_usage(&mut self) -> Vec<(String, profile::Bytes)> {
179 self.db.per_query_memory_usage()
180 }
181 pub fn request_cancellation(&mut self) {
182 self.db.request_cancellation();
183 }
184 pub fn raw_database(&self) -> &RootDatabase {
185 &self.db
186 }
187 pub fn raw_database_mut(&mut self) -> &mut RootDatabase {
188 &mut self.db
189 }
190}
191
192impl Default for AnalysisHost {
193 fn default() -> AnalysisHost {
194 AnalysisHost::new(None)
195 }
196}
197
198/// Analysis is a snapshot of a world state at a moment in time. It is the main
199/// entry point for asking semantic information about the world. When the world
200/// state is advanced using `AnalysisHost::apply_change` method, all existing
201/// `Analysis` are canceled (most method return `Err(Canceled)`).
202#[derive(Debug)]
203pub struct Analysis {
204 db: salsa::Snapshot<RootDatabase>,
205}
206
207// As a general design guideline, `Analysis` API are intended to be independent
208// from the language server protocol. That is, when exposing some functionality
209// we should think in terms of "what API makes most sense" and not in terms of
210// "what types LSP uses". Although currently LSP is the only consumer of the
211// API, the API should in theory be usable as a library, or via a different
212// protocol.
213impl Analysis {
214 // Creates an analysis instance for a single file, without any extenal
215 // dependencies, stdlib support or ability to apply changes. See
216 // `AnalysisHost` for creating a fully-featured analysis.
217 pub fn from_single_file(text: String) -> (Analysis, FileId) {
218 let mut host = AnalysisHost::default();
219 let file_id = FileId(0);
220 let mut file_set = FileSet::default();
221 file_set.insert(file_id, VfsPath::new_virtual_path("/main.rs".to_string()));
222 let source_root = SourceRoot::new_local(file_set);
223
224 let mut change = AnalysisChange::new();
225 change.set_roots(vec![source_root]);
226 let mut crate_graph = CrateGraph::default();
227 // FIXME: cfg options
228 // Default to enable test for single file.
229 let mut cfg_options = CfgOptions::default();
230 cfg_options.insert_atom("test".into());
231 crate_graph.add_crate_root(
232 file_id,
233 Edition::Edition2018,
234 None,
235 cfg_options,
236 Env::default(),
237 Default::default(),
238 );
239 change.change_file(file_id, Some(Arc::new(text)));
240 change.set_crate_graph(crate_graph);
241 host.apply_change(change);
242 (host.analysis(), file_id)
243 }
244
245 /// Debug info about the current state of the analysis.
246 pub fn status(&self) -> Cancelable<String> {
247 self.with_db(|db| status::status(&*db))
248 }
249
250 pub fn prime_caches(&self, files: Vec<FileId>) -> Cancelable<()> {
251 self.with_db(|db| prime_caches::prime_caches(db, files))
252 }
253
254 /// Gets the text of the source file.
255 pub fn file_text(&self, file_id: FileId) -> Cancelable<Arc<String>> {
256 self.with_db(|db| db.file_text(file_id))
257 }
258
259 /// Gets the syntax tree of the file.
260 pub fn parse(&self, file_id: FileId) -> Cancelable<SourceFile> {
261 self.with_db(|db| db.parse(file_id).tree())
262 }
263
264 /// Gets the file's `LineIndex`: data structure to convert between absolute
265 /// offsets and line/column representation.
266 pub fn file_line_index(&self, file_id: FileId) -> Cancelable<Arc<LineIndex>> {
267 self.with_db(|db| db.line_index(file_id))
268 }
269
270 /// Selects the next syntactic nodes encompassing the range.
271 pub fn extend_selection(&self, frange: FileRange) -> Cancelable<TextRange> {
272 self.with_db(|db| extend_selection::extend_selection(db, frange))
273 }
274
275 /// Returns position of the matching brace (all types of braces are
276 /// supported).
277 pub fn matching_brace(&self, position: FilePosition) -> Cancelable<Option<TextSize>> {
278 self.with_db(|db| {
279 let parse = db.parse(position.file_id);
280 let file = parse.tree();
281 matching_brace::matching_brace(&file, position.offset)
282 })
283 }
284
285 /// Returns a syntax tree represented as `String`, for debug purposes.
286 // FIXME: use a better name here.
287 pub fn syntax_tree(
288 &self,
289 file_id: FileId,
290 text_range: Option<TextRange>,
291 ) -> Cancelable<String> {
292 self.with_db(|db| syntax_tree::syntax_tree(&db, file_id, text_range))
293 }
294
295 pub fn expand_macro(&self, position: FilePosition) -> Cancelable<Option<ExpandedMacro>> {
296 self.with_db(|db| expand_macro::expand_macro(db, position))
297 }
298
299 /// Returns an edit to remove all newlines in the range, cleaning up minor
300 /// stuff like trailing commas.
301 pub fn join_lines(&self, frange: FileRange) -> Cancelable<TextEdit> {
302 self.with_db(|db| {
303 let parse = db.parse(frange.file_id);
304 join_lines::join_lines(&parse.tree(), frange.range)
305 })
306 }
307
308 /// Returns an edit which should be applied when opening a new line, fixing
309 /// up minor stuff like continuing the comment.
310 /// The edit will be a snippet (with `$0`).
311 pub fn on_enter(&self, position: FilePosition) -> Cancelable<Option<TextEdit>> {
312 self.with_db(|db| typing::on_enter(&db, position))
313 }
314
315 /// Returns an edit which should be applied after a character was typed.
316 ///
317 /// This is useful for some on-the-fly fixups, like adding `;` to `let =`
318 /// automatically.
319 pub fn on_char_typed(
320 &self,
321 position: FilePosition,
322 char_typed: char,
323 ) -> Cancelable<Option<SourceChange>> {
324 // Fast path to not even parse the file.
325 if !typing::TRIGGER_CHARS.contains(char_typed) {
326 return Ok(None);
327 }
328 self.with_db(|db| typing::on_char_typed(&db, position, char_typed))
329 }
330
331 /// Returns a tree representation of symbols in the file. Useful to draw a
332 /// file outline.
333 pub fn file_structure(&self, file_id: FileId) -> Cancelable<Vec<StructureNode>> {
334 self.with_db(|db| file_structure::file_structure(&db.parse(file_id).tree()))
335 }
336
337 /// Returns a list of the places in the file where type hints can be displayed.
338 pub fn inlay_hints(
339 &self,
340 file_id: FileId,
341 config: &InlayHintsConfig,
342 ) -> Cancelable<Vec<InlayHint>> {
343 self.with_db(|db| inlay_hints::inlay_hints(db, file_id, config))
344 }
345
346 /// Returns the set of folding ranges.
347 pub fn folding_ranges(&self, file_id: FileId) -> Cancelable<Vec<Fold>> {
348 self.with_db(|db| folding_ranges::folding_ranges(&db.parse(file_id).tree()))
349 }
350
351 /// Fuzzy searches for a symbol.
352 pub fn symbol_search(&self, query: Query) -> Cancelable<Vec<NavigationTarget>> {
353 self.with_db(|db| {
354 symbol_index::world_symbols(db, query)
355 .into_iter()
356 .map(|s| s.to_nav(db))
357 .collect::<Vec<_>>()
358 })
359 }
360
361 /// Returns the definitions from the symbol at `position`.
362 pub fn goto_definition(
363 &self,
364 position: FilePosition,
365 ) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
366 self.with_db(|db| goto_definition::goto_definition(db, position))
367 }
368
369 /// Returns the impls from the symbol at `position`.
370 pub fn goto_implementation(
371 &self,
372 position: FilePosition,
373 ) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
374 self.with_db(|db| goto_implementation::goto_implementation(db, position))
375 }
376
377 /// Returns the type definitions for the symbol at `position`.
378 pub fn goto_type_definition(
379 &self,
380 position: FilePosition,
381 ) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
382 self.with_db(|db| goto_type_definition::goto_type_definition(db, position))
383 }
384
385 /// Finds all usages of the reference at point.
386 pub fn find_all_refs(
387 &self,
388 position: FilePosition,
389 search_scope: Option<SearchScope>,
390 ) -> Cancelable<Option<ReferenceSearchResult>> {
391 self.with_db(|db| {
392 references::find_all_refs(&Semantics::new(db), position, search_scope).map(|it| it.info)
393 })
394 }
395
396 /// Returns a short text describing element at position.
397 pub fn hover(&self, position: FilePosition) -> Cancelable<Option<RangeInfo<HoverResult>>> {
398 self.with_db(|db| hover::hover(db, position))
399 }
400
401 /// Computes parameter information for the given call expression.
402 pub fn call_info(&self, position: FilePosition) -> Cancelable<Option<CallInfo>> {
403 self.with_db(|db| call_info::call_info(db, position))
404 }
405
406 /// Computes call hierarchy candidates for the given file position.
407 pub fn call_hierarchy(
408 &self,
409 position: FilePosition,
410 ) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
411 self.with_db(|db| call_hierarchy::call_hierarchy(db, position))
412 }
413
414 /// Computes incoming calls for the given file position.
415 pub fn incoming_calls(&self, position: FilePosition) -> Cancelable<Option<Vec<CallItem>>> {
416 self.with_db(|db| call_hierarchy::incoming_calls(db, position))
417 }
418
419 /// Computes incoming calls for the given file position.
420 pub fn outgoing_calls(&self, position: FilePosition) -> Cancelable<Option<Vec<CallItem>>> {
421 self.with_db(|db| call_hierarchy::outgoing_calls(db, position))
422 }
423
424 /// Returns a `mod name;` declaration which created the current module.
425 pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<NavigationTarget>> {
426 self.with_db(|db| parent_module::parent_module(db, position))
427 }
428
429 /// Returns crates this file belongs too.
430 pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
431 self.with_db(|db| parent_module::crate_for(db, file_id))
432 }
433
434 /// Returns the edition of the given crate.
435 pub fn crate_edition(&self, crate_id: CrateId) -> Cancelable<Edition> {
436 self.with_db(|db| db.crate_graph()[crate_id].edition)
437 }
438
439 /// Returns the root file of the given crate.
440 pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> {
441 self.with_db(|db| db.crate_graph()[crate_id].root_file_id)
442 }
443
444 /// Returns the set of possible targets to run for the current file.
445 pub fn runnables(&self, file_id: FileId) -> Cancelable<Vec<Runnable>> {
446 self.with_db(|db| runnables::runnables(db, file_id))
447 }
448
449 /// Computes syntax highlighting for the given file
450 pub fn highlight(&self, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> {
451 self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false))
452 }
453
454 /// Computes syntax highlighting for the given file range.
455 pub fn highlight_range(&self, frange: FileRange) -> Cancelable<Vec<HighlightedRange>> {
456 self.with_db(|db| {
457 syntax_highlighting::highlight(db, frange.file_id, Some(frange.range), false)
458 })
459 }
460
461 /// Computes syntax highlighting for the given file.
462 pub fn highlight_as_html(&self, file_id: FileId, rainbow: bool) -> Cancelable<String> {
463 self.with_db(|db| syntax_highlighting::highlight_as_html(db, file_id, rainbow))
464 }
465
466 /// Computes completions at the given position.
467 pub fn completions(
468 &self,
469 config: &CompletionConfig,
470 position: FilePosition,
471 ) -> Cancelable<Option<Vec<CompletionItem>>> {
472 self.with_db(|db| completion::completions(db, config, position).map(Into::into))
473 }
474
475 /// Computes resolved assists with source changes for the given position.
476 pub fn resolved_assists(
477 &self,
478 config: &AssistConfig,
479 frange: FileRange,
480 ) -> Cancelable<Vec<ResolvedAssist>> {
481 self.with_db(|db| assists::Assist::resolved(db, config, frange))
482 }
483
484 /// Computes unresolved assists (aka code actions aka intentions) for the given
485 /// position.
486 pub fn unresolved_assists(
487 &self,
488 config: &AssistConfig,
489 frange: FileRange,
490 ) -> Cancelable<Vec<Assist>> {
491 self.with_db(|db| Assist::unresolved(db, config, frange))
492 }
493
494 /// Computes the set of diagnostics for the given file.
495 pub fn diagnostics(
496 &self,
497 file_id: FileId,
498 enable_experimental: bool,
499 ) -> Cancelable<Vec<Diagnostic>> {
500 self.with_db(|db| diagnostics::diagnostics(db, file_id, enable_experimental))
501 }
502
503 /// Returns the edit required to rename reference at the position to the new
504 /// name.
505 pub fn rename(
506 &self,
507 position: FilePosition,
508 new_name: &str,
509 ) -> Cancelable<Option<RangeInfo<SourceChange>>> {
510 self.with_db(|db| references::rename(db, position, new_name))
511 }
512
513 pub fn structural_search_replace(
514 &self,
515 query: &str,
516 parse_only: bool,
517 resolve_context: FilePosition,
518 selections: Vec<FileRange>,
519 ) -> Cancelable<Result<SourceChange, SsrError>> {
520 self.with_db(|db| {
521 let rule: ssr::SsrRule = query.parse()?;
522 let mut match_finder = ssr::MatchFinder::in_context(db, resolve_context, selections);
523 match_finder.add_rule(rule)?;
524 let edits = if parse_only { Vec::new() } else { match_finder.edits() };
525 Ok(SourceChange::from(edits))
526 })
527 }
528
529 /// Performs an operation on that may be Canceled.
530 fn with_db<F, T>(&self, f: F) -> Cancelable<T>
531 where
532 F: FnOnce(&RootDatabase) -> T + std::panic::UnwindSafe,
533 {
534 self.db.catch_canceled(f)
535 }
536}
537
538#[test]
539fn analysis_is_send() {
540 fn is_send<T: Send>() {}
541 is_send::<Analysis>();
542}