mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-02 19:53:46 +00:00
Store all the data in the Salsa Database
This commit is contained in:
parent
2cb2074c4b
commit
ee4d904cfb
@ -6,13 +6,14 @@ use ra_syntax::{
|
||||
|
||||
use crate::{
|
||||
FileId, Cancelable,
|
||||
db::{self, SyntaxDatabase},
|
||||
db::{self, SyntaxDatabase, input::FilesDatabase},
|
||||
descriptors::module::{ModulesDatabase, ModuleTree, ModuleId},
|
||||
};
|
||||
|
||||
pub(crate) fn resolve_based_completion(db: &db::RootDatabase, file_id: FileId, offset: TextUnit) -> Cancelable<Option<Vec<CompletionItem>>> {
|
||||
let source_root_id = db.file_source_root(file_id);
|
||||
let file = db.file_syntax(file_id);
|
||||
let module_tree = db.module_tree()?;
|
||||
let module_tree = db.module_tree(source_root_id)?;
|
||||
let file = {
|
||||
let edit = AtomEdit::insert(offset, "intellijRulezz".to_string());
|
||||
file.reparse(&edit)
|
||||
|
83
crates/ra_analysis/src/db/input.rs
Normal file
83
crates/ra_analysis/src/db/input.rs
Normal file
@ -0,0 +1,83 @@
|
||||
use std::{
|
||||
sync::Arc,
|
||||
hash::{Hasher, Hash},
|
||||
};
|
||||
|
||||
use salsa;
|
||||
use rustc_hash::FxHashSet;
|
||||
|
||||
use crate::{FileId, FileResolverImp, CrateGraph, symbol_index::SymbolIndex};
|
||||
|
||||
salsa::query_group! {
|
||||
pub(crate) trait FilesDatabase: salsa::Database {
|
||||
fn file_text(file_id: FileId) -> Arc<String> {
|
||||
type FileTextQuery;
|
||||
storage input;
|
||||
}
|
||||
fn file_source_root(file_id: FileId) -> SourceRootId {
|
||||
type FileSourceRootQuery;
|
||||
storage input;
|
||||
}
|
||||
fn source_root(id: SourceRootId) -> Arc<SourceRoot> {
|
||||
type SourceRootQuery;
|
||||
storage input;
|
||||
}
|
||||
fn libraries() -> Arc<Vec<SourceRootId>> {
|
||||
type LibrarieseQuery;
|
||||
storage input;
|
||||
}
|
||||
fn library_symbols(id: SourceRootId) -> Arc<SymbolIndex> {
|
||||
type LibrarySymbolsQuery;
|
||||
storage input;
|
||||
}
|
||||
fn crate_graph() -> Arc<CrateGraph> {
|
||||
type CrateGraphQuery;
|
||||
storage input;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub(crate) struct SourceRootId(pub(crate) u32);
|
||||
|
||||
#[derive(Clone, Default, Debug, Eq)]
|
||||
pub(crate) struct SourceRoot {
|
||||
pub(crate) file_resolver: FileResolverImp,
|
||||
pub(crate) files: FxHashSet<FileId>,
|
||||
}
|
||||
|
||||
impl PartialEq for SourceRoot {
|
||||
fn eq(&self, other: &SourceRoot) -> bool {
|
||||
self.file_resolver == other.file_resolver
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for SourceRoot {
|
||||
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
||||
self.file_resolver.hash(hasher);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) const WORKSPACE: SourceRootId = SourceRootId(0);
|
||||
|
||||
|
||||
#[derive(Default, Debug, Eq)]
|
||||
pub(crate) struct FileSet {
|
||||
pub(crate) files: FxHashSet<FileId>,
|
||||
pub(crate) resolver: FileResolverImp,
|
||||
}
|
||||
|
||||
impl PartialEq for FileSet {
|
||||
fn eq(&self, other: &FileSet) -> bool {
|
||||
self.files == other.files && self.resolver == other.resolver
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for FileSet {
|
||||
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
||||
let mut files = self.files.iter().cloned().collect::<Vec<_>>();
|
||||
files.sort();
|
||||
files.hash(hasher);
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
pub(crate) mod input;
|
||||
|
||||
use std::{
|
||||
fmt,
|
||||
hash::{Hash, Hasher},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use ra_editor::LineIndex;
|
||||
use ra_syntax::File;
|
||||
use rustc_hash::FxHashSet;
|
||||
use salsa;
|
||||
|
||||
use crate::{
|
||||
@ -14,7 +14,7 @@ use crate::{
|
||||
Cancelable, Canceled,
|
||||
descriptors::module::{SubmodulesQuery, ModuleTreeQuery, ModulesDatabase},
|
||||
symbol_index::SymbolIndex,
|
||||
FileId, FileResolverImp,
|
||||
FileId,
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
@ -58,9 +58,13 @@ impl Clone for RootDatabase {
|
||||
|
||||
salsa::database_storage! {
|
||||
pub(crate) struct RootDatabaseStorage for RootDatabase {
|
||||
impl FilesDatabase {
|
||||
fn file_text() for FileTextQuery;
|
||||
fn file_set() for FileSetQuery;
|
||||
impl input::FilesDatabase {
|
||||
fn file_text() for input::FileTextQuery;
|
||||
fn file_source_root() for input::FileSourceRootQuery;
|
||||
fn source_root() for input::SourceRootQuery;
|
||||
fn libraries() for input::LibrarieseQuery;
|
||||
fn library_symbols() for input::LibrarySymbolsQuery;
|
||||
fn crate_graph() for input::CrateGraphQuery;
|
||||
}
|
||||
impl SyntaxDatabase {
|
||||
fn file_syntax() for FileSyntaxQuery;
|
||||
@ -75,40 +79,7 @@ salsa::database_storage! {
|
||||
}
|
||||
|
||||
salsa::query_group! {
|
||||
pub(crate) trait FilesDatabase: salsa::Database {
|
||||
fn file_text(file_id: FileId) -> Arc<String> {
|
||||
type FileTextQuery;
|
||||
storage input;
|
||||
}
|
||||
fn file_set() -> Arc<FileSet> {
|
||||
type FileSetQuery;
|
||||
storage input;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Eq)]
|
||||
pub(crate) struct FileSet {
|
||||
pub(crate) files: FxHashSet<FileId>,
|
||||
pub(crate) resolver: FileResolverImp,
|
||||
}
|
||||
|
||||
impl PartialEq for FileSet {
|
||||
fn eq(&self, other: &FileSet) -> bool {
|
||||
self.files == other.files && self.resolver == other.resolver
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for FileSet {
|
||||
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
||||
let mut files = self.files.iter().cloned().collect::<Vec<_>>();
|
||||
files.sort();
|
||||
files.hash(hasher);
|
||||
}
|
||||
}
|
||||
|
||||
salsa::query_group! {
|
||||
pub(crate) trait SyntaxDatabase: FilesDatabase {
|
||||
pub(crate) trait SyntaxDatabase: input::FilesDatabase {
|
||||
fn file_syntax(file_id: FileId) -> File {
|
||||
type FileSyntaxQuery;
|
||||
}
|
@ -9,7 +9,7 @@ use ra_syntax::{
|
||||
|
||||
use crate::{
|
||||
FileId, Cancelable, FileResolverImp,
|
||||
db,
|
||||
db::{self, input::{SourceRoot, SourceRootId}},
|
||||
};
|
||||
|
||||
use super::{
|
||||
@ -35,9 +35,12 @@ pub(super) fn modules(root: ast::Root<'_>) -> impl Iterator<Item = (SmolStr, ast
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn module_tree(db: &impl ModulesDatabase) -> Cancelable<Arc<ModuleTree>> {
|
||||
pub(super) fn module_tree(
|
||||
db: &impl ModulesDatabase,
|
||||
source_root: SourceRootId,
|
||||
) -> Cancelable<Arc<ModuleTree>> {
|
||||
db::check_canceled(db)?;
|
||||
let res = create_module_tree(db)?;
|
||||
let res = create_module_tree(db, source_root)?;
|
||||
Ok(Arc::new(res))
|
||||
}
|
||||
|
||||
@ -50,6 +53,7 @@ pub struct Submodule {
|
||||
|
||||
fn create_module_tree<'a>(
|
||||
db: &impl ModulesDatabase,
|
||||
source_root: SourceRootId,
|
||||
) -> Cancelable<ModuleTree> {
|
||||
let mut tree = ModuleTree {
|
||||
mods: Vec::new(),
|
||||
@ -59,12 +63,13 @@ fn create_module_tree<'a>(
|
||||
let mut roots = FxHashMap::default();
|
||||
let mut visited = FxHashSet::default();
|
||||
|
||||
for &file_id in db.file_set().files.iter() {
|
||||
let source_root = db.source_root(source_root);
|
||||
for &file_id in source_root.files.iter() {
|
||||
if visited.contains(&file_id) {
|
||||
continue; // TODO: use explicit crate_roots here
|
||||
}
|
||||
assert!(!roots.contains_key(&file_id));
|
||||
let module_id = build_subtree(db, &mut tree, &mut visited, &mut roots, None, file_id)?;
|
||||
let module_id = build_subtree(db, &source_root, &mut tree, &mut visited, &mut roots, None, file_id)?;
|
||||
roots.insert(file_id, module_id);
|
||||
}
|
||||
Ok(tree)
|
||||
@ -72,6 +77,7 @@ fn create_module_tree<'a>(
|
||||
|
||||
fn build_subtree(
|
||||
db: &impl ModulesDatabase,
|
||||
source_root: &SourceRoot,
|
||||
tree: &mut ModuleTree,
|
||||
visited: &mut FxHashSet<FileId>,
|
||||
roots: &mut FxHashMap<FileId, ModuleId>,
|
||||
@ -84,10 +90,8 @@ fn build_subtree(
|
||||
parent,
|
||||
children: Vec::new(),
|
||||
});
|
||||
let file_set = db.file_set();
|
||||
let file_resolver = &file_set.resolver;
|
||||
for name in db.submodules(file_id)?.iter() {
|
||||
let (points_to, problem) = resolve_submodule(file_id, name, file_resolver);
|
||||
let (points_to, problem) = resolve_submodule(file_id, name, &source_root.file_resolver);
|
||||
let link = tree.push_link(LinkData {
|
||||
name: name.clone(),
|
||||
owner: id,
|
||||
@ -102,7 +106,7 @@ fn build_subtree(
|
||||
tree.module_mut(module_id).parent = Some(link);
|
||||
Ok(module_id)
|
||||
}
|
||||
None => build_subtree(db, tree, visited, roots, Some(link), file_id),
|
||||
None => build_subtree(db, source_root, tree, visited, roots, Some(link), file_id),
|
||||
})
|
||||
.collect::<Cancelable<Vec<_>>>()?;
|
||||
tree.link_mut(link).points_to = points_to;
|
||||
|
@ -7,12 +7,12 @@ use ra_syntax::{ast::{self, NameOwner, AstNode}, SmolStr, SyntaxNode};
|
||||
|
||||
use crate::{
|
||||
FileId, Cancelable,
|
||||
db::SyntaxDatabase,
|
||||
db::{SyntaxDatabase, input::SourceRootId},
|
||||
};
|
||||
|
||||
salsa::query_group! {
|
||||
pub(crate) trait ModulesDatabase: SyntaxDatabase {
|
||||
fn module_tree() -> Cancelable<Arc<ModuleTree>> {
|
||||
fn module_tree(source_root_id: SourceRootId) -> Cancelable<Arc<ModuleTree>> {
|
||||
type ModuleTreeQuery;
|
||||
use fn imp::module_tree;
|
||||
}
|
||||
@ -110,15 +110,9 @@ impl ModuleId {
|
||||
}
|
||||
|
||||
impl LinkId {
|
||||
pub(crate) fn name(self, tree: &ModuleTree) -> SmolStr {
|
||||
tree.link(self).name.clone()
|
||||
}
|
||||
pub(crate) fn owner(self, tree: &ModuleTree) -> ModuleId {
|
||||
tree.link(self).owner
|
||||
}
|
||||
fn points_to(self, tree: &ModuleTree) -> &[ModuleId] {
|
||||
&tree.link(self).points_to
|
||||
}
|
||||
pub(crate) fn bind_source<'a>(
|
||||
self,
|
||||
tree: &ModuleTree,
|
||||
|
@ -1,7 +1,5 @@
|
||||
use std::{
|
||||
fmt,
|
||||
hash::{Hash, Hasher},
|
||||
iter,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
@ -14,12 +12,16 @@ use ra_syntax::{
|
||||
};
|
||||
use relative_path::RelativePath;
|
||||
use rustc_hash::FxHashSet;
|
||||
use salsa::{ParallelDatabase, Database};
|
||||
|
||||
use crate::{
|
||||
db::SyntaxDatabase,
|
||||
AnalysisChange,
|
||||
db::{
|
||||
self, SyntaxDatabase,
|
||||
input::{SourceRootId, FilesDatabase, SourceRoot, WORKSPACE}
|
||||
},
|
||||
descriptors::module::{ModulesDatabase, ModuleTree, Problem},
|
||||
descriptors::{FnDescriptor},
|
||||
roots::{ReadonlySourceRoot, SourceRoot, WritableSourceRoot},
|
||||
CrateGraph, CrateId, Diagnostic, FileId, FileResolver, FileSystemEdit, Position,
|
||||
Query, SourceChange, SourceFileEdit, Cancelable,
|
||||
};
|
||||
@ -80,96 +82,123 @@ impl Default for FileResolverImp {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Default)]
|
||||
pub(crate) struct AnalysisHostImpl {
|
||||
data: WorldData,
|
||||
db: db::RootDatabase,
|
||||
}
|
||||
|
||||
|
||||
impl AnalysisHostImpl {
|
||||
pub fn new() -> AnalysisHostImpl {
|
||||
AnalysisHostImpl {
|
||||
data: WorldData::default(),
|
||||
}
|
||||
AnalysisHostImpl::default()
|
||||
}
|
||||
pub fn analysis(&self) -> AnalysisImpl {
|
||||
AnalysisImpl {
|
||||
data: self.data.clone(),
|
||||
db: self.db.fork() // freeze revision here
|
||||
}
|
||||
}
|
||||
pub fn change_files(&mut self, changes: &mut dyn Iterator<Item = (FileId, Option<String>)>) {
|
||||
self.data_mut().root.apply_changes(changes, None);
|
||||
}
|
||||
pub fn set_file_resolver(&mut self, resolver: FileResolverImp) {
|
||||
self.data_mut()
|
||||
.root
|
||||
.apply_changes(&mut iter::empty(), Some(resolver));
|
||||
}
|
||||
pub fn set_crate_graph(&mut self, graph: CrateGraph) {
|
||||
let mut visited = FxHashSet::default();
|
||||
for &file_id in graph.crate_roots.values() {
|
||||
if !visited.insert(file_id) {
|
||||
panic!("duplicate crate root: {:?}", file_id);
|
||||
pub fn apply_change(&mut self, change: AnalysisChange) {
|
||||
for (file_id, text) in change.files_changed {
|
||||
self.db
|
||||
.query(db::input::FileTextQuery)
|
||||
.set(file_id, Arc::new(text))
|
||||
}
|
||||
if !(change.files_added.is_empty() && change.files_removed.is_empty()) {
|
||||
let file_resolver = change.file_resolver
|
||||
.expect("change resolver when changing set of files");
|
||||
let mut source_root = SourceRoot::clone(&self.db.source_root(WORKSPACE));
|
||||
for (file_id, text) in change.files_added {
|
||||
self.db
|
||||
.query(db::input::FileTextQuery)
|
||||
.set(file_id, Arc::new(text));
|
||||
self.db
|
||||
.query(db::input::FileSourceRootQuery)
|
||||
.set(file_id, db::input::WORKSPACE);
|
||||
source_root.files.insert(file_id);
|
||||
}
|
||||
for file_id in change.files_removed {
|
||||
self.db
|
||||
.query(db::input::FileTextQuery)
|
||||
.set(file_id, Arc::new(String::new()));
|
||||
source_root.files.remove(&file_id);
|
||||
}
|
||||
source_root.file_resolver = file_resolver;
|
||||
self.db
|
||||
.query(db::input::SourceRootQuery)
|
||||
.set(WORKSPACE, Arc::new(source_root))
|
||||
}
|
||||
if !change.libraries_added.is_empty() {
|
||||
let mut libraries = Vec::clone(&self.db.libraries());
|
||||
for library in change.libraries_added {
|
||||
let source_root_id = SourceRootId(1 + libraries.len() as u32);
|
||||
libraries.push(source_root_id);
|
||||
let mut files = FxHashSet::default();
|
||||
for (file_id, text) in library.files {
|
||||
files.insert(file_id);
|
||||
self.db
|
||||
.query(db::input::FileSourceRootQuery)
|
||||
.set_constant(file_id, source_root_id);
|
||||
self.db
|
||||
.query(db::input::FileTextQuery)
|
||||
.set_constant(file_id, Arc::new(text));
|
||||
}
|
||||
let source_root = SourceRoot {
|
||||
files,
|
||||
file_resolver: library.file_resolver,
|
||||
};
|
||||
self.db
|
||||
.query(db::input::SourceRootQuery)
|
||||
.set(source_root_id, Arc::new(source_root));
|
||||
self.db
|
||||
.query(db::input::LibrarySymbolsQuery)
|
||||
.set(source_root_id, Arc::new(library.symbol_index));
|
||||
}
|
||||
self.db
|
||||
.query(db::input::LibrarieseQuery)
|
||||
.set((), Arc::new(libraries));
|
||||
}
|
||||
if let Some(crate_graph) = change.crate_graph {
|
||||
self.db.query(db::input::CrateGraphQuery)
|
||||
.set((), Arc::new(crate_graph))
|
||||
}
|
||||
self.data_mut().crate_graph = graph;
|
||||
}
|
||||
pub fn add_library(&mut self, root: ReadonlySourceRoot) {
|
||||
self.data_mut().libs.push(root);
|
||||
}
|
||||
fn data_mut(&mut self) -> &mut WorldData {
|
||||
&mut self.data
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct AnalysisImpl {
|
||||
data: WorldData,
|
||||
}
|
||||
|
||||
impl fmt::Debug for AnalysisImpl {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.data.fmt(f)
|
||||
}
|
||||
db: db::RootDatabase,
|
||||
}
|
||||
|
||||
impl AnalysisImpl {
|
||||
fn root(&self, file_id: FileId) -> &SourceRoot {
|
||||
if self.data.root.contains(file_id) {
|
||||
return &self.data.root;
|
||||
}
|
||||
self
|
||||
.data
|
||||
.libs
|
||||
.iter()
|
||||
.find(|it| it.contains(file_id))
|
||||
.unwrap()
|
||||
}
|
||||
pub fn file_syntax(&self, file_id: FileId) -> File {
|
||||
self.root(file_id).db().file_syntax(file_id)
|
||||
self.db.file_syntax(file_id)
|
||||
}
|
||||
pub fn file_line_index(&self, file_id: FileId) -> Arc<LineIndex> {
|
||||
self.root(file_id).db().file_lines(file_id)
|
||||
self.db.file_lines(file_id)
|
||||
}
|
||||
pub fn world_symbols(&self, query: Query) -> Cancelable<Vec<(FileId, FileSymbol)>> {
|
||||
let mut buf = Vec::new();
|
||||
if query.libs {
|
||||
for lib in self.data.libs.iter() {
|
||||
lib.symbols(&mut buf)?;
|
||||
}
|
||||
} else {
|
||||
self.data.root.symbols(&mut buf)?;
|
||||
for &lib_id in self.db.libraries().iter() {
|
||||
buf.push(self.db.library_symbols(lib_id));
|
||||
}
|
||||
for &file_id in self.db.source_root(WORKSPACE).files.iter() {
|
||||
buf.push(self.db.file_symbols(file_id)?);
|
||||
}
|
||||
Ok(query.search(&buf))
|
||||
}
|
||||
fn module_tree(&self, file_id: FileId) -> Cancelable<Arc<ModuleTree>> {
|
||||
let source_root = self.db.file_source_root(file_id);
|
||||
self.db.module_tree(source_root)
|
||||
}
|
||||
pub fn parent_module(&self, file_id: FileId) -> Cancelable<Vec<(FileId, FileSymbol)>> {
|
||||
let root = self.root(file_id);
|
||||
let module_tree = root.db().module_tree()?;
|
||||
let module_tree = self.module_tree(file_id)?;
|
||||
|
||||
let res = module_tree.modules_for_file(file_id)
|
||||
.into_iter()
|
||||
.filter_map(|module_id| {
|
||||
let link = module_id.parent_link(&module_tree)?;
|
||||
let file_id = link.owner(&module_tree).file_id(&module_tree);
|
||||
let syntax = root.db().file_syntax(file_id);
|
||||
let syntax = self.db.file_syntax(file_id);
|
||||
let decl = link.bind_source(&module_tree, syntax.ast());
|
||||
|
||||
let sym = FileSymbol {
|
||||
@ -183,8 +212,8 @@ impl AnalysisImpl {
|
||||
Ok(res)
|
||||
}
|
||||
pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
|
||||
let module_tree = self.root(file_id).db().module_tree()?;
|
||||
let crate_graph = &self.data.crate_graph;
|
||||
let module_tree = self.module_tree(file_id)?;
|
||||
let crate_graph = self.db.crate_graph();
|
||||
let res = module_tree.modules_for_file(file_id)
|
||||
.into_iter()
|
||||
.map(|it| it.root(&module_tree))
|
||||
@ -195,7 +224,7 @@ impl AnalysisImpl {
|
||||
Ok(res)
|
||||
}
|
||||
pub fn crate_root(&self, crate_id: CrateId) -> FileId {
|
||||
self.data.crate_graph.crate_roots[&crate_id]
|
||||
self.db.crate_graph().crate_roots[&crate_id]
|
||||
}
|
||||
pub fn completions(&self, file_id: FileId, offset: TextUnit) -> Cancelable<Option<Vec<CompletionItem>>> {
|
||||
let mut res = Vec::new();
|
||||
@ -205,8 +234,7 @@ impl AnalysisImpl {
|
||||
res.extend(scope_based);
|
||||
has_completions = true;
|
||||
}
|
||||
let root = self.root(file_id);
|
||||
if let Some(scope_based) = crate::completion::resolve_based_completion(root.db(), file_id, offset)? {
|
||||
if let Some(scope_based) = crate::completion::resolve_based_completion(&self.db, file_id, offset)? {
|
||||
res.extend(scope_based);
|
||||
has_completions = true;
|
||||
}
|
||||
@ -222,9 +250,8 @@ impl AnalysisImpl {
|
||||
file_id: FileId,
|
||||
offset: TextUnit,
|
||||
) -> Cancelable<Vec<(FileId, FileSymbol)>> {
|
||||
let root = self.root(file_id);
|
||||
let module_tree = root.db().module_tree()?;
|
||||
let file = root.db().file_syntax(file_id);
|
||||
let module_tree = self.module_tree(file_id)?;
|
||||
let file = self.db.file_syntax(file_id);
|
||||
let syntax = file.syntax();
|
||||
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) {
|
||||
// First try to resolve the symbol locally
|
||||
@ -273,8 +300,7 @@ impl AnalysisImpl {
|
||||
}
|
||||
|
||||
pub fn find_all_refs(&self, file_id: FileId, offset: TextUnit) -> Vec<(FileId, TextRange)> {
|
||||
let root = self.root(file_id);
|
||||
let file = root.db().file_syntax(file_id);
|
||||
let file = self.db.file_syntax(file_id);
|
||||
let syntax = file.syntax();
|
||||
|
||||
let mut ret = vec![];
|
||||
@ -305,9 +331,8 @@ impl AnalysisImpl {
|
||||
}
|
||||
|
||||
pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
|
||||
let root = self.root(file_id);
|
||||
let module_tree = root.db().module_tree()?;
|
||||
let syntax = root.db().file_syntax(file_id);
|
||||
let module_tree = self.module_tree(file_id)?;
|
||||
let syntax = self.db.file_syntax(file_id);
|
||||
|
||||
let mut res = ra_editor::diagnostics(&syntax)
|
||||
.into_iter()
|
||||
@ -396,8 +421,7 @@ impl AnalysisImpl {
|
||||
file_id: FileId,
|
||||
offset: TextUnit,
|
||||
) -> Cancelable<Option<(FnDescriptor, Option<usize>)>> {
|
||||
let root = self.root(file_id);
|
||||
let file = root.db().file_syntax(file_id);
|
||||
let file = self.db.file_syntax(file_id);
|
||||
let syntax = file.syntax();
|
||||
|
||||
// Find the calling expression and it's NameRef
|
||||
@ -491,13 +515,6 @@ impl AnalysisImpl {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Debug)]
|
||||
struct WorldData {
|
||||
crate_graph: CrateGraph,
|
||||
root: WritableSourceRoot,
|
||||
libs: Vec<ReadonlySourceRoot>,
|
||||
}
|
||||
|
||||
impl SourceChange {
|
||||
pub(crate) fn from_local_edit(file_id: FileId, label: &str, edit: LocalEdit) -> SourceChange {
|
||||
let file_edit = SourceFileEdit {
|
||||
|
@ -9,17 +9,23 @@ extern crate salsa;
|
||||
mod db;
|
||||
mod descriptors;
|
||||
mod imp;
|
||||
mod roots;
|
||||
mod symbol_index;
|
||||
mod completion;
|
||||
|
||||
use std::{fmt::Debug, sync::Arc};
|
||||
use std::{
|
||||
fmt::Debug,
|
||||
sync::Arc,
|
||||
collections::BTreeMap,
|
||||
};
|
||||
|
||||
use ra_syntax::{AtomEdit, File, TextRange, TextUnit};
|
||||
use relative_path::{RelativePath, RelativePathBuf};
|
||||
use rustc_hash::FxHashMap;
|
||||
use rayon::prelude::*;
|
||||
|
||||
use crate::imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp};
|
||||
use crate::{
|
||||
imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp},
|
||||
symbol_index::SymbolIndex,
|
||||
};
|
||||
|
||||
pub use crate::{
|
||||
descriptors::FnDescriptor,
|
||||
@ -49,9 +55,9 @@ pub struct FileId(pub u32);
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct CrateId(pub u32);
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
|
||||
pub struct CrateGraph {
|
||||
pub crate_roots: FxHashMap<CrateId, FileId>,
|
||||
pub crate_roots: BTreeMap<CrateId, FileId>,
|
||||
}
|
||||
|
||||
pub trait FileResolver: Debug + Send + Sync + 'static {
|
||||
@ -59,6 +65,41 @@ pub trait FileResolver: Debug + Send + Sync + 'static {
|
||||
fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId>;
|
||||
}
|
||||
|
||||
#[derive(Debug, 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>,
|
||||
}
|
||||
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct AnalysisHost {
|
||||
imp: AnalysisHostImpl,
|
||||
@ -75,20 +116,8 @@ impl AnalysisHost {
|
||||
imp: self.imp.analysis(),
|
||||
}
|
||||
}
|
||||
pub fn change_file(&mut self, file_id: FileId, text: Option<String>) {
|
||||
self.change_files(::std::iter::once((file_id, text)));
|
||||
}
|
||||
pub fn change_files(&mut self, mut changes: impl Iterator<Item = (FileId, Option<String>)>) {
|
||||
self.imp.change_files(&mut changes)
|
||||
}
|
||||
pub fn set_file_resolver(&mut self, resolver: Arc<FileResolver>) {
|
||||
self.imp.set_file_resolver(FileResolverImp::new(resolver));
|
||||
}
|
||||
pub fn set_crate_graph(&mut self, graph: CrateGraph) {
|
||||
self.imp.set_crate_graph(graph)
|
||||
}
|
||||
pub fn add_library(&mut self, data: LibraryData) {
|
||||
self.imp.add_library(data.root)
|
||||
pub fn apply_change(&mut self, change: AnalysisChange) {
|
||||
self.imp.apply_change(change)
|
||||
}
|
||||
}
|
||||
|
||||
@ -266,14 +295,18 @@ impl Analysis {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct LibraryData {
|
||||
root: roots::ReadonlySourceRoot,
|
||||
files: Vec<(FileId, String)>,
|
||||
file_resolver: FileResolverImp,
|
||||
symbol_index: SymbolIndex,
|
||||
}
|
||||
|
||||
impl LibraryData {
|
||||
pub fn prepare(files: Vec<(FileId, String)>, file_resolver: Arc<FileResolver>) -> LibraryData {
|
||||
let file_resolver = FileResolverImp::new(file_resolver);
|
||||
let root = roots::ReadonlySourceRoot::new(files, file_resolver);
|
||||
LibraryData { root }
|
||||
let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, text)| {
|
||||
let file = File::parse(text);
|
||||
(*file_id, file)
|
||||
}));
|
||||
LibraryData { files, file_resolver: FileResolverImp::new(file_resolver), symbol_index }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@ use rayon::prelude::*;
|
||||
|
||||
use crate::{FileId, Query};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Default, Debug)]
|
||||
pub(crate) struct SymbolIndex {
|
||||
symbols: Vec<(FileId, FileSymbol)>,
|
||||
map: fst::Map,
|
||||
|
@ -5,15 +5,17 @@ extern crate relative_path;
|
||||
extern crate rustc_hash;
|
||||
extern crate test_utils;
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::{
|
||||
sync::Arc,
|
||||
collections::BTreeMap,
|
||||
};
|
||||
|
||||
use ra_syntax::TextRange;
|
||||
use relative_path::{RelativePath, RelativePathBuf};
|
||||
use rustc_hash::FxHashMap;
|
||||
use test_utils::{assert_eq_dbg, extract_offset};
|
||||
|
||||
use ra_analysis::{
|
||||
Analysis, AnalysisHost, CrateGraph, CrateId, FileId, FileResolver, FnDescriptor,
|
||||
AnalysisChange, Analysis, AnalysisHost, CrateGraph, CrateId, FileId, FileResolver, FnDescriptor,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -45,14 +47,16 @@ impl FileResolver for FileMap {
|
||||
fn analysis_host(files: &[(&str, &str)]) -> AnalysisHost {
|
||||
let mut host = AnalysisHost::new();
|
||||
let mut file_map = Vec::new();
|
||||
let mut change = AnalysisChange::new();
|
||||
for (id, &(path, contents)) in files.iter().enumerate() {
|
||||
let file_id = FileId((id + 1) as u32);
|
||||
assert!(path.starts_with('/'));
|
||||
let path = RelativePathBuf::from_path(&path[1..]).unwrap();
|
||||
host.change_file(file_id, Some(contents.to_string()));
|
||||
change.add_file(file_id, contents.to_string());
|
||||
file_map.push((file_id, path));
|
||||
}
|
||||
host.set_file_resolver(Arc::new(FileMap(file_map)));
|
||||
change.set_file_resolver(Arc::new(FileMap(file_map)));
|
||||
host.apply_change(change);
|
||||
host
|
||||
}
|
||||
|
||||
@ -128,12 +132,14 @@ fn test_resolve_crate_root() {
|
||||
|
||||
let crate_graph = CrateGraph {
|
||||
crate_roots: {
|
||||
let mut m = FxHashMap::default();
|
||||
let mut m = BTreeMap::default();
|
||||
m.insert(CrateId(1), FileId(1));
|
||||
m
|
||||
},
|
||||
};
|
||||
host.set_crate_graph(crate_graph);
|
||||
let mut change = AnalysisChange::new();
|
||||
change.set_crate_graph(crate_graph);
|
||||
host.apply_change(change);
|
||||
let snap = host.analysis();
|
||||
|
||||
assert_eq!(snap.crate_for(FileId(2)).unwrap(), vec![CrateId(1)],);
|
||||
|
@ -22,15 +22,18 @@ impl PathMap {
|
||||
pub fn new() -> PathMap {
|
||||
Default::default()
|
||||
}
|
||||
pub fn get_or_insert(&mut self, path: PathBuf, root: Root) -> FileId {
|
||||
self.path2id
|
||||
pub fn get_or_insert(&mut self, path: PathBuf, root: Root) -> (bool, FileId) {
|
||||
let mut inserted = false;
|
||||
let file_id = self.path2id
|
||||
.get(path.as_path())
|
||||
.map(|&id| id)
|
||||
.unwrap_or_else(|| {
|
||||
inserted = true;
|
||||
let id = self.new_file_id();
|
||||
self.insert(path, id, root);
|
||||
id
|
||||
})
|
||||
});
|
||||
(inserted, file_id)
|
||||
}
|
||||
pub fn get_id(&self, path: &Path) -> Option<FileId> {
|
||||
self.path2id.get(path).map(|&id| id)
|
||||
@ -105,8 +108,8 @@ mod test {
|
||||
#[test]
|
||||
fn test_resolve() {
|
||||
let mut m = PathMap::new();
|
||||
let id1 = m.get_or_insert(PathBuf::from("/foo"), Root::Workspace);
|
||||
let id2 = m.get_or_insert(PathBuf::from("/foo/bar.rs"), Root::Workspace);
|
||||
let (_, id1) = m.get_or_insert(PathBuf::from("/foo"), Root::Workspace);
|
||||
let (_, id2) = m.get_or_insert(PathBuf::from("/foo/bar.rs"), Root::Workspace);
|
||||
assert_eq!(m.resolve(id1, &RelativePath::new("bar.rs")), Some(id2),)
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,11 @@ use std::{
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
collections::BTreeMap,
|
||||
};
|
||||
|
||||
use languageserver_types::Url;
|
||||
use ra_analysis::{Analysis, AnalysisHost, CrateGraph, CrateId, FileId, FileResolver, LibraryData};
|
||||
use ra_analysis::{Analysis, AnalysisHost, AnalysisChange, CrateGraph, CrateId, FileId, FileResolver, LibraryData};
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use crate::{
|
||||
@ -39,30 +40,40 @@ impl ServerWorldState {
|
||||
}
|
||||
}
|
||||
pub fn apply_fs_changes(&mut self, events: Vec<FileEvent>) {
|
||||
let mut change = AnalysisChange::new();
|
||||
let mut inserted = false;
|
||||
{
|
||||
let pm = &mut self.path_map;
|
||||
let mm = &mut self.mem_map;
|
||||
let changes = events
|
||||
events
|
||||
.into_iter()
|
||||
.map(|event| {
|
||||
let text = match event.kind {
|
||||
FileEventKind::Add(text) => Some(text),
|
||||
FileEventKind::Add(text) => text,
|
||||
};
|
||||
(event.path, text)
|
||||
})
|
||||
.map(|(path, text)| (pm.get_or_insert(path, Root::Workspace), text))
|
||||
.filter_map(|(id, text)| {
|
||||
if mm.contains_key(&id) {
|
||||
mm.insert(id, text);
|
||||
.map(|(path, text)| {
|
||||
let (ins, file_id) = pm.get_or_insert(path, Root::Workspace);
|
||||
inserted |= ins;
|
||||
(file_id, text)
|
||||
})
|
||||
.filter_map(|(file_id, text)| {
|
||||
if mm.contains_key(&file_id) {
|
||||
mm.insert(file_id, Some(text));
|
||||
None
|
||||
} else {
|
||||
Some((id, text))
|
||||
Some((file_id, text))
|
||||
}
|
||||
})
|
||||
.for_each(|(file_id, text)| {
|
||||
change.add_file(file_id, text)
|
||||
});
|
||||
self.analysis_host.change_files(changes);
|
||||
}
|
||||
self.analysis_host
|
||||
.set_file_resolver(Arc::new(self.path_map.clone()));
|
||||
if inserted {
|
||||
change.set_file_resolver(Arc::new(self.path_map.clone()))
|
||||
}
|
||||
self.analysis_host.apply_change(change);
|
||||
}
|
||||
pub fn events_to_files(
|
||||
&mut self,
|
||||
@ -76,24 +87,31 @@ impl ServerWorldState {
|
||||
let FileEventKind::Add(text) = event.kind;
|
||||
(event.path, text)
|
||||
})
|
||||
.map(|(path, text)| (pm.get_or_insert(path, Root::Lib), text))
|
||||
.map(|(path, text)| (pm.get_or_insert(path, Root::Lib).1, text))
|
||||
.collect()
|
||||
};
|
||||
let resolver = Arc::new(self.path_map.clone());
|
||||
(files, resolver)
|
||||
}
|
||||
pub fn add_lib(&mut self, data: LibraryData) {
|
||||
self.analysis_host.add_library(data);
|
||||
let mut change = AnalysisChange::new();
|
||||
change.add_library(data);
|
||||
self.analysis_host.apply_change(change);
|
||||
}
|
||||
|
||||
pub fn add_mem_file(&mut self, path: PathBuf, text: String) -> FileId {
|
||||
let file_id = self.path_map.get_or_insert(path, Root::Workspace);
|
||||
self.analysis_host
|
||||
.set_file_resolver(Arc::new(self.path_map.clone()));
|
||||
self.mem_map.insert(file_id, None);
|
||||
let (inserted, file_id) = self.path_map.get_or_insert(path, Root::Workspace);
|
||||
if self.path_map.get_root(file_id) != Root::Lib {
|
||||
self.analysis_host.change_file(file_id, Some(text));
|
||||
let mut change = AnalysisChange::new();
|
||||
if inserted {
|
||||
change.add_file(file_id, text);
|
||||
change.set_file_resolver(Arc::new(self.path_map.clone()));
|
||||
} else {
|
||||
change.change_file(file_id, text);
|
||||
}
|
||||
self.analysis_host.apply_change(change);
|
||||
}
|
||||
self.mem_map.insert(file_id, None);
|
||||
file_id
|
||||
}
|
||||
|
||||
@ -103,7 +121,9 @@ impl ServerWorldState {
|
||||
.get_id(path)
|
||||
.ok_or_else(|| format_err!("change to unknown file: {}", path.display()))?;
|
||||
if self.path_map.get_root(file_id) != Root::Lib {
|
||||
self.analysis_host.change_file(file_id, Some(text));
|
||||
let mut change = AnalysisChange::new();
|
||||
change.change_file(file_id, text);
|
||||
self.analysis_host.apply_change(change);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -120,12 +140,16 @@ impl ServerWorldState {
|
||||
// Do this via file watcher ideally.
|
||||
let text = fs::read_to_string(path).ok();
|
||||
if self.path_map.get_root(file_id) != Root::Lib {
|
||||
self.analysis_host.change_file(file_id, text);
|
||||
let mut change = AnalysisChange::new();
|
||||
if let Some(text) = text {
|
||||
change.change_file(file_id, text);
|
||||
}
|
||||
self.analysis_host.apply_change(change);
|
||||
}
|
||||
Ok(file_id)
|
||||
}
|
||||
pub fn set_workspaces(&mut self, ws: Vec<CargoWorkspace>) {
|
||||
let mut crate_roots = FxHashMap::default();
|
||||
let mut crate_roots = BTreeMap::default();
|
||||
ws.iter()
|
||||
.flat_map(|ws| {
|
||||
ws.packages()
|
||||
@ -140,7 +164,9 @@ impl ServerWorldState {
|
||||
});
|
||||
let crate_graph = CrateGraph { crate_roots };
|
||||
self.workspaces = Arc::new(ws);
|
||||
self.analysis_host.set_crate_graph(crate_graph);
|
||||
let mut change = AnalysisChange::new();
|
||||
change.set_crate_graph(crate_graph);
|
||||
self.analysis_host.apply_change(change);
|
||||
}
|
||||
pub fn snapshot(&self) -> ServerWorld {
|
||||
ServerWorld {
|
||||
|
Loading…
Reference in New Issue
Block a user