rust/crates/ra_analysis/src/lib.rs

339 lines
9.9 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
extern crate fst;
2018-09-16 04:54:24 -05:00
extern crate ra_editor;
extern crate ra_syntax;
2018-08-13 11:28:34 -05:00
extern crate rayon;
2018-08-28 10:22:52 -05:00
extern crate relative_path;
extern crate rustc_hash;
extern crate salsa;
2018-08-13 07:10:20 -05:00
2018-11-27 09:45:16 -06:00
macro_rules! ctry {
($expr:expr) => {
match $expr {
None => return Ok(None),
Some(it) => it,
}
};
}
2018-11-25 10:02:14 -06:00
mod arena;
2018-11-27 12:45:42 -06:00
mod syntax_ptr;
mod input;
mod db;
2018-11-18 06:44:24 -06:00
mod loc2id;
mod imp;
mod completion;
2018-11-27 12:45:42 -06:00
mod hir;
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-11-07 09:32:33 -06:00
use ra_syntax::{AtomEdit, SourceFileNode, TextRange, TextUnit};
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, FileResolverImp},
symbol_index::SymbolIndex,
};
2018-10-15 12:15:53 -05:00
pub use crate::{
completion::CompletionItem,
2018-11-27 14:40:23 -06:00
hir::FnDescriptor,
2018-10-31 15:41:43 -05:00
input::{CrateGraph, CrateId, FileId, FileResolver},
};
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-08-10 07:07:43 -05:00
2018-10-20 14:15:03 -05:00
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
2018-10-20 14:35:55 -05:00
pub struct Canceled;
2018-10-20 13:46:17 -05:00
2018-10-20 14:35:55 -05:00
pub type Cancelable<T> = Result<T, Canceled>;
2018-10-20 13:46:17 -05:00
2018-10-20 14:35:55 -05:00
impl std::fmt::Display for Canceled {
2018-10-20 13:52:49 -05:00
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fmt.write_str("Canceled")
}
}
2018-10-31 15:41:43 -05:00
impl std::error::Error for Canceled {}
2018-10-20 13:52:49 -05:00
2018-10-25 08:03:49 -05:00
#[derive(Default)]
pub struct AnalysisChange {
files_added: Vec<(FileId, String)>,
files_changed: Vec<(FileId, String)>,
files_removed: Vec<(FileId)>,
libraries_added: Vec<LibraryData>,
crate_graph: Option<CrateGraph>,
file_resolver: Option<FileResolverImp>,
}
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")
.field("files_added", &self.files_added.len())
.field("files_changed", &self.files_changed.len())
.field("files_removed", &self.files_removed.len())
.field("libraries_added", &self.libraries_added.len())
.field("crate_graph", &self.crate_graph)
.field("file_resolver", &self.file_resolver)
.finish()
}
}
impl AnalysisChange {
pub fn new() -> AnalysisChange {
AnalysisChange::default()
}
pub fn add_file(&mut self, file_id: FileId, text: String) {
self.files_added.push((file_id, text))
}
pub fn change_file(&mut self, file_id: FileId, new_text: String) {
self.files_changed.push((file_id, new_text))
}
pub fn remove_file(&mut self, file_id: FileId) {
self.files_removed.push(file_id)
}
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);
}
pub fn set_file_resolver(&mut self, file_resolver: Arc<FileResolver>) {
self.file_resolver = Some(FileResolverImp::new(file_resolver));
}
}
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
}
2018-11-05 05:57:41 -06:00
#[derive(Clone, Copy, Debug)]
2018-11-05 05:24:38 -06:00
pub struct FilePosition {
pub file_id: FileId,
pub offset: TextUnit,
}
2018-08-30 05:12:49 -05:00
#[derive(Debug)]
pub struct SourceChange {
pub label: String,
2018-11-07 09:32:33 -06:00
pub source_file_edits: Vec<SourceFileNodeEdit>,
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-11-07 09:32:33 -06:00
pub struct SourceFileNodeEdit {
2018-08-30 05:12:49 -05:00
pub file_id: FileId,
pub edits: Vec<AtomEdit>,
}
#[derive(Debug)]
pub enum FileSystemEdit {
CreateFile {
anchor: FileId,
path: RelativePathBuf,
},
MoveFile {
file: FileId,
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
}
}
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,
2018-10-20 14:09:12 -05:00
) -> Cancelable<Vec<(FileId, FileSymbol)>> {
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)>> {
Ok(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)
}
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,
2018-10-20 14:09:12 -05:00
) -> Cancelable<Option<(FnDescriptor, 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 {
files: Vec<(FileId, String)>,
file_resolver: FileResolverImp,
symbol_index: SymbolIndex,
2018-09-03 13:26:59 -05:00
}
impl LibraryData {
2018-09-10 04:57:40 -05:00
pub fn prepare(files: Vec<(FileId, String)>, file_resolver: Arc<FileResolver>) -> 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-10-31 15:41:43 -05:00
LibraryData {
files,
file_resolver: FileResolverImp::new(file_resolver),
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>();
}