2018-08-29 10:23:57 -05:00
|
|
|
use std::{
|
2018-10-15 16:44:23 -05:00
|
|
|
hash::{Hash, Hasher},
|
|
|
|
sync::Arc,
|
2018-08-29 10:23:57 -05:00
|
|
|
};
|
|
|
|
|
2018-10-31 12:59:17 -05:00
|
|
|
use ra_editor::{self, find_node_at_offset, FileSymbol, LineIndex, LocalEdit};
|
2018-09-16 04:54:24 -05:00
|
|
|
use ra_syntax::{
|
2018-10-15 16:44:23 -05:00
|
|
|
ast::{self, ArgListOwner, Expr, NameOwner},
|
|
|
|
AstNode, File, SmolStr,
|
2018-08-29 10:23:57 -05:00
|
|
|
SyntaxKind::*,
|
2018-10-15 16:44:23 -05:00
|
|
|
SyntaxNodeRef, TextRange, TextUnit,
|
2018-08-29 10:23:57 -05:00
|
|
|
};
|
2018-10-29 05:32:45 -05:00
|
|
|
use rayon::prelude::*;
|
2018-10-15 16:44:23 -05:00
|
|
|
use relative_path::RelativePath;
|
|
|
|
use rustc_hash::FxHashSet;
|
2018-10-25 02:57:55 -05:00
|
|
|
use salsa::{ParallelDatabase, Database};
|
2018-08-29 10:23:57 -05:00
|
|
|
|
2018-10-15 12:15:53 -05:00
|
|
|
use crate::{
|
2018-10-25 02:57:55 -05:00
|
|
|
AnalysisChange,
|
|
|
|
db::{
|
2018-10-29 05:58:11 -05:00
|
|
|
self, SyntaxDatabase, FileSyntaxQuery,
|
2018-10-25 02:57:55 -05:00
|
|
|
},
|
2018-10-25 09:52:50 -05:00
|
|
|
input::{SourceRootId, FilesDatabase, SourceRoot, WORKSPACE},
|
2018-10-31 07:13:49 -05:00
|
|
|
descriptors::{
|
2018-10-24 13:24:32 -05:00
|
|
|
DescriptorDatabase, DeclarationDescriptor,
|
2018-10-31 07:13:49 -05:00
|
|
|
module::{ModuleTree, Problem},
|
|
|
|
function::{FnDescriptor, FnId},
|
|
|
|
},
|
2018-10-31 12:59:17 -05:00
|
|
|
completion::{scope_completion, resolve_based_completion, CompletionItem},
|
2018-10-29 05:32:45 -05:00
|
|
|
symbol_index::SymbolIndex,
|
2018-10-31 07:13:49 -05:00
|
|
|
syntax_ptr::SyntaxPtrDatabase,
|
2018-10-20 14:09:12 -05:00
|
|
|
CrateGraph, CrateId, Diagnostic, FileId, FileResolver, FileSystemEdit, Position,
|
2018-10-20 13:52:49 -05:00
|
|
|
Query, SourceChange, SourceFileEdit, Cancelable,
|
2018-08-29 10:23:57 -05:00
|
|
|
};
|
|
|
|
|
2018-09-10 04:57:40 -05:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub(crate) struct FileResolverImp {
|
2018-10-15 16:44:23 -05:00
|
|
|
inner: Arc<FileResolver>,
|
2018-09-10 04:57:40 -05:00
|
|
|
}
|
|
|
|
|
2018-10-15 13:54:12 -05:00
|
|
|
impl PartialEq for FileResolverImp {
|
|
|
|
fn eq(&self, other: &FileResolverImp) -> bool {
|
|
|
|
self.inner() == other.inner()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 16:44:23 -05:00
|
|
|
impl Eq for FileResolverImp {}
|
2018-10-15 13:54:12 -05:00
|
|
|
|
|
|
|
impl Hash for FileResolverImp {
|
|
|
|
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
|
|
|
self.inner().hash(hasher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 04:57:40 -05:00
|
|
|
impl FileResolverImp {
|
|
|
|
pub(crate) fn new(inner: Arc<FileResolver>) -> FileResolverImp {
|
|
|
|
FileResolverImp { inner }
|
|
|
|
}
|
|
|
|
pub(crate) fn file_stem(&self, file_id: FileId) -> String {
|
|
|
|
self.inner.file_stem(file_id)
|
|
|
|
}
|
|
|
|
pub(crate) fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId> {
|
|
|
|
self.inner.resolve(file_id, path)
|
|
|
|
}
|
2018-10-15 13:54:12 -05:00
|
|
|
fn inner(&self) -> *const FileResolver {
|
|
|
|
&*self.inner
|
|
|
|
}
|
2018-09-10 04:57:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FileResolverImp {
|
|
|
|
fn default() -> FileResolverImp {
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct DummyResolver;
|
|
|
|
impl FileResolver for DummyResolver {
|
|
|
|
fn file_stem(&self, _file_: FileId) -> String {
|
|
|
|
panic!("file resolver not set")
|
|
|
|
}
|
2018-10-15 16:44:23 -05:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
_file_id: FileId,
|
|
|
|
_path: &::relative_path::RelativePath,
|
|
|
|
) -> Option<FileId> {
|
2018-09-10 04:57:40 -05:00
|
|
|
panic!("file resolver not set")
|
|
|
|
}
|
|
|
|
}
|
2018-10-15 16:44:23 -05:00
|
|
|
FileResolverImp {
|
|
|
|
inner: Arc::new(DummyResolver),
|
|
|
|
}
|
2018-09-10 04:57:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 16:59:43 -05:00
|
|
|
#[derive(Debug)]
|
2018-08-30 04:51:46 -05:00
|
|
|
pub(crate) struct AnalysisHostImpl {
|
2018-10-25 02:57:55 -05:00
|
|
|
db: db::RootDatabase,
|
2018-08-30 04:51:46 -05:00
|
|
|
}
|
|
|
|
|
2018-10-25 02:57:55 -05:00
|
|
|
|
2018-08-30 04:51:46 -05:00
|
|
|
impl AnalysisHostImpl {
|
|
|
|
pub fn new() -> AnalysisHostImpl {
|
2018-10-30 16:59:43 -05:00
|
|
|
let db = db::RootDatabase::default();
|
|
|
|
db.query(crate::input::SourceRootQuery)
|
|
|
|
.set(WORKSPACE, Default::default());
|
|
|
|
db.query(crate::input::CrateGraphQuery)
|
|
|
|
.set((), Default::default());
|
2018-10-31 13:53:09 -05:00
|
|
|
db.query(crate::input::LibrariesQuery)
|
|
|
|
.set((), Default::default());
|
2018-10-30 16:59:43 -05:00
|
|
|
AnalysisHostImpl { db }
|
2018-08-30 04:51:46 -05:00
|
|
|
}
|
2018-09-10 04:57:40 -05:00
|
|
|
pub fn analysis(&self) -> AnalysisImpl {
|
2018-08-30 04:51:46 -05:00
|
|
|
AnalysisImpl {
|
2018-10-25 02:57:55 -05:00
|
|
|
db: self.db.fork() // freeze revision here
|
2018-08-30 04:51:46 -05:00
|
|
|
}
|
|
|
|
}
|
2018-10-25 02:57:55 -05:00
|
|
|
pub fn apply_change(&mut self, change: AnalysisChange) {
|
2018-10-25 08:03:49 -05:00
|
|
|
log::info!("apply_change {:?}", change);
|
|
|
|
|
2018-10-25 02:57:55 -05:00
|
|
|
for (file_id, text) in change.files_changed {
|
|
|
|
self.db
|
2018-10-25 09:52:50 -05:00
|
|
|
.query(crate::input::FileTextQuery)
|
2018-10-25 02:57:55 -05:00
|
|
|
.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
|
2018-10-25 09:52:50 -05:00
|
|
|
.query(crate::input::FileTextQuery)
|
2018-10-25 02:57:55 -05:00
|
|
|
.set(file_id, Arc::new(text));
|
|
|
|
self.db
|
2018-10-25 09:52:50 -05:00
|
|
|
.query(crate::input::FileSourceRootQuery)
|
|
|
|
.set(file_id, crate::input::WORKSPACE);
|
2018-10-25 02:57:55 -05:00
|
|
|
source_root.files.insert(file_id);
|
|
|
|
}
|
|
|
|
for file_id in change.files_removed {
|
|
|
|
self.db
|
2018-10-25 09:52:50 -05:00
|
|
|
.query(crate::input::FileTextQuery)
|
2018-10-25 02:57:55 -05:00
|
|
|
.set(file_id, Arc::new(String::new()));
|
|
|
|
source_root.files.remove(&file_id);
|
2018-08-31 11:14:08 -05:00
|
|
|
}
|
2018-10-25 02:57:55 -05:00
|
|
|
source_root.file_resolver = file_resolver;
|
|
|
|
self.db
|
2018-10-25 09:52:50 -05:00
|
|
|
.query(crate::input::SourceRootQuery)
|
2018-10-25 02:57:55 -05:00
|
|
|
.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
|
2018-10-25 09:52:50 -05:00
|
|
|
.query(crate::input::FileSourceRootQuery)
|
2018-10-25 02:57:55 -05:00
|
|
|
.set_constant(file_id, source_root_id);
|
|
|
|
self.db
|
2018-10-25 09:52:50 -05:00
|
|
|
.query(crate::input::FileTextQuery)
|
2018-10-25 02:57:55 -05:00
|
|
|
.set_constant(file_id, Arc::new(text));
|
|
|
|
}
|
|
|
|
let source_root = SourceRoot {
|
|
|
|
files,
|
|
|
|
file_resolver: library.file_resolver,
|
|
|
|
};
|
|
|
|
self.db
|
2018-10-25 09:52:50 -05:00
|
|
|
.query(crate::input::SourceRootQuery)
|
2018-10-25 02:57:55 -05:00
|
|
|
.set(source_root_id, Arc::new(source_root));
|
|
|
|
self.db
|
2018-10-25 09:52:50 -05:00
|
|
|
.query(crate::input::LibrarySymbolsQuery)
|
2018-10-25 02:57:55 -05:00
|
|
|
.set(source_root_id, Arc::new(library.symbol_index));
|
|
|
|
}
|
|
|
|
self.db
|
2018-10-31 13:05:14 -05:00
|
|
|
.query(crate::input::LibrariesQuery)
|
2018-10-25 02:57:55 -05:00
|
|
|
.set((), Arc::new(libraries));
|
|
|
|
}
|
|
|
|
if let Some(crate_graph) = change.crate_graph {
|
2018-10-25 09:52:50 -05:00
|
|
|
self.db.query(crate::input::CrateGraphQuery)
|
2018-10-25 02:57:55 -05:00
|
|
|
.set((), Arc::new(crate_graph))
|
2018-08-31 11:14:08 -05:00
|
|
|
}
|
2018-08-30 04:51:46 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-29 10:23:57 -05:00
|
|
|
|
2018-10-25 02:57:55 -05:00
|
|
|
#[derive(Debug)]
|
2018-08-29 10:23:57 -05:00
|
|
|
pub(crate) struct AnalysisImpl {
|
2018-10-31 12:59:17 -05:00
|
|
|
pub(crate) db: db::RootDatabase,
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AnalysisImpl {
|
2018-09-15 15:19:41 -05:00
|
|
|
pub fn file_syntax(&self, file_id: FileId) -> File {
|
2018-10-25 02:57:55 -05:00
|
|
|
self.db.file_syntax(file_id)
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
2018-09-15 15:19:41 -05:00
|
|
|
pub fn file_line_index(&self, file_id: FileId) -> Arc<LineIndex> {
|
2018-10-25 02:57:55 -05:00
|
|
|
self.db.file_lines(file_id)
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
2018-10-20 14:29:26 -05:00
|
|
|
pub fn world_symbols(&self, query: Query) -> Cancelable<Vec<(FileId, FileSymbol)>> {
|
2018-10-29 05:32:45 -05:00
|
|
|
let buf: Vec<Arc<SymbolIndex>> = if query.libs {
|
|
|
|
self.db.libraries().iter()
|
|
|
|
.map(|&lib_id| self.db.library_symbols(lib_id))
|
|
|
|
.collect()
|
2018-10-25 09:59:03 -05:00
|
|
|
} else {
|
2018-10-29 05:32:45 -05:00
|
|
|
let files = &self.db.source_root(WORKSPACE).files;
|
|
|
|
let db = self.db.clone();
|
|
|
|
files.par_iter()
|
|
|
|
.map_with(db, |db, &file_id| db.file_symbols(file_id))
|
|
|
|
.filter_map(|it| it.ok())
|
|
|
|
.collect()
|
|
|
|
};
|
2018-10-29 05:58:11 -05:00
|
|
|
self.db.query(FileSyntaxQuery)
|
|
|
|
.sweep(salsa::SweepStrategy::default().discard_values());
|
2018-10-20 14:29:26 -05:00
|
|
|
Ok(query.search(&buf))
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
2018-10-25 02:57:55 -05:00
|
|
|
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)
|
|
|
|
}
|
2018-10-20 13:52:49 -05:00
|
|
|
pub fn parent_module(&self, file_id: FileId) -> Cancelable<Vec<(FileId, FileSymbol)>> {
|
2018-10-25 02:57:55 -05:00
|
|
|
let module_tree = self.module_tree(file_id)?;
|
2018-10-23 11:15:31 -05:00
|
|
|
|
|
|
|
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);
|
2018-10-25 02:57:55 -05:00
|
|
|
let syntax = self.db.file_syntax(file_id);
|
2018-09-15 12:29:22 -05:00
|
|
|
let decl = link.bind_source(&module_tree, syntax.ast());
|
2018-10-23 11:15:31 -05:00
|
|
|
|
2018-08-29 10:23:57 -05:00
|
|
|
let sym = FileSymbol {
|
2018-10-23 11:15:31 -05:00
|
|
|
name: decl.name().unwrap().text(),
|
2018-09-15 12:29:22 -05:00
|
|
|
node_range: decl.syntax().range(),
|
2018-08-29 10:23:57 -05:00
|
|
|
kind: MODULE,
|
|
|
|
};
|
2018-10-23 11:15:31 -05:00
|
|
|
Some((file_id, sym))
|
2018-08-29 10:23:57 -05:00
|
|
|
})
|
2018-10-20 13:52:49 -05:00
|
|
|
.collect();
|
|
|
|
Ok(res)
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
2018-10-20 14:15:03 -05:00
|
|
|
pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
|
2018-10-25 02:57:55 -05:00
|
|
|
let module_tree = self.module_tree(file_id)?;
|
|
|
|
let crate_graph = self.db.crate_graph();
|
2018-10-23 11:15:31 -05:00
|
|
|
let res = module_tree.modules_for_file(file_id)
|
|
|
|
.into_iter()
|
|
|
|
.map(|it| it.root(&module_tree))
|
|
|
|
.map(|it| it.file_id(&module_tree))
|
|
|
|
.filter_map(|it| crate_graph.crate_id_for_crate_root(it))
|
|
|
|
.collect();
|
|
|
|
|
2018-10-20 14:15:03 -05:00
|
|
|
Ok(res)
|
2018-08-31 11:14:08 -05:00
|
|
|
}
|
2018-09-02 08:36:03 -05:00
|
|
|
pub fn crate_root(&self, crate_id: CrateId) -> FileId {
|
2018-10-25 02:57:55 -05:00
|
|
|
self.db.crate_graph().crate_roots[&crate_id]
|
2018-09-02 08:36:03 -05:00
|
|
|
}
|
2018-10-24 10:37:25 -05:00
|
|
|
pub fn completions(&self, file_id: FileId, offset: TextUnit) -> Cancelable<Option<Vec<CompletionItem>>> {
|
|
|
|
let mut res = Vec::new();
|
|
|
|
let mut has_completions = false;
|
2018-10-31 12:59:17 -05:00
|
|
|
if let Some(scope_based) = scope_completion(&self.db, file_id, offset) {
|
2018-10-24 10:37:25 -05:00
|
|
|
res.extend(scope_based);
|
|
|
|
has_completions = true;
|
|
|
|
}
|
2018-10-31 12:59:17 -05:00
|
|
|
if let Some(scope_based) = resolve_based_completion(&self.db, file_id, offset)? {
|
2018-10-24 10:37:25 -05:00
|
|
|
res.extend(scope_based);
|
|
|
|
has_completions = true;
|
|
|
|
}
|
|
|
|
let res = if has_completions {
|
|
|
|
Some(res)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
Ok(res)
|
|
|
|
}
|
2018-08-29 10:23:57 -05:00
|
|
|
pub fn approximately_resolve_symbol(
|
|
|
|
&self,
|
2018-09-03 11:46:30 -05:00
|
|
|
file_id: FileId,
|
2018-08-29 10:23:57 -05:00
|
|
|
offset: TextUnit,
|
2018-10-20 14:15:03 -05:00
|
|
|
) -> Cancelable<Vec<(FileId, FileSymbol)>> {
|
2018-10-25 02:57:55 -05:00
|
|
|
let module_tree = self.module_tree(file_id)?;
|
|
|
|
let file = self.db.file_syntax(file_id);
|
2018-08-29 10:23:57 -05:00
|
|
|
let syntax = file.syntax();
|
|
|
|
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) {
|
2018-10-05 14:21:40 -05:00
|
|
|
// First try to resolve the symbol locally
|
2018-10-31 07:13:49 -05:00
|
|
|
return if let Some((name, range)) = resolve_local_name(&self.db, file_id, name_ref) {
|
2018-10-06 11:02:15 -05:00
|
|
|
let mut vec = vec![];
|
2018-10-15 16:44:23 -05:00
|
|
|
vec.push((
|
|
|
|
file_id,
|
|
|
|
FileSymbol {
|
|
|
|
name,
|
|
|
|
node_range: range,
|
|
|
|
kind: NAME,
|
|
|
|
},
|
|
|
|
));
|
2018-10-20 14:29:26 -05:00
|
|
|
Ok(vec)
|
2018-10-05 14:21:40 -05:00
|
|
|
} else {
|
|
|
|
// If that fails try the index based approach.
|
2018-10-20 14:29:26 -05:00
|
|
|
self.index_resolve(name_ref)
|
|
|
|
};
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
|
|
|
if let Some(name) = find_node_at_offset::<ast::Name>(syntax, offset) {
|
|
|
|
if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) {
|
|
|
|
if module.has_semi() {
|
2018-09-15 12:29:22 -05:00
|
|
|
let file_ids = self.resolve_module(&*module_tree, file_id, module);
|
2018-08-29 10:23:57 -05:00
|
|
|
|
2018-10-15 16:44:23 -05:00
|
|
|
let res = file_ids
|
|
|
|
.into_iter()
|
|
|
|
.map(|id| {
|
|
|
|
let name = module
|
|
|
|
.name()
|
|
|
|
.map(|n| n.text())
|
|
|
|
.unwrap_or_else(|| SmolStr::new(""));
|
|
|
|
let symbol = FileSymbol {
|
|
|
|
name,
|
|
|
|
node_range: TextRange::offset_len(0.into(), 0.into()),
|
|
|
|
kind: MODULE,
|
|
|
|
};
|
|
|
|
(id, symbol)
|
|
|
|
})
|
|
|
|
.collect();
|
2018-08-29 10:23:57 -05:00
|
|
|
|
2018-10-20 14:15:03 -05:00
|
|
|
return Ok(res);
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-20 14:15:03 -05:00
|
|
|
Ok(vec![])
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
|
|
|
|
2018-10-20 14:02:41 -05:00
|
|
|
pub fn find_all_refs(&self, file_id: FileId, offset: TextUnit) -> Vec<(FileId, TextRange)> {
|
2018-10-25 02:57:55 -05:00
|
|
|
let file = self.db.file_syntax(file_id);
|
2018-10-18 12:40:12 -05:00
|
|
|
let syntax = file.syntax();
|
|
|
|
|
|
|
|
let mut ret = vec![];
|
|
|
|
|
2018-10-24 13:24:32 -05:00
|
|
|
if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, offset) {
|
|
|
|
let decl = DeclarationDescriptor::new(binding);
|
|
|
|
|
|
|
|
ret.push((file_id, decl.range));
|
|
|
|
|
|
|
|
ret.extend(decl.find_all_refs().into_iter()
|
|
|
|
.map(|ref_desc| (file_id, ref_desc.range )));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-10-18 12:40:12 -05:00
|
|
|
// Find the symbol we are looking for
|
|
|
|
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) {
|
|
|
|
|
|
|
|
// We are only handing local references for now
|
2018-10-31 07:13:49 -05:00
|
|
|
if let Some(resolved) = resolve_local_name(&self.db, file_id, name_ref) {
|
2018-10-18 12:40:12 -05:00
|
|
|
|
|
|
|
ret.push((file_id, resolved.1));
|
|
|
|
|
|
|
|
if let Some(fn_def) = find_node_at_offset::<ast::FnDef>(syntax, offset) {
|
|
|
|
|
|
|
|
let refs : Vec<_> = fn_def.syntax().descendants()
|
|
|
|
.filter_map(ast::NameRef::cast)
|
2018-10-31 07:13:49 -05:00
|
|
|
.filter(|&n: &ast::NameRef| resolve_local_name(&self.db, file_id, n) == Some(resolved.clone()))
|
2018-10-18 12:40:12 -05:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
for r in refs {
|
|
|
|
ret.push((file_id, r.syntax().range()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
2018-10-20 14:15:03 -05:00
|
|
|
pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
|
2018-10-25 02:57:55 -05:00
|
|
|
let module_tree = self.module_tree(file_id)?;
|
|
|
|
let syntax = self.db.file_syntax(file_id);
|
2018-09-03 11:46:30 -05:00
|
|
|
|
2018-09-16 04:54:24 -05:00
|
|
|
let mut res = ra_editor::diagnostics(&syntax)
|
2018-08-29 10:23:57 -05:00
|
|
|
.into_iter()
|
2018-10-15 16:44:23 -05:00
|
|
|
.map(|d| Diagnostic {
|
|
|
|
range: d.range,
|
|
|
|
message: d.msg,
|
|
|
|
fix: None,
|
|
|
|
})
|
2018-08-29 10:23:57 -05:00
|
|
|
.collect::<Vec<_>>();
|
2018-10-23 11:15:31 -05:00
|
|
|
if let Some(m) = module_tree.any_module_for_file(file_id) {
|
|
|
|
for (name_node, problem) in m.problems(&module_tree, syntax.ast()) {
|
|
|
|
let diag = match problem {
|
|
|
|
Problem::UnresolvedModule { candidate } => {
|
|
|
|
let create_file = FileSystemEdit::CreateFile {
|
|
|
|
anchor: file_id,
|
|
|
|
path: candidate.clone(),
|
|
|
|
};
|
|
|
|
let fix = SourceChange {
|
|
|
|
label: "create module".to_string(),
|
|
|
|
source_file_edits: Vec::new(),
|
|
|
|
file_system_edits: vec![create_file],
|
|
|
|
cursor_position: None,
|
|
|
|
};
|
|
|
|
Diagnostic {
|
|
|
|
range: name_node.range(),
|
|
|
|
message: "unresolved module".to_string(),
|
|
|
|
fix: Some(fix),
|
|
|
|
}
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
2018-10-23 11:15:31 -05:00
|
|
|
Problem::NotDirOwner { move_to, candidate } => {
|
|
|
|
let move_file = FileSystemEdit::MoveFile {
|
|
|
|
file: file_id,
|
|
|
|
path: move_to.clone(),
|
|
|
|
};
|
|
|
|
let create_file = FileSystemEdit::CreateFile {
|
|
|
|
anchor: file_id,
|
|
|
|
path: move_to.join(candidate),
|
|
|
|
};
|
|
|
|
let fix = SourceChange {
|
|
|
|
label: "move file and create module".to_string(),
|
|
|
|
source_file_edits: Vec::new(),
|
|
|
|
file_system_edits: vec![move_file, create_file],
|
|
|
|
cursor_position: None,
|
|
|
|
};
|
|
|
|
Diagnostic {
|
|
|
|
range: name_node.range(),
|
|
|
|
message: "can't declare module at this location".to_string(),
|
|
|
|
fix: Some(fix),
|
|
|
|
}
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
2018-10-23 11:15:31 -05:00
|
|
|
};
|
|
|
|
res.push(diag)
|
|
|
|
}
|
|
|
|
};
|
2018-10-20 14:15:03 -05:00
|
|
|
Ok(res)
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
|
|
|
|
2018-09-05 16:59:07 -05:00
|
|
|
pub fn assists(&self, file_id: FileId, range: TextRange) -> Vec<SourceChange> {
|
2018-08-29 10:23:57 -05:00
|
|
|
let file = self.file_syntax(file_id);
|
2018-09-05 16:59:07 -05:00
|
|
|
let offset = range.start();
|
2018-08-29 10:23:57 -05:00
|
|
|
let actions = vec![
|
2018-10-15 16:44:23 -05:00
|
|
|
(
|
|
|
|
"flip comma",
|
|
|
|
ra_editor::flip_comma(&file, offset).map(|f| f()),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"add `#[derive]`",
|
|
|
|
ra_editor::add_derive(&file, offset).map(|f| f()),
|
|
|
|
),
|
2018-09-16 04:54:24 -05:00
|
|
|
("add impl", ra_editor::add_impl(&file, offset).map(|f| f())),
|
2018-10-15 16:44:23 -05:00
|
|
|
(
|
|
|
|
"introduce variable",
|
|
|
|
ra_editor::introduce_variable(&file, range).map(|f| f()),
|
|
|
|
),
|
2018-08-29 10:23:57 -05:00
|
|
|
];
|
2018-10-15 16:44:23 -05:00
|
|
|
actions
|
|
|
|
.into_iter()
|
2018-08-30 08:27:09 -05:00
|
|
|
.filter_map(|(name, local_edit)| {
|
2018-10-15 16:44:23 -05:00
|
|
|
Some(SourceChange::from_local_edit(file_id, name, local_edit?))
|
2018-08-30 08:27:09 -05:00
|
|
|
})
|
|
|
|
.collect()
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
|
|
|
|
2018-10-15 16:44:23 -05:00
|
|
|
pub fn resolve_callable(
|
|
|
|
&self,
|
|
|
|
file_id: FileId,
|
|
|
|
offset: TextUnit,
|
2018-10-20 14:29:26 -05:00
|
|
|
) -> Cancelable<Option<(FnDescriptor, Option<usize>)>> {
|
2018-10-25 02:57:55 -05:00
|
|
|
let file = self.db.file_syntax(file_id);
|
2018-10-09 09:08:17 -05:00
|
|
|
let syntax = file.syntax();
|
|
|
|
|
|
|
|
// Find the calling expression and it's NameRef
|
2018-10-20 14:29:26 -05:00
|
|
|
let calling_node = match FnCallNode::with_node(syntax, offset) {
|
|
|
|
Some(node) => node,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
let name_ref = match calling_node.name_ref() {
|
|
|
|
Some(name) => name,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
2018-10-09 09:08:17 -05:00
|
|
|
|
|
|
|
// Resolve the function's NameRef (NOTE: this isn't entirely accurate).
|
2018-10-20 14:29:26 -05:00
|
|
|
let file_symbols = self.index_resolve(name_ref)?;
|
2018-10-25 08:25:24 -05:00
|
|
|
for (fn_fiel_id, fs) in file_symbols {
|
2018-10-09 09:08:17 -05:00
|
|
|
if fs.kind == FN_DEF {
|
2018-10-25 08:25:24 -05:00
|
|
|
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()) {
|
2018-10-12 06:54:57 -05:00
|
|
|
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;
|
|
|
|
|
|
|
|
let num_params = descriptor.params.len();
|
2018-10-15 16:44:23 -05:00
|
|
|
let has_self = fn_def.param_list().and_then(|l| l.self_param()).is_some();
|
2018-10-12 06:54:57 -05:00
|
|
|
|
|
|
|
if num_params == 1 {
|
|
|
|
if !has_self {
|
2018-10-30 13:09:53 -05:00
|
|
|
current_parameter = Some(0);
|
2018-10-12 06:54:57 -05:00
|
|
|
}
|
|
|
|
} else if num_params > 1 {
|
|
|
|
// Count how many parameters into the call we are.
|
|
|
|
// TODO: This is best effort for now and should be fixed at some point.
|
|
|
|
// It may be better to see where we are in the arg_list and then check
|
|
|
|
// where offset is in that list (or beyond).
|
|
|
|
// Revisit this after we get documentation comments in.
|
|
|
|
if let Some(ref arg_list) = calling_node.arg_list() {
|
|
|
|
let start = arg_list.syntax().range().start();
|
|
|
|
|
|
|
|
let range_search = TextRange::from_to(start, offset);
|
2018-10-15 16:44:23 -05:00
|
|
|
let mut commas: usize = arg_list
|
|
|
|
.syntax()
|
|
|
|
.text()
|
|
|
|
.slice(range_search)
|
|
|
|
.to_string()
|
2018-10-16 10:45:10 -05:00
|
|
|
.matches(',')
|
2018-10-12 06:54:57 -05:00
|
|
|
.count();
|
|
|
|
|
|
|
|
// If we have a method call eat the first param since it's just self.
|
|
|
|
if has_self {
|
2018-10-17 18:25:37 -05:00
|
|
|
commas += 1;
|
2018-10-12 06:54:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
current_parameter = Some(commas);
|
2018-10-09 09:08:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 14:29:26 -05:00
|
|
|
return Ok(Some((descriptor, current_parameter)));
|
2018-10-12 06:54:57 -05:00
|
|
|
}
|
2018-10-09 09:08:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 14:29:26 -05:00
|
|
|
Ok(None)
|
2018-10-09 09:08:17 -05:00
|
|
|
}
|
|
|
|
|
2018-10-20 14:29:26 -05:00
|
|
|
fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable<Vec<(FileId, FileSymbol)>> {
|
2018-08-29 10:23:57 -05:00
|
|
|
let name = name_ref.text();
|
|
|
|
let mut query = Query::new(name.to_string());
|
|
|
|
query.exact();
|
|
|
|
query.limit(4);
|
2018-10-20 14:02:41 -05:00
|
|
|
self.world_symbols(query)
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
|
|
|
|
2018-10-15 16:44:23 -05:00
|
|
|
fn resolve_module(
|
|
|
|
&self,
|
2018-10-23 11:15:31 -05:00
|
|
|
module_tree: &ModuleTree,
|
2018-10-15 16:44:23 -05:00
|
|
|
file_id: FileId,
|
|
|
|
module: ast::Module,
|
|
|
|
) -> Vec<FileId> {
|
2018-08-29 10:23:57 -05:00
|
|
|
let name = match module.name() {
|
|
|
|
Some(name) => name.text(),
|
|
|
|
None => return Vec::new(),
|
|
|
|
};
|
2018-10-23 11:15:31 -05:00
|
|
|
let module_id = match module_tree.any_module_for_file(file_id) {
|
|
|
|
Some(id) => id,
|
|
|
|
None => return Vec::new(),
|
|
|
|
};
|
|
|
|
module_id.child(module_tree, name.as_str())
|
|
|
|
.map(|it| it.file_id(module_tree))
|
|
|
|
.into_iter()
|
|
|
|
.collect()
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 04:34:31 -05:00
|
|
|
impl SourceChange {
|
|
|
|
pub(crate) fn from_local_edit(file_id: FileId, label: &str, edit: LocalEdit) -> SourceChange {
|
|
|
|
let file_edit = SourceFileEdit {
|
|
|
|
file_id,
|
|
|
|
edits: edit.edit.into_atoms(),
|
|
|
|
};
|
|
|
|
SourceChange {
|
|
|
|
label: label.to_string(),
|
|
|
|
source_file_edits: vec![file_edit],
|
|
|
|
file_system_edits: vec![],
|
2018-10-15 16:44:23 -05:00
|
|
|
cursor_position: edit
|
|
|
|
.cursor_position
|
|
|
|
.map(|offset| Position { offset, file_id }),
|
2018-08-30 04:34:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-31 11:14:08 -05:00
|
|
|
|
|
|
|
impl CrateGraph {
|
|
|
|
fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> {
|
2018-10-15 16:44:23 -05:00
|
|
|
let (&crate_id, _) = self
|
|
|
|
.crate_roots
|
2018-08-31 11:14:08 -05:00
|
|
|
.iter()
|
|
|
|
.find(|(_crate_id, &root_id)| root_id == file_id)?;
|
|
|
|
Some(crate_id)
|
|
|
|
}
|
|
|
|
}
|
2018-10-09 09:08:17 -05:00
|
|
|
|
|
|
|
enum FnCallNode<'a> {
|
|
|
|
CallExpr(ast::CallExpr<'a>),
|
2018-10-15 16:44:23 -05:00
|
|
|
MethodCallExpr(ast::MethodCallExpr<'a>),
|
2018-10-09 09:08:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FnCallNode<'a> {
|
|
|
|
pub fn with_node(syntax: SyntaxNodeRef, offset: TextUnit) -> Option<FnCallNode> {
|
|
|
|
if let Some(expr) = find_node_at_offset::<ast::CallExpr>(syntax, offset) {
|
|
|
|
return Some(FnCallNode::CallExpr(expr));
|
|
|
|
}
|
|
|
|
if let Some(expr) = find_node_at_offset::<ast::MethodCallExpr>(syntax, offset) {
|
|
|
|
return Some(FnCallNode::MethodCallExpr(expr));
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn name_ref(&self) -> Option<ast::NameRef> {
|
|
|
|
match *self {
|
2018-10-15 16:44:23 -05:00
|
|
|
FnCallNode::CallExpr(call_expr) => Some(match call_expr.expr()? {
|
|
|
|
Expr::PathExpr(path_expr) => path_expr.path()?.segment()?.name_ref()?,
|
|
|
|
_ => return None,
|
|
|
|
}),
|
|
|
|
|
|
|
|
FnCallNode::MethodCallExpr(call_expr) => call_expr
|
|
|
|
.syntax()
|
|
|
|
.children()
|
|
|
|
.filter_map(ast::NameRef::cast)
|
|
|
|
.nth(0),
|
2018-10-09 09:08:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn arg_list(&self) -> Option<ast::ArgList> {
|
|
|
|
match *self {
|
|
|
|
FnCallNode::CallExpr(expr) => expr.arg_list(),
|
2018-10-15 16:44:23 -05:00
|
|
|
FnCallNode::MethodCallExpr(expr) => expr.arg_list(),
|
2018-10-09 09:08:17 -05:00
|
|
|
}
|
|
|
|
}
|
2018-10-15 12:15:53 -05:00
|
|
|
}
|
2018-10-31 07:13:49 -05:00
|
|
|
|
|
|
|
fn resolve_local_name(
|
|
|
|
db: &db::RootDatabase,
|
|
|
|
file_id: FileId,
|
|
|
|
name_ref: ast::NameRef,
|
|
|
|
) -> Option<(SmolStr, TextRange)> {
|
|
|
|
let fn_def = name_ref.syntax().ancestors().find_map(ast::FnDef::cast)?;
|
|
|
|
let fn_id = FnId::new(file_id, fn_def);
|
|
|
|
let scopes = db.fn_scopes(fn_id);
|
|
|
|
let scope_entry = crate::descriptors::function::resolve_local_name(name_ref, &scopes)?;
|
|
|
|
let syntax = db.resolve_syntax_ptr(scope_entry.ptr().into_global(file_id));
|
|
|
|
Some((scope_entry.name().clone(), syntax.range()))
|
|
|
|
}
|