2018-08-17 11:54:08 -05:00
|
|
|
use std::{
|
2018-08-28 11:42:55 -05:00
|
|
|
fs,
|
2018-08-17 11:54:08 -05:00
|
|
|
path::{PathBuf, Path},
|
|
|
|
collections::HashMap,
|
2018-09-02 06:46:15 -05:00
|
|
|
sync::Arc,
|
2018-08-17 11:54:08 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
use languageserver_types::Url;
|
2018-09-03 13:26:59 -05:00
|
|
|
use libanalysis::{FileId, AnalysisHost, Analysis, CrateGraph, CrateId, LibraryData};
|
2018-08-17 11:54:08 -05:00
|
|
|
|
|
|
|
use {
|
|
|
|
Result,
|
2018-09-04 03:40:45 -05:00
|
|
|
path_map::{PathMap, Root},
|
2018-08-17 11:54:08 -05:00
|
|
|
vfs::{FileEvent, FileEventKind},
|
2018-09-02 06:46:15 -05:00
|
|
|
project_model::CargoWorkspace,
|
2018-08-17 11:54:08 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ServerWorldState {
|
2018-09-02 06:46:15 -05:00
|
|
|
pub workspaces: Arc<Vec<CargoWorkspace>>,
|
2018-08-30 04:51:46 -05:00
|
|
|
pub analysis_host: AnalysisHost,
|
2018-08-17 11:54:08 -05:00
|
|
|
pub path_map: PathMap,
|
|
|
|
pub mem_map: HashMap<FileId, Option<String>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ServerWorld {
|
2018-09-02 06:46:15 -05:00
|
|
|
pub workspaces: Arc<Vec<CargoWorkspace>>,
|
2018-08-29 10:03:14 -05:00
|
|
|
pub analysis: Analysis,
|
2018-08-17 11:54:08 -05:00
|
|
|
pub path_map: PathMap,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ServerWorldState {
|
|
|
|
pub fn new() -> ServerWorldState {
|
|
|
|
ServerWorldState {
|
2018-09-02 06:46:15 -05:00
|
|
|
workspaces: Arc::new(Vec::new()),
|
2018-08-30 04:51:46 -05:00
|
|
|
analysis_host: AnalysisHost::new(),
|
2018-08-17 11:54:08 -05:00
|
|
|
path_map: PathMap::new(),
|
|
|
|
mem_map: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn apply_fs_changes(&mut self, events: Vec<FileEvent>) {
|
|
|
|
let pm = &mut self.path_map;
|
|
|
|
let mm = &mut self.mem_map;
|
|
|
|
let changes = events.into_iter()
|
|
|
|
.map(|event| {
|
|
|
|
let text = match event.kind {
|
|
|
|
FileEventKind::Add(text) => Some(text),
|
|
|
|
};
|
|
|
|
(event.path, text)
|
|
|
|
})
|
|
|
|
.map(|(path, text)| {
|
2018-09-04 03:40:45 -05:00
|
|
|
(pm.get_or_insert(path, Root::Workspace), text)
|
2018-08-17 11:54:08 -05:00
|
|
|
})
|
|
|
|
.filter_map(|(id, text)| {
|
|
|
|
if mm.contains_key(&id) {
|
|
|
|
mm.insert(id, text);
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some((id, text))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-30 04:51:46 -05:00
|
|
|
self.analysis_host.change_files(changes);
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
2018-09-03 13:26:59 -05:00
|
|
|
pub fn events_to_files(&mut self, events: Vec<FileEvent>) -> Vec<(FileId, String)> {
|
2018-09-03 13:03:37 -05:00
|
|
|
let pm = &mut self.path_map;
|
2018-09-03 13:26:59 -05:00
|
|
|
events.into_iter()
|
2018-09-03 13:03:37 -05:00
|
|
|
.map(|event| {
|
|
|
|
let text = match event.kind {
|
|
|
|
FileEventKind::Add(text) => text,
|
|
|
|
};
|
|
|
|
(event.path, text)
|
|
|
|
})
|
2018-09-04 03:40:45 -05:00
|
|
|
.map(|(path, text)| (pm.get_or_insert(path, Root::Lib), text))
|
2018-09-03 13:26:59 -05:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
pub fn add_lib(&mut self, data: LibraryData) {
|
|
|
|
self.analysis_host.add_library(data);
|
2018-09-03 13:03:37 -05:00
|
|
|
}
|
2018-08-17 11:54:08 -05:00
|
|
|
|
2018-08-30 08:27:09 -05:00
|
|
|
pub fn add_mem_file(&mut self, path: PathBuf, text: String) -> FileId {
|
2018-09-04 03:40:45 -05:00
|
|
|
let file_id = self.path_map.get_or_insert(path, Root::Workspace);
|
2018-08-17 11:54:08 -05:00
|
|
|
self.mem_map.insert(file_id, None);
|
2018-09-04 03:40:45 -05:00
|
|
|
if self.path_map.get_root(file_id) != Root::Lib {
|
|
|
|
self.analysis_host.change_file(file_id, Some(text));
|
|
|
|
}
|
2018-08-30 08:27:09 -05:00
|
|
|
file_id
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn change_mem_file(&mut self, path: &Path, text: String) -> Result<()> {
|
|
|
|
let file_id = self.path_map.get_id(path).ok_or_else(|| {
|
|
|
|
format_err!("change to unknown file: {}", path.display())
|
|
|
|
})?;
|
2018-09-04 03:40:45 -05:00
|
|
|
if self.path_map.get_root(file_id) != Root::Lib {
|
|
|
|
self.analysis_host.change_file(file_id, Some(text));
|
|
|
|
}
|
2018-08-17 11:54:08 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-08-30 08:27:09 -05:00
|
|
|
pub fn remove_mem_file(&mut self, path: &Path) -> Result<FileId> {
|
2018-08-17 11:54:08 -05:00
|
|
|
let file_id = self.path_map.get_id(path).ok_or_else(|| {
|
|
|
|
format_err!("change to unknown file: {}", path.display())
|
|
|
|
})?;
|
2018-08-28 11:42:55 -05:00
|
|
|
match self.mem_map.remove(&file_id) {
|
|
|
|
Some(_) => (),
|
2018-08-17 11:54:08 -05:00
|
|
|
None => bail!("unmatched close notification"),
|
|
|
|
};
|
2018-08-28 11:42:55 -05:00
|
|
|
// Do this via file watcher ideally.
|
|
|
|
let text = fs::read_to_string(path).ok();
|
2018-09-04 03:40:45 -05:00
|
|
|
if self.path_map.get_root(file_id) != Root::Lib {
|
|
|
|
self.analysis_host.change_file(file_id, text);
|
|
|
|
}
|
2018-08-30 08:27:09 -05:00
|
|
|
Ok(file_id)
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
2018-09-02 06:46:15 -05:00
|
|
|
pub fn set_workspaces(&mut self, ws: Vec<CargoWorkspace>) {
|
2018-09-02 08:36:03 -05:00
|
|
|
let mut crate_roots = HashMap::new();
|
|
|
|
ws.iter()
|
|
|
|
.flat_map(|ws| {
|
|
|
|
ws.packages()
|
|
|
|
.flat_map(move |pkg| pkg.targets(ws))
|
|
|
|
.map(move |tgt| tgt.root(ws))
|
|
|
|
})
|
|
|
|
.for_each(|root| {
|
|
|
|
if let Some(file_id) = self.path_map.get_id(root) {
|
|
|
|
let crate_id = CrateId(crate_roots.len() as u32);
|
|
|
|
crate_roots.insert(crate_id, file_id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let crate_graph = CrateGraph { crate_roots };
|
2018-09-02 06:46:15 -05:00
|
|
|
self.workspaces = Arc::new(ws);
|
2018-09-02 08:36:03 -05:00
|
|
|
self.analysis_host.set_crate_graph(crate_graph);
|
2018-09-02 06:46:15 -05:00
|
|
|
}
|
2018-08-21 14:24:59 -05:00
|
|
|
pub fn snapshot(&self) -> ServerWorld {
|
2018-08-17 11:54:08 -05:00
|
|
|
ServerWorld {
|
2018-09-02 06:46:15 -05:00
|
|
|
workspaces: Arc::clone(&self.workspaces),
|
2018-08-30 04:51:46 -05:00
|
|
|
analysis: self.analysis_host.analysis(self.path_map.clone()),
|
2018-08-17 11:54:08 -05:00
|
|
|
path_map: self.path_map.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ServerWorld {
|
2018-08-29 10:03:14 -05:00
|
|
|
pub fn analysis(&self) -> &Analysis {
|
2018-08-17 11:54:08 -05:00
|
|
|
&self.analysis
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn uri_to_file_id(&self, uri: &Url) -> Result<FileId> {
|
|
|
|
let path = uri.to_file_path()
|
|
|
|
.map_err(|()| format_err!("invalid uri: {}", uri))?;
|
|
|
|
self.path_map.get_id(&path).ok_or_else(|| format_err!("unknown file: {}", path.display()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> {
|
|
|
|
let path = self.path_map.get_path(id);
|
|
|
|
let url = Url::from_file_path(path)
|
|
|
|
.map_err(|()| format_err!("can't convert path to url: {}", path.display()))?;
|
|
|
|
Ok(url)
|
|
|
|
}
|
|
|
|
}
|