rust/crates/ide/src/lib.rs

653 lines
22 KiB
Rust
Raw Normal View History

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;
2021-02-13 05:07:47 -06:00
mod annotations;
2020-07-16 11:13:43 -05:00
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;
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;
2021-03-16 07:37:00 -05:00
mod move_item;
2019-01-11 09:17:20 -06:00
mod parent_module;
2019-02-08 04:51:22 -06:00
mod references;
mod fn_references;
2020-07-16 11:13:43 -05:00
mod runnables;
2021-03-08 23:11:28 -06:00
mod ssr;
2020-07-16 11:13:43 -05:00
mod status;
mod syntax_highlighting;
mod syntax_tree;
2019-03-23 06:07:21 -05:00
mod typing;
mod markdown_remove;
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;
2021-03-08 23:11:28 -06:00
2020-10-24 03:39:57 -05:00
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,
};
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::{
2021-02-13 05:07:47 -06:00
annotations::{Annotation, AnnotationConfig, AnnotationKind},
call_hierarchy::CallItem,
2021-04-12 09:58:01 -05:00
diagnostics::{Diagnostic, DiagnosticsConfig, Severity},
2021-01-20 08:25:34 -06:00
display::navigation_target::NavigationTarget,
2019-11-19 08:56:48 -06:00
expand_macro::ExpandedMacro,
file_structure::{StructureNode, StructureNodeKind},
2019-03-22 05:29:58 -05:00
folding_ranges::{Fold, FoldKind},
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,
2021-03-16 07:37:00 -05:00
move_item::Direction,
prime_caches::PrimeCachesProgress,
references::{rename::RenameError, ReferenceSearchResult},
runnables::{Runnable, RunnableKind, TestId},
2020-02-26 12:39:32 -06:00
syntax_highlighting::{
tags::{Highlight, HlMod, HlMods, HlOperator, 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
};
pub use hir::{Documentation, Semantics};
2021-05-03 10:16:35 -05:00
pub use ide_assists::{
Assist, AssistConfig, AssistId, AssistKind, AssistResolveStrategy, SingleResolve,
};
pub use ide_completion::{
2021-03-11 21:13:27 -06:00
CompletionConfig, CompletionItem, CompletionItemKind, CompletionRelevance, ImportEdit,
InsertTextFormat,
};
pub use ide_db::{
2021-01-18 07:09:09 -06:00
base_db::{
Canceled, Change, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange,
SourceRoot, SourceRootId,
},
call_info::CallInfo,
2020-08-18 09:41:21 -05:00
label::Label,
line_index::{LineCol, LineColUtf16, LineIndex},
search::{ReferenceAccess, SearchScope},
source_change::{FileSystemEdit, SourceChange},
2020-02-06 05:52:32 -06:00
symbol_index::Query,
RootDatabase, SymbolKind,
2020-02-06 05:52:32 -06:00
};
2021-02-22 13:14:58 -06:00
pub use ide_ssr::SsrError;
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
}
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 13:33:36 -06:00
/// Applies changes to the current state of the world. If there are
/// outstanding snapshots, they will be canceled.
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();
}
pub fn raw_database(&self) -> &RootDatabase {
2019-06-15 08:29:23 -05:00
&self.db
}
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);
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();
// FIXME: cfg options
// Default to enable test for single file.
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.
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
}
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 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
}
/// 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 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-02-11 10:18:27 -06:00
/// Selects the next syntactic nodes encompassing the range.
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-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 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
}
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 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
}
/// 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);
}
self.with_db(|db| typing::on_char_typed(&db, position, char_typed))
2019-01-08 13:33:36 -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-07-19 16:20:09 -05:00
/// Returns a list of the places in the file where type hints can be displayed.
pub fn inlay_hints(
&self,
file_id: FileId,
2020-03-31 09:02:55 -05:00
config: &InlayHintsConfig,
) -> 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 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-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-09-19 16:07:23 -05:00
/// Returns the impls from the symbol at `position`.
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-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.
pub fn find_all_refs(
&self,
position: FilePosition,
search_scope: Option<SearchScope>,
) -> Cancelable<Option<ReferenceSearchResult>> {
self.with_db(|db| references::find_all_refs(&Semantics::new(db), position, search_scope))
2019-01-08 13:33:36 -06: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>> {
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.
pub fn hover(
&self,
position: FilePosition,
links_in_hover: bool,
markdown: bool,
) -> Cancelable<Option<RangeInfo<HoverResult>>> {
self.with_db(|db| hover::hover(db, position, links_in_hover, markdown))
2019-01-08 13:33:36 -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,
) -> 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
}
/// 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 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
}
/// 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-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 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
}
2021-02-27 08:59:52 -06: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))
}
/// 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>> {
self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false))
2019-01-08 13:33:36 -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>> {
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.
pub fn completions(
&self,
2020-03-31 09:02:55 -05:00
config: &CompletionConfig,
2020-05-17 05:09:53 -05:00
position: FilePosition,
) -> Cancelable<Option<Vec<CompletionItem>>> {
self.with_db(|db| ide_completion::completions(db, config, position).map(Into::into))
2019-01-08 13:33:36 -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| {
ide_completion::resolve_completion_edits(
2020-12-04 08:03:22 -06:00
db,
config,
position,
full_import_path,
imported_name,
)
})?
.unwrap_or_default())
}
/// Computes assists (aka code actions aka intentions) for the given
/// 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(
&self,
config: &AssistConfig,
2021-05-03 09:08:09 -05:00
resolve: AssistResolveStrategy,
frange: FileRange,
) -> Cancelable<Vec<Assist>> {
2021-03-08 23:11:28 -06:00
self.with_db(|db| {
2021-05-03 10:03:28 -05:00
let ssr_assists = ssr::ssr_assists(db, &resolve, frange);
2021-03-08 23:11:28 -06:00
let mut acc = Assist::get(db, config, resolve, frange);
2021-05-03 09:08:09 -05:00
acc.extend(ssr_assists.into_iter());
2021-03-08 23:11:28 -06:00
acc
})
}
2019-01-08 13:33:36 -06:00
/// Computes the set of diagnostics for the given file.
pub fn diagnostics(
&self,
config: &DiagnosticsConfig,
2021-05-03 09:08:09 -05:00
resolve: AssistResolveStrategy,
file_id: FileId,
) -> Cancelable<Vec<Diagnostic>> {
2021-05-03 10:03:28 -05:00
self.with_db(|db| diagnostics::diagnostics(db, config, &resolve, file_id))
2019-01-08 13:33:36 -06: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 09:08:09 -05:00
resolve: AssistResolveStrategy,
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 10:03:28 -05:00
let ssr_assists = ssr::ssr_assists(db, &resolve, frange);
2021-05-03 09:08:09 -05:00
let diagnostic_assists = if include_fixes {
2021-05-03 10:03:28 -05:00
diagnostics::diagnostics(db, diagnostics_config, &resolve, frange.file_id)
2021-05-03 09:08:09 -05:00
.into_iter()
.filter_map(|it| it.fix)
.filter(|it| it.target.intersect(frange.range).is_some())
.collect()
} else {
Vec::new()
};
let mut res = Assist::get(db, assist_config, resolve, frange);
2021-05-03 09:08:09 -05:00
res.extend(ssr_assists.into_iter());
res.extend(diagnostic_assists.into_iter());
res
})
}
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,
) -> Cancelable<Result<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))
}
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))
}
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,
selections: Vec<FileRange>,
) -> Cancelable<Result<SourceChange, SsrError>> {
self.with_db(|db| {
2021-02-22 13:14:58 -06:00
let rule: ide_ssr::SsrRule = query.parse()?;
let mut match_finder =
ide_ssr::MatchFinder::in_context(db, resolve_context, selections);
2020-08-13 09:45:10 -05:00
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))
})
}
2021-02-13 05:07:47 -06: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 07:37:00 -05: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 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>();
}