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