Merge #162
162: Db everywhere r=matklad a=matklad This PR continues our switch to salsa. Now *all* state is handled by a single salsa database. Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
5932bd0bb5
8
Cargo.lock
generated
8
Cargo.lock
generated
@ -602,12 +602,13 @@ name = "ra_analysis"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"fst 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ra_editor 0.1.0",
|
||||
"ra_syntax 0.1.0",
|
||||
"rayon 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"salsa 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"salsa 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"test_utils 0.1.0",
|
||||
]
|
||||
|
||||
@ -833,11 +834,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "salsa"
|
||||
version = "0.6.0"
|
||||
version = "0.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"derive-new 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"lock_api 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@ -1348,7 +1350,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
|
||||
"checksum ryu 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7153dd96dade874ab973e098cb62fcdbb89a03682e46b144fd09550998d4a4a7"
|
||||
"checksum safemem 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dca453248a96cb0749e36ccdfe2b0b4e54a61bfef89fb97ec621eb8e0a93dd9"
|
||||
"checksum salsa 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6c8f8b59428c040fbac0f6a2e698ae892e33d23d7519713ba8b243edb3082dad"
|
||||
"checksum salsa 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ba7fe802c02c7b0074b0b4794442d73e3c7d071967300a98bb0f5dfc25e9f1ef"
|
||||
"checksum same-file 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "10f7794e2fda7f594866840e95f5c5962e886e228e68b6505885811a94dd728c"
|
||||
"checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27"
|
||||
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
|
||||
|
@ -5,12 +5,13 @@ version = "0.1.0"
|
||||
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
log = "0.4.5"
|
||||
relative-path = "0.4.0"
|
||||
rayon = "1.0.2"
|
||||
fst = "0.3.1"
|
||||
ra_syntax = { path = "../ra_syntax" }
|
||||
ra_editor = { path = "../ra_editor" }
|
||||
salsa = "0.6.0"
|
||||
salsa = "0.6.2"
|
||||
rustc-hash = "1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
|
@ -6,13 +6,15 @@ use ra_syntax::{
|
||||
|
||||
use crate::{
|
||||
FileId, Cancelable,
|
||||
input::FilesDatabase,
|
||||
db::{self, SyntaxDatabase},
|
||||
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)
|
||||
|
@ -1,12 +1,9 @@
|
||||
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,20 +11,14 @@ use crate::{
|
||||
Cancelable, Canceled,
|
||||
descriptors::module::{SubmodulesQuery, ModuleTreeQuery, ModulesDatabase},
|
||||
symbol_index::SymbolIndex,
|
||||
FileId, FileResolverImp,
|
||||
FileId,
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, Debug)]
|
||||
pub(crate) struct RootDatabase {
|
||||
runtime: salsa::Runtime<RootDatabase>,
|
||||
}
|
||||
|
||||
impl fmt::Debug for RootDatabase {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.write_str("RootDatabase { ... }")
|
||||
}
|
||||
}
|
||||
|
||||
impl salsa::Database for RootDatabase {
|
||||
fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> {
|
||||
&self.runtime
|
||||
@ -58,9 +49,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 crate::input::FilesDatabase {
|
||||
fn file_text() for crate::input::FileTextQuery;
|
||||
fn file_source_root() for crate::input::FileSourceRootQuery;
|
||||
fn source_root() for crate::input::SourceRootQuery;
|
||||
fn libraries() for crate::input::LibrarieseQuery;
|
||||
fn library_symbols() for crate::input::LibrarySymbolsQuery;
|
||||
fn crate_graph() for crate::input::CrateGraphQuery;
|
||||
}
|
||||
impl SyntaxDatabase {
|
||||
fn file_syntax() for FileSyntaxQuery;
|
||||
@ -75,40 +70,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: crate::input::FilesDatabase {
|
||||
fn file_syntax(file_id: FileId) -> File {
|
||||
type FileSyntaxQuery;
|
||||
}
|
||||
|
@ -8,8 +8,8 @@ use ra_syntax::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
FileId, Cancelable, FileResolverImp,
|
||||
db,
|
||||
FileId, Cancelable, FileResolverImp, db,
|
||||
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;
|
||||
|
@ -8,11 +8,12 @@ use ra_syntax::{ast::{self, NameOwner, AstNode}, SmolStr, SyntaxNode};
|
||||
use crate::{
|
||||
FileId, Cancelable,
|
||||
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 +111,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,17 @@ 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 +83,128 @@ 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) {
|
||||
log::info!("apply_change {:?}", change);
|
||||
|
||||
for (file_id, text) in change.files_changed {
|
||||
self.db
|
||||
.query(crate::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(crate::input::FileTextQuery)
|
||||
.set(file_id, Arc::new(text));
|
||||
self.db
|
||||
.query(crate::input::FileSourceRootQuery)
|
||||
.set(file_id, crate::input::WORKSPACE);
|
||||
source_root.files.insert(file_id);
|
||||
}
|
||||
for file_id in change.files_removed {
|
||||
self.db
|
||||
.query(crate::input::FileTextQuery)
|
||||
.set(file_id, Arc::new(String::new()));
|
||||
source_root.files.remove(&file_id);
|
||||
}
|
||||
source_root.file_resolver = file_resolver;
|
||||
self.db
|
||||
.query(crate::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(crate::input::FileSourceRootQuery)
|
||||
.set_constant(file_id, source_root_id);
|
||||
self.db
|
||||
.query(crate::input::FileTextQuery)
|
||||
.set_constant(file_id, Arc::new(text));
|
||||
}
|
||||
let source_root = SourceRoot {
|
||||
files,
|
||||
file_resolver: library.file_resolver,
|
||||
};
|
||||
self.db
|
||||
.query(crate::input::SourceRootQuery)
|
||||
.set(source_root_id, Arc::new(source_root));
|
||||
self.db
|
||||
.query(crate::input::LibrarySymbolsQuery)
|
||||
.set(source_root_id, Arc::new(library.symbol_index));
|
||||
}
|
||||
self.db
|
||||
.query(crate::input::LibrarieseQuery)
|
||||
.set((), Arc::new(libraries));
|
||||
}
|
||||
if let Some(crate_graph) = change.crate_graph {
|
||||
self.db.query(crate::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)?;
|
||||
for &lib_id in self.db.libraries().iter() {
|
||||
buf.push(self.db.library_symbols(lib_id));
|
||||
}
|
||||
} else {
|
||||
self.data.root.symbols(&mut buf)?;
|
||||
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 +218,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 +230,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 +240,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 +256,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 +306,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 +337,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 +427,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
|
||||
@ -412,9 +442,10 @@ impl AnalysisImpl {
|
||||
|
||||
// Resolve the function's NameRef (NOTE: this isn't entirely accurate).
|
||||
let file_symbols = self.index_resolve(name_ref)?;
|
||||
for (_, fs) in file_symbols {
|
||||
for (fn_fiel_id, fs) in file_symbols {
|
||||
if fs.kind == FN_DEF {
|
||||
if let Some(fn_def) = find_node_at_offset(syntax, fs.node_range.start()) {
|
||||
let fn_file = self.db.file_syntax(fn_fiel_id);
|
||||
if let Some(fn_def) = find_node_at_offset(fn_file.syntax(), fs.node_range.start()) {
|
||||
if let Some(descriptor) = FnDescriptor::new(fn_def) {
|
||||
// If we have a calling expression let's find which argument we are on
|
||||
let mut current_parameter = None;
|
||||
@ -491,13 +522,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 {
|
||||
|
79
crates/ra_analysis/src/input.rs
Normal file
79
crates/ra_analysis/src/input.rs
Normal file
@ -0,0 +1,79 @@
|
||||
use std::{
|
||||
sync::Arc,
|
||||
fmt,
|
||||
};
|
||||
|
||||
use salsa;
|
||||
use rustc_hash::FxHashSet;
|
||||
use relative_path::RelativePath;
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use crate::{symbol_index::SymbolIndex, FileResolverImp};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct FileId(pub u32);
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct CrateId(pub u32);
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct CrateGraph {
|
||||
pub(crate) crate_roots: FxHashMap<CrateId, FileId>,
|
||||
}
|
||||
|
||||
impl CrateGraph {
|
||||
pub fn new() -> CrateGraph {
|
||||
CrateGraph::default()
|
||||
}
|
||||
pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId{
|
||||
let crate_id = CrateId(self.crate_roots.len() as u32);
|
||||
let prev = self.crate_roots.insert(crate_id, file_id);
|
||||
assert!(prev.is_none());
|
||||
crate_id
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FileResolver: fmt::Debug + Send + Sync + 'static {
|
||||
fn file_stem(&self, file_id: FileId) -> String;
|
||||
fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId>;
|
||||
}
|
||||
|
||||
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, PartialEq, Eq)]
|
||||
pub(crate) struct SourceRoot {
|
||||
pub(crate) file_resolver: FileResolverImp,
|
||||
pub(crate) files: FxHashSet<FileId>,
|
||||
}
|
||||
|
||||
pub(crate) const WORKSPACE: SourceRootId = SourceRootId(0);
|
@ -6,23 +6,30 @@ extern crate relative_path;
|
||||
extern crate rustc_hash;
|
||||
extern crate salsa;
|
||||
|
||||
mod input;
|
||||
mod db;
|
||||
mod descriptors;
|
||||
mod imp;
|
||||
mod roots;
|
||||
mod symbol_index;
|
||||
mod completion;
|
||||
|
||||
use std::{fmt::Debug, sync::Arc};
|
||||
use std::{
|
||||
fmt,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use ra_syntax::{AtomEdit, File, TextRange, TextUnit};
|
||||
use relative_path::{RelativePath, RelativePathBuf};
|
||||
use rustc_hash::FxHashMap;
|
||||
use relative_path::RelativePathBuf;
|
||||
use rayon::prelude::*;
|
||||
|
||||
use crate::imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp};
|
||||
use crate::{
|
||||
imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp},
|
||||
symbol_index::SymbolIndex,
|
||||
};
|
||||
|
||||
pub use crate::{
|
||||
descriptors::FnDescriptor,
|
||||
input::{FileId, FileResolver, CrateGraph, CrateId}
|
||||
};
|
||||
pub use ra_editor::{
|
||||
CompletionItem, FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable,
|
||||
@ -43,20 +50,52 @@ impl std::fmt::Display for Canceled {
|
||||
impl std::error::Error for Canceled {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct FileId(pub u32);
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct CrateId(pub u32);
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct CrateGraph {
|
||||
pub crate_roots: FxHashMap<CrateId, FileId>,
|
||||
#[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>,
|
||||
}
|
||||
|
||||
pub trait FileResolver: Debug + Send + Sync + 'static {
|
||||
fn file_stem(&self, file_id: FileId) -> String;
|
||||
fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId>;
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -75,20 +114,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 +293,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 }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,116 +0,0 @@
|
||||
use std::{sync::Arc};
|
||||
|
||||
use rustc_hash::FxHashSet;
|
||||
use rayon::prelude::*;
|
||||
use salsa::Database;
|
||||
|
||||
use crate::{
|
||||
Cancelable,
|
||||
db::{self, FilesDatabase, SyntaxDatabase},
|
||||
imp::FileResolverImp,
|
||||
symbol_index::SymbolIndex,
|
||||
FileId,
|
||||
};
|
||||
|
||||
pub(crate) trait SourceRoot {
|
||||
fn contains(&self, file_id: FileId) -> bool;
|
||||
fn db(&self) -> &db::RootDatabase;
|
||||
fn symbols(&self, acc: &mut Vec<Arc<SymbolIndex>>) -> Cancelable<()>;
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone)]
|
||||
pub(crate) struct WritableSourceRoot {
|
||||
db: db::RootDatabase,
|
||||
}
|
||||
|
||||
impl WritableSourceRoot {
|
||||
pub fn apply_changes(
|
||||
&mut self,
|
||||
changes: &mut dyn Iterator<Item = (FileId, Option<String>)>,
|
||||
file_resolver: Option<FileResolverImp>,
|
||||
) {
|
||||
let mut changed = FxHashSet::default();
|
||||
let mut removed = FxHashSet::default();
|
||||
for (file_id, text) in changes {
|
||||
match text {
|
||||
None => {
|
||||
removed.insert(file_id);
|
||||
}
|
||||
Some(text) => {
|
||||
self.db
|
||||
.query(db::FileTextQuery)
|
||||
.set(file_id, Arc::new(text));
|
||||
changed.insert(file_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
let file_set = self.db.file_set();
|
||||
let mut files: FxHashSet<FileId> = file_set.files.clone();
|
||||
for file_id in removed {
|
||||
files.remove(&file_id);
|
||||
}
|
||||
files.extend(changed);
|
||||
let resolver = file_resolver.unwrap_or_else(|| file_set.resolver.clone());
|
||||
self.db
|
||||
.query(db::FileSetQuery)
|
||||
.set((), Arc::new(db::FileSet { files, resolver }));
|
||||
}
|
||||
}
|
||||
|
||||
impl SourceRoot for WritableSourceRoot {
|
||||
fn contains(&self, file_id: FileId) -> bool {
|
||||
self.db.file_set().files.contains(&file_id)
|
||||
}
|
||||
fn db(&self) -> &db::RootDatabase {
|
||||
&self.db
|
||||
}
|
||||
fn symbols<'a>(&'a self, acc: &mut Vec<Arc<SymbolIndex>>) -> Cancelable<()> {
|
||||
for &file_id in self.db.file_set().files.iter() {
|
||||
let symbols = self.db.file_symbols(file_id)?;
|
||||
acc.push(symbols)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct ReadonlySourceRoot {
|
||||
db: db::RootDatabase,
|
||||
symbol_index: Arc<SymbolIndex>,
|
||||
}
|
||||
|
||||
impl ReadonlySourceRoot {
|
||||
pub(crate) fn new(
|
||||
files: Vec<(FileId, String)>,
|
||||
resolver: FileResolverImp,
|
||||
) -> ReadonlySourceRoot {
|
||||
let db = db::RootDatabase::default();
|
||||
let mut file_ids = FxHashSet::default();
|
||||
for (file_id, text) in files {
|
||||
file_ids.insert(file_id);
|
||||
db.query(db::FileTextQuery).set(file_id, Arc::new(text));
|
||||
}
|
||||
|
||||
db.query(db::FileSetQuery)
|
||||
.set((), Arc::new(db::FileSet { files: file_ids, resolver }));
|
||||
let file_set = db.file_set();
|
||||
let symbol_index =
|
||||
SymbolIndex::for_files(file_set.files.par_iter()
|
||||
.map_with(db.clone(), |db, &file_id| (file_id, db.file_syntax(file_id))));
|
||||
|
||||
ReadonlySourceRoot { db, symbol_index: Arc::new(symbol_index) }
|
||||
}
|
||||
}
|
||||
|
||||
impl SourceRoot for ReadonlySourceRoot {
|
||||
fn contains(&self, file_id: FileId) -> bool {
|
||||
self.db.file_set().files.contains(&file_id)
|
||||
}
|
||||
fn db(&self) -> &db::RootDatabase {
|
||||
&self.db
|
||||
}
|
||||
fn symbols(&self, acc: &mut Vec<Arc<SymbolIndex>>) -> Cancelable<()> {
|
||||
acc.push(Arc::clone(&self.symbol_index));
|
||||
Ok(())
|
||||
}
|
||||
}
|
@ -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,16 @@ extern crate relative_path;
|
||||
extern crate rustc_hash;
|
||||
extern crate test_utils;
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::{
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
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 +46,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
|
||||
}
|
||||
|
||||
@ -126,17 +129,17 @@ fn test_resolve_crate_root() {
|
||||
let snap = host.analysis();
|
||||
assert!(snap.crate_for(FileId(2)).unwrap().is_empty());
|
||||
|
||||
let crate_graph = CrateGraph {
|
||||
crate_roots: {
|
||||
let mut m = FxHashMap::default();
|
||||
m.insert(CrateId(1), FileId(1));
|
||||
m
|
||||
},
|
||||
let crate_graph = {
|
||||
let mut g = CrateGraph::new();
|
||||
g.add_crate_root(FileId(1));
|
||||
g
|
||||
};
|
||||
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)],);
|
||||
assert_eq!(snap.crate_for(FileId(2)).unwrap(), vec![CrateId(0)],);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -8,7 +8,7 @@ use gen_lsp_server::{
|
||||
handle_shutdown, ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse,
|
||||
};
|
||||
use languageserver_types::NumberOrString;
|
||||
use ra_analysis::{FileId, LibraryData};
|
||||
use ra_analysis::{Canceled, FileId, LibraryData};
|
||||
use rayon::{self, ThreadPool};
|
||||
use rustc_hash::FxHashSet;
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
@ -376,7 +376,7 @@ impl<'a> PoolDispatcher<'a> {
|
||||
Err(e) => {
|
||||
match e.downcast::<LspError>() {
|
||||
Ok(lsp_error) => RawResponse::err(id, lsp_error.code, lsp_error.message),
|
||||
Err(e) => RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string())
|
||||
Err(e) => RawResponse::err(id, ErrorCode::InternalError as i32, format!("{}\n{}", e, e.backtrace()))
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -408,14 +408,22 @@ fn update_file_notifications_on_threadpool(
|
||||
pool.spawn(move || {
|
||||
for file_id in subscriptions {
|
||||
match handlers::publish_diagnostics(&world, file_id) {
|
||||
Err(e) => error!("failed to compute diagnostics: {:?}", e),
|
||||
Err(e) => {
|
||||
if !is_canceled(&e) {
|
||||
error!("failed to compute diagnostics: {:?}", e);
|
||||
}
|
||||
},
|
||||
Ok(params) => {
|
||||
let not = RawNotification::new::<req::PublishDiagnostics>(¶ms);
|
||||
sender.send(Task::Notify(not));
|
||||
}
|
||||
}
|
||||
match handlers::publish_decorations(&world, file_id) {
|
||||
Err(e) => error!("failed to compute decorations: {:?}", e),
|
||||
Err(e) => {
|
||||
if !is_canceled(&e) {
|
||||
error!("failed to compute decorations: {:?}", e);
|
||||
}
|
||||
},
|
||||
Ok(params) => {
|
||||
let not = RawNotification::new::<req::PublishDecorations>(¶ms);
|
||||
sender.send(Task::Notify(not))
|
||||
@ -432,3 +440,7 @@ fn feedback(intrnal_mode: bool, msg: &str, sender: &Sender<RawMessage>) {
|
||||
let not = RawNotification::new::<req::InternalFeedback>(&msg.to_string());
|
||||
sender.send(RawMessage::Notification(not));
|
||||
}
|
||||
|
||||
fn is_canceled(e: &failure::Error) -> bool {
|
||||
e.downcast_ref::<Canceled>().is_some()
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
use std::{
|
||||
fmt,
|
||||
path::{Component, Path, PathBuf},
|
||||
};
|
||||
|
||||
use im;
|
||||
use ra_analysis::{FileId, FileResolver};
|
||||
@ -10,7 +13,7 @@ pub enum Root {
|
||||
Lib,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
#[derive(Default, Clone)]
|
||||
pub struct PathMap {
|
||||
next_id: u32,
|
||||
path2id: im::HashMap<PathBuf, FileId>,
|
||||
@ -18,19 +21,28 @@ pub struct PathMap {
|
||||
id2root: im::HashMap<FileId, Root>,
|
||||
}
|
||||
|
||||
impl fmt::Debug for PathMap {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str("PathMap { ... }")
|
||||
}
|
||||
}
|
||||
|
||||
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 +117,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),)
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ use std::{
|
||||
};
|
||||
|
||||
use languageserver_types::Url;
|
||||
use ra_analysis::{Analysis, AnalysisHost, CrateGraph, CrateId, FileId, FileResolver, LibraryData};
|
||||
use ra_analysis::{Analysis, AnalysisHost, AnalysisChange, CrateGraph, FileId, FileResolver, LibraryData};
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use crate::{
|
||||
@ -39,30 +39,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 +86,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 +120,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 +139,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_graph = CrateGraph::new();
|
||||
ws.iter()
|
||||
.flat_map(|ws| {
|
||||
ws.packages()
|
||||
@ -134,13 +157,13 @@ impl ServerWorldState {
|
||||
})
|
||||
.for_each(|root| {
|
||||
if let Some(file_id) = self.path_map.get_id(root) {
|
||||
let crate_id = CrateId(crate_roots.len() as u32);
|
||||
crate_roots.insert(crate_id, file_id);
|
||||
crate_graph.add_crate_root(file_id);
|
||||
}
|
||||
});
|
||||
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…
x
Reference in New Issue
Block a user