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-10 18:13:39 +00:00
|
|
|
|
2018-08-10 19:23:17 +00:00
|
|
|
use once_cell::sync::OnceCell;
|
2018-08-13 16:28:34 +00:00
|
|
|
use rayon::prelude::*;
|
2018-08-10 12:07:43 +00:00
|
|
|
|
|
|
|
use std::{
|
2018-08-13 16:28:34 +00:00
|
|
|
sync::{
|
|
|
|
Arc,
|
|
|
|
atomic::{AtomicUsize, Ordering::SeqCst},
|
|
|
|
},
|
2018-08-10 12:07:43 +00:00
|
|
|
collections::hash_map::HashMap,
|
|
|
|
path::{PathBuf, Path},
|
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-13 13:35:17 +00:00
|
|
|
use libsyntax2::{
|
|
|
|
TextUnit,
|
|
|
|
ast::{self, AstNode},
|
|
|
|
algo::{find_leaf_at_offset, ancestors},
|
|
|
|
};
|
2018-08-13 12:10:20 +00:00
|
|
|
use libeditor::{LineIndex, FileSymbol};
|
|
|
|
|
2018-08-13 13:35:17 +00:00
|
|
|
use self::symbol_index::FileSymbols;
|
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-13 16:28:34 +00:00
|
|
|
const INDEXING_THRESHOLD: usize = 128;
|
2018-08-10 12:07:43 +00:00
|
|
|
|
|
|
|
pub struct WorldState {
|
|
|
|
data: Arc<WorldData>
|
|
|
|
}
|
|
|
|
|
2018-08-10 21:55:32 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2018-08-10 12:07:43 +00:00
|
|
|
pub struct World {
|
|
|
|
data: Arc<WorldData>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WorldState {
|
|
|
|
pub fn new() -> WorldState {
|
|
|
|
WorldState {
|
|
|
|
data: Arc::new(WorldData::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn snapshot(&self) -> World {
|
|
|
|
World { data: self.data.clone() }
|
|
|
|
}
|
|
|
|
|
2018-08-13 10:46:05 +00:00
|
|
|
pub fn change_file(&mut self, path: PathBuf, text: Option<String>) {
|
|
|
|
self.change_files(::std::iter::once((path, text)));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn change_files(&mut self, changes: impl Iterator<Item=(PathBuf, Option<String>)>) {
|
2018-08-10 12:07:43 +00:00
|
|
|
let data = self.data_mut();
|
2018-08-13 16:28:34 +00:00
|
|
|
let mut cnt = 0;
|
2018-08-13 10:46:05 +00:00
|
|
|
for (path, text) in changes {
|
2018-08-13 16:28:34 +00:00
|
|
|
cnt += 1;
|
2018-08-13 10:46:05 +00:00
|
|
|
data.file_map.remove(&path);
|
|
|
|
if let Some(text) = text {
|
|
|
|
let file_data = FileData::new(text);
|
|
|
|
data.file_map.insert(path, Arc::new(file_data));
|
|
|
|
} else {
|
|
|
|
data.file_map.remove(&path);
|
|
|
|
}
|
2018-08-10 12:07:43 +00:00
|
|
|
}
|
2018-08-13 16:28:34 +00:00
|
|
|
*data.unindexed.get_mut() += cnt;
|
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 16:28:34 +00:00
|
|
|
unindexed: AtomicUsize::new(
|
|
|
|
self.data.unindexed.load(SeqCst)
|
|
|
|
),
|
2018-08-13 10:46:05 +00:00
|
|
|
file_map: self.data.file_map.clone(),
|
2018-08-10 12:07:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
Arc::get_mut(&mut self.data).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl World {
|
|
|
|
pub fn file_syntax(&self, path: &Path) -> Result<ast::File> {
|
2018-08-10 19:23:17 +00:00
|
|
|
let data = self.file_data(path)?;
|
2018-08-13 12:10:20 +00:00
|
|
|
Ok(data.syntax(path).clone())
|
2018-08-10 12:07:43 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 19:23:17 +00:00
|
|
|
pub fn file_line_index(&self, path: &Path) -> Result<LineIndex> {
|
|
|
|
let data = self.file_data(path)?;
|
|
|
|
let index = data.lines
|
|
|
|
.get_or_init(|| {
|
|
|
|
trace!("calc line index: {}", path.display());
|
2018-08-13 10:46:05 +00:00
|
|
|
LineIndex::new(&data.text)
|
2018-08-10 19:23:17 +00:00
|
|
|
});
|
|
|
|
Ok(index.clone())
|
|
|
|
}
|
|
|
|
|
2018-08-13 14:19:27 +00:00
|
|
|
pub fn world_symbols<'a>(&'a self, mut query: Query) -> impl Iterator<Item=(&'a Path, &'a FileSymbol)> + 'a {
|
2018-08-13 16:28:34 +00:00
|
|
|
self.reindex();
|
2018-08-13 12:35:53 +00:00
|
|
|
self.data.file_map.iter()
|
|
|
|
.flat_map(move |(path, data)| {
|
2018-08-13 14:19:27 +00:00
|
|
|
let symbols = data.symbols();
|
2018-08-13 16:28:34 +00:00
|
|
|
let path: &'a Path = path.as_path();
|
2018-08-13 14:19:27 +00:00
|
|
|
query.process(symbols).into_iter().map(move |s| (path, s))
|
2018-08-13 12:35:53 +00:00
|
|
|
})
|
2018-08-13 12:10:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 13:35:17 +00:00
|
|
|
pub fn approximately_resolve_symbol<'a>(
|
|
|
|
&'a self,
|
|
|
|
path: &Path,
|
|
|
|
offset: TextUnit,
|
|
|
|
) -> Result<Vec<(&'a Path, &'a FileSymbol)>> {
|
|
|
|
let file = self.file_syntax(path)?;
|
|
|
|
let syntax = file.syntax();
|
|
|
|
let syntax = syntax.as_ref();
|
|
|
|
let name_ref =
|
|
|
|
find_leaf_at_offset(syntax, offset)
|
|
|
|
.left_biased()
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|node| ancestors(node))
|
|
|
|
.flat_map(ast::NameRef::cast)
|
|
|
|
.next();
|
|
|
|
let name = match name_ref {
|
|
|
|
None => return Ok(vec![]),
|
|
|
|
Some(name_ref) => name_ref.text(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut query = Query::new(name.to_string());
|
|
|
|
query.exact();
|
2018-08-13 14:19:27 +00:00
|
|
|
query.limit(4);
|
|
|
|
Ok(self.world_symbols(query).collect())
|
2018-08-13 13:35:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:28:34 +00:00
|
|
|
fn reindex(&self) {
|
|
|
|
let data = &*self.data;
|
|
|
|
let unindexed = data.unindexed.load(SeqCst);
|
|
|
|
if unindexed < INDEXING_THRESHOLD {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if unindexed == data.unindexed.compare_and_swap(unindexed, 0, SeqCst) {
|
|
|
|
let now = Instant::now();
|
|
|
|
data.file_map
|
|
|
|
.par_iter()
|
|
|
|
.for_each(|(_, data)| {
|
|
|
|
data.symbols();
|
|
|
|
});
|
|
|
|
info!("parallel indexing took {:?}", now.elapsed());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 19:23:17 +00:00
|
|
|
fn file_data(&self, path: &Path) -> Result<Arc<FileData>> {
|
2018-08-13 10:46:05 +00:00
|
|
|
match self.data.file_map.get(path) {
|
|
|
|
Some(data) => Ok(data.clone()),
|
|
|
|
None => bail!("unknown file: {}", path.display()),
|
2018-08-10 12:07:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 12:35:53 +00:00
|
|
|
|
|
|
|
pub type SearchResult = ::std::result::Result<Continue, Break>;
|
|
|
|
|
|
|
|
pub struct Continue;
|
|
|
|
|
|
|
|
pub struct Break;
|
|
|
|
|
|
|
|
pub const CONTINUE: SearchResult = Ok(Continue);
|
|
|
|
pub const BREAK: SearchResult = Err(Break);
|
2018-08-13 12:10:20 +00:00
|
|
|
|
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-13 16:28:34 +00:00
|
|
|
unindexed: AtomicUsize,
|
2018-08-13 10:46:05 +00:00
|
|
|
file_map: HashMap<PathBuf, Arc<FileData>>,
|
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-10 19:23:17 +00:00
|
|
|
syntax: OnceCell<ast::File>,
|
|
|
|
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
|
|
|
|
|
|
|
fn syntax(&self, path: &Path) -> &ast::File {
|
|
|
|
self.syntax
|
|
|
|
.get_or_init(|| {
|
|
|
|
trace!("parsing: {}", path.display());
|
|
|
|
ast::File::parse(&self.text)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-13 14:19:27 +00:00
|
|
|
fn syntax_transient(&self) -> ast::File {
|
|
|
|
self.syntax.get().map(|s| s.clone())
|
|
|
|
.unwrap_or_else(|| ast::File::parse(&self.text))
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|