7 use languageserver_types::Url;
8 use ra_analysis::{Analysis, AnalysisHost, CrateGraph, CrateId, FileId, FileResolver, LibraryData};
9 use rustc_hash::FxHashMap;
12 path_map::{PathMap, Root},
13 project_model::CargoWorkspace,
14 vfs::{FileEvent, FileEventKind},
19 pub struct ServerWorldState {
20 pub workspaces: Arc<Vec<CargoWorkspace>>,
21 pub analysis_host: AnalysisHost,
22 pub path_map: PathMap,
23 pub mem_map: FxHashMap<FileId, Option<String>>,
26 pub struct ServerWorld {
27 pub workspaces: Arc<Vec<CargoWorkspace>>,
28 pub analysis: Analysis,
29 pub path_map: PathMap,
32 impl ServerWorldState {
33 pub fn new() -> ServerWorldState {
35 workspaces: Arc::new(Vec::new()),
36 analysis_host: AnalysisHost::new(),
37 path_map: PathMap::new(),
38 mem_map: FxHashMap::default(),
41 pub fn apply_fs_changes(&mut self, events: Vec<FileEvent>) {
43 let pm = &mut self.path_map;
44 let mm = &mut self.mem_map;
48 let text = match event.kind {
49 FileEventKind::Add(text) => Some(text),
53 .map(|(path, text)| (pm.get_or_insert(path, Root::Workspace), text))
54 .filter_map(|(id, text)| {
55 if mm.contains_key(&id) {
62 self.analysis_host.change_files(changes);
65 .set_file_resolver(Arc::new(self.path_map.clone()));
67 pub fn events_to_files(
69 events: Vec<FileEvent>,
70 ) -> (Vec<(FileId, String)>, Arc<FileResolver>) {
72 let pm = &mut self.path_map;
76 let FileEventKind::Add(text) = event.kind;
79 .map(|(path, text)| (pm.get_or_insert(path, Root::Lib), text))
82 let resolver = Arc::new(self.path_map.clone());
85 pub fn add_lib(&mut self, data: LibraryData) {
86 self.analysis_host.add_library(data);
89 pub fn add_mem_file(&mut self, path: PathBuf, text: String) -> FileId {
90 let file_id = self.path_map.get_or_insert(path, Root::Workspace);
92 .set_file_resolver(Arc::new(self.path_map.clone()));
93 self.mem_map.insert(file_id, None);
94 if self.path_map.get_root(file_id) != Root::Lib {
95 self.analysis_host.change_file(file_id, Some(text));
100 pub fn change_mem_file(&mut self, path: &Path, text: String) -> Result<()> {
104 .ok_or_else(|| format_err!("change to unknown file: {}", path.display()))?;
105 if self.path_map.get_root(file_id) != Root::Lib {
106 self.analysis_host.change_file(file_id, Some(text));
111 pub fn remove_mem_file(&mut self, path: &Path) -> Result<FileId> {
115 .ok_or_else(|| format_err!("change to unknown file: {}", path.display()))?;
116 match self.mem_map.remove(&file_id) {
118 None => bail!("unmatched close notification"),
120 // Do this via file watcher ideally.
121 let text = fs::read_to_string(path).ok();
122 if self.path_map.get_root(file_id) != Root::Lib {
123 self.analysis_host.change_file(file_id, text);
127 pub fn set_workspaces(&mut self, ws: Vec<CargoWorkspace>) {
128 let mut crate_roots = FxHashMap::default();
132 .flat_map(move |pkg| pkg.targets(ws))
133 .map(move |tgt| tgt.root(ws))
136 if let Some(file_id) = self.path_map.get_id(root) {
137 let crate_id = CrateId(crate_roots.len() as u32);
138 crate_roots.insert(crate_id, file_id);
141 let crate_graph = CrateGraph { crate_roots };
142 self.workspaces = Arc::new(ws);
143 self.analysis_host.set_crate_graph(crate_graph);
145 pub fn snapshot(&self) -> ServerWorld {
147 workspaces: Arc::clone(&self.workspaces),
148 analysis: self.analysis_host.analysis(),
149 path_map: self.path_map.clone(),
155 pub fn analysis(&self) -> &Analysis {
159 pub fn uri_to_file_id(&self, uri: &Url) -> Result<FileId> {
162 .map_err(|()| format_err!("invalid uri: {}", uri))?;
165 .ok_or_else(|| format_err!("unknown file: {}", path.display()))
168 pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> {
169 let path = self.path_map.get_path(id);
170 let url = Url::from_file_path(path)
171 .map_err(|()| format_err!("can't convert path to url: {}", path.display()))?;