2019-01-08 13:33:36 -06:00
|
|
|
use hir::{
|
2019-01-16 07:39:01 -06:00
|
|
|
self, Problem, source_binder
|
2019-01-08 13:33:36 -06:00
|
|
|
};
|
2019-02-03 10:27:36 -06:00
|
|
|
use ra_ide_api_light::{self, LocalEdit, Severity};
|
2019-01-08 13:33:36 -06:00
|
|
|
use ra_syntax::{
|
2019-01-14 10:09:03 -06:00
|
|
|
algo::find_node_at_offset, ast::{self, NameOwner}, AstNode,
|
|
|
|
SourceFile,
|
|
|
|
TextRange,
|
2019-01-08 13:33:36 -06:00
|
|
|
};
|
2019-02-08 02:52:18 -06:00
|
|
|
use ra_db::SourceDatabase;
|
2019-01-08 13:33:36 -06:00
|
|
|
|
|
|
|
use crate::{
|
2019-02-03 12:26:35 -06:00
|
|
|
CrateId, db, Diagnostic, FileId, FilePosition, FileSystemEdit,
|
2019-02-08 02:52:18 -06:00
|
|
|
Query, SourceChange, SourceFileEdit,
|
|
|
|
symbol_index::FileSymbol,
|
2019-01-08 13:33:36 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
impl db::RootDatabase {
|
|
|
|
/// Returns `Vec` for the same reason as `parent_module`
|
2019-01-15 09:30:58 -06:00
|
|
|
pub(crate) fn crate_for(&self, file_id: FileId) -> Vec<CrateId> {
|
2019-01-15 09:13:11 -06:00
|
|
|
let module = match source_binder::module_from_file_id(self, file_id) {
|
2019-01-08 13:33:36 -06:00
|
|
|
Some(it) => it,
|
2019-01-15 09:30:58 -06:00
|
|
|
None => return Vec::new(),
|
2019-01-08 13:33:36 -06:00
|
|
|
};
|
2019-01-15 09:30:58 -06:00
|
|
|
let krate = match module.krate(self) {
|
2019-01-08 13:33:36 -06:00
|
|
|
Some(it) => it,
|
2019-01-15 09:30:58 -06:00
|
|
|
None => return Vec::new(),
|
2019-01-08 13:33:36 -06:00
|
|
|
};
|
2019-01-15 09:30:58 -06:00
|
|
|
vec![krate.crate_id()]
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
2019-01-14 10:09:03 -06:00
|
|
|
|
2019-01-15 12:09:51 -06:00
|
|
|
pub(crate) fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> {
|
2019-01-26 02:51:36 -06:00
|
|
|
let file = self.parse(position.file_id);
|
2019-01-08 13:33:36 -06:00
|
|
|
// Find the binding associated with the offset
|
2019-01-15 12:09:51 -06:00
|
|
|
let (binding, descr) = match find_binding(self, &file, position) {
|
|
|
|
None => return Vec::new(),
|
2019-01-08 13:33:36 -06:00
|
|
|
Some(it) => it,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut ret = binding
|
|
|
|
.name()
|
|
|
|
.into_iter()
|
|
|
|
.map(|name| (position.file_id, name.syntax().range()))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
ret.extend(
|
|
|
|
descr
|
2019-01-15 10:04:49 -06:00
|
|
|
.scopes(self)
|
2019-01-08 13:33:36 -06:00
|
|
|
.find_all_refs(binding)
|
|
|
|
.into_iter()
|
|
|
|
.map(|ref_desc| (position.file_id, ref_desc.range)),
|
|
|
|
);
|
|
|
|
|
2019-01-15 12:09:51 -06:00
|
|
|
return ret;
|
2019-01-08 13:33:36 -06:00
|
|
|
|
|
|
|
fn find_binding<'a>(
|
|
|
|
db: &db::RootDatabase,
|
|
|
|
source_file: &'a SourceFile,
|
|
|
|
position: FilePosition,
|
2019-01-15 12:09:51 -06:00
|
|
|
) -> Option<(&'a ast::BindPat, hir::Function)> {
|
2019-01-08 13:33:36 -06:00
|
|
|
let syntax = source_file.syntax();
|
|
|
|
if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) {
|
2019-01-15 12:09:51 -06:00
|
|
|
let descr = source_binder::function_from_child_node(
|
2019-01-08 13:33:36 -06:00
|
|
|
db,
|
|
|
|
position.file_id,
|
|
|
|
binding.syntax(),
|
2019-01-15 12:09:51 -06:00
|
|
|
)?;
|
|
|
|
return Some((binding, descr));
|
2019-01-08 13:33:36 -06:00
|
|
|
};
|
2019-01-15 12:09:51 -06:00
|
|
|
let name_ref = find_node_at_offset::<ast::NameRef>(syntax, position.offset)?;
|
|
|
|
let descr =
|
|
|
|
source_binder::function_from_child_node(db, position.file_id, name_ref.syntax())?;
|
2019-01-15 10:04:49 -06:00
|
|
|
let scope = descr.scopes(db);
|
2019-01-15 12:09:51 -06:00
|
|
|
let resolved = scope.resolve_local_name(name_ref)?;
|
2019-01-23 08:37:10 -06:00
|
|
|
let resolved = resolved.ptr().to_node(source_file);
|
2019-01-15 12:09:51 -06:00
|
|
|
let binding = find_node_at_offset::<ast::BindPat>(syntax, resolved.range().end())?;
|
|
|
|
Some((binding, descr))
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:17:10 -06:00
|
|
|
pub(crate) fn diagnostics(&self, file_id: FileId) -> Vec<Diagnostic> {
|
2019-01-26 02:51:36 -06:00
|
|
|
let syntax = self.parse(file_id);
|
2019-01-08 13:33:36 -06:00
|
|
|
|
|
|
|
let mut res = ra_ide_api_light::diagnostics(&syntax)
|
|
|
|
.into_iter()
|
|
|
|
.map(|d| Diagnostic {
|
|
|
|
range: d.range,
|
|
|
|
message: d.msg,
|
|
|
|
severity: d.severity,
|
|
|
|
fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)),
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2019-01-15 09:13:11 -06:00
|
|
|
if let Some(m) = source_binder::module_from_file_id(self, file_id) {
|
2019-01-15 11:56:06 -06:00
|
|
|
for (name_node, problem) in m.problems(self) {
|
2019-01-08 13:33:36 -06:00
|
|
|
let source_root = self.file_source_root(file_id);
|
|
|
|
let diag = match problem {
|
|
|
|
Problem::UnresolvedModule { candidate } => {
|
|
|
|
let create_file = FileSystemEdit::CreateFile {
|
|
|
|
source_root,
|
|
|
|
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(),
|
|
|
|
severity: Severity::Error,
|
|
|
|
fix: Some(fix),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Problem::NotDirOwner { move_to, candidate } => {
|
|
|
|
let move_file = FileSystemEdit::MoveFile {
|
|
|
|
src: file_id,
|
|
|
|
dst_source_root: source_root,
|
|
|
|
dst_path: move_to.clone(),
|
|
|
|
};
|
|
|
|
let create_file = FileSystemEdit::CreateFile {
|
|
|
|
source_root,
|
|
|
|
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(),
|
|
|
|
severity: Severity::Error,
|
|
|
|
fix: Some(fix),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
res.push(diag)
|
|
|
|
}
|
|
|
|
};
|
2019-01-15 12:17:10 -06:00
|
|
|
res
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
|
2019-01-16 07:39:01 -06:00
|
|
|
pub(crate) fn index_resolve(&self, name_ref: &ast::NameRef) -> Vec<FileSymbol> {
|
2019-01-08 13:33:36 -06:00
|
|
|
let name = name_ref.text();
|
|
|
|
let mut query = Query::new(name.to_string());
|
|
|
|
query.exact();
|
|
|
|
query.limit(4);
|
|
|
|
crate::symbol_index::world_symbols(self, query)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SourceChange {
|
|
|
|
pub(crate) fn from_local_edit(file_id: FileId, edit: LocalEdit) -> SourceChange {
|
|
|
|
let file_edit = SourceFileEdit {
|
|
|
|
file_id,
|
|
|
|
edit: edit.edit,
|
|
|
|
};
|
|
|
|
SourceChange {
|
|
|
|
label: edit.label,
|
|
|
|
source_file_edits: vec![file_edit],
|
|
|
|
file_system_edits: vec![],
|
|
|
|
cursor_position: edit
|
|
|
|
.cursor_position
|
|
|
|
.map(|offset| FilePosition { offset, file_id }),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|