mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 22:54:58 +00:00
swtich lsp server to vfs
This commit is contained in:
parent
6a755ed83a
commit
a5ef8ad05b
14 changed files with 235 additions and 399 deletions
|
@ -1,11 +1,9 @@
|
|||
mod caps;
|
||||
mod conv;
|
||||
mod main_loop;
|
||||
mod path_map;
|
||||
mod project_model;
|
||||
pub mod req;
|
||||
mod server_world;
|
||||
mod vfs;
|
||||
|
||||
pub type Result<T> = ::std::result::Result<T, ::failure::Error>;
|
||||
pub use crate::{caps::server_capabilities, main_loop::main_loop, main_loop::LspError};
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
mod handlers;
|
||||
mod subscriptions;
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::{
|
||||
path::PathBuf,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use crossbeam_channel::{unbounded, select, Receiver, Sender};
|
||||
use gen_lsp_server::{
|
||||
|
@ -9,8 +12,8 @@ use gen_lsp_server::{
|
|||
};
|
||||
use languageserver_types::NumberOrString;
|
||||
use ra_analysis::{Canceled, FileId, LibraryData};
|
||||
use ra_vfs::{VfsTask};
|
||||
use rayon;
|
||||
use thread_worker::Worker;
|
||||
use threadpool::ThreadPool;
|
||||
use rustc_hash::FxHashSet;
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
|
@ -19,10 +22,9 @@ use failure_derive::Fail;
|
|||
|
||||
use crate::{
|
||||
main_loop::subscriptions::Subscriptions,
|
||||
project_model::{workspace_loader, CargoWorkspace},
|
||||
project_model::{workspace_loader},
|
||||
req,
|
||||
server_world::{ServerWorld, ServerWorldState},
|
||||
vfs::{self, FileEvent},
|
||||
Result,
|
||||
};
|
||||
|
||||
|
@ -50,32 +52,42 @@ enum Task {
|
|||
|
||||
pub fn main_loop(
|
||||
internal_mode: bool,
|
||||
root: PathBuf,
|
||||
ws_root: PathBuf,
|
||||
publish_decorations: bool,
|
||||
msg_receiver: &Receiver<RawMessage>,
|
||||
msg_sender: &Sender<RawMessage>,
|
||||
) -> Result<()> {
|
||||
let pool = ThreadPool::new(8);
|
||||
let (task_sender, task_receiver) = unbounded::<Task>();
|
||||
let (fs_worker, fs_watcher) = vfs::roots_loader();
|
||||
let (ws_worker, ws_watcher) = workspace_loader();
|
||||
|
||||
ws_worker.send(ws_root.clone());
|
||||
// FIXME: support dynamic workspace loading.
|
||||
let workspaces = match ws_worker.recv().unwrap() {
|
||||
Ok(ws) => vec![ws],
|
||||
Err(e) => {
|
||||
log::warn!("loading workspace failed: {}", e);
|
||||
Vec::new()
|
||||
}
|
||||
};
|
||||
ws_worker.shutdown();
|
||||
ws_watcher
|
||||
.shutdown()
|
||||
.map_err(|_| format_err!("ws watcher died"))?;
|
||||
let mut state = ServerWorldState::new(ws_root.clone(), workspaces);
|
||||
|
||||
log::info!("server initialized, serving requests");
|
||||
let mut state = ServerWorldState::default();
|
||||
|
||||
let mut pending_requests = FxHashSet::default();
|
||||
let mut subs = Subscriptions::new();
|
||||
let main_res = main_loop_inner(
|
||||
internal_mode,
|
||||
publish_decorations,
|
||||
root,
|
||||
&pool,
|
||||
msg_sender,
|
||||
msg_receiver,
|
||||
task_sender,
|
||||
task_receiver.clone(),
|
||||
fs_worker,
|
||||
ws_worker,
|
||||
&mut state,
|
||||
&mut pending_requests,
|
||||
&mut subs,
|
||||
|
@ -88,12 +100,11 @@ pub fn main_loop(
|
|||
drop(pool);
|
||||
log::info!("...threadpool has finished");
|
||||
|
||||
let fs_res = fs_watcher.shutdown();
|
||||
let ws_res = ws_watcher.shutdown();
|
||||
let vfs = Arc::try_unwrap(state.vfs).expect("all snapshots should be dead");
|
||||
let vfs_res = vfs.into_inner().shutdown();
|
||||
|
||||
main_res?;
|
||||
fs_res.map_err(|_| format_err!("fs watcher died"))?;
|
||||
ws_res.map_err(|_| format_err!("ws watcher died"))?;
|
||||
vfs_res.map_err(|_| format_err!("fs watcher died"))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -101,28 +112,22 @@ pub fn main_loop(
|
|||
fn main_loop_inner(
|
||||
internal_mode: bool,
|
||||
publish_decorations: bool,
|
||||
ws_root: PathBuf,
|
||||
pool: &ThreadPool,
|
||||
msg_sender: &Sender<RawMessage>,
|
||||
msg_receiver: &Receiver<RawMessage>,
|
||||
task_sender: Sender<Task>,
|
||||
task_receiver: Receiver<Task>,
|
||||
fs_worker: Worker<PathBuf, (PathBuf, Vec<FileEvent>)>,
|
||||
ws_worker: Worker<PathBuf, Result<CargoWorkspace>>,
|
||||
state: &mut ServerWorldState,
|
||||
pending_requests: &mut FxHashSet<u64>,
|
||||
subs: &mut Subscriptions,
|
||||
) -> Result<()> {
|
||||
let (libdata_sender, libdata_receiver) = unbounded();
|
||||
ws_worker.send(ws_root.clone());
|
||||
fs_worker.send(ws_root.clone());
|
||||
loop {
|
||||
#[derive(Debug)]
|
||||
enum Event {
|
||||
Msg(RawMessage),
|
||||
Task(Task),
|
||||
Fs(PathBuf, Vec<FileEvent>),
|
||||
Ws(Result<CargoWorkspace>),
|
||||
Vfs(VfsTask),
|
||||
Lib(LibraryData),
|
||||
}
|
||||
log::trace!("selecting");
|
||||
|
@ -132,77 +137,19 @@ fn main_loop_inner(
|
|||
None => bail!("client exited without shutdown"),
|
||||
},
|
||||
recv(task_receiver, task) => Event::Task(task.unwrap()),
|
||||
recv(fs_worker.out, events) => match events {
|
||||
None => bail!("roots watcher died"),
|
||||
Some((pb, events)) => Event::Fs(pb, events),
|
||||
}
|
||||
recv(ws_worker.out, ws) => match ws {
|
||||
None => bail!("workspace watcher died"),
|
||||
Some(ws) => Event::Ws(ws),
|
||||
recv(state.vfs.read().task_receiver(), task) => match task {
|
||||
None => bail!("vfs died"),
|
||||
Some(task) => Event::Vfs(task),
|
||||
}
|
||||
recv(libdata_receiver, data) => Event::Lib(data.unwrap())
|
||||
};
|
||||
let mut state_changed = false;
|
||||
match event {
|
||||
Event::Task(task) => on_task(task, msg_sender, pending_requests),
|
||||
Event::Fs(root, events) => {
|
||||
log::info!("fs change, {}, {} events", root.display(), events.len());
|
||||
if root == ws_root {
|
||||
state.apply_fs_changes(events);
|
||||
} else {
|
||||
let (files, resolver) = state.events_to_files(events);
|
||||
let sender = libdata_sender.clone();
|
||||
pool.execute(move || {
|
||||
let start = ::std::time::Instant::now();
|
||||
log::info!("indexing {} ... ", root.display());
|
||||
let data = LibraryData::prepare(files, resolver);
|
||||
log::info!("indexed {:?} {}", start.elapsed(), root.display());
|
||||
sender.send(data);
|
||||
});
|
||||
}
|
||||
Event::Vfs(task) => {
|
||||
state.vfs.write().handle_task(task);
|
||||
state_changed = true;
|
||||
}
|
||||
Event::Ws(ws) => match ws {
|
||||
Ok(ws) => {
|
||||
let workspaces = vec![ws];
|
||||
feedback(internal_mode, "workspace loaded", msg_sender);
|
||||
for ws in workspaces.iter() {
|
||||
// Add each library as constant input. If library is
|
||||
// within the workspace, don't treat it as a library.
|
||||
//
|
||||
// HACK: If source roots are nested, pick the outer one.
|
||||
|
||||
let mut roots = ws
|
||||
.packages()
|
||||
.filter(|pkg| !pkg.is_member(ws))
|
||||
.filter_map(|pkg| {
|
||||
let root = pkg.root(ws).to_path_buf();
|
||||
if root.starts_with(&ws_root) {
|
||||
None
|
||||
} else {
|
||||
Some(root)
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
roots.sort_by_key(|it| it.as_os_str().len());
|
||||
let unique = roots
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|&(idx, long)| {
|
||||
!roots[..idx].iter().any(|short| long.starts_with(short))
|
||||
})
|
||||
.map(|(_idx, root)| root);
|
||||
|
||||
for root in unique {
|
||||
log::debug!("sending root, {}", root.display());
|
||||
fs_worker.send(root.to_owned());
|
||||
}
|
||||
}
|
||||
state.set_workspaces(workspaces);
|
||||
state_changed = true;
|
||||
}
|
||||
Err(e) => log::warn!("loading workspace failed: {}", e),
|
||||
},
|
||||
Event::Lib(lib) => {
|
||||
feedback(internal_mode, "library loaded", msg_sender);
|
||||
state.add_lib(lib);
|
||||
|
@ -234,6 +181,18 @@ fn main_loop_inner(
|
|||
},
|
||||
};
|
||||
|
||||
for lib in state.process_changes() {
|
||||
let (root, files) = lib;
|
||||
let sender = libdata_sender.clone();
|
||||
pool.execute(move || {
|
||||
let start = ::std::time::Instant::now();
|
||||
log::info!("indexing {:?} ... ", root);
|
||||
let data = LibraryData::prepare(root, files);
|
||||
log::info!("indexed {:?} {:?}", start.elapsed(), root);
|
||||
sender.send(data);
|
||||
});
|
||||
}
|
||||
|
||||
if state_changed {
|
||||
update_file_notifications_on_threadpool(
|
||||
pool,
|
||||
|
@ -336,8 +295,13 @@ fn on_notification(
|
|||
let path = uri
|
||||
.to_file_path()
|
||||
.map_err(|()| format_err!("invalid uri: {}", uri))?;
|
||||
let file_id = state.add_mem_file(path, params.text_document.text);
|
||||
subs.add_sub(file_id);
|
||||
if let Some(file_id) = state
|
||||
.vfs
|
||||
.write()
|
||||
.add_file_overlay(&path, params.text_document.text)
|
||||
{
|
||||
subs.add_sub(FileId(file_id.0));
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
Err(not) => not,
|
||||
|
@ -353,7 +317,7 @@ fn on_notification(
|
|||
.pop()
|
||||
.ok_or_else(|| format_err!("empty changes"))?
|
||||
.text;
|
||||
state.change_mem_file(path.as_path(), text)?;
|
||||
state.vfs.write().change_file_overlay(path.as_path(), text);
|
||||
return Ok(());
|
||||
}
|
||||
Err(not) => not,
|
||||
|
@ -364,8 +328,9 @@ fn on_notification(
|
|||
let path = uri
|
||||
.to_file_path()
|
||||
.map_err(|()| format_err!("invalid uri: {}", uri))?;
|
||||
let file_id = state.remove_mem_file(path.as_path())?;
|
||||
subs.remove_sub(file_id);
|
||||
if let Some(file_id) = state.vfs.write().remove_file_overlay(path.as_path()) {
|
||||
subs.remove_sub(FileId(file_id.0));
|
||||
}
|
||||
let params = req::PublishDiagnosticsParams {
|
||||
uri,
|
||||
diagnostics: Vec::new(),
|
||||
|
|
|
@ -326,9 +326,9 @@ pub fn handle_runnables(
|
|||
None => return Ok(None),
|
||||
};
|
||||
let file_id = world.analysis().crate_root(crate_id)?;
|
||||
let path = world.path_map.get_path(file_id);
|
||||
let path = world.vfs.read().file2path(ra_vfs::VfsFile(file_id.0));
|
||||
let res = world.workspaces.iter().find_map(|ws| {
|
||||
let tgt = ws.target_by_root(path)?;
|
||||
let tgt = ws.target_by_root(&path)?;
|
||||
let res = CargoTargetSpec {
|
||||
package: tgt.package(ws).name(ws).to_string(),
|
||||
target: tgt.name(ws).to_string(),
|
||||
|
|
|
@ -1,105 +0,0 @@
|
|||
use std::{
|
||||
fmt,
|
||||
path::{Component, Path, PathBuf},
|
||||
};
|
||||
|
||||
use im;
|
||||
use ra_analysis::{FileId};
|
||||
use relative_path::RelativePath;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Root {
|
||||
Workspace,
|
||||
Lib,
|
||||
}
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct PathMap {
|
||||
next_id: u32,
|
||||
path2id: im::HashMap<PathBuf, FileId>,
|
||||
id2path: im::HashMap<FileId, PathBuf>,
|
||||
id2root: im::HashMap<FileId, Root>,
|
||||
}
|
||||
|
||||
impl fmt::Debug for PathMap {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str("PathMap { ... }")
|
||||
}
|
||||
}
|
||||
|
||||
impl PathMap {
|
||||
pub fn get_or_insert(&mut self, path: PathBuf, root: Root) -> (bool, FileId) {
|
||||
let mut inserted = false;
|
||||
let file_id = self
|
||||
.path2id
|
||||
.get(path.as_path())
|
||||
.map(|&id| id)
|
||||
.unwrap_or_else(|| {
|
||||
inserted = true;
|
||||
let id = self.new_file_id();
|
||||
self.insert(path, id, root);
|
||||
id
|
||||
});
|
||||
(inserted, file_id)
|
||||
}
|
||||
pub fn get_id(&self, path: &Path) -> Option<FileId> {
|
||||
self.path2id.get(path).cloned()
|
||||
}
|
||||
pub fn get_path(&self, file_id: FileId) -> &Path {
|
||||
self.id2path.get(&file_id).unwrap().as_path()
|
||||
}
|
||||
pub fn get_root(&self, file_id: FileId) -> Root {
|
||||
self.id2root[&file_id]
|
||||
}
|
||||
fn insert(&mut self, path: PathBuf, file_id: FileId, root: Root) {
|
||||
self.path2id.insert(path.clone(), file_id);
|
||||
self.id2path.insert(file_id, path.clone());
|
||||
self.id2root.insert(file_id, root);
|
||||
}
|
||||
|
||||
fn new_file_id(&mut self) -> FileId {
|
||||
let id = FileId(self.next_id);
|
||||
self.next_id += 1;
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize(path: &Path) -> PathBuf {
|
||||
let mut components = path.components().peekable();
|
||||
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
|
||||
components.next();
|
||||
PathBuf::from(c.as_os_str())
|
||||
} else {
|
||||
PathBuf::new()
|
||||
};
|
||||
|
||||
for component in components {
|
||||
match component {
|
||||
Component::Prefix(..) => unreachable!(),
|
||||
Component::RootDir => {
|
||||
ret.push(component.as_os_str());
|
||||
}
|
||||
Component::CurDir => {}
|
||||
Component::ParentDir => {
|
||||
ret.pop();
|
||||
}
|
||||
Component::Normal(c) => {
|
||||
ret.push(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_resolve() {
|
||||
let mut m = PathMap::default();
|
||||
let (_, id1) = m.get_or_insert(PathBuf::from("/foo"), Root::Workspace);
|
||||
let (_, id2) = m.get_or_insert(PathBuf::from("/foo/bar.rs"), Root::Workspace);
|
||||
assert_eq!(m.resolve(id1, &RelativePath::new("bar.rs")), Some(id2),)
|
||||
}
|
||||
}
|
|
@ -69,6 +69,7 @@ impl Package {
|
|||
pub fn targets<'a>(self, ws: &'a CargoWorkspace) -> impl Iterator<Item = Target> + 'a {
|
||||
ws.pkg(self).targets.iter().cloned()
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn is_member(self, ws: &CargoWorkspace) -> bool {
|
||||
ws.pkg(self).is_member
|
||||
}
|
||||
|
|
|
@ -1,154 +1,62 @@
|
|||
use std::{
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
path::{PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use languageserver_types::Url;
|
||||
use ra_analysis::{
|
||||
Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData,
|
||||
SourceRootId
|
||||
};
|
||||
use ra_vfs::{Vfs, VfsChange, VfsFile};
|
||||
use rustc_hash::FxHashMap;
|
||||
use failure::{bail, format_err};
|
||||
use relative_path::RelativePathBuf;
|
||||
use parking_lot::RwLock;
|
||||
use failure::{format_err};
|
||||
|
||||
use crate::{
|
||||
path_map::{PathMap, Root},
|
||||
project_model::{CargoWorkspace, TargetKind},
|
||||
vfs::{FileEvent, FileEventKind},
|
||||
Result,
|
||||
};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
#[derive(Debug)]
|
||||
pub struct ServerWorldState {
|
||||
pub workspaces: Arc<Vec<CargoWorkspace>>,
|
||||
pub analysis_host: AnalysisHost,
|
||||
pub path_map: PathMap,
|
||||
pub mem_map: FxHashMap<FileId, Option<String>>,
|
||||
pub vfs: Arc<RwLock<Vfs>>,
|
||||
}
|
||||
|
||||
pub struct ServerWorld {
|
||||
pub workspaces: Arc<Vec<CargoWorkspace>>,
|
||||
pub analysis: Analysis,
|
||||
pub path_map: PathMap,
|
||||
pub vfs: Arc<RwLock<Vfs>>,
|
||||
}
|
||||
|
||||
impl ServerWorldState {
|
||||
pub fn apply_fs_changes(&mut self, events: Vec<FileEvent>) {
|
||||
pub fn new(root: PathBuf, workspaces: Vec<CargoWorkspace>) -> ServerWorldState {
|
||||
let mut change = AnalysisChange::new();
|
||||
let mut inserted = false;
|
||||
{
|
||||
let pm = &mut self.path_map;
|
||||
let mm = &mut self.mem_map;
|
||||
events
|
||||
.into_iter()
|
||||
.map(|event| {
|
||||
let text = match event.kind {
|
||||
FileEventKind::Add(text) => text,
|
||||
};
|
||||
(event.path, text)
|
||||
})
|
||||
.map(|(path, text)| {
|
||||
let (ins, file_id) = pm.get_or_insert(path, Root::Workspace);
|
||||
inserted |= ins;
|
||||
(file_id, text)
|
||||
})
|
||||
.filter_map(|(file_id, text)| {
|
||||
if mm.contains_key(&file_id) {
|
||||
mm.insert(file_id, Some(text));
|
||||
None
|
||||
} else {
|
||||
Some((file_id, text))
|
||||
}
|
||||
})
|
||||
.for_each(|(file_id, text)| change.add_file(file_id, text));
|
||||
}
|
||||
if inserted {
|
||||
change.set_file_resolver(Arc::new(self.path_map.clone()))
|
||||
}
|
||||
self.analysis_host.apply_change(change);
|
||||
}
|
||||
pub fn events_to_files(
|
||||
&mut self,
|
||||
events: Vec<FileEvent>,
|
||||
) -> (Vec<(FileId, String)>, Arc<FileResolver>) {
|
||||
let files = {
|
||||
let pm = &mut self.path_map;
|
||||
events
|
||||
.into_iter()
|
||||
.map(|event| {
|
||||
let FileEventKind::Add(text) = event.kind;
|
||||
(event.path, text)
|
||||
})
|
||||
.map(|(path, text)| (pm.get_or_insert(path, Root::Lib).1, text))
|
||||
.collect()
|
||||
};
|
||||
let resolver = Arc::new(self.path_map.clone());
|
||||
(files, resolver)
|
||||
}
|
||||
pub fn add_lib(&mut self, data: LibraryData) {
|
||||
let mut change = AnalysisChange::new();
|
||||
change.add_library(data);
|
||||
self.analysis_host.apply_change(change);
|
||||
}
|
||||
|
||||
pub fn add_mem_file(&mut self, path: PathBuf, text: String) -> FileId {
|
||||
let (inserted, file_id) = self.path_map.get_or_insert(path, Root::Workspace);
|
||||
if self.path_map.get_root(file_id) != Root::Lib {
|
||||
let mut change = AnalysisChange::new();
|
||||
if inserted {
|
||||
change.add_file(file_id, text);
|
||||
change.set_file_resolver(Arc::new(self.path_map.clone()));
|
||||
} else {
|
||||
change.change_file(file_id, text);
|
||||
let mut roots = Vec::new();
|
||||
roots.push(root);
|
||||
for ws in workspaces.iter() {
|
||||
for pkg in ws.packages() {
|
||||
roots.push(pkg.root(&ws).to_path_buf());
|
||||
}
|
||||
self.analysis_host.apply_change(change);
|
||||
}
|
||||
self.mem_map.insert(file_id, None);
|
||||
file_id
|
||||
}
|
||||
let (mut vfs, roots) = Vfs::new(roots);
|
||||
for r in roots {
|
||||
change.add_root(SourceRootId(r.0));
|
||||
}
|
||||
|
||||
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()))?;
|
||||
if self.path_map.get_root(file_id) != Root::Lib {
|
||||
let mut change = AnalysisChange::new();
|
||||
change.change_file(file_id, text);
|
||||
self.analysis_host.apply_change(change);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remove_mem_file(&mut self, path: &Path) -> Result<FileId> {
|
||||
let file_id = self
|
||||
.path_map
|
||||
.get_id(path)
|
||||
.ok_or_else(|| format_err!("change to unknown file: {}", path.display()))?;
|
||||
match self.mem_map.remove(&file_id) {
|
||||
Some(_) => (),
|
||||
None => bail!("unmatched close notification"),
|
||||
};
|
||||
// Do this via file watcher ideally.
|
||||
let text = fs::read_to_string(path).ok();
|
||||
if self.path_map.get_root(file_id) != Root::Lib {
|
||||
let mut change = AnalysisChange::new();
|
||||
if let Some(text) = text {
|
||||
change.change_file(file_id, text);
|
||||
}
|
||||
self.analysis_host.apply_change(change);
|
||||
}
|
||||
Ok(file_id)
|
||||
}
|
||||
pub fn set_workspaces(&mut self, ws: Vec<CargoWorkspace>) {
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
let mut pkg_to_lib_crate = FxHashMap::default();
|
||||
let mut pkg_crates = FxHashMap::default();
|
||||
for ws in ws.iter() {
|
||||
for ws in workspaces.iter() {
|
||||
for pkg in ws.packages() {
|
||||
for tgt in pkg.targets(ws) {
|
||||
let root = tgt.root(ws);
|
||||
if let Some(file_id) = self.path_map.get_id(root) {
|
||||
if let Some(file_id) = vfs.load(root) {
|
||||
let file_id = FileId(file_id.0);
|
||||
let crate_id = crate_graph.add_crate_root(file_id);
|
||||
if tgt.kind(ws) == TargetKind::Lib {
|
||||
pkg_to_lib_crate.insert(pkg, crate_id);
|
||||
|
@ -170,16 +78,64 @@ impl ServerWorldState {
|
|||
}
|
||||
}
|
||||
}
|
||||
self.workspaces = Arc::new(ws);
|
||||
let mut change = AnalysisChange::new();
|
||||
change.set_crate_graph(crate_graph);
|
||||
|
||||
let mut analysis_host = AnalysisHost::default();
|
||||
analysis_host.apply_change(change);
|
||||
ServerWorldState {
|
||||
workspaces: Arc::new(workspaces),
|
||||
analysis_host,
|
||||
vfs: Arc::new(RwLock::new(vfs)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a vec of libraries
|
||||
/// FIXME: better API here
|
||||
pub fn process_changes(
|
||||
&mut self,
|
||||
) -> Vec<(SourceRootId, Vec<(FileId, RelativePathBuf, Arc<String>)>)> {
|
||||
let mut libs = Vec::new();
|
||||
let mut change = AnalysisChange::new();
|
||||
for c in self.vfs.write().commit_changes() {
|
||||
match c {
|
||||
VfsChange::AddRoot { root, files } => {
|
||||
let files = files
|
||||
.into_iter()
|
||||
.map(|(vfsfile, path, text)| (FileId(vfsfile.0), path, text))
|
||||
.collect();
|
||||
libs.push((SourceRootId(root.0), files));
|
||||
}
|
||||
VfsChange::AddFile {
|
||||
root,
|
||||
file,
|
||||
path,
|
||||
text,
|
||||
} => {
|
||||
change.add_file(SourceRootId(root.0), FileId(file.0), path, text);
|
||||
}
|
||||
VfsChange::RemoveFile { root, file, path } => {
|
||||
change.remove_file(SourceRootId(root.0), FileId(file.0), path)
|
||||
}
|
||||
VfsChange::ChangeFile { file, text } => {
|
||||
change.change_file(FileId(file.0), text);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.analysis_host.apply_change(change);
|
||||
libs
|
||||
}
|
||||
|
||||
pub fn add_lib(&mut self, data: LibraryData) {
|
||||
let mut change = AnalysisChange::new();
|
||||
change.add_library(data);
|
||||
self.analysis_host.apply_change(change);
|
||||
}
|
||||
|
||||
pub fn snapshot(&self) -> ServerWorld {
|
||||
ServerWorld {
|
||||
workspaces: Arc::clone(&self.workspaces),
|
||||
analysis: self.analysis_host.analysis(),
|
||||
path_map: self.path_map.clone(),
|
||||
vfs: Arc::clone(&self.vfs),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -193,15 +149,18 @@ impl ServerWorld {
|
|||
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()))
|
||||
let file = self
|
||||
.vfs
|
||||
.read()
|
||||
.path2file(&path)
|
||||
.ok_or_else(|| format_err!("unknown file: {}", path.display()))?;
|
||||
Ok(FileId(file.0))
|
||||
}
|
||||
|
||||
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()))?;
|
||||
let path = self.vfs.read().file2path(VfsFile(id.0));
|
||||
let url = Url::from_file_path(&path)
|
||||
.map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;
|
||||
Ok(url)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,67 +0,0 @@
|
|||
use std::{
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use walkdir::WalkDir;
|
||||
use thread_worker::{WorkerHandle, Worker};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FileEvent {
|
||||
pub path: PathBuf,
|
||||
pub kind: FileEventKind,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum FileEventKind {
|
||||
Add(String),
|
||||
}
|
||||
|
||||
pub fn roots_loader() -> (Worker<PathBuf, (PathBuf, Vec<FileEvent>)>, WorkerHandle) {
|
||||
thread_worker::spawn::<PathBuf, (PathBuf, Vec<FileEvent>), _>(
|
||||
"roots loader",
|
||||
128,
|
||||
|input_receiver, output_sender| {
|
||||
input_receiver
|
||||
.map(|path| {
|
||||
log::debug!("loading {} ...", path.as_path().display());
|
||||
let events = load_root(path.as_path());
|
||||
log::debug!("... loaded {}", path.as_path().display());
|
||||
(path, events)
|
||||
})
|
||||
.for_each(|it| output_sender.send(it))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn load_root(path: &Path) -> Vec<FileEvent> {
|
||||
let mut res = Vec::new();
|
||||
for entry in WalkDir::new(path) {
|
||||
let entry = match entry {
|
||||
Ok(entry) => entry,
|
||||
Err(e) => {
|
||||
log::warn!("watcher error: {}", e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if !entry.file_type().is_file() {
|
||||
continue;
|
||||
}
|
||||
let path = entry.path();
|
||||
if path.extension().and_then(|os| os.to_str()) != Some("rs") {
|
||||
continue;
|
||||
}
|
||||
let text = match fs::read_to_string(path) {
|
||||
Ok(text) => text,
|
||||
Err(e) => {
|
||||
log::warn!("watcher error: {}", e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
res.push(FileEvent {
|
||||
path: path.to_owned(),
|
||||
kind: FileEventKind::Add(text),
|
||||
})
|
||||
}
|
||||
res
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue