aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_project_model/src/cargo_workspace.rs6
-rw-r--r--crates/rust-analyzer/src/global_state.rs23
-rw-r--r--crates/rust-analyzer/src/main_loop.rs73
-rw-r--r--crates/rust-analyzer/src/reload.rs45
-rw-r--r--crates/vfs/src/lib.rs2
5 files changed, 107 insertions, 42 deletions
diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs
index 6d1154056..4182ca156 100644
--- a/crates/ra_project_model/src/cargo_workspace.rs
+++ b/crates/ra_project_model/src/cargo_workspace.rs
@@ -155,7 +155,7 @@ impl CargoWorkspace {
155 if let Some(target) = cargo_features.target.as_ref() { 155 if let Some(target) = cargo_features.target.as_ref() {
156 meta.other_options(vec![String::from("--filter-platform"), target.clone()]); 156 meta.other_options(vec![String::from("--filter-platform"), target.clone()]);
157 } 157 }
158 let meta = meta.exec().with_context(|| { 158 let mut meta = meta.exec().with_context(|| {
159 format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display()) 159 format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display())
160 })?; 160 })?;
161 161
@@ -175,6 +175,7 @@ impl CargoWorkspace {
175 175
176 let ws_members = &meta.workspace_members; 176 let ws_members = &meta.workspace_members;
177 177
178 meta.packages.sort_by(|a, b| a.id.cmp(&b.id));
178 for meta_pkg in meta.packages { 179 for meta_pkg in meta.packages {
179 let cargo_metadata::Package { id, edition, name, manifest_path, version, .. } = 180 let cargo_metadata::Package { id, edition, name, manifest_path, version, .. } =
180 meta_pkg; 181 meta_pkg;
@@ -210,7 +211,7 @@ impl CargoWorkspace {
210 } 211 }
211 } 212 }
212 let resolve = meta.resolve.expect("metadata executed with deps"); 213 let resolve = meta.resolve.expect("metadata executed with deps");
213 for node in resolve.nodes { 214 for mut node in resolve.nodes {
214 let source = match pkg_by_id.get(&node.id) { 215 let source = match pkg_by_id.get(&node.id) {
215 Some(&src) => src, 216 Some(&src) => src,
216 // FIXME: replace this and a similar branch below with `.unwrap`, once 217 // FIXME: replace this and a similar branch below with `.unwrap`, once
@@ -221,6 +222,7 @@ impl CargoWorkspace {
221 continue; 222 continue;
222 } 223 }
223 }; 224 };
225 node.deps.sort_by(|a, b| a.pkg.cmp(&b.pkg));
224 for dep_node in node.deps { 226 for dep_node in node.deps {
225 let pkg = match pkg_by_id.get(&dep_node.pkg) { 227 let pkg = match pkg_by_id.get(&dep_node.pkg) {
226 Some(&pkg) => pkg, 228 Some(&pkg) => pkg,
diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs
index c8d34e15a..9a9a6547a 100644
--- a/crates/rust-analyzer/src/global_state.rs
+++ b/crates/rust-analyzer/src/global_state.rs
@@ -26,6 +26,7 @@ use crate::{
26 to_proto::url_from_abs_path, 26 to_proto::url_from_abs_path,
27 Result, 27 Result,
28}; 28};
29use ra_prof::profile;
29 30
30#[derive(Eq, PartialEq, Copy, Clone)] 31#[derive(Eq, PartialEq, Copy, Clone)]
31pub(crate) enum Status { 32pub(crate) enum Status {
@@ -122,6 +123,10 @@ impl GlobalState {
122 } 123 }
123 124
124 pub(crate) fn process_changes(&mut self) -> bool { 125 pub(crate) fn process_changes(&mut self) -> bool {
126 let _p = profile("GlobalState::process_changes");
127 let mut fs_changes = Vec::new();
128 let mut has_fs_changes = false;
129
125 let change = { 130 let change = {
126 let mut change = AnalysisChange::new(); 131 let mut change = AnalysisChange::new();
127 let (vfs, line_endings_map) = &mut *self.vfs.write(); 132 let (vfs, line_endings_map) = &mut *self.vfs.write();
@@ -130,13 +135,14 @@ impl GlobalState {
130 return false; 135 return false;
131 } 136 }
132 137
133 let fs_op = changed_files.iter().any(|it| it.is_created_or_deleted());
134 if fs_op {
135 let roots = self.source_root_config.partition(&vfs);
136 change.set_roots(roots)
137 }
138
139 for file in changed_files { 138 for file in changed_files {
139 if file.is_created_or_deleted() {
140 if let Some(path) = vfs.file_path(file.file_id).as_path() {
141 fs_changes.push((path.to_path_buf(), file.change_kind));
142 has_fs_changes = true;
143 }
144 }
145
140 let text = if file.exists() { 146 let text = if file.exists() {
141 let bytes = vfs.file_contents(file.file_id).to_vec(); 147 let bytes = vfs.file_contents(file.file_id).to_vec();
142 match String::from_utf8(bytes).ok() { 148 match String::from_utf8(bytes).ok() {
@@ -152,10 +158,15 @@ impl GlobalState {
152 }; 158 };
153 change.change_file(file.file_id, text); 159 change.change_file(file.file_id, text);
154 } 160 }
161 if has_fs_changes {
162 let roots = self.source_root_config.partition(&vfs);
163 change.set_roots(roots);
164 }
155 change 165 change
156 }; 166 };
157 167
158 self.analysis_host.apply_change(change); 168 self.analysis_host.apply_change(change);
169 self.maybe_refresh(&fs_changes);
159 true 170 true
160 } 171 }
161 172
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs
index 96e2399ce..702f25a19 100644
--- a/crates/rust-analyzer/src/main_loop.rs
+++ b/crates/rust-analyzer/src/main_loop.rs
@@ -22,6 +22,7 @@ use crate::{
22 Result, 22 Result,
23}; 23};
24use ra_project_model::ProjectWorkspace; 24use ra_project_model::ProjectWorkspace;
25use vfs::ChangeKind;
25 26
26pub fn main_loop(config: Config, connection: Connection) -> Result<()> { 27pub fn main_loop(config: Config, connection: Connection) -> Result<()> {
27 log::info!("initial config: {:#?}", config); 28 log::info!("initial config: {:#?}", config);
@@ -197,39 +198,49 @@ impl GlobalState {
197 } 198 }
198 self.analysis_host.maybe_collect_garbage(); 199 self.analysis_host.maybe_collect_garbage();
199 } 200 }
200 Event::Vfs(task) => match task { 201 Event::Vfs(mut task) => {
201 vfs::loader::Message::Loaded { files } => { 202 let _p = profile("GlobalState::handle_event/vfs");
202 let vfs = &mut self.vfs.write().0; 203 loop {
203 for (path, contents) in files { 204 match task {
204 let path = VfsPath::from(path); 205 vfs::loader::Message::Loaded { files } => {
205 if !self.mem_docs.contains(&path) { 206 let vfs = &mut self.vfs.write().0;
206 vfs.set_file_contents(path, contents) 207 for (path, contents) in files {
208 let path = VfsPath::from(path);
209 if !self.mem_docs.contains(&path) {
210 vfs.set_file_contents(path, contents)
211 }
212 }
213 }
214 vfs::loader::Message::Progress { n_total, n_done } => {
215 if n_total == 0 {
216 self.transition(Status::Invalid);
217 } else {
218 let state = if n_done == 0 {
219 self.transition(Status::Loading);
220 Progress::Begin
221 } else if n_done < n_total {
222 Progress::Report
223 } else {
224 assert_eq!(n_done, n_total);
225 self.transition(Status::Ready);
226 Progress::End
227 };
228 self.report_progress(
229 "roots scanned",
230 state,
231 Some(format!("{}/{}", n_done, n_total)),
232 Some(Progress::percentage(n_done, n_total)),
233 )
234 }
207 } 235 }
208 } 236 }
209 } 237 // Coalesce many VFS event into a single loop turn
210 vfs::loader::Message::Progress { n_total, n_done } => { 238 task = match self.loader.receiver.try_recv() {
211 if n_total == 0 { 239 Ok(task) => task,
212 self.transition(Status::Invalid); 240 Err(_) => break,
213 } else {
214 let state = if n_done == 0 {
215 self.transition(Status::Loading);
216 Progress::Begin
217 } else if n_done < n_total {
218 Progress::Report
219 } else {
220 assert_eq!(n_done, n_total);
221 self.transition(Status::Ready);
222 Progress::End
223 };
224 self.report_progress(
225 "roots scanned",
226 state,
227 Some(format!("{}/{}", n_done, n_total)),
228 Some(Progress::percentage(n_done, n_total)),
229 )
230 } 241 }
231 } 242 }
232 }, 243 }
233 Event::Flycheck(task) => match task { 244 Event::Flycheck(task) => match task {
234 flycheck::Message::AddDiagnostic { workspace_root, diagnostic } => { 245 flycheck::Message::AddDiagnostic { workspace_root, diagnostic } => {
235 let diagnostics = crate::diagnostics::to_proto::map_rust_diagnostic_to_lsp( 246 let diagnostics = crate::diagnostics::to_proto::map_rust_diagnostic_to_lsp(
@@ -428,7 +439,9 @@ impl GlobalState {
428 if let Some(flycheck) = &this.flycheck { 439 if let Some(flycheck) = &this.flycheck {
429 flycheck.handle.update(); 440 flycheck.handle.update();
430 } 441 }
431 this.maybe_refresh(params.text_document.uri.as_str()); 442 if let Ok(abs_path) = from_proto::abs_path(&params.text_document.uri) {
443 this.maybe_refresh(&[(abs_path, ChangeKind::Modify)]);
444 }
432 Ok(()) 445 Ok(())
433 })? 446 })?
434 .on::<lsp_types::notification::DidChangeConfiguration>(|this, _params| { 447 .on::<lsp_types::notification::DidChangeConfiguration>(|this, _params| {
diff --git a/crates/rust-analyzer/src/reload.rs b/crates/rust-analyzer/src/reload.rs
index 0a201fceb..ffe234a5b 100644
--- a/crates/rust-analyzer/src/reload.rs
+++ b/crates/rust-analyzer/src/reload.rs
@@ -6,7 +6,7 @@ use flycheck::FlycheckHandle;
6use ra_db::{CrateGraph, SourceRoot, VfsPath}; 6use ra_db::{CrateGraph, SourceRoot, VfsPath};
7use ra_ide::AnalysisChange; 7use ra_ide::AnalysisChange;
8use ra_project_model::{PackageRoot, ProcMacroClient, ProjectWorkspace}; 8use ra_project_model::{PackageRoot, ProcMacroClient, ProjectWorkspace};
9use vfs::{file_set::FileSetConfig, AbsPath}; 9use vfs::{file_set::FileSetConfig, AbsPath, AbsPathBuf, ChangeKind};
10 10
11use crate::{ 11use crate::{
12 config::{Config, FilesWatcher, LinkedProject}, 12 config::{Config, FilesWatcher, LinkedProject},
@@ -14,9 +14,11 @@ use crate::{
14 lsp_ext, 14 lsp_ext,
15 main_loop::Task, 15 main_loop::Task,
16}; 16};
17use ra_prof::profile;
17 18
18impl GlobalState { 19impl GlobalState {
19 pub(crate) fn update_configuration(&mut self, config: Config) { 20 pub(crate) fn update_configuration(&mut self, config: Config) {
21 let _p = profile("GlobalState::update_configuration");
20 let old_config = mem::replace(&mut self.config, config); 22 let old_config = mem::replace(&mut self.config, config);
21 if self.config.lru_capacity != old_config.lru_capacity { 23 if self.config.lru_capacity != old_config.lru_capacity {
22 self.analysis_host.update_lru_capacity(old_config.lru_capacity); 24 self.analysis_host.update_lru_capacity(old_config.lru_capacity);
@@ -27,8 +29,8 @@ impl GlobalState {
27 self.reload_flycheck(); 29 self.reload_flycheck();
28 } 30 }
29 } 31 }
30 pub(crate) fn maybe_refresh(&mut self, saved_doc_url: &str) { 32 pub(crate) fn maybe_refresh(&mut self, changes: &[(AbsPathBuf, ChangeKind)]) {
31 if !(saved_doc_url.ends_with("Cargo.toml") || saved_doc_url.ends_with("Cargo.lock")) { 33 if !changes.iter().any(|(path, kind)| is_interesting(path, *kind)) {
32 return; 34 return;
33 } 35 }
34 match self.status { 36 match self.status {
@@ -40,6 +42,41 @@ impl GlobalState {
40 } else { 42 } else {
41 self.transition(Status::NeedsReload); 43 self.transition(Status::NeedsReload);
42 } 44 }
45
46 fn is_interesting(path: &AbsPath, change_kind: ChangeKind) -> bool {
47 const IMPLICIT_TARGET_FILES: &[&str] = &["build.rs", "src/main.rs", "src/lib.rs"];
48 const IMPLICIT_TARGET_DIRS: &[&str] = &["src/bin", "examples", "tests", "benches"];
49
50 if path.ends_with("Cargo.toml") || path.ends_with("Cargo.lock") {
51 return true;
52 }
53 if change_kind == ChangeKind::Modify {
54 return false;
55 }
56 if path.extension().map(|it| it.to_str()) != Some("rs".into()) {
57 return false;
58 }
59 if IMPLICIT_TARGET_FILES.iter().any(|it| path.ends_with(it)) {
60 return true;
61 }
62 let parent = match path.parent() {
63 Some(it) => it,
64 None => return false,
65 };
66 if IMPLICIT_TARGET_DIRS.iter().any(|it| parent.ends_with(it)) {
67 return true;
68 }
69 if path.ends_with("main.rs") {
70 let grand_parent = match parent.parent() {
71 Some(it) => it,
72 None => return false,
73 };
74 if IMPLICIT_TARGET_DIRS.iter().any(|it| grand_parent.ends_with(it)) {
75 return true;
76 }
77 }
78 false
79 }
43 } 80 }
44 pub(crate) fn transition(&mut self, new_status: Status) { 81 pub(crate) fn transition(&mut self, new_status: Status) {
45 self.status = new_status; 82 self.status = new_status;
@@ -79,6 +116,7 @@ impl GlobalState {
79 }); 116 });
80 } 117 }
81 pub(crate) fn switch_workspaces(&mut self, workspaces: Vec<anyhow::Result<ProjectWorkspace>>) { 118 pub(crate) fn switch_workspaces(&mut self, workspaces: Vec<anyhow::Result<ProjectWorkspace>>) {
119 let _p = profile("GlobalState::switch_workspaces");
82 log::info!("reloading projects: {:?}", self.config.linked_projects); 120 log::info!("reloading projects: {:?}", self.config.linked_projects);
83 121
84 let mut has_errors = false; 122 let mut has_errors = false;
@@ -267,6 +305,7 @@ pub(crate) struct SourceRootConfig {
267 305
268impl SourceRootConfig { 306impl SourceRootConfig {
269 pub(crate) fn partition(&self, vfs: &vfs::Vfs) -> Vec<SourceRoot> { 307 pub(crate) fn partition(&self, vfs: &vfs::Vfs) -> Vec<SourceRoot> {
308 let _p = profile("SourceRootConfig::partition");
270 self.fsc 309 self.fsc
271 .partition(vfs) 310 .partition(vfs)
272 .into_iter() 311 .into_iter()
diff --git a/crates/vfs/src/lib.rs b/crates/vfs/src/lib.rs
index 024e58018..3bfecd08f 100644
--- a/crates/vfs/src/lib.rs
+++ b/crates/vfs/src/lib.rs
@@ -70,7 +70,7 @@ impl ChangedFile {
70 } 70 }
71} 71}
72 72
73#[derive(Eq, PartialEq)] 73#[derive(Eq, PartialEq, Copy, Clone, Debug)]
74pub enum ChangeKind { 74pub enum ChangeKind {
75 Create, 75 Create,
76 Modify, 76 Modify,