2020-08-13 15:42:52 +00:00
|
|
|
//! ide crate provides "ide-centric" APIs for the rust-analyzer. That is,
|
2019-01-08 19:45:52 +00:00
|
|
|
//! it generally operates with files and text ranges, and returns results as
|
|
|
|
//! Strings, suitable for displaying to the human.
|
|
|
|
//!
|
|
|
|
//! What powers this API are the `RootDatabase` struct, which defines a `salsa`
|
2020-08-13 14:36:55 +00:00
|
|
|
//! database, and the `hir` crate, where majority of the analysis happens.
|
2019-01-08 19:33:36 +00:00
|
|
|
//! However, IDE specific bits of the analysis (most notably completion) happen
|
|
|
|
//! in this crate.
|
2019-02-03 19:15:31 +00:00
|
|
|
|
|
|
|
// For proving that RootDatabase is RefUnwindSafe.
|
|
|
|
#![recursion_limit = "128"]
|
|
|
|
|
2020-04-06 14:58:16 +00:00
|
|
|
#[allow(unused)]
|
|
|
|
macro_rules! eprintln {
|
|
|
|
($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
|
|
|
|
}
|
|
|
|
|
2020-10-02 12:26:40 +00:00
|
|
|
#[cfg(test)]
|
2020-10-02 15:34:31 +00:00
|
|
|
mod fixture;
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-07-08 20:37:35 +00:00
|
|
|
mod markup;
|
2020-03-05 11:42:04 +00:00
|
|
|
mod prime_caches;
|
2020-07-16 16:13:43 +00:00
|
|
|
mod display;
|
|
|
|
|
2021-02-13 11:07:47 +00:00
|
|
|
mod annotations;
|
2020-07-16 16:13:43 +00:00
|
|
|
mod call_hierarchy;
|
|
|
|
mod diagnostics;
|
|
|
|
mod expand_macro;
|
|
|
|
mod extend_selection;
|
|
|
|
mod file_structure;
|
|
|
|
mod folding_ranges;
|
2019-01-11 09:53:16 +00:00
|
|
|
mod goto_definition;
|
2020-05-30 23:54:54 +00:00
|
|
|
mod goto_implementation;
|
2020-07-16 16:13:43 +00:00
|
|
|
mod goto_type_definition;
|
2020-12-28 18:29:58 +00:00
|
|
|
mod view_hir;
|
2019-01-08 19:33:36 +00:00
|
|
|
mod hover;
|
2020-07-16 16:13:43 +00:00
|
|
|
mod inlay_hints;
|
|
|
|
mod join_lines;
|
|
|
|
mod matching_brace;
|
2021-03-16 12:37:00 +00:00
|
|
|
mod move_item;
|
2019-01-11 15:17:20 +00:00
|
|
|
mod parent_module;
|
2019-02-08 10:51:22 +00:00
|
|
|
mod references;
|
2020-09-02 14:27:57 +00:00
|
|
|
mod fn_references;
|
2020-07-16 16:13:43 +00:00
|
|
|
mod runnables;
|
2021-03-09 05:11:28 +00:00
|
|
|
mod ssr;
|
2020-07-16 16:13:43 +00:00
|
|
|
mod status;
|
|
|
|
mod syntax_highlighting;
|
2019-03-04 06:54:54 +00:00
|
|
|
mod syntax_tree;
|
2019-03-23 11:07:21 +00:00
|
|
|
mod typing;
|
2020-10-05 17:27:29 +00:00
|
|
|
mod markdown_remove;
|
2020-08-31 08:38:10 +00:00
|
|
|
mod doc_links;
|
2021-05-11 14:15:31 +00:00
|
|
|
mod view_crate_graph;
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2019-02-08 08:52:18 +00:00
|
|
|
use std::sync::Arc;
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-10-24 08:39:57 +00:00
|
|
|
use cfg::CfgOptions;
|
2021-03-09 05:11:28 +00:00
|
|
|
|
2020-10-24 08:39:57 +00:00
|
|
|
use ide_db::base_db::{
|
2019-01-17 11:11:00 +00:00
|
|
|
salsa::{self, ParallelDatabase},
|
2020-06-11 09:04:09 +00:00
|
|
|
CheckCanceled, Env, FileLoader, FileSet, SourceDatabase, VfsPath,
|
2019-01-17 11:11:00 +00:00
|
|
|
};
|
2020-08-13 14:39:16 +00:00
|
|
|
use ide_db::{
|
2020-02-06 11:52:32 +00:00
|
|
|
symbol_index::{self, FileSymbol},
|
|
|
|
LineIndexDatabase,
|
|
|
|
};
|
2020-12-17 11:17:13 +00:00
|
|
|
use syntax::SourceFile;
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-02-06 11:52:32 +00:00
|
|
|
use crate::display::ToNav;
|
2019-01-08 19:33:36 +00:00
|
|
|
|
|
|
|
pub use crate::{
|
2021-02-13 11:07:47 +00:00
|
|
|
annotations::{Annotation, AnnotationConfig, AnnotationKind},
|
2019-12-30 14:12:06 +00:00
|
|
|
call_hierarchy::CallItem,
|
2021-04-12 14:58:01 +00:00
|
|
|
diagnostics::{Diagnostic, DiagnosticsConfig, Severity},
|
2021-01-20 14:25:34 +00:00
|
|
|
display::navigation_target::NavigationTarget,
|
2019-11-19 14:56:48 +00:00
|
|
|
expand_macro::ExpandedMacro,
|
2021-03-15 09:55:27 +00:00
|
|
|
file_structure::{StructureNode, StructureNodeKind},
|
2019-03-22 10:29:58 +00:00
|
|
|
folding_ranges::{Fold, FoldKind},
|
2020-06-10 18:24:36 +00:00
|
|
|
hover::{HoverAction, HoverConfig, HoverGotoTypeData, HoverResult},
|
2020-03-31 14:02:55 +00:00
|
|
|
inlay_hints::{InlayHint, InlayHintsConfig, InlayKind},
|
2020-07-08 20:37:35 +00:00
|
|
|
markup::Markup,
|
2021-03-16 12:37:00 +00:00
|
|
|
move_item::Direction,
|
2020-10-06 15:58:03 +00:00
|
|
|
prime_caches::PrimeCachesProgress,
|
2021-02-07 17:38:12 +00:00
|
|
|
references::{rename::RenameError, ReferenceSearchResult},
|
2020-02-14 23:06:14 +00:00
|
|
|
runnables::{Runnable, RunnableKind, TestId},
|
2020-02-26 18:39:32 +00:00
|
|
|
syntax_highlighting::{
|
2021-04-05 18:39:17 +00:00
|
|
|
tags::{Highlight, HlMod, HlMods, HlOperator, HlPunct, HlTag},
|
2021-01-09 11:48:15 +00:00
|
|
|
HlRange,
|
2020-02-26 18:39:32 +00:00
|
|
|
},
|
2019-01-08 19:33:36 +00:00
|
|
|
};
|
2021-02-17 14:53:31 +00:00
|
|
|
pub use hir::{Documentation, Semantics};
|
2021-05-03 15:16:35 +00:00
|
|
|
pub use ide_assists::{
|
|
|
|
Assist, AssistConfig, AssistId, AssistKind, AssistResolveStrategy, SingleResolve,
|
|
|
|
};
|
2021-02-17 14:53:31 +00:00
|
|
|
pub use ide_completion::{
|
2021-03-12 03:13:27 +00:00
|
|
|
CompletionConfig, CompletionItem, CompletionItemKind, CompletionRelevance, ImportEdit,
|
|
|
|
InsertTextFormat,
|
2020-10-18 10:09:00 +00:00
|
|
|
};
|
2020-12-17 11:17:13 +00:00
|
|
|
pub use ide_db::{
|
2021-01-18 13:09:09 +00:00
|
|
|
base_db::{
|
|
|
|
Canceled, Change, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange,
|
|
|
|
SourceRoot, SourceRootId,
|
|
|
|
},
|
2020-12-17 11:17:13 +00:00
|
|
|
call_info::CallInfo,
|
2020-08-18 14:41:21 +00:00
|
|
|
label::Label,
|
2021-02-12 19:09:53 +00:00
|
|
|
line_index::{LineCol, LineColUtf16, LineIndex},
|
2021-02-09 15:03:39 +00:00
|
|
|
search::{ReferenceAccess, SearchScope},
|
2021-01-14 21:43:36 +00:00
|
|
|
source_change::{FileSystemEdit, SourceChange},
|
2020-02-06 11:52:32 +00:00
|
|
|
symbol_index::Query,
|
2021-03-15 09:55:27 +00:00
|
|
|
RootDatabase, SymbolKind,
|
2020-02-06 11:52:32 +00:00
|
|
|
};
|
2021-02-22 19:14:58 +00:00
|
|
|
pub use ide_ssr::SsrError;
|
2020-12-17 11:17:13 +00:00
|
|
|
pub use syntax::{TextRange, TextSize};
|
2020-08-12 15:03:06 +00:00
|
|
|
pub use text_edit::{Indel, TextEdit};
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2019-01-15 18:02:42 +00:00
|
|
|
pub type Cancelable<T> = Result<T, Canceled>;
|
|
|
|
|
2019-09-19 21:07:23 +00:00
|
|
|
/// Info associated with a text range.
|
2019-01-08 19:33:36 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RangeInfo<T> {
|
|
|
|
pub range: TextRange,
|
|
|
|
pub info: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> RangeInfo<T> {
|
2019-01-11 11:14:09 +00:00
|
|
|
pub fn new(range: TextRange, info: T) -> RangeInfo<T> {
|
2019-01-08 19:33:36 +00:00
|
|
|
RangeInfo { range, info }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `AnalysisHost` stores the current state of the world.
|
2019-06-07 17:49:29 +00:00
|
|
|
#[derive(Debug)]
|
2019-01-08 19:33:36 +00:00
|
|
|
pub struct AnalysisHost {
|
2020-02-06 11:52:32 +00:00
|
|
|
db: RootDatabase,
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AnalysisHost {
|
2020-03-10 17:56:15 +00:00
|
|
|
pub fn new(lru_capacity: Option<usize>) -> AnalysisHost {
|
|
|
|
AnalysisHost { db: RootDatabase::new(lru_capacity) }
|
2019-06-07 17:49:29 +00:00
|
|
|
}
|
2020-03-20 20:09:23 +00:00
|
|
|
|
|
|
|
pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) {
|
|
|
|
self.db.update_lru_capacity(lru_capacity);
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Returns a snapshot of the current state, which you can query for
|
|
|
|
/// semantic information.
|
|
|
|
pub fn analysis(&self) -> Analysis {
|
2019-02-08 11:49:43 +00:00
|
|
|
Analysis { db: self.db.snapshot() }
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Applies changes to the current state of the world. If there are
|
|
|
|
/// outstanding snapshots, they will be canceled.
|
2020-10-02 13:45:09 +00:00
|
|
|
pub fn apply_change(&mut self, change: Change) {
|
2019-01-08 19:33:36 +00:00
|
|
|
self.db.apply_change(change)
|
|
|
|
}
|
2019-01-25 16:11:58 +00:00
|
|
|
|
|
|
|
pub fn collect_garbage(&mut self) {
|
|
|
|
self.db.collect_garbage();
|
|
|
|
}
|
2019-06-30 11:40:01 +00:00
|
|
|
/// NB: this clears the database
|
2020-08-12 14:32:36 +00:00
|
|
|
pub fn per_query_memory_usage(&mut self) -> Vec<(String, profile::Bytes)> {
|
2019-06-30 11:40:01 +00:00
|
|
|
self.db.per_query_memory_usage()
|
|
|
|
}
|
2020-01-24 15:35:37 +00:00
|
|
|
pub fn request_cancellation(&mut self) {
|
|
|
|
self.db.request_cancellation();
|
|
|
|
}
|
2020-04-24 19:57:10 +00:00
|
|
|
pub fn raw_database(&self) -> &RootDatabase {
|
2019-06-15 13:29:23 +00:00
|
|
|
&self.db
|
|
|
|
}
|
2020-04-24 19:57:10 +00:00
|
|
|
pub fn raw_database_mut(&mut self) -> &mut RootDatabase {
|
2019-06-26 06:12:46 +00:00
|
|
|
&mut self.db
|
|
|
|
}
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 13:26:40 +00:00
|
|
|
impl Default for AnalysisHost {
|
|
|
|
fn default() -> AnalysisHost {
|
|
|
|
AnalysisHost::new(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Analysis is a snapshot of a world state at a moment in time. It is the main
|
|
|
|
/// entry point for asking semantic information about the world. When the world
|
|
|
|
/// state is advanced using `AnalysisHost::apply_change` method, all existing
|
|
|
|
/// `Analysis` are canceled (most method return `Err(Canceled)`).
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Analysis {
|
2020-02-06 11:52:32 +00:00
|
|
|
db: salsa::Snapshot<RootDatabase>,
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 11:43:59 +00:00
|
|
|
// As a general design guideline, `Analysis` API are intended to be independent
|
|
|
|
// from the language server protocol. That is, when exposing some functionality
|
|
|
|
// we should think in terms of "what API makes most sense" and not in terms of
|
|
|
|
// "what types LSP uses". Although currently LSP is the only consumer of the
|
|
|
|
// API, the API should in theory be usable as a library, or via a different
|
|
|
|
// protocol.
|
2019-01-08 19:33:36 +00:00
|
|
|
impl Analysis {
|
2019-03-20 20:35:24 +00:00
|
|
|
// Creates an analysis instance for a single file, without any extenal
|
|
|
|
// dependencies, stdlib support or ability to apply changes. See
|
|
|
|
// `AnalysisHost` for creating a fully-featured analysis.
|
|
|
|
pub fn from_single_file(text: String) -> (Analysis, FileId) {
|
|
|
|
let mut host = AnalysisHost::default();
|
2020-06-11 09:04:09 +00:00
|
|
|
let file_id = FileId(0);
|
|
|
|
let mut file_set = FileSet::default();
|
|
|
|
file_set.insert(file_id, VfsPath::new_virtual_path("/main.rs".to_string()));
|
|
|
|
let source_root = SourceRoot::new_local(file_set);
|
|
|
|
|
2020-10-02 13:45:09 +00:00
|
|
|
let mut change = Change::new();
|
2020-06-11 09:04:09 +00:00
|
|
|
change.set_roots(vec![source_root]);
|
2019-03-20 20:35:24 +00:00
|
|
|
let mut crate_graph = CrateGraph::default();
|
2019-09-29 23:38:16 +00:00
|
|
|
// FIXME: cfg options
|
|
|
|
// Default to enable test for single file.
|
2019-10-08 11:22:49 +00:00
|
|
|
let mut cfg_options = CfgOptions::default();
|
|
|
|
cfg_options.insert_atom("test".into());
|
2020-03-08 13:26:57 +00:00
|
|
|
crate_graph.add_crate_root(
|
|
|
|
file_id,
|
|
|
|
Edition::Edition2018,
|
|
|
|
None,
|
|
|
|
cfg_options,
|
|
|
|
Env::default(),
|
2020-03-11 03:04:02 +00:00
|
|
|
Default::default(),
|
2020-03-08 13:26:57 +00:00
|
|
|
);
|
2020-06-11 09:04:09 +00:00
|
|
|
change.change_file(file_id, Some(Arc::new(text)));
|
2019-03-20 20:35:24 +00:00
|
|
|
change.set_crate_graph(crate_graph);
|
|
|
|
host.apply_change(change);
|
|
|
|
(host.analysis(), file_id)
|
|
|
|
}
|
|
|
|
|
2019-09-19 21:07:23 +00:00
|
|
|
/// Debug info about the current state of the analysis.
|
2020-09-29 20:05:18 +00:00
|
|
|
pub fn status(&self, file_id: Option<FileId>) -> Cancelable<String> {
|
|
|
|
self.with_db(|db| status::status(&*db, file_id))
|
2019-01-22 21:15:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 15:58:03 +00:00
|
|
|
pub fn prime_caches<F>(&self, cb: F) -> Cancelable<()>
|
|
|
|
where
|
|
|
|
F: Fn(PrimeCachesProgress) + Sync + std::panic::UnwindSafe,
|
|
|
|
{
|
|
|
|
self.with_db(move |db| prime_caches::prime_caches(db, &cb))
|
2020-03-05 11:42:04 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Gets the text of the source file.
|
2019-07-25 17:22:41 +00:00
|
|
|
pub fn file_text(&self, file_id: FileId) -> Cancelable<Arc<String>> {
|
|
|
|
self.with_db(|db| db.file_text(file_id))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Gets the syntax tree of the file.
|
2019-07-25 17:22:41 +00:00
|
|
|
pub fn parse(&self, file_id: FileId) -> Cancelable<SourceFile> {
|
|
|
|
self.with_db(|db| db.parse(file_id).tree())
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2021-04-16 15:31:47 +00:00
|
|
|
/// Returns true if this file belongs to an immutable library.
|
|
|
|
pub fn is_library_file(&self, file_id: FileId) -> Cancelable<bool> {
|
|
|
|
use ide_db::base_db::SourceDatabaseExt;
|
|
|
|
self.with_db(|db| db.source_root(db.file_source_root(file_id)).is_library)
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Gets the file's `LineIndex`: data structure to convert between absolute
|
|
|
|
/// offsets and line/column representation.
|
2019-07-25 17:22:41 +00:00
|
|
|
pub fn file_line_index(&self, file_id: FileId) -> Cancelable<Arc<LineIndex>> {
|
|
|
|
self.with_db(|db| db.line_index(file_id))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2019-02-11 16:18:27 +00:00
|
|
|
/// Selects the next syntactic nodes encompassing the range.
|
2019-01-20 18:05:01 +00:00
|
|
|
pub fn extend_selection(&self, frange: FileRange) -> Cancelable<TextRange> {
|
|
|
|
self.with_db(|db| extend_selection::extend_selection(db, frange))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2019-02-11 16:18:27 +00:00
|
|
|
/// Returns position of the matching brace (all types of braces are
|
2019-01-08 19:33:36 +00:00
|
|
|
/// supported).
|
2020-04-24 21:40:41 +00:00
|
|
|
pub fn matching_brace(&self, position: FilePosition) -> Cancelable<Option<TextSize>> {
|
2019-07-25 17:22:41 +00:00
|
|
|
self.with_db(|db| {
|
|
|
|
let parse = db.parse(position.file_id);
|
|
|
|
let file = parse.tree();
|
|
|
|
matching_brace::matching_brace(&file, position.offset)
|
|
|
|
})
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Returns a syntax tree represented as `String`, for debug purposes.
|
|
|
|
// FIXME: use a better name here.
|
2019-07-25 17:22:41 +00:00
|
|
|
pub fn syntax_tree(
|
|
|
|
&self,
|
|
|
|
file_id: FileId,
|
|
|
|
text_range: Option<TextRange>,
|
|
|
|
) -> Cancelable<String> {
|
|
|
|
self.with_db(|db| syntax_tree::syntax_tree(&db, file_id, text_range))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2020-12-28 18:29:58 +00:00
|
|
|
pub fn view_hir(&self, position: FilePosition) -> Cancelable<String> {
|
|
|
|
self.with_db(|db| view_hir::view_hir(&db, position))
|
|
|
|
}
|
|
|
|
|
2021-05-11 22:14:59 +00:00
|
|
|
/// Renders the crate graph to GraphViz "dot" syntax.
|
2021-05-11 14:15:31 +00:00
|
|
|
pub fn view_crate_graph(&self) -> Cancelable<Result<String, String>> {
|
|
|
|
self.with_db(|db| view_crate_graph::view_crate_graph(&db))
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:56:48 +00:00
|
|
|
pub fn expand_macro(&self, position: FilePosition) -> Cancelable<Option<ExpandedMacro>> {
|
2019-11-17 18:47:50 +00:00
|
|
|
self.with_db(|db| expand_macro::expand_macro(db, position))
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Returns an edit to remove all newlines in the range, cleaning up minor
|
|
|
|
/// stuff like trailing commas.
|
2020-05-21 17:50:23 +00:00
|
|
|
pub fn join_lines(&self, frange: FileRange) -> Cancelable<TextEdit> {
|
2019-07-25 17:22:41 +00:00
|
|
|
self.with_db(|db| {
|
|
|
|
let parse = db.parse(frange.file_id);
|
2020-05-21 17:50:23 +00:00
|
|
|
join_lines::join_lines(&parse.tree(), frange.range)
|
2019-07-25 17:22:41 +00:00
|
|
|
})
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Returns an edit which should be applied when opening a new line, fixing
|
|
|
|
/// up minor stuff like continuing the comment.
|
2020-05-25 12:12:53 +00:00
|
|
|
/// The edit will be a snippet (with `$0`).
|
|
|
|
pub fn on_enter(&self, position: FilePosition) -> Cancelable<Option<TextEdit>> {
|
2019-07-25 17:22:41 +00:00
|
|
|
self.with_db(|db| typing::on_enter(&db, position))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2019-10-25 08:19:26 +00:00
|
|
|
/// Returns an edit which should be applied after a character was typed.
|
|
|
|
///
|
|
|
|
/// This is useful for some on-the-fly fixups, like adding `;` to `let =`
|
|
|
|
/// automatically.
|
|
|
|
pub fn on_char_typed(
|
|
|
|
&self,
|
|
|
|
position: FilePosition,
|
|
|
|
char_typed: char,
|
|
|
|
) -> Cancelable<Option<SourceChange>> {
|
2019-10-25 09:04:17 +00:00
|
|
|
// Fast path to not even parse the file.
|
|
|
|
if !typing::TRIGGER_CHARS.contains(char_typed) {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
2019-10-25 08:19:26 +00:00
|
|
|
self.with_db(|db| typing::on_char_typed(&db, position, char_typed))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Returns a tree representation of symbols in the file. Useful to draw a
|
|
|
|
/// file outline.
|
2019-07-25 17:22:41 +00:00
|
|
|
pub fn file_structure(&self, file_id: FileId) -> Cancelable<Vec<StructureNode>> {
|
2020-07-16 16:13:43 +00:00
|
|
|
self.with_db(|db| file_structure::file_structure(&db.parse(file_id).tree()))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2019-07-19 21:20:09 +00:00
|
|
|
/// Returns a list of the places in the file where type hints can be displayed.
|
2019-11-18 17:02:28 +00:00
|
|
|
pub fn inlay_hints(
|
|
|
|
&self,
|
|
|
|
file_id: FileId,
|
2020-03-31 14:02:55 +00:00
|
|
|
config: &InlayHintsConfig,
|
2019-11-18 17:02:28 +00:00
|
|
|
) -> Cancelable<Vec<InlayHint>> {
|
2020-03-31 14:02:55 +00:00
|
|
|
self.with_db(|db| inlay_hints::inlay_hints(db, file_id, config))
|
2019-07-19 21:20:09 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Returns the set of folding ranges.
|
2019-07-25 17:22:41 +00:00
|
|
|
pub fn folding_ranges(&self, file_id: FileId) -> Cancelable<Vec<Fold>> {
|
|
|
|
self.with_db(|db| folding_ranges::folding_ranges(&db.parse(file_id).tree()))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Fuzzy searches for a symbol.
|
|
|
|
pub fn symbol_search(&self, query: Query) -> Cancelable<Vec<NavigationTarget>> {
|
2019-01-10 09:20:32 +00:00
|
|
|
self.with_db(|db| {
|
2019-01-15 15:19:09 +00:00
|
|
|
symbol_index::world_symbols(db, query)
|
2019-01-10 09:20:32 +00:00
|
|
|
.into_iter()
|
2019-11-11 08:15:19 +00:00
|
|
|
.map(|s| s.to_nav(db))
|
2019-01-15 15:19:09 +00:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
})
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2019-09-19 21:07:23 +00:00
|
|
|
/// Returns the definitions from the symbol at `position`.
|
2019-01-08 22:38:51 +00:00
|
|
|
pub fn goto_definition(
|
2019-01-08 19:33:36 +00:00
|
|
|
&self,
|
|
|
|
position: FilePosition,
|
2019-01-11 11:14:09 +00:00
|
|
|
) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
|
2019-01-20 17:55:08 +00:00
|
|
|
self.with_db(|db| goto_definition::goto_definition(db, position))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2019-09-19 21:07:23 +00:00
|
|
|
/// Returns the impls from the symbol at `position`.
|
2019-01-28 14:26:32 +00:00
|
|
|
pub fn goto_implementation(
|
|
|
|
&self,
|
|
|
|
position: FilePosition,
|
|
|
|
) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
|
2020-05-30 23:54:54 +00:00
|
|
|
self.with_db(|db| goto_implementation::goto_implementation(db, position))
|
2019-01-28 14:26:32 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 21:07:23 +00:00
|
|
|
/// Returns the type definitions for the symbol at `position`.
|
2019-04-23 18:11:27 +00:00
|
|
|
pub fn goto_type_definition(
|
|
|
|
&self,
|
|
|
|
position: FilePosition,
|
|
|
|
) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
|
|
|
|
self.with_db(|db| goto_type_definition::goto_type_definition(db, position))
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Finds all usages of the reference at point.
|
2019-02-17 11:38:32 +00:00
|
|
|
pub fn find_all_refs(
|
|
|
|
&self,
|
|
|
|
position: FilePosition,
|
2019-10-24 11:01:02 +00:00
|
|
|
search_scope: Option<SearchScope>,
|
2019-02-17 11:38:32 +00:00
|
|
|
) -> Cancelable<Option<ReferenceSearchResult>> {
|
2021-01-18 20:10:01 +00:00
|
|
|
self.with_db(|db| references::find_all_refs(&Semantics::new(db), position, search_scope))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2020-09-02 14:27:57 +00:00
|
|
|
/// Finds all methods and free functions for the file. Does not return tests!
|
2020-09-02 13:03:05 +00:00
|
|
|
pub fn find_all_methods(&self, file_id: FileId) -> Cancelable<Vec<FileRange>> {
|
2020-09-02 14:27:57 +00:00
|
|
|
self.with_db(|db| fn_references::find_all_methods(db, file_id))
|
2020-09-02 13:03:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-11 16:18:27 +00:00
|
|
|
/// Returns a short text describing element at position.
|
2020-09-26 05:02:09 +00:00
|
|
|
pub fn hover(
|
|
|
|
&self,
|
|
|
|
position: FilePosition,
|
|
|
|
links_in_hover: bool,
|
2020-10-05 17:27:29 +00:00
|
|
|
markdown: bool,
|
2020-09-26 05:02:09 +00:00
|
|
|
) -> Cancelable<Option<RangeInfo<HoverResult>>> {
|
2020-10-05 17:27:29 +00:00
|
|
|
self.with_db(|db| hover::hover(db, position, links_in_hover, markdown))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2020-08-30 08:02:29 +00:00
|
|
|
/// Return URL(s) for the documentation of the symbol under the cursor.
|
2020-08-31 23:38:32 +00:00
|
|
|
pub fn external_docs(
|
2020-08-30 08:02:29 +00:00
|
|
|
&self,
|
|
|
|
position: FilePosition,
|
2020-08-31 08:38:10 +00:00
|
|
|
) -> Cancelable<Option<doc_links::DocumentationLink>> {
|
2020-09-01 08:26:10 +00:00
|
|
|
self.with_db(|db| doc_links::external_docs(db, &position))
|
2020-08-30 08:02:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Computes parameter information for the given call expression.
|
|
|
|
pub fn call_info(&self, position: FilePosition) -> Cancelable<Option<CallInfo>> {
|
2020-10-24 08:07:10 +00:00
|
|
|
self.with_db(|db| ide_db::call_info::call_info(db, position))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2019-12-30 14:12:06 +00:00
|
|
|
/// Computes call hierarchy candidates for the given file position.
|
|
|
|
pub fn call_hierarchy(
|
|
|
|
&self,
|
|
|
|
position: FilePosition,
|
|
|
|
) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
|
|
|
|
self.with_db(|db| call_hierarchy::call_hierarchy(db, position))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes incoming calls for the given file position.
|
|
|
|
pub fn incoming_calls(&self, position: FilePosition) -> Cancelable<Option<Vec<CallItem>>> {
|
|
|
|
self.with_db(|db| call_hierarchy::incoming_calls(db, position))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes incoming calls for the given file position.
|
|
|
|
pub fn outgoing_calls(&self, position: FilePosition) -> Cancelable<Option<Vec<CallItem>>> {
|
|
|
|
self.with_db(|db| call_hierarchy::outgoing_calls(db, position))
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Returns a `mod name;` declaration which created the current module.
|
|
|
|
pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<NavigationTarget>> {
|
2019-01-15 18:02:42 +00:00
|
|
|
self.with_db(|db| parent_module::parent_module(db, position))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Returns crates this file belongs too.
|
|
|
|
pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
|
2019-02-08 10:50:18 +00:00
|
|
|
self.with_db(|db| parent_module::crate_for(db, file_id))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2019-12-04 22:05:01 +00:00
|
|
|
/// Returns the edition of the given crate.
|
|
|
|
pub fn crate_edition(&self, crate_id: CrateId) -> Cancelable<Edition> {
|
2020-03-09 10:11:59 +00:00
|
|
|
self.with_db(|db| db.crate_graph()[crate_id].edition)
|
2019-12-04 22:05:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Returns the root file of the given crate.
|
|
|
|
pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> {
|
2020-03-09 10:11:59 +00:00
|
|
|
self.with_db(|db| db.crate_graph()[crate_id].root_file_id)
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Returns the set of possible targets to run for the current file.
|
|
|
|
pub fn runnables(&self, file_id: FileId) -> Cancelable<Vec<Runnable>> {
|
2019-01-20 17:55:08 +00:00
|
|
|
self.with_db(|db| runnables::runnables(db, file_id))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2021-02-27 14:59:52 +00:00
|
|
|
/// Returns the set of tests for the given file position.
|
|
|
|
pub fn related_tests(
|
|
|
|
&self,
|
|
|
|
position: FilePosition,
|
|
|
|
search_scope: Option<SearchScope>,
|
|
|
|
) -> Cancelable<Vec<Runnable>> {
|
|
|
|
self.with_db(|db| runnables::related_tests(db, position, search_scope))
|
|
|
|
}
|
|
|
|
|
2020-02-25 19:44:47 +00:00
|
|
|
/// Computes syntax highlighting for the given file
|
2021-01-09 11:48:15 +00:00
|
|
|
pub fn highlight(&self, file_id: FileId) -> Cancelable<Vec<HlRange>> {
|
2020-06-14 12:43:43 +00:00
|
|
|
self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2020-02-25 13:38:50 +00:00
|
|
|
/// Computes syntax highlighting for the given file range.
|
2021-01-09 11:48:15 +00:00
|
|
|
pub fn highlight_range(&self, frange: FileRange) -> Cancelable<Vec<HlRange>> {
|
2020-06-14 12:43:43 +00:00
|
|
|
self.with_db(|db| {
|
|
|
|
syntax_highlighting::highlight(db, frange.file_id, Some(frange.range), false)
|
|
|
|
})
|
2020-02-25 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 10:42:34 +00:00
|
|
|
/// Computes syntax highlighting for the given file.
|
2019-05-25 14:29:39 +00:00
|
|
|
pub fn highlight_as_html(&self, file_id: FileId, rainbow: bool) -> Cancelable<String> {
|
|
|
|
self.with_db(|db| syntax_highlighting::highlight_as_html(db, file_id, rainbow))
|
2019-05-25 10:42:34 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Computes completions at the given position.
|
2020-03-10 17:47:09 +00:00
|
|
|
pub fn completions(
|
|
|
|
&self,
|
2020-03-31 14:02:55 +00:00
|
|
|
config: &CompletionConfig,
|
2020-05-17 10:09:53 +00:00
|
|
|
position: FilePosition,
|
2020-03-10 17:47:09 +00:00
|
|
|
) -> Cancelable<Option<Vec<CompletionItem>>> {
|
2021-02-17 14:53:31 +00:00
|
|
|
self.with_db(|db| ide_completion::completions(db, config, position).map(Into::into))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2020-12-04 14:03:22 +00:00
|
|
|
/// Resolves additional completion data at the position given.
|
|
|
|
pub fn resolve_completion_edits(
|
|
|
|
&self,
|
|
|
|
config: &CompletionConfig,
|
|
|
|
position: FilePosition,
|
|
|
|
full_import_path: &str,
|
2020-12-29 12:35:49 +00:00
|
|
|
imported_name: String,
|
2020-12-04 14:03:22 +00:00
|
|
|
) -> Cancelable<Vec<TextEdit>> {
|
|
|
|
Ok(self
|
|
|
|
.with_db(|db| {
|
2021-02-17 14:53:31 +00:00
|
|
|
ide_completion::resolve_completion_edits(
|
2020-12-04 14:03:22 +00:00
|
|
|
db,
|
|
|
|
config,
|
|
|
|
position,
|
|
|
|
full_import_path,
|
|
|
|
imported_name,
|
|
|
|
)
|
|
|
|
})?
|
|
|
|
.unwrap_or_default())
|
|
|
|
}
|
|
|
|
|
2020-12-24 12:32:29 +00:00
|
|
|
/// Computes assists (aka code actions aka intentions) for the given
|
2020-12-26 11:11:42 +00:00
|
|
|
/// position. If `resolve == false`, computes enough info to show the
|
|
|
|
/// lightbulb list in the editor, but doesn't compute actual edits, to
|
|
|
|
/// improve performance.
|
|
|
|
pub fn assists(
|
2020-06-02 20:21:48 +00:00
|
|
|
&self,
|
|
|
|
config: &AssistConfig,
|
2021-05-03 14:08:09 +00:00
|
|
|
resolve: AssistResolveStrategy,
|
2020-06-02 20:21:48 +00:00
|
|
|
frange: FileRange,
|
2020-12-26 11:11:42 +00:00
|
|
|
) -> Cancelable<Vec<Assist>> {
|
2021-03-09 05:11:28 +00:00
|
|
|
self.with_db(|db| {
|
2021-05-03 15:03:28 +00:00
|
|
|
let ssr_assists = ssr::ssr_assists(db, &resolve, frange);
|
2021-03-09 05:11:28 +00:00
|
|
|
let mut acc = Assist::get(db, config, resolve, frange);
|
2021-05-03 14:08:09 +00:00
|
|
|
acc.extend(ssr_assists.into_iter());
|
2021-03-09 05:11:28 +00:00
|
|
|
acc
|
|
|
|
})
|
2020-06-02 20:21:48 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Computes the set of diagnostics for the given file.
|
2020-07-24 15:39:44 +00:00
|
|
|
pub fn diagnostics(
|
|
|
|
&self,
|
2020-08-18 14:03:15 +00:00
|
|
|
config: &DiagnosticsConfig,
|
2021-05-03 14:08:09 +00:00
|
|
|
resolve: AssistResolveStrategy,
|
2020-07-24 15:39:44 +00:00
|
|
|
file_id: FileId,
|
|
|
|
) -> Cancelable<Vec<Diagnostic>> {
|
2021-05-03 15:03:28 +00:00
|
|
|
self.with_db(|db| diagnostics::diagnostics(db, config, &resolve, file_id))
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-08 23:47:12 +00:00
|
|
|
|
2021-04-13 08:27:00 +00:00
|
|
|
/// Convenience function to return assists + quick fixes for diagnostics
|
|
|
|
pub fn assists_with_fixes(
|
|
|
|
&self,
|
|
|
|
assist_config: &AssistConfig,
|
|
|
|
diagnostics_config: &DiagnosticsConfig,
|
2021-05-03 14:08:09 +00:00
|
|
|
resolve: AssistResolveStrategy,
|
2021-04-13 08:27:00 +00:00
|
|
|
frange: FileRange,
|
|
|
|
) -> Cancelable<Vec<Assist>> {
|
|
|
|
let include_fixes = match &assist_config.allowed {
|
|
|
|
Some(it) => it.iter().any(|&it| it == AssistKind::None || it == AssistKind::QuickFix),
|
|
|
|
None => true,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.with_db(|db| {
|
2021-05-03 15:03:28 +00:00
|
|
|
let ssr_assists = ssr::ssr_assists(db, &resolve, frange);
|
2021-05-03 14:08:09 +00:00
|
|
|
let diagnostic_assists = if include_fixes {
|
2021-05-03 15:03:28 +00:00
|
|
|
diagnostics::diagnostics(db, diagnostics_config, &resolve, frange.file_id)
|
2021-05-03 14:08:09 +00:00
|
|
|
.into_iter()
|
2021-05-17 23:11:07 +00:00
|
|
|
.flat_map(|it| it.fixes.unwrap_or_default())
|
2021-05-03 14:08:09 +00:00
|
|
|
.filter(|it| it.target.intersect(frange.range).is_some())
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
};
|
|
|
|
|
2021-04-13 08:27:00 +00:00
|
|
|
let mut res = Assist::get(db, assist_config, resolve, frange);
|
2021-05-03 14:08:09 +00:00
|
|
|
res.extend(ssr_assists.into_iter());
|
|
|
|
res.extend(diagnostic_assists.into_iter());
|
|
|
|
|
2021-04-13 08:27:00 +00:00
|
|
|
res
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Returns the edit required to rename reference at the position to the new
|
|
|
|
/// name.
|
|
|
|
pub fn rename(
|
|
|
|
&self,
|
|
|
|
position: FilePosition,
|
|
|
|
new_name: &str,
|
2021-01-18 19:25:40 +00:00
|
|
|
) -> Cancelable<Result<SourceChange, RenameError>> {
|
2020-11-02 15:31:38 +00:00
|
|
|
self.with_db(|db| references::rename::rename(db, position, new_name))
|
2019-01-10 09:20:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 20:35:15 +00:00
|
|
|
pub fn prepare_rename(
|
|
|
|
&self,
|
|
|
|
position: FilePosition,
|
|
|
|
) -> Cancelable<Result<RangeInfo<()>, RenameError>> {
|
|
|
|
self.with_db(|db| references::rename::prepare_rename(db, position))
|
|
|
|
}
|
|
|
|
|
2020-12-22 19:19:51 +00:00
|
|
|
pub fn will_rename_file(
|
|
|
|
&self,
|
|
|
|
file_id: FileId,
|
|
|
|
new_name_stem: &str,
|
|
|
|
) -> Cancelable<Option<SourceChange>> {
|
|
|
|
self.with_db(|db| references::rename::will_rename_file(db, file_id, new_name_stem))
|
|
|
|
}
|
|
|
|
|
2020-02-10 22:45:38 +00:00
|
|
|
pub fn structural_search_replace(
|
|
|
|
&self,
|
|
|
|
query: &str,
|
2020-03-15 21:23:18 +00:00
|
|
|
parse_only: bool,
|
2020-08-13 14:45:10 +00:00
|
|
|
resolve_context: FilePosition,
|
2020-07-29 01:44:01 +00:00
|
|
|
selections: Vec<FileRange>,
|
2020-02-10 22:45:38 +00:00
|
|
|
) -> Cancelable<Result<SourceChange, SsrError>> {
|
|
|
|
self.with_db(|db| {
|
2021-02-22 19:14:58 +00:00
|
|
|
let rule: ide_ssr::SsrRule = query.parse()?;
|
|
|
|
let mut match_finder =
|
|
|
|
ide_ssr::MatchFinder::in_context(db, resolve_context, selections);
|
2020-08-13 14:45:10 +00:00
|
|
|
match_finder.add_rule(rule)?;
|
2021-01-14 17:35:22 +00:00
|
|
|
let edits = if parse_only { Default::default() } else { match_finder.edits() };
|
2020-06-08 19:44:42 +00:00
|
|
|
Ok(SourceChange::from(edits))
|
2020-02-10 22:45:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-13 11:07:47 +00:00
|
|
|
pub fn annotations(
|
|
|
|
&self,
|
|
|
|
file_id: FileId,
|
|
|
|
config: AnnotationConfig,
|
|
|
|
) -> Cancelable<Vec<Annotation>> {
|
|
|
|
self.with_db(|db| annotations::annotations(db, file_id, config))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve_annotation(&self, annotation: Annotation) -> Cancelable<Annotation> {
|
|
|
|
self.with_db(|db| annotations::resolve_annotation(db, annotation))
|
|
|
|
}
|
|
|
|
|
2021-03-16 12:37:00 +00:00
|
|
|
pub fn move_item(
|
|
|
|
&self,
|
|
|
|
range: FileRange,
|
|
|
|
direction: Direction,
|
|
|
|
) -> Cancelable<Option<TextEdit>> {
|
|
|
|
self.with_db(|db| move_item::move_item(db, range, direction))
|
|
|
|
}
|
|
|
|
|
2019-09-19 21:07:23 +00:00
|
|
|
/// Performs an operation on that may be Canceled.
|
2020-08-13 14:45:10 +00:00
|
|
|
fn with_db<F, T>(&self, f: F) -> Cancelable<T>
|
|
|
|
where
|
|
|
|
F: FnOnce(&RootDatabase) -> T + std::panic::UnwindSafe,
|
|
|
|
{
|
2019-01-10 09:20:32 +00:00
|
|
|
self.db.catch_canceled(f)
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn analysis_is_send() {
|
|
|
|
fn is_send<T: Send>() {}
|
|
|
|
is_send::<Analysis>();
|
|
|
|
}
|