2020-08-13 10:42:52 -05:00
|
|
|
//! ide crate provides "ide-centric" APIs for the rust-analyzer. That is,
|
2019-01-08 13:45:52 -06: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 09:36:55 -05:00
|
|
|
//! database, and the `hir` crate, where majority of the analysis happens.
|
2019-01-08 13:33:36 -06:00
|
|
|
//! However, IDE specific bits of the analysis (most notably completion) happen
|
|
|
|
//! in this crate.
|
2019-02-03 13:15:31 -06:00
|
|
|
|
|
|
|
// For proving that RootDatabase is RefUnwindSafe.
|
|
|
|
#![recursion_limit = "128"]
|
|
|
|
|
2020-04-06 09:58:16 -05:00
|
|
|
#[allow(unused)]
|
|
|
|
macro_rules! eprintln {
|
|
|
|
($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
|
|
|
|
}
|
|
|
|
|
2020-10-02 07:26:40 -05:00
|
|
|
#[cfg(test)]
|
2020-10-02 10:34:31 -05:00
|
|
|
mod fixture;
|
2019-01-08 13:33:36 -06:00
|
|
|
|
2020-07-08 15:37:35 -05:00
|
|
|
mod markup;
|
2020-03-05 05:42:04 -06:00
|
|
|
mod prime_caches;
|
2020-07-16 11:13:43 -05:00
|
|
|
mod display;
|
|
|
|
|
|
|
|
mod call_hierarchy;
|
|
|
|
mod diagnostics;
|
|
|
|
mod expand_macro;
|
|
|
|
mod extend_selection;
|
|
|
|
mod file_structure;
|
|
|
|
mod folding_ranges;
|
2019-01-11 03:53:16 -06:00
|
|
|
mod goto_definition;
|
2020-05-30 18:54:54 -05:00
|
|
|
mod goto_implementation;
|
2020-07-16 11:13:43 -05:00
|
|
|
mod goto_type_definition;
|
2020-12-28 12:29:58 -06:00
|
|
|
mod view_hir;
|
2019-01-08 13:33:36 -06:00
|
|
|
mod hover;
|
2020-07-16 11:13:43 -05:00
|
|
|
mod inlay_hints;
|
|
|
|
mod join_lines;
|
|
|
|
mod matching_brace;
|
2019-01-11 09:17:20 -06:00
|
|
|
mod parent_module;
|
2019-02-08 04:51:22 -06:00
|
|
|
mod references;
|
2020-09-02 09:27:57 -05:00
|
|
|
mod fn_references;
|
2020-07-16 11:13:43 -05:00
|
|
|
mod runnables;
|
|
|
|
mod status;
|
|
|
|
mod syntax_highlighting;
|
2019-03-04 00:54:54 -06:00
|
|
|
mod syntax_tree;
|
2019-03-23 06:07:21 -05:00
|
|
|
mod typing;
|
2020-10-05 12:27:29 -05:00
|
|
|
mod markdown_remove;
|
2020-08-31 03:38:10 -05:00
|
|
|
mod doc_links;
|
2019-01-08 13:33:36 -06:00
|
|
|
|
2019-02-08 02:52:18 -06:00
|
|
|
use std::sync::Arc;
|
2019-01-08 13:33:36 -06:00
|
|
|
|
2020-10-24 03:39:57 -05:00
|
|
|
use cfg::CfgOptions;
|
|
|
|
use ide_db::base_db::{
|
2019-01-17 05:11:00 -06:00
|
|
|
salsa::{self, ParallelDatabase},
|
2020-06-11 04:04:09 -05:00
|
|
|
CheckCanceled, Env, FileLoader, FileSet, SourceDatabase, VfsPath,
|
2019-01-17 05:11:00 -06:00
|
|
|
};
|
2020-08-13 09:39:16 -05:00
|
|
|
use ide_db::{
|
2020-02-06 05:52:32 -06:00
|
|
|
symbol_index::{self, FileSymbol},
|
|
|
|
LineIndexDatabase,
|
|
|
|
};
|
2020-12-17 05:17:13 -06:00
|
|
|
use syntax::SourceFile;
|
2019-01-08 13:33:36 -06:00
|
|
|
|
2020-02-06 05:52:32 -06:00
|
|
|
use crate::display::ToNav;
|
2019-01-08 13:33:36 -06:00
|
|
|
|
|
|
|
pub use crate::{
|
2019-12-30 08:12:06 -06:00
|
|
|
call_hierarchy::CallItem,
|
2020-08-18 09:22:01 -05:00
|
|
|
diagnostics::{Diagnostic, DiagnosticsConfig, Fix, Severity},
|
2020-12-17 05:29:05 -06:00
|
|
|
display::navigation_target::{NavigationTarget, SymbolKind},
|
2019-11-19 08:56:48 -06:00
|
|
|
expand_macro::ExpandedMacro,
|
2020-07-16 11:13:43 -05:00
|
|
|
file_structure::StructureNode,
|
2019-03-22 05:29:58 -05:00
|
|
|
folding_ranges::{Fold, FoldKind},
|
2020-06-10 13:24:36 -05:00
|
|
|
hover::{HoverAction, HoverConfig, HoverGotoTypeData, HoverResult},
|
2020-03-31 09:02:55 -05:00
|
|
|
inlay_hints::{InlayHint, InlayHintsConfig, InlayKind},
|
2020-07-08 15:37:35 -05:00
|
|
|
markup::Markup,
|
2020-10-06 10:58:03 -05:00
|
|
|
prime_caches::PrimeCachesProgress,
|
2020-11-02 09:31:38 -06:00
|
|
|
references::{rename::RenameError, Declaration, ReferenceSearchResult},
|
2020-02-14 17:06:14 -06:00
|
|
|
runnables::{Runnable, RunnableKind, TestId},
|
2020-02-26 12:39:32 -06:00
|
|
|
syntax_highlighting::{
|
2021-01-10 06:33:03 -06:00
|
|
|
tags::{Highlight, HlMod, HlMods, HlPunct, HlTag},
|
2021-01-09 05:48:15 -06:00
|
|
|
HlRange,
|
2020-02-26 12:39:32 -06:00
|
|
|
},
|
2019-01-08 13:33:36 -06:00
|
|
|
};
|
2021-01-06 11:43:46 -06:00
|
|
|
pub use assists::{Assist, AssistConfig, AssistId, AssistKind, InsertUseConfig};
|
2020-10-18 05:09:00 -05:00
|
|
|
pub use completion::{
|
2021-01-06 11:23:53 -06:00
|
|
|
CompletionConfig, CompletionItem, CompletionItemKind, CompletionScore, ImportEdit,
|
|
|
|
InsertTextFormat,
|
2020-10-18 05:09:00 -05:00
|
|
|
};
|
2020-10-24 03:39:57 -05:00
|
|
|
pub use hir::{Documentation, Semantics};
|
|
|
|
pub use ide_db::base_db::{
|
2020-10-02 08:45:09 -05:00
|
|
|
Canceled, Change, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRoot,
|
2020-06-11 04:04:09 -05:00
|
|
|
SourceRootId,
|
2019-07-04 15:05:17 -05:00
|
|
|
};
|
2020-12-17 05:17:13 -06:00
|
|
|
pub use ide_db::{
|
|
|
|
call_info::CallInfo,
|
2021-01-11 17:05:07 -06:00
|
|
|
search::{FileReference, ReferenceAccess, ReferenceKind},
|
2020-12-17 05:17:13 -06:00
|
|
|
};
|
2020-08-13 09:39:16 -05:00
|
|
|
pub use ide_db::{
|
2020-08-18 09:41:21 -05:00
|
|
|
label::Label,
|
2020-02-06 05:52:32 -06:00
|
|
|
line_index::{LineCol, LineIndex},
|
2020-03-04 05:46:40 -06:00
|
|
|
search::SearchScope,
|
2021-01-14 15:43:36 -06:00
|
|
|
source_change::{FileSystemEdit, SourceChange},
|
2020-02-06 05:52:32 -06:00
|
|
|
symbol_index::Query,
|
|
|
|
RootDatabase,
|
|
|
|
};
|
2020-08-13 09:45:10 -05:00
|
|
|
pub use ssr::SsrError;
|
2020-12-17 05:17:13 -06:00
|
|
|
pub use syntax::{TextRange, TextSize};
|
2020-08-12 10:03:06 -05:00
|
|
|
pub use text_edit::{Indel, TextEdit};
|
2019-01-08 13:33:36 -06:00
|
|
|
|
2019-01-15 12:02:42 -06:00
|
|
|
pub type Cancelable<T> = Result<T, Canceled>;
|
|
|
|
|
2019-09-19 16:07:23 -05:00
|
|
|
/// Info associated with a text range.
|
2019-01-08 13:33:36 -06:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RangeInfo<T> {
|
|
|
|
pub range: TextRange,
|
|
|
|
pub info: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> RangeInfo<T> {
|
2019-01-11 05:14:09 -06:00
|
|
|
pub fn new(range: TextRange, info: T) -> RangeInfo<T> {
|
2019-01-08 13:33:36 -06:00
|
|
|
RangeInfo { range, info }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `AnalysisHost` stores the current state of the world.
|
2019-06-07 12:49:29 -05:00
|
|
|
#[derive(Debug)]
|
2019-01-08 13:33:36 -06:00
|
|
|
pub struct AnalysisHost {
|
2020-02-06 05:52:32 -06:00
|
|
|
db: RootDatabase,
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AnalysisHost {
|
2020-03-10 12:56:15 -05:00
|
|
|
pub fn new(lru_capacity: Option<usize>) -> AnalysisHost {
|
|
|
|
AnalysisHost { db: RootDatabase::new(lru_capacity) }
|
2019-06-07 12:49:29 -05:00
|
|
|
}
|
2020-03-20 15:09:23 -05:00
|
|
|
|
|
|
|
pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) {
|
|
|
|
self.db.update_lru_capacity(lru_capacity);
|
|
|
|
}
|
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
/// Returns a snapshot of the current state, which you can query for
|
|
|
|
/// semantic information.
|
|
|
|
pub fn analysis(&self) -> Analysis {
|
2019-02-08 05:49:43 -06:00
|
|
|
Analysis { db: self.db.snapshot() }
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
/// Applies changes to the current state of the world. If there are
|
|
|
|
/// outstanding snapshots, they will be canceled.
|
2020-10-02 08:45:09 -05:00
|
|
|
pub fn apply_change(&mut self, change: Change) {
|
2019-01-08 13:33:36 -06:00
|
|
|
self.db.apply_change(change)
|
|
|
|
}
|
2019-01-25 10:11:58 -06:00
|
|
|
|
|
|
|
pub fn collect_garbage(&mut self) {
|
|
|
|
self.db.collect_garbage();
|
|
|
|
}
|
2019-06-30 06:40:01 -05:00
|
|
|
/// NB: this clears the database
|
2020-08-12 09:32:36 -05:00
|
|
|
pub fn per_query_memory_usage(&mut self) -> Vec<(String, profile::Bytes)> {
|
2019-06-30 06:40:01 -05:00
|
|
|
self.db.per_query_memory_usage()
|
|
|
|
}
|
2020-01-24 09:35:37 -06:00
|
|
|
pub fn request_cancellation(&mut self) {
|
|
|
|
self.db.request_cancellation();
|
|
|
|
}
|
2020-04-24 14:57:10 -05:00
|
|
|
pub fn raw_database(&self) -> &RootDatabase {
|
2019-06-15 08:29:23 -05:00
|
|
|
&self.db
|
|
|
|
}
|
2020-04-24 14:57:10 -05:00
|
|
|
pub fn raw_database_mut(&mut self) -> &mut RootDatabase {
|
2019-06-26 01:12:46 -05:00
|
|
|
&mut self.db
|
|
|
|
}
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
|
2020-05-06 08:26:40 -05:00
|
|
|
impl Default for AnalysisHost {
|
|
|
|
fn default() -> AnalysisHost {
|
|
|
|
AnalysisHost::new(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 13:33:36 -06: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 05:52:32 -06:00
|
|
|
db: salsa::Snapshot<RootDatabase>,
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
|
2019-02-16 05:43:59 -06: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 13:33:36 -06:00
|
|
|
impl Analysis {
|
2019-03-20 15:35:24 -05: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 04:04:09 -05: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 08:45:09 -05:00
|
|
|
let mut change = Change::new();
|
2020-06-11 04:04:09 -05:00
|
|
|
change.set_roots(vec![source_root]);
|
2019-03-20 15:35:24 -05:00
|
|
|
let mut crate_graph = CrateGraph::default();
|
2019-09-29 18:38:16 -05:00
|
|
|
// FIXME: cfg options
|
|
|
|
// Default to enable test for single file.
|
2019-10-08 06:22:49 -05:00
|
|
|
let mut cfg_options = CfgOptions::default();
|
|
|
|
cfg_options.insert_atom("test".into());
|
2020-03-08 08:26:57 -05:00
|
|
|
crate_graph.add_crate_root(
|
|
|
|
file_id,
|
|
|
|
Edition::Edition2018,
|
|
|
|
None,
|
|
|
|
cfg_options,
|
|
|
|
Env::default(),
|
2020-03-10 22:04:02 -05:00
|
|
|
Default::default(),
|
2020-03-08 08:26:57 -05:00
|
|
|
);
|
2020-06-11 04:04:09 -05:00
|
|
|
change.change_file(file_id, Some(Arc::new(text)));
|
2019-03-20 15:35:24 -05:00
|
|
|
change.set_crate_graph(crate_graph);
|
|
|
|
host.apply_change(change);
|
|
|
|
(host.analysis(), file_id)
|
|
|
|
}
|
|
|
|
|
2019-09-19 16:07:23 -05:00
|
|
|
/// Debug info about the current state of the analysis.
|
2020-09-29 15:05:18 -05:00
|
|
|
pub fn status(&self, file_id: Option<FileId>) -> Cancelable<String> {
|
|
|
|
self.with_db(|db| status::status(&*db, file_id))
|
2019-01-22 15:15:03 -06:00
|
|
|
}
|
|
|
|
|
2020-10-06 10:58:03 -05: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 05:42:04 -06:00
|
|
|
}
|
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
/// Gets the text of the source file.
|
2019-07-25 12:22:41 -05:00
|
|
|
pub fn file_text(&self, file_id: FileId) -> Cancelable<Arc<String>> {
|
|
|
|
self.with_db(|db| db.file_text(file_id))
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
/// Gets the syntax tree of the file.
|
2019-07-25 12:22:41 -05:00
|
|
|
pub fn parse(&self, file_id: FileId) -> Cancelable<SourceFile> {
|
|
|
|
self.with_db(|db| db.parse(file_id).tree())
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
/// Gets the file's `LineIndex`: data structure to convert between absolute
|
|
|
|
/// offsets and line/column representation.
|
2019-07-25 12:22:41 -05: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 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-02-11 10:18:27 -06:00
|
|
|
/// Selects the next syntactic nodes encompassing the range.
|
2019-01-20 12:05:01 -06:00
|
|
|
pub fn extend_selection(&self, frange: FileRange) -> Cancelable<TextRange> {
|
|
|
|
self.with_db(|db| extend_selection::extend_selection(db, frange))
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-02-11 10:18:27 -06:00
|
|
|
/// Returns position of the matching brace (all types of braces are
|
2019-01-08 13:33:36 -06:00
|
|
|
/// supported).
|
2020-04-24 16:40:41 -05:00
|
|
|
pub fn matching_brace(&self, position: FilePosition) -> Cancelable<Option<TextSize>> {
|
2019-07-25 12:22:41 -05: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 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
/// Returns a syntax tree represented as `String`, for debug purposes.
|
|
|
|
// FIXME: use a better name here.
|
2019-07-25 12:22:41 -05: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 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2020-12-28 12:29:58 -06:00
|
|
|
pub fn view_hir(&self, position: FilePosition) -> Cancelable<String> {
|
|
|
|
self.with_db(|db| view_hir::view_hir(&db, position))
|
|
|
|
}
|
|
|
|
|
2019-11-19 08:56:48 -06:00
|
|
|
pub fn expand_macro(&self, position: FilePosition) -> Cancelable<Option<ExpandedMacro>> {
|
2019-11-17 12:47:50 -06:00
|
|
|
self.with_db(|db| expand_macro::expand_macro(db, position))
|
|
|
|
}
|
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
/// Returns an edit to remove all newlines in the range, cleaning up minor
|
|
|
|
/// stuff like trailing commas.
|
2020-05-21 12:50:23 -05:00
|
|
|
pub fn join_lines(&self, frange: FileRange) -> Cancelable<TextEdit> {
|
2019-07-25 12:22:41 -05:00
|
|
|
self.with_db(|db| {
|
|
|
|
let parse = db.parse(frange.file_id);
|
2020-05-21 12:50:23 -05:00
|
|
|
join_lines::join_lines(&parse.tree(), frange.range)
|
2019-07-25 12:22:41 -05:00
|
|
|
})
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
/// Returns an edit which should be applied when opening a new line, fixing
|
|
|
|
/// up minor stuff like continuing the comment.
|
2020-05-25 07:12:53 -05:00
|
|
|
/// The edit will be a snippet (with `$0`).
|
|
|
|
pub fn on_enter(&self, position: FilePosition) -> Cancelable<Option<TextEdit>> {
|
2019-07-25 12:22:41 -05:00
|
|
|
self.with_db(|db| typing::on_enter(&db, position))
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-10-25 03:19:26 -05: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 04:04:17 -05:00
|
|
|
// Fast path to not even parse the file.
|
|
|
|
if !typing::TRIGGER_CHARS.contains(char_typed) {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
2019-10-25 03:19:26 -05:00
|
|
|
self.with_db(|db| typing::on_char_typed(&db, position, char_typed))
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
/// Returns a tree representation of symbols in the file. Useful to draw a
|
|
|
|
/// file outline.
|
2019-07-25 12:22:41 -05:00
|
|
|
pub fn file_structure(&self, file_id: FileId) -> Cancelable<Vec<StructureNode>> {
|
2020-07-16 11:13:43 -05:00
|
|
|
self.with_db(|db| file_structure::file_structure(&db.parse(file_id).tree()))
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-07-19 16:20:09 -05:00
|
|
|
/// Returns a list of the places in the file where type hints can be displayed.
|
2019-11-18 11:02:28 -06:00
|
|
|
pub fn inlay_hints(
|
|
|
|
&self,
|
|
|
|
file_id: FileId,
|
2020-03-31 09:02:55 -05:00
|
|
|
config: &InlayHintsConfig,
|
2019-11-18 11:02:28 -06:00
|
|
|
) -> Cancelable<Vec<InlayHint>> {
|
2020-03-31 09:02:55 -05:00
|
|
|
self.with_db(|db| inlay_hints::inlay_hints(db, file_id, config))
|
2019-07-19 16:20:09 -05:00
|
|
|
}
|
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
/// Returns the set of folding ranges.
|
2019-07-25 12:22:41 -05: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 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
/// Fuzzy searches for a symbol.
|
|
|
|
pub fn symbol_search(&self, query: Query) -> Cancelable<Vec<NavigationTarget>> {
|
2019-01-10 03:20:32 -06:00
|
|
|
self.with_db(|db| {
|
2019-01-15 09:19:09 -06:00
|
|
|
symbol_index::world_symbols(db, query)
|
2019-01-10 03:20:32 -06:00
|
|
|
.into_iter()
|
2019-11-11 02:15:19 -06:00
|
|
|
.map(|s| s.to_nav(db))
|
2019-01-15 09:19:09 -06:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
})
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-09-19 16:07:23 -05:00
|
|
|
/// Returns the definitions from the symbol at `position`.
|
2019-01-08 16:38:51 -06:00
|
|
|
pub fn goto_definition(
|
2019-01-08 13:33:36 -06:00
|
|
|
&self,
|
|
|
|
position: FilePosition,
|
2019-01-11 05:14:09 -06:00
|
|
|
) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
|
2019-01-20 11:55:08 -06:00
|
|
|
self.with_db(|db| goto_definition::goto_definition(db, position))
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-09-19 16:07:23 -05:00
|
|
|
/// Returns the impls from the symbol at `position`.
|
2019-01-28 08:26:32 -06:00
|
|
|
pub fn goto_implementation(
|
|
|
|
&self,
|
|
|
|
position: FilePosition,
|
|
|
|
) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
|
2020-05-30 18:54:54 -05:00
|
|
|
self.with_db(|db| goto_implementation::goto_implementation(db, position))
|
2019-01-28 08:26:32 -06:00
|
|
|
}
|
|
|
|
|
2019-09-19 16:07:23 -05:00
|
|
|
/// Returns the type definitions for the symbol at `position`.
|
2019-04-23 13:11:27 -05: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 13:33:36 -06:00
|
|
|
/// Finds all usages of the reference at point.
|
2019-02-17 05:38:32 -06:00
|
|
|
pub fn find_all_refs(
|
|
|
|
&self,
|
|
|
|
position: FilePosition,
|
2019-10-24 06:01:02 -05:00
|
|
|
search_scope: Option<SearchScope>,
|
2019-02-17 05:38:32 -06:00
|
|
|
) -> Cancelable<Option<ReferenceSearchResult>> {
|
2020-07-01 07:11:34 -05:00
|
|
|
self.with_db(|db| {
|
|
|
|
references::find_all_refs(&Semantics::new(db), position, search_scope).map(|it| it.info)
|
|
|
|
})
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2020-09-02 09:27:57 -05:00
|
|
|
/// Finds all methods and free functions for the file. Does not return tests!
|
2020-09-02 08:03:05 -05:00
|
|
|
pub fn find_all_methods(&self, file_id: FileId) -> Cancelable<Vec<FileRange>> {
|
2020-09-02 09:27:57 -05:00
|
|
|
self.with_db(|db| fn_references::find_all_methods(db, file_id))
|
2020-09-02 08:03:05 -05:00
|
|
|
}
|
|
|
|
|
2019-02-11 10:18:27 -06:00
|
|
|
/// Returns a short text describing element at position.
|
2020-09-26 00:02:09 -05:00
|
|
|
pub fn hover(
|
|
|
|
&self,
|
|
|
|
position: FilePosition,
|
|
|
|
links_in_hover: bool,
|
2020-10-05 12:27:29 -05:00
|
|
|
markdown: bool,
|
2020-09-26 00:02:09 -05:00
|
|
|
) -> Cancelable<Option<RangeInfo<HoverResult>>> {
|
2020-10-05 12:27:29 -05:00
|
|
|
self.with_db(|db| hover::hover(db, position, links_in_hover, markdown))
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2020-08-30 03:02:29 -05:00
|
|
|
/// Return URL(s) for the documentation of the symbol under the cursor.
|
2020-08-31 18:38:32 -05:00
|
|
|
pub fn external_docs(
|
2020-08-30 03:02:29 -05:00
|
|
|
&self,
|
|
|
|
position: FilePosition,
|
2020-08-31 03:38:10 -05:00
|
|
|
) -> Cancelable<Option<doc_links::DocumentationLink>> {
|
2020-09-01 03:26:10 -05:00
|
|
|
self.with_db(|db| doc_links::external_docs(db, &position))
|
2020-08-30 03:02:29 -05:00
|
|
|
}
|
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
/// Computes parameter information for the given call expression.
|
|
|
|
pub fn call_info(&self, position: FilePosition) -> Cancelable<Option<CallInfo>> {
|
2020-10-24 03:07:10 -05:00
|
|
|
self.with_db(|db| ide_db::call_info::call_info(db, position))
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-12-30 08:12:06 -06: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 13:33:36 -06:00
|
|
|
/// Returns a `mod name;` declaration which created the current module.
|
|
|
|
pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<NavigationTarget>> {
|
2019-01-15 12:02:42 -06:00
|
|
|
self.with_db(|db| parent_module::parent_module(db, position))
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
/// Returns crates this file belongs too.
|
|
|
|
pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
|
2019-02-08 04:50:18 -06:00
|
|
|
self.with_db(|db| parent_module::crate_for(db, file_id))
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-12-04 16:05:01 -06:00
|
|
|
/// Returns the edition of the given crate.
|
|
|
|
pub fn crate_edition(&self, crate_id: CrateId) -> Cancelable<Edition> {
|
2020-03-09 05:11:59 -05:00
|
|
|
self.with_db(|db| db.crate_graph()[crate_id].edition)
|
2019-12-04 16:05:01 -06:00
|
|
|
}
|
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
/// Returns the root file of the given crate.
|
|
|
|
pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> {
|
2020-03-09 05:11:59 -05:00
|
|
|
self.with_db(|db| db.crate_graph()[crate_id].root_file_id)
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-01-08 13:33:36 -06: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 11:55:08 -06:00
|
|
|
self.with_db(|db| runnables::runnables(db, file_id))
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2020-02-25 13:44:47 -06:00
|
|
|
/// Computes syntax highlighting for the given file
|
2021-01-09 05:48:15 -06:00
|
|
|
pub fn highlight(&self, file_id: FileId) -> Cancelable<Vec<HlRange>> {
|
2020-06-14 07:43:43 -05:00
|
|
|
self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false))
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2020-02-25 07:38:50 -06:00
|
|
|
/// Computes syntax highlighting for the given file range.
|
2021-01-09 05:48:15 -06:00
|
|
|
pub fn highlight_range(&self, frange: FileRange) -> Cancelable<Vec<HlRange>> {
|
2020-06-14 07:43:43 -05:00
|
|
|
self.with_db(|db| {
|
|
|
|
syntax_highlighting::highlight(db, frange.file_id, Some(frange.range), false)
|
|
|
|
})
|
2020-02-25 07:38:50 -06:00
|
|
|
}
|
|
|
|
|
2019-05-25 05:42:34 -05:00
|
|
|
/// Computes syntax highlighting for the given file.
|
2019-05-25 09:29:39 -05: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 05:42:34 -05:00
|
|
|
}
|
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
/// Computes completions at the given position.
|
2020-03-10 12:47:09 -05:00
|
|
|
pub fn completions(
|
|
|
|
&self,
|
2020-03-31 09:02:55 -05:00
|
|
|
config: &CompletionConfig,
|
2020-05-17 05:09:53 -05:00
|
|
|
position: FilePosition,
|
2020-03-10 12:47:09 -05:00
|
|
|
) -> Cancelable<Option<Vec<CompletionItem>>> {
|
2020-05-17 05:09:53 -05:00
|
|
|
self.with_db(|db| completion::completions(db, config, position).map(Into::into))
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2020-12-04 08:03:22 -06: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 06:35:49 -06:00
|
|
|
imported_name: String,
|
2020-12-04 08:03:22 -06:00
|
|
|
) -> Cancelable<Vec<TextEdit>> {
|
|
|
|
Ok(self
|
|
|
|
.with_db(|db| {
|
|
|
|
completion::resolve_completion_edits(
|
|
|
|
db,
|
|
|
|
config,
|
|
|
|
position,
|
|
|
|
full_import_path,
|
|
|
|
imported_name,
|
|
|
|
)
|
|
|
|
})?
|
|
|
|
.unwrap_or_default())
|
|
|
|
}
|
|
|
|
|
2020-12-24 06:32:29 -06:00
|
|
|
/// Computes assists (aka code actions aka intentions) for the given
|
2020-12-26 05:11:42 -06: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 15:21:48 -05:00
|
|
|
&self,
|
|
|
|
config: &AssistConfig,
|
2020-12-26 05:11:42 -06:00
|
|
|
resolve: bool,
|
2020-06-02 15:21:48 -05:00
|
|
|
frange: FileRange,
|
2020-12-26 05:11:42 -06:00
|
|
|
) -> Cancelable<Vec<Assist>> {
|
|
|
|
self.with_db(|db| Assist::get(db, config, resolve, frange))
|
2020-06-02 15:21:48 -05:00
|
|
|
}
|
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
/// Computes the set of diagnostics for the given file.
|
2020-07-24 10:39:44 -05:00
|
|
|
pub fn diagnostics(
|
|
|
|
&self,
|
2020-08-18 09:03:15 -05:00
|
|
|
config: &DiagnosticsConfig,
|
2020-07-24 10:39:44 -05:00
|
|
|
file_id: FileId,
|
|
|
|
) -> Cancelable<Vec<Diagnostic>> {
|
2020-08-18 09:03:15 -05:00
|
|
|
self.with_db(|db| diagnostics::diagnostics(db, config, file_id))
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-08 17:47:12 -06:00
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
/// Returns the edit required to rename reference at the position to the new
|
|
|
|
/// name.
|
|
|
|
pub fn rename(
|
|
|
|
&self,
|
|
|
|
position: FilePosition,
|
|
|
|
new_name: &str,
|
2020-08-24 09:37:45 -05:00
|
|
|
) -> Cancelable<Result<RangeInfo<SourceChange>, RenameError>> {
|
2020-11-02 09:31:38 -06:00
|
|
|
self.with_db(|db| references::rename::rename(db, position, new_name))
|
2019-01-10 03:20:32 -06:00
|
|
|
}
|
|
|
|
|
2020-12-16 14:35:15 -06: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 13:19:51 -06: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 16:45:38 -06:00
|
|
|
pub fn structural_search_replace(
|
|
|
|
&self,
|
|
|
|
query: &str,
|
2020-03-15 16:23:18 -05:00
|
|
|
parse_only: bool,
|
2020-08-13 09:45:10 -05:00
|
|
|
resolve_context: FilePosition,
|
2020-07-28 20:44:01 -05:00
|
|
|
selections: Vec<FileRange>,
|
2020-02-10 16:45:38 -06:00
|
|
|
) -> Cancelable<Result<SourceChange, SsrError>> {
|
|
|
|
self.with_db(|db| {
|
2020-08-13 09:45:10 -05:00
|
|
|
let rule: ssr::SsrRule = query.parse()?;
|
|
|
|
let mut match_finder = ssr::MatchFinder::in_context(db, resolve_context, selections);
|
|
|
|
match_finder.add_rule(rule)?;
|
2021-01-14 11:35:22 -06:00
|
|
|
let edits = if parse_only { Default::default() } else { match_finder.edits() };
|
2020-06-08 14:44:42 -05:00
|
|
|
Ok(SourceChange::from(edits))
|
2020-02-10 16:45:38 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-19 16:07:23 -05:00
|
|
|
/// Performs an operation on that may be Canceled.
|
2020-08-13 09:45:10 -05:00
|
|
|
fn with_db<F, T>(&self, f: F) -> Cancelable<T>
|
|
|
|
where
|
|
|
|
F: FnOnce(&RootDatabase) -> T + std::panic::UnwindSafe,
|
|
|
|
{
|
2019-01-10 03:20:32 -06:00
|
|
|
self.db.catch_canceled(f)
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn analysis_is_send() {
|
|
|
|
fn is_send<T: Send>() {}
|
|
|
|
is_send::<Analysis>();
|
|
|
|
}
|