2018-08-13 10:46:05 +00:00
|
|
|
#[macro_use]
|
2018-08-10 12:07:43 +00:00
|
|
|
extern crate failure;
|
|
|
|
extern crate parking_lot;
|
2018-08-10 18:13:39 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2018-08-10 19:23:17 +00:00
|
|
|
extern crate once_cell;
|
2018-08-10 18:13:39 +00:00
|
|
|
extern crate libsyntax2;
|
2018-08-10 19:23:17 +00:00
|
|
|
extern crate libeditor;
|
2018-08-13 12:10:20 +00:00
|
|
|
extern crate fst;
|
2018-08-13 16:28:34 +00:00
|
|
|
extern crate rayon;
|
2018-08-13 12:10:20 +00:00
|
|
|
|
|
|
|
mod symbol_index;
|
2018-08-21 15:30:10 +00:00
|
|
|
mod module_map;
|
2018-08-10 18:13:39 +00:00
|
|
|
|
2018-08-10 12:07:43 +00:00
|
|
|
use std::{
|
2018-08-16 21:18:14 +00:00
|
|
|
fmt,
|
2018-08-27 17:58:38 +00:00
|
|
|
path::{Path, PathBuf},
|
2018-08-13 16:28:34 +00:00
|
|
|
sync::{
|
|
|
|
Arc,
|
2018-08-21 15:30:10 +00:00
|
|
|
atomic::{AtomicBool, Ordering::SeqCst},
|
2018-08-13 16:28:34 +00:00
|
|
|
},
|
2018-08-10 12:07:43 +00:00
|
|
|
collections::hash_map::HashMap,
|
2018-08-13 16:28:34 +00:00
|
|
|
time::Instant,
|
2018-08-10 12:07:43 +00:00
|
|
|
};
|
2018-08-13 12:10:20 +00:00
|
|
|
|
2018-08-27 17:58:38 +00:00
|
|
|
use once_cell::sync::OnceCell;
|
|
|
|
use rayon::prelude::*;
|
|
|
|
|
2018-08-13 13:35:17 +00:00
|
|
|
use libsyntax2::{
|
2018-08-25 08:44:58 +00:00
|
|
|
File,
|
2018-08-18 09:42:28 +00:00
|
|
|
TextUnit, TextRange, SmolStr,
|
2018-08-25 08:40:17 +00:00
|
|
|
ast::{self, AstNode, NameOwner},
|
2018-08-17 12:37:17 +00:00
|
|
|
SyntaxKind::*,
|
2018-08-13 13:35:17 +00:00
|
|
|
};
|
2018-08-27 17:58:38 +00:00
|
|
|
use libeditor::{Diagnostic, LineIndex, FileSymbol, find_node_at_offset};
|
2018-08-13 12:10:20 +00:00
|
|
|
|
2018-08-21 15:30:10 +00:00
|
|
|
use self::{
|
|
|
|
symbol_index::FileSymbols,
|
2018-08-21 19:24:59 +00:00
|
|
|
module_map::{ModuleMap, ChangeKind},
|
2018-08-21 15:30:10 +00:00
|
|
|
};
|
2018-08-13 13:07:05 +00:00
|
|
|
pub use self::symbol_index::Query;
|
2018-08-10 12:07:43 +00:00
|
|
|
|
|
|
|
pub type Result<T> = ::std::result::Result<T, ::failure::Error>;
|
|
|
|
|
2018-08-16 21:18:14 +00:00
|
|
|
pub type FileResolver = dyn Fn(FileId, &Path) -> Option<FileId> + Send + Sync;
|
|
|
|
|
2018-08-17 16:54:08 +00:00
|
|
|
#[derive(Debug)]
|
2018-08-10 12:07:43 +00:00
|
|
|
pub struct WorldState {
|
|
|
|
data: Arc<WorldData>
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct World {
|
2018-08-21 15:30:10 +00:00
|
|
|
needs_reindex: AtomicBool,
|
2018-08-16 21:18:14 +00:00
|
|
|
file_resolver: Arc<FileResolver>,
|
2018-08-10 12:07:43 +00:00
|
|
|
data: Arc<WorldData>,
|
|
|
|
}
|
|
|
|
|
2018-08-16 21:18:14 +00:00
|
|
|
impl fmt::Debug for World {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
(&*self.data).fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-21 15:30:10 +00:00
|
|
|
impl Clone for World {
|
|
|
|
fn clone(&self) -> World {
|
|
|
|
World {
|
|
|
|
needs_reindex: AtomicBool::new(self.needs_reindex.load(SeqCst)),
|
|
|
|
file_resolver: Arc::clone(&self.file_resolver),
|
|
|
|
data: Arc::clone(&self.data),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 12:03:27 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2018-08-15 14:24:20 +00:00
|
|
|
pub struct FileId(pub u32);
|
2018-08-14 12:03:27 +00:00
|
|
|
|
2018-08-10 12:07:43 +00:00
|
|
|
impl WorldState {
|
|
|
|
pub fn new() -> WorldState {
|
|
|
|
WorldState {
|
2018-08-21 15:30:10 +00:00
|
|
|
data: Arc::new(WorldData::default()),
|
2018-08-10 12:07:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-21 15:30:10 +00:00
|
|
|
pub fn snapshot(
|
2018-08-21 19:24:59 +00:00
|
|
|
&self,
|
2018-08-21 15:30:10 +00:00
|
|
|
file_resolver: impl Fn(FileId, &Path) -> Option<FileId> + 'static + Send + Sync,
|
|
|
|
) -> World {
|
2018-08-16 21:18:14 +00:00
|
|
|
World {
|
2018-08-21 19:24:59 +00:00
|
|
|
needs_reindex: AtomicBool::new(false),
|
2018-08-16 21:18:14 +00:00
|
|
|
file_resolver: Arc::new(file_resolver),
|
|
|
|
data: self.data.clone()
|
|
|
|
}
|
2018-08-10 12:07:43 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 12:03:27 +00:00
|
|
|
pub fn change_file(&mut self, file_id: FileId, text: Option<String>) {
|
|
|
|
self.change_files(::std::iter::once((file_id, text)));
|
2018-08-13 10:46:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 12:03:27 +00:00
|
|
|
pub fn change_files(&mut self, changes: impl Iterator<Item=(FileId, Option<String>)>) {
|
2018-08-21 19:24:59 +00:00
|
|
|
let data = self.data_mut();
|
|
|
|
for (file_id, text) in changes {
|
|
|
|
let change_kind = if data.file_map.remove(&file_id).is_some() {
|
|
|
|
if text.is_some() {
|
|
|
|
ChangeKind::Update
|
2018-08-21 15:30:10 +00:00
|
|
|
} else {
|
2018-08-21 19:24:59 +00:00
|
|
|
ChangeKind::Delete
|
2018-08-21 15:30:10 +00:00
|
|
|
}
|
2018-08-21 19:24:59 +00:00
|
|
|
} else {
|
|
|
|
ChangeKind::Insert
|
|
|
|
};
|
|
|
|
data.module_map.update_file(file_id, change_kind);
|
|
|
|
data.file_map.remove(&file_id);
|
|
|
|
if let Some(text) = text {
|
|
|
|
let file_data = FileData::new(text);
|
|
|
|
data.file_map.insert(file_id, Arc::new(file_data));
|
|
|
|
} else {
|
|
|
|
data.file_map.remove(&file_id);
|
2018-08-13 10:46:05 +00:00
|
|
|
}
|
2018-08-10 12:07:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn data_mut(&mut self) -> &mut WorldData {
|
|
|
|
if Arc::get_mut(&mut self.data).is_none() {
|
|
|
|
self.data = Arc::new(WorldData {
|
2018-08-13 10:46:05 +00:00
|
|
|
file_map: self.data.file_map.clone(),
|
2018-08-21 15:30:10 +00:00
|
|
|
module_map: self.data.module_map.clone(),
|
2018-08-10 12:07:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
Arc::get_mut(&mut self.data).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-27 18:10:02 +00:00
|
|
|
#[derive(Debug)]
|
2018-08-27 17:58:38 +00:00
|
|
|
pub enum QuickFix {
|
|
|
|
CreateFile(PathBuf),
|
|
|
|
}
|
2018-08-10 12:07:43 +00:00
|
|
|
|
|
|
|
impl World {
|
2018-08-25 08:44:58 +00:00
|
|
|
pub fn file_syntax(&self, file_id: FileId) -> Result<File> {
|
2018-08-14 12:03:27 +00:00
|
|
|
let data = self.file_data(file_id)?;
|
|
|
|
Ok(data.syntax().clone())
|
2018-08-10 12:07:43 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 12:03:27 +00:00
|
|
|
pub fn file_line_index(&self, id: FileId) -> Result<LineIndex> {
|
|
|
|
let data = self.file_data(id)?;
|
2018-08-10 19:23:17 +00:00
|
|
|
let index = data.lines
|
2018-08-14 12:03:27 +00:00
|
|
|
.get_or_init(|| LineIndex::new(&data.text));
|
2018-08-10 19:23:17 +00:00
|
|
|
Ok(index.clone())
|
|
|
|
}
|
|
|
|
|
2018-08-17 12:37:17 +00:00
|
|
|
pub fn world_symbols(&self, mut query: Query) -> Vec<(FileId, FileSymbol)> {
|
2018-08-13 16:28:34 +00:00
|
|
|
self.reindex();
|
2018-08-13 12:35:53 +00:00
|
|
|
self.data.file_map.iter()
|
2018-08-14 12:03:27 +00:00
|
|
|
.flat_map(move |(id, data)| {
|
2018-08-13 14:19:27 +00:00
|
|
|
let symbols = data.symbols();
|
2018-08-14 12:03:27 +00:00
|
|
|
query.process(symbols).into_iter().map(move |s| (*id, s))
|
2018-08-13 12:35:53 +00:00
|
|
|
})
|
2018-08-17 12:37:17 +00:00
|
|
|
.collect()
|
2018-08-13 12:10:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 15:30:10 +00:00
|
|
|
pub fn parent_module(&self, id: FileId) -> Vec<(FileId, FileSymbol)> {
|
|
|
|
let module_map = &self.data.module_map;
|
|
|
|
let id = module_map.file2module(id);
|
|
|
|
module_map
|
2018-08-21 19:24:59 +00:00
|
|
|
.parent_modules(
|
|
|
|
id,
|
|
|
|
&*self.file_resolver,
|
|
|
|
&|file_id| self.file_syntax(file_id).unwrap(),
|
|
|
|
)
|
2018-08-21 15:30:10 +00:00
|
|
|
.into_iter()
|
2018-08-21 19:24:59 +00:00
|
|
|
.map(|(id, name, node)| {
|
2018-08-21 15:30:10 +00:00
|
|
|
let id = module_map.module2file(id);
|
|
|
|
let sym = FileSymbol {
|
2018-08-21 19:24:59 +00:00
|
|
|
name,
|
|
|
|
node_range: node.range(),
|
2018-08-21 15:30:10 +00:00
|
|
|
kind: MODULE,
|
|
|
|
};
|
|
|
|
(id, sym)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2018-08-17 12:37:17 +00:00
|
|
|
pub fn approximately_resolve_symbol(
|
|
|
|
&self,
|
2018-08-14 12:03:27 +00:00
|
|
|
id: FileId,
|
2018-08-13 13:35:17 +00:00
|
|
|
offset: TextUnit,
|
2018-08-17 12:37:17 +00:00
|
|
|
) -> Result<Vec<(FileId, FileSymbol)>> {
|
2018-08-14 12:03:27 +00:00
|
|
|
let file = self.file_syntax(id)?;
|
2018-08-17 19:00:13 +00:00
|
|
|
let syntax = file.syntax();
|
2018-08-26 06:12:18 +00:00
|
|
|
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) {
|
2018-08-17 12:37:17 +00:00
|
|
|
return Ok(self.index_resolve(name_ref));
|
|
|
|
}
|
2018-08-26 06:12:18 +00:00
|
|
|
if let Some(name) = find_node_at_offset::<ast::Name>(syntax, offset) {
|
2018-08-17 12:37:17 +00:00
|
|
|
if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) {
|
|
|
|
if module.has_semi() {
|
2018-08-18 09:42:28 +00:00
|
|
|
let file_ids = self.resolve_module(id, module);
|
|
|
|
|
|
|
|
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 Ok(res);
|
2018-08-17 12:37:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(vec![])
|
|
|
|
}
|
2018-08-13 13:35:17 +00:00
|
|
|
|
2018-08-27 17:58:38 +00:00
|
|
|
pub fn diagnostics(&self, file_id: FileId) -> Result<Vec<(Diagnostic, Option<QuickFix>)>> {
|
|
|
|
let syntax = self.file_syntax(file_id)?;
|
|
|
|
let mut res = libeditor::diagnostics(&syntax)
|
|
|
|
.into_iter()
|
|
|
|
.map(|d| (d, None))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
for module in syntax.ast().modules() {
|
|
|
|
if module.has_semi() && self.resolve_module(file_id, module).is_empty() {
|
|
|
|
if let Some(name) = module.name() {
|
|
|
|
let d = Diagnostic {
|
|
|
|
range: name.syntax().range(),
|
|
|
|
msg: "unresolved module".to_string(),
|
|
|
|
};
|
|
|
|
let quick_fix = self.data.module_map.suggested_child_mod_path(module)
|
|
|
|
.map(QuickFix::CreateFile);
|
|
|
|
|
|
|
|
res.push((d, quick_fix))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2018-08-17 19:00:13 +00:00
|
|
|
fn index_resolve(&self, name_ref: ast::NameRef) -> Vec<(FileId, FileSymbol)> {
|
2018-08-17 12:37:17 +00:00
|
|
|
let name = name_ref.text();
|
2018-08-13 13:35:17 +00:00
|
|
|
let mut query = Query::new(name.to_string());
|
|
|
|
query.exact();
|
2018-08-13 14:19:27 +00:00
|
|
|
query.limit(4);
|
2018-08-17 12:37:17 +00:00
|
|
|
self.world_symbols(query)
|
|
|
|
}
|
|
|
|
|
2018-08-18 09:42:28 +00:00
|
|
|
fn resolve_module(&self, id: FileId, module: ast::Module) -> Vec<FileId> {
|
2018-08-17 12:37:17 +00:00
|
|
|
let name = match module.name() {
|
|
|
|
Some(name) => name.text(),
|
|
|
|
None => return Vec::new(),
|
|
|
|
};
|
2018-08-21 15:30:10 +00:00
|
|
|
let module_map = &self.data.module_map;
|
|
|
|
let id = module_map.file2module(id);
|
|
|
|
module_map
|
2018-08-21 19:24:59 +00:00
|
|
|
.child_module_by_name(
|
|
|
|
id, name.as_str(),
|
|
|
|
&*self.file_resolver,
|
|
|
|
&|file_id| self.file_syntax(file_id).unwrap(),
|
|
|
|
)
|
2018-08-21 15:30:10 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|id| module_map.module2file(id))
|
2018-08-17 13:04:34 +00:00
|
|
|
.collect()
|
2018-08-13 13:35:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:28:34 +00:00
|
|
|
fn reindex(&self) {
|
2018-08-21 15:30:10 +00:00
|
|
|
if self.needs_reindex.compare_and_swap(false, true, SeqCst) {
|
2018-08-13 16:28:34 +00:00
|
|
|
let now = Instant::now();
|
2018-08-21 15:30:10 +00:00
|
|
|
let data = &*self.data;
|
2018-08-13 16:28:34 +00:00
|
|
|
data.file_map
|
|
|
|
.par_iter()
|
2018-08-21 15:30:10 +00:00
|
|
|
.for_each(|(_, data)| drop(data.symbols()));
|
2018-08-13 16:28:34 +00:00
|
|
|
info!("parallel indexing took {:?}", now.elapsed());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 12:03:27 +00:00
|
|
|
fn file_data(&self, file_id: FileId) -> Result<Arc<FileData>> {
|
|
|
|
match self.data.file_map.get(&file_id) {
|
2018-08-13 10:46:05 +00:00
|
|
|
Some(data) => Ok(data.clone()),
|
2018-08-14 12:03:27 +00:00
|
|
|
None => bail!("unknown file: {:?}", file_id),
|
2018-08-10 12:07:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 21:55:32 +00:00
|
|
|
#[derive(Default, Debug)]
|
2018-08-10 12:07:43 +00:00
|
|
|
struct WorldData {
|
2018-08-14 12:03:27 +00:00
|
|
|
file_map: HashMap<FileId, Arc<FileData>>,
|
2018-08-21 15:30:10 +00:00
|
|
|
module_map: ModuleMap,
|
2018-08-10 19:23:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 21:55:32 +00:00
|
|
|
#[derive(Debug)]
|
2018-08-10 19:23:17 +00:00
|
|
|
struct FileData {
|
2018-08-13 10:46:05 +00:00
|
|
|
text: String,
|
2018-08-13 12:10:20 +00:00
|
|
|
symbols: OnceCell<FileSymbols>,
|
2018-08-25 08:44:58 +00:00
|
|
|
syntax: OnceCell<File>,
|
2018-08-10 19:23:17 +00:00
|
|
|
lines: OnceCell<LineIndex>,
|
2018-08-10 12:07:43 +00:00
|
|
|
}
|
2018-08-13 10:46:05 +00:00
|
|
|
|
|
|
|
impl FileData {
|
|
|
|
fn new(text: String) -> FileData {
|
|
|
|
FileData {
|
|
|
|
text,
|
2018-08-13 12:10:20 +00:00
|
|
|
symbols: OnceCell::new(),
|
2018-08-13 10:46:05 +00:00
|
|
|
syntax: OnceCell::new(),
|
|
|
|
lines: OnceCell::new(),
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 12:10:20 +00:00
|
|
|
|
2018-08-25 08:44:58 +00:00
|
|
|
fn syntax(&self) -> &File {
|
2018-08-13 12:10:20 +00:00
|
|
|
self.syntax
|
2018-08-25 08:44:58 +00:00
|
|
|
.get_or_init(|| File::parse(&self.text))
|
2018-08-13 12:10:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 08:44:58 +00:00
|
|
|
fn syntax_transient(&self) -> File {
|
2018-08-13 14:19:27 +00:00
|
|
|
self.syntax.get().map(|s| s.clone())
|
2018-08-25 08:44:58 +00:00
|
|
|
.unwrap_or_else(|| File::parse(&self.text))
|
2018-08-13 14:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn symbols(&self) -> &FileSymbols {
|
|
|
|
let syntax = self.syntax_transient();
|
2018-08-13 12:10:20 +00:00
|
|
|
self.symbols
|
2018-08-13 14:19:27 +00:00
|
|
|
.get_or_init(|| FileSymbols::new(&syntax))
|
2018-08-13 12:10:20 +00:00
|
|
|
}
|
2018-08-13 10:46:05 +00:00
|
|
|
}
|