rust/crates/ra_analysis/src/lib.rs

399 lines
11 KiB
Rust
Raw Normal View History

2018-11-01 06:31:25 -05:00
//! ra_analyzer crate is the brain of Rust analyzer. It relies on the `salsa`
2018-11-11 12:28:55 -06:00
//! crate, which provides and incremental on-demand database of facts.
2018-11-01 06:31:25 -05:00
2018-11-27 19:09:44 -06:00
macro_rules! ctry {
($expr:expr) => {
match $expr {
None => return Ok(None),
Some(it) => it,
}
};
}
mod db;
mod imp;
mod completion;
mod symbol_index;
pub mod mock_analysis;
2018-08-10 13:13:39 -05:00
2018-10-31 15:41:43 -05:00
use std::{fmt, sync::Arc};
2018-08-30 08:27:09 -05:00
2018-12-19 03:20:54 -06:00
use rustc_hash::FxHashMap;
use ra_syntax::{SourceFileNode, TextRange, TextUnit};
2018-12-21 02:24:16 -06:00
use ra_text_edit::TextEdit;
use rayon::prelude::*;
2018-10-31 15:41:43 -05:00
use relative_path::RelativePathBuf;
2018-08-30 05:12:49 -05:00
use crate::{
imp::{AnalysisHostImpl, AnalysisImpl},
symbol_index::SymbolIndex,
};
2018-10-15 12:15:53 -05:00
pub use crate::{
completion::CompletionItem,
};
pub use ra_editor::{
2018-10-31 15:41:43 -05:00
FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode,
2018-10-15 12:15:53 -05:00
};
2018-11-27 19:09:44 -06:00
pub use hir::FnSignatureInfo;
2018-08-10 07:07:43 -05:00
pub use ra_db::{
2018-11-27 18:42:26 -06:00
Canceled, Cancelable, FilePosition,
2018-12-19 07:13:16 -06:00
CrateGraph, CrateId, SourceRootId, FileId
};
2018-10-20 13:52:49 -05:00
2018-10-25 08:03:49 -05:00
#[derive(Default)]
pub struct AnalysisChange {
2018-12-19 07:19:53 -06:00
new_roots: Vec<(SourceRootId, bool)>,
2018-12-19 03:20:54 -06:00
roots_changed: FxHashMap<SourceRootId, RootChange>,
2018-12-19 06:04:15 -06:00
files_changed: Vec<(FileId, Arc<String>)>,
libraries_added: Vec<LibraryData>,
crate_graph: Option<CrateGraph>,
2018-12-19 03:20:54 -06:00
}
#[derive(Default)]
struct RootChange {
added: Vec<AddFile>,
removed: Vec<RemoveFile>,
}
#[derive(Debug)]
struct AddFile {
file_id: FileId,
path: RelativePathBuf,
text: Arc<String>,
}
#[derive(Debug)]
struct RemoveFile {
file_id: FileId,
path: RelativePathBuf,
}
2018-10-25 08:03:49 -05:00
impl fmt::Debug for AnalysisChange {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("AnalysisChange")
2018-12-19 06:40:42 -06:00
.field("new_roots", &self.new_roots)
2018-12-19 03:20:54 -06:00
.field("roots_changed", &self.roots_changed)
2018-10-25 08:03:49 -05:00
.field("files_changed", &self.files_changed.len())
.field("libraries_added", &self.libraries_added.len())
.field("crate_graph", &self.crate_graph)
2018-12-19 03:20:54 -06:00
.finish()
}
}
impl fmt::Debug for RootChange {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("AnalysisChange")
.field("added", &self.added.len())
.field("removed", &self.removed.len())
2018-10-25 08:03:49 -05:00
.finish()
}
}
impl AnalysisChange {
pub fn new() -> AnalysisChange {
AnalysisChange::default()
}
2018-12-19 07:19:53 -06:00
pub fn add_root(&mut self, root_id: SourceRootId, is_local: bool) {
self.new_roots.push((root_id, is_local));
2018-12-19 06:04:15 -06:00
}
2018-12-19 03:20:54 -06:00
pub fn add_file(
&mut self,
root_id: SourceRootId,
file_id: FileId,
path: RelativePathBuf,
text: Arc<String>,
) {
let file = AddFile {
file_id,
path,
text,
};
self.roots_changed
.entry(root_id)
.or_default()
.added
.push(file);
}
2018-12-19 06:04:15 -06:00
pub fn change_file(&mut self, file_id: FileId, new_text: Arc<String>) {
self.files_changed.push((file_id, new_text))
}
2018-12-19 03:20:54 -06:00
pub fn remove_file(&mut self, root_id: SourceRootId, file_id: FileId, path: RelativePathBuf) {
let file = RemoveFile { file_id, path };
self.roots_changed
.entry(root_id)
.or_default()
.removed
.push(file);
}
pub fn add_library(&mut self, data: LibraryData) {
self.libraries_added.push(data)
}
pub fn set_crate_graph(&mut self, graph: CrateGraph) {
self.crate_graph = Some(graph);
}
}
2018-11-01 06:31:25 -05:00
/// `AnalysisHost` stores the current state of the world.
2018-11-04 05:09:21 -06:00
#[derive(Debug, Default)]
2018-08-30 05:12:49 -05:00
pub struct AnalysisHost {
imp: AnalysisHostImpl,
2018-08-30 05:12:49 -05:00
}
impl AnalysisHost {
2018-11-01 06:31:25 -05:00
/// Returns a snapshot of the current state, which you can query for
/// semantic information.
2018-09-10 04:57:40 -05:00
pub fn analysis(&self) -> Analysis {
Analysis {
imp: self.imp.analysis(),
}
2018-08-30 05:12:49 -05:00
}
2018-11-01 06:31:25 -05: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: AnalysisChange) {
self.imp.apply_change(change)
2018-09-03 11:46:30 -05:00
}
2018-08-30 05:12:49 -05:00
}
#[derive(Debug)]
pub struct SourceChange {
pub label: String,
2018-12-21 02:15:23 -06:00
pub source_file_edits: Vec<SourceFileEdit>,
2018-08-30 05:12:49 -05:00
pub file_system_edits: Vec<FileSystemEdit>,
2018-11-05 05:24:38 -06:00
pub cursor_position: Option<FilePosition>,
2018-08-30 05:12:49 -05:00
}
#[derive(Debug)]
2018-12-21 02:15:23 -06:00
pub struct SourceFileEdit {
2018-08-30 05:12:49 -05:00
pub file_id: FileId,
2018-12-21 02:24:16 -06:00
pub edit: TextEdit,
2018-08-30 05:12:49 -05:00
}
#[derive(Debug)]
pub enum FileSystemEdit {
CreateFile {
2018-12-21 03:18:14 -06:00
source_root: SourceRootId,
2018-08-30 05:12:49 -05:00
path: RelativePathBuf,
},
MoveFile {
2018-12-21 03:18:14 -06:00
src: FileId,
dst_source_root: SourceRootId,
dst_path: RelativePathBuf,
},
2018-08-30 05:12:49 -05:00
}
#[derive(Debug)]
pub struct Diagnostic {
pub message: String,
pub range: TextRange,
pub fix: Option<SourceChange>,
}
#[derive(Debug)]
pub struct Query {
query: String,
lowercased: String,
only_types: bool,
2018-09-03 11:46:30 -05:00
libs: bool,
2018-08-30 05:12:49 -05:00
exact: bool,
limit: usize,
}
impl Query {
pub fn new(query: String) -> Query {
let lowercased = query.to_lowercase();
Query {
query,
lowercased,
only_types: false,
2018-09-03 11:46:30 -05:00
libs: false,
2018-08-30 05:12:49 -05:00
exact: false,
limit: usize::max_value(),
2018-08-30 05:12:49 -05:00
}
}
pub fn only_types(&mut self) {
self.only_types = true;
}
2018-09-03 11:46:30 -05:00
pub fn libs(&mut self) {
self.libs = true;
}
2018-08-30 05:12:49 -05:00
pub fn exact(&mut self) {
self.exact = true;
}
pub fn limit(&mut self, limit: usize) {
self.limit = limit
}
}
/// Result of "goto def" query.
#[derive(Debug)]
pub struct ReferenceResolution {
/// The range of the reference itself. Client does not know what constitutes
/// a reference, it handles us only the offset. It's helpful to tell the
/// client where the reference was.
pub reference_range: TextRange,
/// What this reference resolves to.
pub resolves_to: Vec<(FileId, FileSymbol)>,
}
impl ReferenceResolution {
fn new(reference_range: TextRange) -> ReferenceResolution {
ReferenceResolution {
reference_range,
resolves_to: Vec::new(),
}
}
fn add_resolution(&mut self, file_id: FileId, symbol: FileSymbol) {
self.resolves_to.push((file_id, symbol))
}
}
2018-11-01 06:31:25 -05: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)`).
2018-10-15 14:36:08 -05:00
#[derive(Debug)]
2018-08-30 05:12:49 -05:00
pub struct Analysis {
pub(crate) imp: AnalysisImpl,
2018-08-30 05:12:49 -05:00
}
impl Analysis {
2018-11-07 09:32:33 -06:00
pub fn file_syntax(&self, file_id: FileId) -> SourceFileNode {
2018-09-02 12:08:58 -05:00
self.imp.file_syntax(file_id).clone()
2018-08-30 05:12:49 -05:00
}
2018-09-15 15:19:41 -05:00
pub fn file_line_index(&self, file_id: FileId) -> Arc<LineIndex> {
self.imp.file_line_index(file_id)
2018-08-30 05:12:49 -05:00
}
2018-11-07 09:32:33 -06:00
pub fn extend_selection(&self, file: &SourceFileNode, range: TextRange) -> TextRange {
2018-09-16 04:54:24 -05:00
ra_editor::extend_selection(file, range).unwrap_or(range)
2018-08-30 05:12:49 -05:00
}
2018-11-07 09:32:33 -06:00
pub fn matching_brace(&self, file: &SourceFileNode, offset: TextUnit) -> Option<TextUnit> {
2018-09-16 04:54:24 -05:00
ra_editor::matching_brace(file, offset)
2018-08-30 05:12:49 -05:00
}
pub fn syntax_tree(&self, file_id: FileId) -> String {
let file = self.imp.file_syntax(file_id);
2018-09-16 04:54:24 -05:00
ra_editor::syntax_tree(&file)
2018-08-30 05:12:49 -05:00
}
pub fn join_lines(&self, file_id: FileId, range: TextRange) -> SourceChange {
let file = self.imp.file_syntax(file_id);
2018-09-16 04:54:24 -05:00
SourceChange::from_local_edit(file_id, "join lines", ra_editor::join_lines(&file, range))
2018-08-30 05:12:49 -05:00
}
2018-11-05 05:57:41 -06:00
pub fn on_enter(&self, position: FilePosition) -> Option<SourceChange> {
let file = self.imp.file_syntax(position.file_id);
let edit = ra_editor::on_enter(&file, position.offset)?;
let res = SourceChange::from_local_edit(position.file_id, "on enter", edit);
Some(res)
}
2018-11-05 05:57:41 -06:00
pub fn on_eq_typed(&self, position: FilePosition) -> Option<SourceChange> {
let file = self.imp.file_syntax(position.file_id);
Some(SourceChange::from_local_edit(
2018-11-05 05:57:41 -06:00
position.file_id,
"add semicolon",
2018-11-05 05:57:41 -06:00
ra_editor::on_eq_typed(&file, position.offset)?,
))
2018-08-30 05:12:49 -05:00
}
pub fn file_structure(&self, file_id: FileId) -> Vec<StructureNode> {
let file = self.imp.file_syntax(file_id);
2018-09-16 04:54:24 -05:00
ra_editor::file_structure(&file)
2018-08-30 05:12:49 -05:00
}
2018-10-20 14:09:12 -05:00
pub fn folding_ranges(&self, file_id: FileId) -> Vec<Fold> {
let file = self.imp.file_syntax(file_id);
ra_editor::folding_ranges(&file)
}
pub fn symbol_search(&self, query: Query) -> Cancelable<Vec<(FileId, FileSymbol)>> {
2018-10-20 14:29:26 -05:00
self.imp.world_symbols(query)
2018-08-30 05:12:49 -05:00
}
pub fn approximately_resolve_symbol(
&self,
2018-11-05 05:57:41 -06:00
position: FilePosition,
) -> Cancelable<Option<ReferenceResolution>> {
2018-11-05 05:57:41 -06:00
self.imp.approximately_resolve_symbol(position)
2018-08-30 05:12:49 -05:00
}
2018-11-05 05:57:41 -06:00
pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
2018-12-04 14:44:00 -06:00
self.imp.find_all_refs(position)
}
2018-11-05 15:37:27 -06:00
pub fn doc_comment_for(
&self,
file_id: FileId,
2018-11-07 08:13:16 -06:00
symbol: FileSymbol,
2018-11-05 15:37:27 -06:00
) -> Cancelable<Option<String>> {
self.imp.doc_comment_for(file_id, symbol)
}
pub fn doc_text_for(&self, file_id: FileId, symbol: FileSymbol) -> Cancelable<Option<String>> {
self.imp.doc_text_for(file_id, symbol)
2018-11-05 15:37:27 -06:00
}
2018-11-05 05:57:41 -06:00
pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<(FileId, FileSymbol)>> {
self.imp.parent_module(position)
2018-08-30 05:12:49 -05:00
}
2018-10-20 14:02:41 -05:00
pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
2018-10-20 14:15:03 -05:00
self.imp.crate_for(file_id)
2018-09-02 08:36:03 -05:00
}
2018-10-20 14:02:41 -05:00
pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> {
Ok(self.imp.crate_root(crate_id))
2018-08-31 11:14:08 -05:00
}
2018-10-20 14:02:41 -05:00
pub fn runnables(&self, file_id: FileId) -> Cancelable<Vec<Runnable>> {
let file = self.imp.file_syntax(file_id);
2018-10-20 14:02:41 -05:00
Ok(ra_editor::runnables(&file))
2018-08-30 05:12:49 -05:00
}
2018-10-20 14:02:41 -05:00
pub fn highlight(&self, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> {
let file = self.imp.file_syntax(file_id);
2018-10-20 14:02:41 -05:00
Ok(ra_editor::highlight(&file))
2018-08-30 05:12:49 -05:00
}
2018-11-05 05:57:41 -06:00
pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> {
self.imp.completions(position)
2018-08-30 05:12:49 -05:00
}
2018-10-20 14:02:41 -05:00
pub fn assists(&self, file_id: FileId, range: TextRange) -> Cancelable<Vec<SourceChange>> {
Ok(self.imp.assists(file_id, range))
2018-08-30 05:12:49 -05:00
}
2018-10-20 14:02:41 -05:00
pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
2018-10-20 14:15:03 -05:00
self.imp.diagnostics(file_id)
2018-08-30 05:12:49 -05:00
}
pub fn resolve_callable(
&self,
2018-11-05 05:57:41 -06:00
position: FilePosition,
) -> Cancelable<Option<(FnSignatureInfo, Option<usize>)>> {
2018-11-05 05:57:41 -06:00
self.imp.resolve_callable(position)
}
2018-08-30 05:12:49 -05:00
}
2018-09-03 13:26:59 -05:00
#[derive(Debug)]
pub struct LibraryData {
2018-12-19 03:20:54 -06:00
root_id: SourceRootId,
root_change: RootChange,
symbol_index: SymbolIndex,
2018-09-03 13:26:59 -05:00
}
impl LibraryData {
2018-12-19 03:20:54 -06:00
pub fn prepare(
root_id: SourceRootId,
files: Vec<(FileId, RelativePathBuf, Arc<String>)>,
) -> LibraryData {
let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, _, text)| {
2018-11-07 09:32:33 -06:00
let file = SourceFileNode::parse(text);
(*file_id, file)
}));
2018-12-19 03:20:54 -06:00
let mut root_change = RootChange::default();
root_change.added = files
.into_iter()
.map(|(file_id, path, text)| AddFile {
file_id,
path,
text,
})
.collect();
2018-10-31 15:41:43 -05:00
LibraryData {
2018-12-19 03:20:54 -06:00
root_id,
root_change,
2018-10-31 15:41:43 -05:00
symbol_index,
}
2018-09-03 13:26:59 -05:00
}
}
2018-10-15 14:29:24 -05:00
#[test]
fn analysis_is_send() {
fn is_send<T: Send>() {}
is_send::<Analysis>();
}