rust-analyzer/crates/ra_lsp_server/src/server_world.rs

203 lines
6.6 KiB
Rust
Raw Normal View History

2018-08-17 16:54:08 +00:00
use std::{
2019-01-08 19:33:36 +00:00
path::PathBuf,
2018-09-02 11:46:15 +00:00
sync::Arc,
2018-08-17 16:54:08 +00:00
};
2019-01-14 10:55:56 +00:00
use lsp_types::Url;
2019-01-08 19:33:36 +00:00
use ra_ide_api::{
2018-12-19 09:48:34 +00:00
Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData,
2018-12-19 12:04:15 +00:00
SourceRootId
2018-10-31 20:41:43 +00:00
};
use ra_vfs::{Vfs, VfsChange, VfsFile, VfsRoot};
use relative_path::RelativePathBuf;
2018-12-19 12:04:15 +00:00
use parking_lot::RwLock;
2019-01-08 19:33:36 +00:00
use failure::format_err;
use gen_lsp_server::ErrorCode;
2018-08-17 16:54:08 +00:00
2018-10-15 17:15:53 +00:00
use crate::{
project_model::ProjectWorkspace,
vfs_filter::IncludeRustFiles,
Result,
LspError,
2018-08-17 16:54:08 +00:00
};
2018-12-19 12:04:15 +00:00
#[derive(Debug)]
2018-08-17 16:54:08 +00:00
pub struct ServerWorldState {
2018-12-19 12:40:42 +00:00
pub roots_to_scan: usize,
pub root: PathBuf,
2019-01-10 17:13:08 +00:00
pub workspaces: Arc<Vec<ProjectWorkspace>>,
2018-08-30 09:51:46 +00:00
pub analysis_host: AnalysisHost,
2018-12-19 12:04:15 +00:00
pub vfs: Arc<RwLock<Vfs>>,
2018-08-17 16:54:08 +00:00
}
pub struct ServerWorld {
2019-01-10 17:13:08 +00:00
pub workspaces: Arc<Vec<ProjectWorkspace>>,
2018-08-29 15:03:14 +00:00
pub analysis: Analysis,
2018-12-19 12:04:15 +00:00
pub vfs: Arc<RwLock<Vfs>>,
2018-08-17 16:54:08 +00:00
}
impl ServerWorldState {
2019-01-10 17:13:08 +00:00
pub fn new(root: PathBuf, workspaces: Vec<ProjectWorkspace>) -> ServerWorldState {
let mut change = AnalysisChange::new();
2018-08-17 16:54:08 +00:00
2018-12-19 12:04:15 +00:00
let mut roots = Vec::new();
roots.push(IncludeRustFiles::member(root.clone()));
2018-12-19 12:04:15 +00:00
for ws in workspaces.iter() {
roots.extend(IncludeRustFiles::from_roots(ws.to_roots()));
2018-09-04 08:40:45 +00:00
}
2018-12-19 12:04:15 +00:00
let (mut vfs, roots) = Vfs::new(roots);
2019-02-09 12:06:12 +00:00
let roots_to_scan = roots.len();
2018-12-19 12:04:15 +00:00
for r in roots {
2018-12-19 13:19:53 +00:00
let is_local = vfs.root2path(r).starts_with(&root);
2019-01-04 13:01:06 +00:00
change.add_root(SourceRootId(r.0.into()), is_local);
2018-09-04 08:40:45 +00:00
}
2018-08-17 16:54:08 +00:00
// Create crate graph from all the workspaces
let mut crate_graph = CrateGraph::default();
2019-02-09 10:08:24 +00:00
let mut load = |path: &std::path::Path| {
let vfs_file = vfs.load(path);
vfs_file.map(|f| FileId(f.0.into()))
};
2018-12-19 12:04:15 +00:00
for ws in workspaces.iter() {
2019-02-09 10:08:24 +00:00
crate_graph.extend(ws.to_crate_graph(&mut load));
2018-12-08 20:16:11 +00:00
}
change.set_crate_graph(crate_graph);
2018-12-19 12:04:15 +00:00
let mut analysis_host = AnalysisHost::default();
analysis_host.apply_change(change);
ServerWorldState {
2018-12-19 12:40:42 +00:00
roots_to_scan,
root,
2018-12-19 12:04:15 +00:00
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>)>)> {
2018-12-19 12:40:42 +00:00
let changes = self.vfs.write().commit_changes();
if changes.is_empty() {
return Vec::new();
}
2018-12-19 12:04:15 +00:00
let mut libs = Vec::new();
let mut change = AnalysisChange::new();
2018-12-19 12:40:42 +00:00
for c in changes {
2018-12-19 12:04:15 +00:00
match c {
VfsChange::AddRoot { root, files } => {
2018-12-19 12:40:42 +00:00
let root_path = self.vfs.read().root2path(root);
if root_path.starts_with(&self.root) {
self.roots_to_scan -= 1;
for (file, path, text) in files {
2019-01-04 13:01:06 +00:00
change.add_file(
SourceRootId(root.0.into()),
FileId(file.0.into()),
path,
text,
);
2018-12-19 12:40:42 +00:00
}
} else {
let files = files
.into_iter()
2019-01-04 13:01:06 +00:00
.map(|(vfsfile, path, text)| (FileId(vfsfile.0.into()), path, text))
2018-12-19 12:40:42 +00:00
.collect();
2019-01-04 13:01:06 +00:00
libs.push((SourceRootId(root.0.into()), files));
2018-12-19 12:40:42 +00:00
}
2018-12-19 12:04:15 +00:00
}
2019-02-08 11:49:43 +00:00
VfsChange::AddFile { root, file, path, text } => {
change.add_file(SourceRootId(root.0.into()), FileId(file.0.into()), path, text);
2018-12-19 12:04:15 +00:00
}
VfsChange::RemoveFile { root, file, path } => {
2019-01-04 13:01:06 +00:00
change.remove_file(SourceRootId(root.0.into()), FileId(file.0.into()), path)
2018-12-19 12:04:15 +00:00
}
VfsChange::ChangeFile { file, text } => {
2019-01-04 13:01:06 +00:00
change.change_file(FileId(file.0.into()), text);
2018-12-19 12:04:15 +00:00
}
}
}
self.analysis_host.apply_change(change);
2018-12-19 12:04:15 +00:00
libs
2018-09-02 11:46:15 +00:00
}
2018-12-19 12:04:15 +00:00
pub fn add_lib(&mut self, data: LibraryData) {
2018-12-19 12:40:42 +00:00
self.roots_to_scan -= 1;
2018-12-19 12:04:15 +00:00
let mut change = AnalysisChange::new();
change.add_library(data);
self.analysis_host.apply_change(change);
}
2018-08-21 19:24:59 +00:00
pub fn snapshot(&self) -> ServerWorld {
2018-08-17 16:54:08 +00:00
ServerWorld {
2018-09-02 11:46:15 +00:00
workspaces: Arc::clone(&self.workspaces),
2018-09-10 09:57:40 +00:00
analysis: self.analysis_host.analysis(),
2018-12-19 12:04:15 +00:00
vfs: Arc::clone(&self.vfs),
2018-08-17 16:54:08 +00:00
}
}
2019-01-25 16:11:58 +00:00
2019-01-26 17:33:33 +00:00
pub fn maybe_collect_garbage(&mut self) {
self.analysis_host.maybe_collect_garbage()
}
pub fn collect_garbage(&mut self) {
2019-01-25 16:11:58 +00:00
self.analysis_host.collect_garbage()
}
2018-08-17 16:54:08 +00:00
}
impl ServerWorld {
2018-08-29 15:03:14 +00:00
pub fn analysis(&self) -> &Analysis {
2018-08-17 16:54:08 +00:00
&self.analysis
}
pub fn uri_to_file_id(&self, uri: &Url) -> Result<FileId> {
2019-02-08 11:49:43 +00:00
let path = uri.to_file_path().map_err(|()| format_err!("invalid uri: {}", uri))?;
let file = self.vfs.read().path2file(&path).ok_or_else(|| {
// Check whether give path is exists,
// if so, maybe this file is outside out current workspace
if path.exists() {
LspError {
code: ErrorCode::InvalidRequest as i32,
message: "Rust file outside current workspace is not supported yet.".to_string(),
}
.into()
} else {
format_err!("unknown file: {}", path.display())
}
})?;
2019-01-04 13:01:06 +00:00
Ok(FileId(file.0.into()))
2018-08-17 16:54:08 +00:00
}
pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> {
2019-01-04 13:01:06 +00:00
let path = self.vfs.read().file2path(VfsFile(id.0.into()));
2018-12-19 12:04:15 +00:00
let url = Url::from_file_path(&path)
.map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;
2018-08-17 16:54:08 +00:00
Ok(url)
}
2018-12-21 09:18:14 +00:00
pub fn path_to_uri(&self, root: SourceRootId, path: &RelativePathBuf) -> Result<Url> {
2019-01-04 13:01:06 +00:00
let base = self.vfs.read().root2path(VfsRoot(root.0.into()));
2018-12-21 09:18:14 +00:00
let path = path.to_path(base);
let url = Url::from_file_path(&path)
.map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;
Ok(url)
}
2019-01-22 21:15:03 +00:00
pub fn status(&self) -> String {
let mut res = String::new();
if self.workspaces.is_empty() {
res.push_str("no workspaces\n")
} else {
res.push_str("workspaces:\n");
for w in self.workspaces.iter() {
res += &format!("{} packages loaded\n", w.count());
2019-01-22 21:15:03 +00:00
}
}
res.push_str("\nanalysis:\n");
res.push_str(&self.analysis.status());
res
}
2018-08-17 16:54:08 +00:00
}