rust/crates/ra_lsp_server/src/server_world.rs

167 lines
5.3 KiB
Rust
Raw Normal View History

2018-08-17 11:54:08 -05:00
use std::{
2018-12-19 06:04:15 -06:00
path::{PathBuf},
2018-09-02 06:46:15 -05:00
sync::Arc,
2018-08-17 11:54:08 -05:00
};
use languageserver_types::Url;
2018-10-31 15:41:43 -05:00
use ra_analysis::{
2018-12-19 03:48:34 -06:00
Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData,
2018-12-19 06:04:15 -06:00
SourceRootId
2018-10-31 15:41:43 -05:00
};
2018-12-19 06:04:15 -06:00
use ra_vfs::{Vfs, VfsChange, VfsFile};
use rustc_hash::FxHashMap;
2018-12-19 06:04:15 -06:00
use relative_path::RelativePathBuf;
use parking_lot::RwLock;
use failure::{format_err};
2018-08-17 11:54:08 -05:00
2018-10-15 12:15:53 -05:00
use crate::{
2018-12-08 14:16:11 -06:00
project_model::{CargoWorkspace, TargetKind},
Result,
2018-08-17 11:54:08 -05:00
};
2018-12-19 06:04:15 -06:00
#[derive(Debug)]
2018-08-17 11:54:08 -05:00
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-12-19 06:04:15 -06:00
pub vfs: Arc<RwLock<Vfs>>,
2018-08-17 11:54:08 -05:00
}
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-12-19 06:04:15 -06:00
pub vfs: Arc<RwLock<Vfs>>,
2018-08-17 11:54:08 -05:00
}
impl ServerWorldState {
2018-12-19 06:04:15 -06:00
pub fn new(root: PathBuf, workspaces: Vec<CargoWorkspace>) -> ServerWorldState {
let mut change = AnalysisChange::new();
2018-08-17 11:54:08 -05:00
2018-12-19 06:04:15 -06:00
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());
}
2018-09-04 03:40:45 -05:00
}
2018-12-19 06:04:15 -06:00
let (mut vfs, roots) = Vfs::new(roots);
for r in roots {
change.add_root(SourceRootId(r.0));
2018-09-04 03:40:45 -05:00
}
2018-08-17 11:54:08 -05:00
let mut crate_graph = CrateGraph::default();
2018-12-08 14:16:11 -06:00
let mut pkg_to_lib_crate = FxHashMap::default();
let mut pkg_crates = FxHashMap::default();
2018-12-19 06:04:15 -06:00
for ws in workspaces.iter() {
2018-12-08 14:16:11 -06:00
for pkg in ws.packages() {
for tgt in pkg.targets(ws) {
let root = tgt.root(ws);
2018-12-19 06:04:15 -06:00
if let Some(file_id) = vfs.load(root) {
let file_id = FileId(file_id.0);
2018-12-08 14:16:11 -06:00
let crate_id = crate_graph.add_crate_root(file_id);
if tgt.kind(ws) == TargetKind::Lib {
pkg_to_lib_crate.insert(pkg, crate_id);
}
pkg_crates
.entry(pkg)
.or_insert_with(Vec::new)
.push(crate_id);
}
}
}
for pkg in ws.packages() {
for dep in pkg.dependencies(ws) {
2018-12-08 16:02:53 -06:00
if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
2018-12-08 14:16:11 -06:00
for &from in pkg_crates.get(&pkg).into_iter().flatten() {
2018-12-08 16:02:53 -06:00
crate_graph.add_dep(from, dep.name.clone(), to);
2018-12-08 14:16:11 -06:00
}
}
}
2018-12-08 14:16:11 -06:00
}
}
change.set_crate_graph(crate_graph);
2018-12-19 06:04:15 -06:00
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);
2018-12-19 06:04:15 -06:00
libs
2018-09-02 06:46:15 -05:00
}
2018-12-19 06:04:15 -06:00
pub fn add_lib(&mut self, data: LibraryData) {
let mut change = AnalysisChange::new();
change.add_library(data);
self.analysis_host.apply_change(change);
}
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-09-10 04:57:40 -05:00
analysis: self.analysis_host.analysis(),
2018-12-19 06:04:15 -06:00
vfs: Arc::clone(&self.vfs),
2018-08-17 11:54:08 -05:00
}
}
}
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()
2018-08-17 11:54:08 -05:00
.map_err(|()| format_err!("invalid uri: {}", uri))?;
2018-12-19 06:04:15 -06:00
let file = self
.vfs
.read()
.path2file(&path)
.ok_or_else(|| format_err!("unknown file: {}", path.display()))?;
Ok(FileId(file.0))
2018-08-17 11:54:08 -05:00
}
pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> {
2018-12-19 06:04:15 -06:00
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()))?;
2018-08-17 11:54:08 -05:00
Ok(url)
}
}