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