2018-08-29 10:23:57 -05:00
|
|
|
use std::{
|
|
|
|
sync::{
|
|
|
|
Arc,
|
|
|
|
atomic::{AtomicBool, Ordering::SeqCst},
|
|
|
|
},
|
|
|
|
fmt,
|
2018-09-02 12:08:58 -05:00
|
|
|
collections::{HashSet, VecDeque},
|
2018-09-15 15:19:41 -05:00
|
|
|
iter,
|
2018-08-29 10:23:57 -05:00
|
|
|
};
|
|
|
|
|
2018-09-10 04:57:40 -05:00
|
|
|
use relative_path::RelativePath;
|
2018-08-30 05:12:49 -05:00
|
|
|
use libeditor::{self, FileSymbol, LineIndex, find_node_at_offset, LocalEdit};
|
2018-08-29 10:23:57 -05:00
|
|
|
use libsyntax2::{
|
|
|
|
TextUnit, TextRange, SmolStr, File, AstNode,
|
|
|
|
SyntaxKind::*,
|
|
|
|
ast::{self, NameOwner},
|
|
|
|
};
|
|
|
|
|
|
|
|
use {
|
2018-08-30 04:34:31 -05:00
|
|
|
FileId, FileResolver, Query, Diagnostic, SourceChange, SourceFileEdit, Position, FileSystemEdit,
|
2018-08-31 11:14:08 -05:00
|
|
|
JobToken, CrateGraph, CrateId,
|
2018-09-03 11:46:30 -05:00
|
|
|
roots::{SourceRoot, ReadonlySourceRoot, WritableSourceRoot},
|
2018-09-15 12:29:22 -05:00
|
|
|
descriptors::{ModuleTreeDescriptor, Problem},
|
2018-08-29 10:23:57 -05:00
|
|
|
};
|
|
|
|
|
2018-09-10 04:57:40 -05:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub(crate) struct FileResolverImp {
|
|
|
|
inner: Arc<FileResolver>
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
fn resolve(&self, _file_id: FileId, _path: &::relative_path::RelativePath) -> Option<FileId> {
|
|
|
|
panic!("file resolver not set")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FileResolverImp { inner: Arc::new(DummyResolver) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 04:51:46 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct AnalysisHostImpl {
|
|
|
|
data: Arc<WorldData>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AnalysisHostImpl {
|
|
|
|
pub fn new() -> AnalysisHostImpl {
|
|
|
|
AnalysisHostImpl {
|
|
|
|
data: Arc::new(WorldData::default()),
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 04:57:40 -05:00
|
|
|
pub fn analysis(&self) -> AnalysisImpl {
|
2018-08-30 04:51:46 -05:00
|
|
|
AnalysisImpl {
|
|
|
|
needs_reindex: AtomicBool::new(false),
|
2018-08-30 05:12:49 -05:00
|
|
|
data: self.data.clone(),
|
2018-08-30 04:51:46 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-30 05:12:49 -05:00
|
|
|
pub fn change_files(&mut self, changes: &mut dyn Iterator<Item=(FileId, Option<String>)>) {
|
2018-08-30 04:51:46 -05:00
|
|
|
let data = self.data_mut();
|
2018-09-15 15:19:41 -05:00
|
|
|
data.root = Arc::new(data.root.apply_changes(changes, None));
|
2018-08-30 04:51:46 -05:00
|
|
|
}
|
2018-09-10 04:57:40 -05:00
|
|
|
pub fn set_file_resolver(&mut self, resolver: FileResolverImp) {
|
|
|
|
let data = self.data_mut();
|
|
|
|
data.file_resolver = resolver.clone();
|
2018-09-15 15:19:41 -05:00
|
|
|
data.root = Arc::new(data.root.apply_changes(&mut iter::empty(), Some(resolver)));
|
2018-09-10 04:57:40 -05:00
|
|
|
}
|
2018-08-31 11:14:08 -05:00
|
|
|
pub fn set_crate_graph(&mut self, graph: CrateGraph) {
|
|
|
|
let mut visited = HashSet::new();
|
|
|
|
for &file_id in graph.crate_roots.values() {
|
|
|
|
if !visited.insert(file_id) {
|
|
|
|
panic!("duplicate crate root: {:?}", file_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.data_mut().crate_graph = graph;
|
|
|
|
}
|
2018-09-03 13:26:59 -05:00
|
|
|
pub fn add_library(&mut self, root: ReadonlySourceRoot) {
|
|
|
|
self.data_mut().libs.push(Arc::new(root));
|
2018-09-03 11:46:30 -05:00
|
|
|
}
|
2018-08-30 04:51:46 -05:00
|
|
|
fn data_mut(&mut self) -> &mut WorldData {
|
2018-08-30 05:12:49 -05:00
|
|
|
Arc::make_mut(&mut self.data)
|
2018-08-30 04:51:46 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-29 10:23:57 -05:00
|
|
|
|
|
|
|
pub(crate) struct AnalysisImpl {
|
2018-08-30 05:12:49 -05:00
|
|
|
needs_reindex: AtomicBool,
|
|
|
|
data: Arc<WorldData>,
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for AnalysisImpl {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
(&*self.data).fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for AnalysisImpl {
|
|
|
|
fn clone(&self) -> AnalysisImpl {
|
|
|
|
AnalysisImpl {
|
|
|
|
needs_reindex: AtomicBool::new(self.needs_reindex.load(SeqCst)),
|
|
|
|
data: Arc::clone(&self.data),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AnalysisImpl {
|
2018-09-03 11:46:30 -05:00
|
|
|
fn root(&self, file_id: FileId) -> &SourceRoot {
|
|
|
|
if self.data.root.contains(file_id) {
|
2018-09-15 15:19:41 -05:00
|
|
|
return &*self.data.root;
|
2018-09-03 11:46:30 -05:00
|
|
|
}
|
2018-09-03 13:03:37 -05:00
|
|
|
&**self.data.libs.iter().find(|it| it.contains(file_id)).unwrap()
|
2018-09-03 11:46:30 -05:00
|
|
|
}
|
2018-09-15 15:19:41 -05:00
|
|
|
pub fn file_syntax(&self, file_id: FileId) -> File {
|
2018-09-03 11:46:30 -05:00
|
|
|
self.root(file_id).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-09-03 11:46:30 -05:00
|
|
|
self.root(file_id).lines(file_id)
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
2018-09-03 11:46:30 -05:00
|
|
|
pub fn world_symbols(&self, query: Query, token: &JobToken) -> Vec<(FileId, FileSymbol)> {
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
if query.libs {
|
|
|
|
self.data.libs.iter()
|
|
|
|
.for_each(|it| it.symbols(&mut buf));
|
|
|
|
} else {
|
|
|
|
self.data.root.symbols(&mut buf);
|
|
|
|
}
|
|
|
|
query.search(&buf, token)
|
|
|
|
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
2018-09-03 11:46:30 -05:00
|
|
|
pub fn parent_module(&self, file_id: FileId) -> Vec<(FileId, FileSymbol)> {
|
2018-09-03 21:09:39 -05:00
|
|
|
let root = self.root(file_id);
|
2018-09-15 12:29:22 -05:00
|
|
|
let module_tree = root.module_tree();
|
|
|
|
module_tree.parent_modules(file_id)
|
|
|
|
.iter()
|
|
|
|
.map(|link| {
|
|
|
|
let file_id = link.owner(&module_tree);
|
|
|
|
let syntax = root.syntax(file_id);
|
|
|
|
let decl = link.bind_source(&module_tree, syntax.ast());
|
2018-08-29 10:23:57 -05:00
|
|
|
let sym = FileSymbol {
|
2018-09-15 12:29:22 -05:00
|
|
|
name: link.name(&module_tree),
|
|
|
|
node_range: decl.syntax().range(),
|
2018-08-29 10:23:57 -05:00
|
|
|
kind: MODULE,
|
|
|
|
};
|
2018-09-15 12:29:22 -05:00
|
|
|
(file_id, sym)
|
2018-08-29 10:23:57 -05:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
2018-09-03 11:46:30 -05:00
|
|
|
pub fn crate_for(&self, file_id: FileId) -> Vec<CrateId> {
|
2018-09-15 12:29:22 -05:00
|
|
|
let module_tree = self.root(file_id).module_tree();
|
2018-08-31 11:14:08 -05:00
|
|
|
let crate_graph = &self.data.crate_graph;
|
|
|
|
let mut res = Vec::new();
|
|
|
|
let mut work = VecDeque::new();
|
2018-09-03 11:46:30 -05:00
|
|
|
work.push_back(file_id);
|
2018-08-31 11:14:08 -05:00
|
|
|
let mut visited = HashSet::new();
|
|
|
|
while let Some(id) = work.pop_front() {
|
|
|
|
if let Some(crate_id) = crate_graph.crate_id_for_crate_root(id) {
|
|
|
|
res.push(crate_id);
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-15 12:29:22 -05:00
|
|
|
let parents = module_tree
|
|
|
|
.parent_modules(id)
|
2018-08-31 11:14:08 -05:00
|
|
|
.into_iter()
|
2018-09-15 12:29:22 -05:00
|
|
|
.map(|link| link.owner(&module_tree))
|
2018-08-31 11:14:08 -05:00
|
|
|
.filter(|&id| visited.insert(id));
|
|
|
|
work.extend(parents);
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
2018-09-02 08:36:03 -05:00
|
|
|
pub fn crate_root(&self, crate_id: CrateId) -> FileId {
|
|
|
|
self.data.crate_graph.crate_roots[&crate_id]
|
|
|
|
}
|
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-08-31 04:04:33 -05:00
|
|
|
token: &JobToken,
|
2018-08-29 10:23:57 -05:00
|
|
|
) -> Vec<(FileId, FileSymbol)> {
|
2018-09-03 11:46:30 -05:00
|
|
|
let root = self.root(file_id);
|
2018-09-15 12:29:22 -05:00
|
|
|
let module_tree = root.module_tree();
|
2018-09-03 11:46:30 -05:00
|
|
|
let file = root.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-08-31 04:04:33 -05:00
|
|
|
return self.index_resolve(name_ref, token);
|
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
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn diagnostics(&self, file_id: FileId) -> Vec<Diagnostic> {
|
2018-09-03 11:46:30 -05:00
|
|
|
let root = self.root(file_id);
|
2018-09-15 12:29:22 -05:00
|
|
|
let module_tree = root.module_tree();
|
2018-09-03 11:46:30 -05:00
|
|
|
let syntax = root.syntax(file_id);
|
|
|
|
|
2018-08-29 10:23:57 -05:00
|
|
|
let mut res = libeditor::diagnostics(&syntax)
|
|
|
|
.into_iter()
|
|
|
|
.map(|d| Diagnostic { range: d.range, message: d.msg, fix: None })
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2018-09-15 12:29:22 -05:00
|
|
|
for (name_node, problem) in module_tree.problems(file_id, 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.syntax().range(),
|
|
|
|
message: "unresolved module".to_string(),
|
|
|
|
fix: Some(fix),
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
2018-09-15 12:29:22 -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.syntax().range(),
|
|
|
|
message: "can't declare module at this location".to_string(),
|
|
|
|
fix: Some(fix),
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
2018-09-15 12:29:22 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
res.push(diag)
|
|
|
|
}
|
2018-08-29 10:23:57 -05:00
|
|
|
res
|
|
|
|
}
|
|
|
|
|
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![
|
|
|
|
("flip comma", libeditor::flip_comma(&file, offset).map(|f| f())),
|
|
|
|
("add `#[derive]`", libeditor::add_derive(&file, offset).map(|f| f())),
|
|
|
|
("add impl", libeditor::add_impl(&file, offset).map(|f| f())),
|
2018-09-05 16:59:07 -05:00
|
|
|
("introduce variable", libeditor::introduce_variable(&file, range).map(|f| f())),
|
2018-08-29 10:23:57 -05:00
|
|
|
];
|
2018-08-30 08:27:09 -05:00
|
|
|
actions.into_iter()
|
|
|
|
.filter_map(|(name, local_edit)| {
|
|
|
|
Some(SourceChange::from_local_edit(
|
|
|
|
file_id, name, local_edit?,
|
2018-08-29 10:23:57 -05:00
|
|
|
))
|
2018-08-30 08:27:09 -05:00
|
|
|
})
|
|
|
|
.collect()
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
|
|
|
|
2018-08-31 04:04:33 -05:00
|
|
|
fn index_resolve(&self, name_ref: ast::NameRef, token: &JobToken) -> 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-08-31 04:04:33 -05:00
|
|
|
self.world_symbols(query, token)
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
|
|
|
|
2018-09-15 12:29:22 -05:00
|
|
|
fn resolve_module(&self, module_tree: &ModuleTreeDescriptor, 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-09-15 12:29:22 -05:00
|
|
|
module_tree.child_module_by_name(file_id, name.as_str())
|
2018-08-29 10:23:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 04:57:40 -05:00
|
|
|
#[derive(Default, Clone, Debug)]
|
2018-08-30 05:12:49 -05:00
|
|
|
struct WorldData {
|
2018-09-10 04:57:40 -05:00
|
|
|
file_resolver: FileResolverImp,
|
2018-08-31 11:14:08 -05:00
|
|
|
crate_graph: CrateGraph,
|
2018-09-15 15:19:41 -05:00
|
|
|
root: Arc<WritableSourceRoot>,
|
2018-09-03 13:03:37 -05:00
|
|
|
libs: Vec<Arc<ReadonlySourceRoot>>,
|
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![],
|
|
|
|
cursor_position: edit.cursor_position
|
|
|
|
.map(|offset| Position { offset, file_id })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-31 11:14:08 -05:00
|
|
|
|
|
|
|
impl CrateGraph {
|
|
|
|
fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> {
|
|
|
|
let (&crate_id, _) = self.crate_roots
|
|
|
|
.iter()
|
|
|
|
.find(|(_crate_id, &root_id)| root_id == file_id)?;
|
|
|
|
Some(crate_id)
|
|
|
|
}
|
|
|
|
}
|