Honor client's dynamic registration caps

cc https://github.com/rust-analyzer/rust-analyzer/pull/5516#issuecomment-757520828
This commit is contained in:
Aleksey Kladov 2021-01-10 22:38:35 +03:00
parent 035fed5f9f
commit bb453edebe
3 changed files with 70 additions and 51 deletions

View file

@ -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 { pub fn location_link(&self) -> bool {
try_or!(self.caps.text_document.as_ref()?.definition?.link_support?, false) try_or!(self.caps.text_document.as_ref()?.definition?.link_support?, false)
} }

View file

@ -108,38 +108,40 @@ impl GlobalState {
); );
}; };
let save_registration_options = lsp_types::TextDocumentSaveRegistrationOptions { if self.config.did_save_text_document_dynamic_registration() {
include_text: Some(false), let save_registration_options = lsp_types::TextDocumentSaveRegistrationOptions {
text_document_registration_options: lsp_types::TextDocumentRegistrationOptions { include_text: Some(false),
document_selector: Some(vec![ text_document_registration_options: lsp_types::TextDocumentRegistrationOptions {
lsp_types::DocumentFilter { document_selector: Some(vec![
language: None, lsp_types::DocumentFilter {
scheme: None, language: None,
pattern: Some("**/*.rs".into()), scheme: None,
}, pattern: Some("**/*.rs".into()),
lsp_types::DocumentFilter { },
language: None, lsp_types::DocumentFilter {
scheme: None, language: None,
pattern: Some("**/Cargo.toml".into()), scheme: None,
}, pattern: Some("**/Cargo.toml".into()),
lsp_types::DocumentFilter { },
language: None, lsp_types::DocumentFilter {
scheme: None, language: None,
pattern: Some("**/Cargo.lock".into()), scheme: None,
}, pattern: Some("**/Cargo.lock".into()),
]), },
}, ]),
}; },
};
let registration = lsp_types::Registration { let registration = lsp_types::Registration {
id: "textDocument/didSave".to_string(), id: "textDocument/didSave".to_string(),
method: "textDocument/didSave".to_string(), method: "textDocument/didSave".to_string(),
register_options: Some(serde_json::to_value(save_registration_options).unwrap()), register_options: Some(serde_json::to_value(save_registration_options).unwrap()),
}; };
self.send_request::<lsp_types::request::RegisterCapability>( self.send_request::<lsp_types::request::RegisterCapability>(
lsp_types::RegistrationParams { registrations: vec![registration] }, lsp_types::RegistrationParams { registrations: vec![registration] },
|_, _| (), |_, _| (),
); );
}
self.fetch_workspaces_request(); self.fetch_workspaces_request();
self.fetch_workspaces_if_needed(); self.fetch_workspaces_if_needed();

View file

@ -182,26 +182,31 @@ impl GlobalState {
} }
if let FilesWatcher::Client = self.config.files().watcher { if let FilesWatcher::Client = self.config.files().watcher {
let registration_options = lsp_types::DidChangeWatchedFilesRegistrationOptions { if self.config.did_change_watched_files_dynamic_registration() {
watchers: workspaces let registration_options = lsp_types::DidChangeWatchedFilesRegistrationOptions {
.iter() watchers: workspaces
.flat_map(ProjectWorkspace::to_roots) .iter()
.filter(|it| it.is_member) .flat_map(ProjectWorkspace::to_roots)
.flat_map(|root| { .filter(|it| it.is_member)
root.include.into_iter().map(|it| format!("{}/**/*.rs", it.display())) .flat_map(|root| {
}) root.include.into_iter().map(|it| format!("{}/**/*.rs", it.display()))
.map(|glob_pattern| lsp_types::FileSystemWatcher { glob_pattern, kind: None }) })
.collect(), .map(|glob_pattern| lsp_types::FileSystemWatcher {
}; glob_pattern,
let registration = lsp_types::Registration { kind: None,
id: "workspace/didChangeWatchedFiles".to_string(), })
method: "workspace/didChangeWatchedFiles".to_string(), .collect(),
register_options: Some(serde_json::to_value(registration_options).unwrap()), };
}; let registration = lsp_types::Registration {
self.send_request::<lsp_types::request::RegisterCapability>( id: "workspace/didChangeWatchedFiles".to_string(),
lsp_types::RegistrationParams { registrations: vec![registration] }, 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(); let mut change = Change::new();