6 use languageserver_types::Url;
8 Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData,
11 use ra_vfs::{Vfs, VfsChange, VfsFile};
12 use rustc_hash::FxHashMap;
13 use relative_path::RelativePathBuf;
14 use parking_lot::RwLock;
15 use failure::{format_err};
18 project_model::{CargoWorkspace, TargetKind},
23 pub struct ServerWorldState {
24 pub workspaces: Arc<Vec<CargoWorkspace>>,
25 pub analysis_host: AnalysisHost,
26 pub vfs: Arc<RwLock<Vfs>>,
29 pub struct ServerWorld {
30 pub workspaces: Arc<Vec<CargoWorkspace>>,
31 pub analysis: Analysis,
32 pub vfs: Arc<RwLock<Vfs>>,
35 impl ServerWorldState {
36 pub fn new(root: PathBuf, workspaces: Vec<CargoWorkspace>) -> ServerWorldState {
37 let mut change = AnalysisChange::new();
39 let mut roots = Vec::new();
41 for ws in workspaces.iter() {
42 for pkg in ws.packages() {
43 roots.push(pkg.root(&ws).to_path_buf());
46 let (mut vfs, roots) = Vfs::new(roots);
48 change.add_root(SourceRootId(r.0));
51 let mut crate_graph = CrateGraph::default();
52 let mut pkg_to_lib_crate = FxHashMap::default();
53 let mut pkg_crates = FxHashMap::default();
54 for ws in workspaces.iter() {
55 for pkg in ws.packages() {
56 for tgt in pkg.targets(ws) {
57 let root = tgt.root(ws);
58 if let Some(file_id) = vfs.load(root) {
59 let file_id = FileId(file_id.0);
60 let crate_id = crate_graph.add_crate_root(file_id);
61 if tgt.kind(ws) == TargetKind::Lib {
62 pkg_to_lib_crate.insert(pkg, crate_id);
66 .or_insert_with(Vec::new)
71 for pkg in ws.packages() {
72 for dep in pkg.dependencies(ws) {
73 if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
74 for &from in pkg_crates.get(&pkg).into_iter().flatten() {
75 crate_graph.add_dep(from, dep.name.clone(), to);
81 change.set_crate_graph(crate_graph);
83 let mut analysis_host = AnalysisHost::default();
84 analysis_host.apply_change(change);
86 workspaces: Arc::new(workspaces),
88 vfs: Arc::new(RwLock::new(vfs)),
92 /// Returns a vec of libraries
93 /// FIXME: better API here
94 pub fn process_changes(
96 ) -> Vec<(SourceRootId, Vec<(FileId, RelativePathBuf, Arc<String>)>)> {
97 let mut libs = Vec::new();
98 let mut change = AnalysisChange::new();
99 for c in self.vfs.write().commit_changes() {
101 VfsChange::AddRoot { root, files } => {
104 .map(|(vfsfile, path, text)| (FileId(vfsfile.0), path, text))
106 libs.push((SourceRootId(root.0), files));
114 change.add_file(SourceRootId(root.0), FileId(file.0), path, text);
116 VfsChange::RemoveFile { root, file, path } => {
117 change.remove_file(SourceRootId(root.0), FileId(file.0), path)
119 VfsChange::ChangeFile { file, text } => {
120 change.change_file(FileId(file.0), text);
124 self.analysis_host.apply_change(change);
128 pub fn add_lib(&mut self, data: LibraryData) {
129 let mut change = AnalysisChange::new();
130 change.add_library(data);
131 self.analysis_host.apply_change(change);
134 pub fn snapshot(&self) -> ServerWorld {
136 workspaces: Arc::clone(&self.workspaces),
137 analysis: self.analysis_host.analysis(),
138 vfs: Arc::clone(&self.vfs),
144 pub fn analysis(&self) -> &Analysis {
148 pub fn uri_to_file_id(&self, uri: &Url) -> Result<FileId> {
151 .map_err(|()| format_err!("invalid uri: {}", uri))?;
156 .ok_or_else(|| format_err!("unknown file: {}", path.display()))?;
160 pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> {
161 let path = self.vfs.read().file2path(VfsFile(id.0));
162 let url = Url::from_file_path(&path)
163 .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;