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