about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2021-01-10 22:38:35 +0300
committerAleksey Kladov <aleksey.kladov@gmail.com>2021-01-10 22:38:35 +0300
commitbb453edebe477ab754d6c78e11db1bf3af08833c (patch)
tree262275f1d401de42d0410d87feb8c789d9f0015c
parent035fed5f9f7c062da7d23190dab1a7021fd48a5d (diff)
downloadrust-bb453edebe477ab754d6c78e11db1bf3af08833c.tar.gz
rust-bb453edebe477ab754d6c78e11db1bf3af08833c.zip
Honor client's dynamic registration caps
cc https://github.com/rust-analyzer/rust-analyzer/pull/5516#issuecomment-757520828
-rw-r--r--crates/rust-analyzer/src/config.rs12
-rw-r--r--crates/rust-analyzer/src/main_loop.rs66
-rw-r--r--crates/rust-analyzer/src/reload.rs45
3 files changed, 71 insertions, 52 deletions
diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs
index 2d3e25cbf2a..27b92a5a9bf 100644
--- a/crates/rust-analyzer/src/config.rs
+++ b/crates/rust-analyzer/src/config.rs
@@ -334,6 +334,18 @@ impl Config {
         }
     }
 
+    pub fn did_save_text_document_dynamic_registration(&self) -> bool {
+        let caps =
+            try_or!(self.caps.text_document.as_ref()?.synchronization.clone()?, Default::default());
+        caps.did_save == Some(true) && caps.dynamic_registration == Some(true)
+    }
+    pub fn did_change_watched_files_dynamic_registration(&self) -> bool {
+        try_or!(
+            self.caps.workspace.as_ref()?.did_change_watched_files.as_ref()?.dynamic_registration?,
+            false
+        )
+    }
+
     pub fn location_link(&self) -> bool {
         try_or!(self.caps.text_document.as_ref()?.definition?.link_support?, false)
     }
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs
index 51fb2eb746f..6d2475a590a 100644
--- a/crates/rust-analyzer/src/main_loop.rs
+++ b/crates/rust-analyzer/src/main_loop.rs
@@ -108,38 +108,40 @@ impl GlobalState {
             );
         };
 
-        let save_registration_options = lsp_types::TextDocumentSaveRegistrationOptions {
-            include_text: Some(false),
-            text_document_registration_options: lsp_types::TextDocumentRegistrationOptions {
-                document_selector: Some(vec![
-                    lsp_types::DocumentFilter {
-                        language: None,
-                        scheme: None,
-                        pattern: Some("**/*.rs".into()),
-                    },
-                    lsp_types::DocumentFilter {
-                        language: None,
-                        scheme: None,
-                        pattern: Some("**/Cargo.toml".into()),
-                    },
-                    lsp_types::DocumentFilter {
-                        language: None,
-                        scheme: None,
-                        pattern: Some("**/Cargo.lock".into()),
-                    },
-                ]),
-            },
-        };
-
-        let registration = lsp_types::Registration {
-            id: "textDocument/didSave".to_string(),
-            method: "textDocument/didSave".to_string(),
-            register_options: Some(serde_json::to_value(save_registration_options).unwrap()),
-        };
-        self.send_request::<lsp_types::request::RegisterCapability>(
-            lsp_types::RegistrationParams { registrations: vec![registration] },
-            |_, _| (),
-        );
+        if self.config.did_save_text_document_dynamic_registration() {
+            let save_registration_options = lsp_types::TextDocumentSaveRegistrationOptions {
+                include_text: Some(false),
+                text_document_registration_options: lsp_types::TextDocumentRegistrationOptions {
+                    document_selector: Some(vec![
+                        lsp_types::DocumentFilter {
+                            language: None,
+                            scheme: None,
+                            pattern: Some("**/*.rs".into()),
+                        },
+                        lsp_types::DocumentFilter {
+                            language: None,
+                            scheme: None,
+                            pattern: Some("**/Cargo.toml".into()),
+                        },
+                        lsp_types::DocumentFilter {
+                            language: None,
+                            scheme: None,
+                            pattern: Some("**/Cargo.lock".into()),
+                        },
+                    ]),
+                },
+            };
+
+            let registration = lsp_types::Registration {
+                id: "textDocument/didSave".to_string(),
+                method: "textDocument/didSave".to_string(),
+                register_options: Some(serde_json::to_value(save_registration_options).unwrap()),
+            };
+            self.send_request::<lsp_types::request::RegisterCapability>(
+                lsp_types::RegistrationParams { registrations: vec![registration] },
+                |_, _| (),
+            );
+        }
 
         self.fetch_workspaces_request();
         self.fetch_workspaces_if_needed();
diff --git a/crates/rust-analyzer/src/reload.rs b/crates/rust-analyzer/src/reload.rs
index accf2ef8c4c..97e20362f65 100644
--- a/crates/rust-analyzer/src/reload.rs
+++ b/crates/rust-analyzer/src/reload.rs
@@ -182,26 +182,31 @@ impl GlobalState {
         }
 
         if let FilesWatcher::Client = self.config.files().watcher {
-            let registration_options = lsp_types::DidChangeWatchedFilesRegistrationOptions {
-                watchers: workspaces
-                    .iter()
-                    .flat_map(ProjectWorkspace::to_roots)
-                    .filter(|it| it.is_member)
-                    .flat_map(|root| {
-                        root.include.into_iter().map(|it| format!("{}/**/*.rs", it.display()))
-                    })
-                    .map(|glob_pattern| lsp_types::FileSystemWatcher { glob_pattern, kind: None })
-                    .collect(),
-            };
-            let registration = lsp_types::Registration {
-                id: "workspace/didChangeWatchedFiles".to_string(),
-                method: "workspace/didChangeWatchedFiles".to_string(),
-                register_options: Some(serde_json::to_value(registration_options).unwrap()),
-            };
-            self.send_request::<lsp_types::request::RegisterCapability>(
-                lsp_types::RegistrationParams { registrations: vec![registration] },
-                |_, _| (),
-            );
+            if self.config.did_change_watched_files_dynamic_registration() {
+                let registration_options = lsp_types::DidChangeWatchedFilesRegistrationOptions {
+                    watchers: workspaces
+                        .iter()
+                        .flat_map(ProjectWorkspace::to_roots)
+                        .filter(|it| it.is_member)
+                        .flat_map(|root| {
+                            root.include.into_iter().map(|it| format!("{}/**/*.rs", it.display()))
+                        })
+                        .map(|glob_pattern| lsp_types::FileSystemWatcher {
+                            glob_pattern,
+                            kind: None,
+                        })
+                        .collect(),
+                };
+                let registration = lsp_types::Registration {
+                    id: "workspace/didChangeWatchedFiles".to_string(),
+                    method: "workspace/didChangeWatchedFiles".to_string(),
+                    register_options: Some(serde_json::to_value(registration_options).unwrap()),
+                };
+                self.send_request::<lsp_types::request::RegisterCapability>(
+                    lsp_types::RegistrationParams { registrations: vec![registration] },
+                    |_, _| (),
+                );
+            }
         }
 
         let mut change = Change::new();