2018-08-29 10:23:57 -05:00
|
|
|
use std::{
|
2018-11-01 07:29:23 -05:00
|
|
|
fmt,
|
2018-10-15 16:44:23 -05:00
|
|
|
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},
|
2018-11-27 16:11:29 -06:00
|
|
|
AstNode, SourceFileNode,
|
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-11-27 18:25:20 -06:00
|
|
|
use ra_db::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE, SyntaxDatabase, SourceFileQuery};
|
2018-10-29 05:32:45 -05:00
|
|
|
use rayon::prelude::*;
|
2018-10-15 16:44:23 -05:00
|
|
|
use rustc_hash::FxHashSet;
|
2018-10-31 15:41:43 -05:00
|
|
|
use salsa::{Database, ParallelDatabase};
|
2018-11-27 19:09:44 -06:00
|
|
|
use hir::{
|
|
|
|
self,
|
|
|
|
FnSignatureInfo,
|
|
|
|
Problem,
|
|
|
|
};
|
2018-08-29 10:23:57 -05:00
|
|
|
|
2018-10-15 12:15:53 -05:00
|
|
|
use crate::{
|
2018-11-07 10:49:49 -06:00
|
|
|
completion::{completions, CompletionItem},
|
2018-11-27 18:25:20 -06:00
|
|
|
db,
|
2018-11-27 17:22:25 -06:00
|
|
|
symbol_index::{SymbolIndex, SymbolsDatabase},
|
2018-11-27 18:25:20 -06:00
|
|
|
AnalysisChange, Cancelable, CrateId, Diagnostic, FileId,
|
2018-11-07 09:32:33 -06:00
|
|
|
FileSystemEdit, FilePosition, Query, SourceChange, SourceFileNodeEdit,
|
2018-08-29 10:23:57 -05:00
|
|
|
};
|
|
|
|
|
2018-11-04 05:09:21 -06:00
|
|
|
#[derive(Debug, Default)]
|
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
|
|
|
}
|
|
|
|
|
|
|
|
impl AnalysisHostImpl {
|
2018-09-10 04:57:40 -05:00
|
|
|
pub fn analysis(&self) -> AnalysisImpl {
|
2018-08-30 04:51:46 -05:00
|
|
|
AnalysisImpl {
|
2018-11-01 07:29:23 -05:00
|
|
|
db: self.db.snapshot(),
|
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-11-27 18:25:20 -06:00
|
|
|
.query_mut(ra_db::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()) {
|
2018-10-31 15:41:43 -05:00
|
|
|
let file_resolver = change
|
|
|
|
.file_resolver
|
2018-10-25 02:57:55 -05:00
|
|
|
.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-11-27 18:25:20 -06:00
|
|
|
.query_mut(ra_db::FileTextQuery)
|
2018-10-25 02:57:55 -05:00
|
|
|
.set(file_id, Arc::new(text));
|
|
|
|
self.db
|
2018-11-27 18:25:20 -06:00
|
|
|
.query_mut(ra_db::FileSourceRootQuery)
|
|
|
|
.set(file_id, ra_db::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-11-27 18:25:20 -06:00
|
|
|
.query_mut(ra_db::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-11-27 18:25:20 -06:00
|
|
|
.query_mut(ra_db::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);
|
2018-11-26 15:12:43 -06:00
|
|
|
log::debug!(
|
|
|
|
"library file: {:?} {:?}",
|
|
|
|
file_id,
|
|
|
|
library.file_resolver.debug_path(file_id)
|
|
|
|
);
|
2018-10-25 02:57:55 -05:00
|
|
|
self.db
|
2018-11-27 18:25:20 -06:00
|
|
|
.query_mut(ra_db::FileSourceRootQuery)
|
2018-10-25 02:57:55 -05:00
|
|
|
.set_constant(file_id, source_root_id);
|
|
|
|
self.db
|
2018-11-27 18:25:20 -06:00
|
|
|
.query_mut(ra_db::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-11-27 18:25:20 -06:00
|
|
|
.query_mut(ra_db::SourceRootQuery)
|
2018-10-25 02:57:55 -05:00
|
|
|
.set(source_root_id, Arc::new(source_root));
|
|
|
|
self.db
|
2018-11-27 17:22:25 -06:00
|
|
|
.query_mut(crate::symbol_index::LibrarySymbolsQuery)
|
2018-10-25 02:57:55 -05:00
|
|
|
.set(source_root_id, Arc::new(library.symbol_index));
|
|
|
|
}
|
|
|
|
self.db
|
2018-11-27 18:25:20 -06:00
|
|
|
.query_mut(ra_db::LibrariesQuery)
|
2018-10-25 02:57:55 -05:00
|
|
|
.set((), Arc::new(libraries));
|
|
|
|
}
|
|
|
|
if let Some(crate_graph) = change.crate_graph {
|
2018-10-31 15:41:43 -05:00
|
|
|
self.db
|
2018-11-27 18:25:20 -06:00
|
|
|
.query_mut(ra_db::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
|
|
|
|
|
|
|
pub(crate) struct AnalysisImpl {
|
2018-11-01 07:29:23 -05:00
|
|
|
pub(crate) db: salsa::Snapshot<db::RootDatabase>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for AnalysisImpl {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let db: &db::RootDatabase = &self.db;
|
|
|
|
fmt.debug_struct("AnalysisImpl").field("db", db).finish()
|
|
|
|
}
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AnalysisImpl {
|
2018-11-07 09:32:33 -06:00
|
|
|
pub fn file_syntax(&self, file_id: FileId) -> SourceFileNode {
|
2018-11-27 17:25:03 -06:00
|
|
|
self.db.source_file(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 {
|
2018-10-31 15:41:43 -05:00
|
|
|
self.db
|
|
|
|
.libraries()
|
|
|
|
.iter()
|
2018-10-29 05:32:45 -05:00
|
|
|
.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;
|
2018-11-01 07:29:23 -05:00
|
|
|
|
|
|
|
/// Need to wrap Snapshot to provide `Clon` impl for `map_with`
|
|
|
|
struct Snap(salsa::Snapshot<db::RootDatabase>);
|
|
|
|
impl Clone for Snap {
|
|
|
|
fn clone(&self) -> Snap {
|
|
|
|
Snap(self.0.snapshot())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let snap = Snap(self.db.snapshot());
|
2018-10-31 15:41:43 -05:00
|
|
|
files
|
|
|
|
.par_iter()
|
2018-11-01 07:29:23 -05:00
|
|
|
.map_with(snap, |db, &file_id| db.0.file_symbols(file_id))
|
2018-10-29 05:32:45 -05:00
|
|
|
.filter_map(|it| it.ok())
|
|
|
|
.collect()
|
|
|
|
};
|
2018-10-31 15:41:43 -05:00
|
|
|
self.db
|
2018-11-27 17:25:03 -06:00
|
|
|
.query(SourceFileQuery)
|
2018-10-29 05:58:11 -05:00
|
|
|
.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-11-20 08:33:27 -06:00
|
|
|
/// This return `Vec`: a module may be included from several places. We
|
|
|
|
/// don't handle this case yet though, so the Vec has length at most one.
|
2018-11-05 05:57:41 -06:00
|
|
|
pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<(FileId, FileSymbol)>> {
|
2018-11-27 16:19:55 -06:00
|
|
|
let descr = match hir::Module::guess_from_position(&*self.db, position)? {
|
2018-11-20 07:21:02 -06:00
|
|
|
None => return Ok(Vec::new()),
|
|
|
|
Some(it) => it,
|
2018-11-05 05:10:20 -06:00
|
|
|
};
|
2018-11-20 07:21:02 -06:00
|
|
|
let (file_id, decl) = match descr.parent_link_source(&*self.db) {
|
|
|
|
None => return Ok(Vec::new()),
|
|
|
|
Some(it) => it,
|
|
|
|
};
|
|
|
|
let decl = decl.borrowed();
|
|
|
|
let decl_name = decl.name().unwrap();
|
|
|
|
let sym = FileSymbol {
|
|
|
|
name: decl_name.text(),
|
|
|
|
node_range: decl_name.syntax().range(),
|
|
|
|
kind: MODULE,
|
|
|
|
};
|
|
|
|
Ok(vec![(file_id, sym)])
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
2018-11-20 07:40:15 -06:00
|
|
|
/// Returns `Vec` for the same reason as `parent_module`
|
2018-10-20 14:15:03 -05:00
|
|
|
pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
|
2018-11-27 16:19:55 -06:00
|
|
|
let descr = match hir::Module::guess_from_file_id(&*self.db, file_id)? {
|
2018-11-20 07:40:15 -06:00
|
|
|
None => return Ok(Vec::new()),
|
|
|
|
Some(it) => it,
|
|
|
|
};
|
|
|
|
let root = descr.crate_root();
|
2018-12-04 13:46:23 -06:00
|
|
|
let file_id = root.source().file_id();
|
2018-10-23 11:15:31 -05:00
|
|
|
|
2018-11-20 07:40:15 -06:00
|
|
|
let crate_graph = self.db.crate_graph();
|
|
|
|
let crate_id = crate_graph.crate_id_for_crate_root(file_id);
|
|
|
|
Ok(crate_id.into_iter().collect())
|
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-11-27 18:25:20 -06:00
|
|
|
self.db.crate_graph().crate_root(crate_id)
|
2018-09-02 08:36:03 -05:00
|
|
|
}
|
2018-11-05 05:57:41 -06:00
|
|
|
pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> {
|
2018-11-07 10:49:49 -06:00
|
|
|
completions(&self.db, position)
|
2018-10-24 10:37:25 -05:00
|
|
|
}
|
2018-08-29 10:23:57 -05:00
|
|
|
pub fn approximately_resolve_symbol(
|
|
|
|
&self,
|
2018-11-05 05:57:41 -06:00
|
|
|
position: FilePosition,
|
2018-10-20 14:15:03 -05:00
|
|
|
) -> Cancelable<Vec<(FileId, FileSymbol)>> {
|
2018-11-27 17:25:03 -06:00
|
|
|
let file = self.db.source_file(position.file_id);
|
2018-08-29 10:23:57 -05:00
|
|
|
let syntax = file.syntax();
|
2018-11-05 05:57:41 -06:00
|
|
|
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, position.offset) {
|
2018-11-27 16:11:29 -06:00
|
|
|
if let Some(fn_descr) =
|
2018-11-27 16:22:17 -06:00
|
|
|
hir::Function::guess_for_name_ref(&*self.db, position.file_id, name_ref)
|
2018-11-05 05:57:41 -06:00
|
|
|
{
|
2018-11-27 16:11:29 -06:00
|
|
|
let scope = fn_descr.scope(&*self.db);
|
|
|
|
// First try to resolve the symbol locally
|
|
|
|
return if let Some(entry) = scope.resolve_local_name(name_ref) {
|
|
|
|
let mut vec = vec![];
|
|
|
|
vec.push((
|
|
|
|
position.file_id,
|
|
|
|
FileSymbol {
|
|
|
|
name: entry.name().clone(),
|
|
|
|
node_range: entry.ptr().range(),
|
|
|
|
kind: NAME,
|
|
|
|
},
|
|
|
|
));
|
|
|
|
Ok(vec)
|
|
|
|
} else {
|
|
|
|
// If that fails try the index based approach.
|
|
|
|
self.index_resolve(name_ref)
|
|
|
|
};
|
|
|
|
}
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
2018-11-05 05:57:41 -06:00
|
|
|
if let Some(name) = find_node_at_offset::<ast::Name>(syntax, position.offset) {
|
2018-08-29 10:23:57 -05:00
|
|
|
if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) {
|
|
|
|
if module.has_semi() {
|
2018-11-20 07:55:35 -06:00
|
|
|
let parent_module =
|
2018-11-27 16:19:55 -06:00
|
|
|
hir::Module::guess_from_file_id(&*self.db, position.file_id)?;
|
2018-11-20 07:55:35 -06:00
|
|
|
let child_name = module.name();
|
|
|
|
match (parent_module, child_name) {
|
|
|
|
(Some(parent_module), Some(child_name)) => {
|
|
|
|
if let Some(child) = parent_module.child(&child_name.text()) {
|
|
|
|
let file_id = child.source().file_id();
|
|
|
|
let symbol = FileSymbol {
|
|
|
|
name: child_name.text(),
|
|
|
|
node_range: TextRange::offset_len(0.into(), 0.into()),
|
|
|
|
kind: MODULE,
|
|
|
|
};
|
|
|
|
return Ok(vec![(file_id, symbol)]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
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-11-05 05:57:41 -06:00
|
|
|
pub fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> {
|
2018-11-27 17:25:03 -06:00
|
|
|
let file = self.db.source_file(position.file_id);
|
2018-10-24 14:54:58 -05:00
|
|
|
// Find the binding associated with the offset
|
2018-11-27 16:11:29 -06:00
|
|
|
let (binding, descr) = match find_binding(&self.db, &file, position) {
|
2018-10-24 14:54:58 -05:00
|
|
|
None => return Vec::new(),
|
|
|
|
Some(it) => it,
|
|
|
|
};
|
2018-10-18 12:40:12 -05:00
|
|
|
|
2018-11-27 16:11:29 -06:00
|
|
|
let mut ret = vec![(position.file_id, binding.syntax().range())];
|
2018-10-31 15:41:43 -05:00
|
|
|
ret.extend(
|
2018-11-27 16:11:29 -06:00
|
|
|
descr
|
|
|
|
.scope(&*self.db)
|
|
|
|
.find_all_refs(binding)
|
2018-10-31 15:41:43 -05:00
|
|
|
.into_iter()
|
2018-11-05 05:57:41 -06:00
|
|
|
.map(|ref_desc| (position.file_id, ref_desc.range)),
|
2018-10-31 15:41:43 -05:00
|
|
|
);
|
2018-10-18 12:40:12 -05:00
|
|
|
|
2018-11-27 16:11:29 -06:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
fn find_binding<'a>(
|
|
|
|
db: &db::RootDatabase,
|
|
|
|
source_file: &'a SourceFileNode,
|
|
|
|
position: FilePosition,
|
2018-11-27 16:22:17 -06:00
|
|
|
) -> Option<(ast::BindPat<'a>, hir::Function)> {
|
2018-11-27 16:11:29 -06:00
|
|
|
let syntax = source_file.syntax();
|
|
|
|
if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) {
|
2018-11-27 16:22:17 -06:00
|
|
|
let descr = hir::Function::guess_for_bind_pat(db, position.file_id, binding)?;
|
2018-11-27 16:11:29 -06:00
|
|
|
return Some((binding, descr));
|
|
|
|
};
|
|
|
|
let name_ref = find_node_at_offset::<ast::NameRef>(syntax, position.offset)?;
|
2018-11-27 16:22:17 -06:00
|
|
|
let descr = hir::Function::guess_for_name_ref(db, position.file_id, name_ref)?;
|
2018-11-27 16:11:29 -06:00
|
|
|
let scope = descr.scope(db);
|
|
|
|
let resolved = scope.resolve_local_name(name_ref)?;
|
|
|
|
let resolved = resolved.ptr().resolve(source_file);
|
|
|
|
let binding = find_node_at_offset::<ast::BindPat>(syntax, resolved.range().end())?;
|
|
|
|
Some((binding, descr))
|
|
|
|
}
|
2018-10-18 12:40:12 -05:00
|
|
|
}
|
|
|
|
|
2018-11-05 15:37:27 -06:00
|
|
|
pub fn doc_comment_for(
|
|
|
|
&self,
|
|
|
|
file_id: FileId,
|
|
|
|
symbol: FileSymbol,
|
|
|
|
) -> Cancelable<Option<String>> {
|
2018-11-27 17:25:03 -06:00
|
|
|
let file = self.db.source_file(file_id);
|
2018-11-05 15:37:27 -06:00
|
|
|
|
|
|
|
Ok(symbol.docs(&file))
|
|
|
|
}
|
|
|
|
|
2018-10-20 14:15:03 -05:00
|
|
|
pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
|
2018-11-27 17:25:03 -06:00
|
|
|
let syntax = self.db.source_file(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-11-27 16:19:55 -06:00
|
|
|
if let Some(m) = hir::Module::guess_from_file_id(&*self.db, file_id)? {
|
2018-11-20 08:16:33 -06:00
|
|
|
for (name_node, problem) in m.problems(&*self.db) {
|
2018-10-23 11:15:31 -05:00
|
|
|
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,
|
2018-11-05 05:57:41 -06:00
|
|
|
position: FilePosition,
|
2018-11-27 14:48:24 -06:00
|
|
|
) -> Cancelable<Option<(FnSignatureInfo, Option<usize>)>> {
|
2018-11-27 17:25:03 -06:00
|
|
|
let file = self.db.source_file(position.file_id);
|
2018-10-09 09:08:17 -05:00
|
|
|
let syntax = file.syntax();
|
|
|
|
|
|
|
|
// Find the calling expression and it's NameRef
|
2018-11-27 09:45:16 -06:00
|
|
|
let calling_node = ctry!(FnCallNode::with_node(syntax, position.offset));
|
|
|
|
let name_ref = ctry!(calling_node.name_ref());
|
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-11-27 14:48:24 -06:00
|
|
|
for (fn_file_id, fs) in file_symbols {
|
2018-10-09 09:08:17 -05:00
|
|
|
if fs.kind == FN_DEF {
|
2018-11-27 17:25:03 -06:00
|
|
|
let fn_file = self.db.source_file(fn_file_id);
|
2018-10-25 08:25:24 -05:00
|
|
|
if let Some(fn_def) = find_node_at_offset(fn_file.syntax(), fs.node_range.start()) {
|
2018-11-27 16:22:17 -06:00
|
|
|
let descr = hir::Function::guess_from_source(&*self.db, fn_file_id, fn_def);
|
2018-11-27 14:48:24 -06:00
|
|
|
if let Some(descriptor) = descr.signature_info(&*self.db) {
|
2018-10-12 06:54:57 -05:00
|
|
|
// 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();
|
|
|
|
|
2018-11-05 05:57:41 -06:00
|
|
|
let range_search = TextRange::from_to(start, position.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-08-30 04:34:31 -05:00
|
|
|
impl SourceChange {
|
|
|
|
pub(crate) fn from_local_edit(file_id: FileId, label: &str, edit: LocalEdit) -> SourceChange {
|
2018-11-07 09:32:33 -06:00
|
|
|
let file_edit = SourceFileNodeEdit {
|
2018-08-30 04:34:31 -05:00
|
|
|
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
|
2018-11-05 05:24:38 -06:00
|
|
|
.map(|offset| FilePosition { offset, file_id }),
|
2018-08-30 04:34:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-31 11:14:08 -05:00
|
|
|
|
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
|
|
|
}
|