Skip to content

Commit 60c501f

Browse files
bors[bot]matklad
andauthored
Merge #7241
7241: Honor client's dynamic registration caps r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 607b9ea + bb453ed commit 60c501f

File tree

3 files changed

+71
-52
lines changed

3 files changed

+71
-52
lines changed

crates/rust-analyzer/src/config.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,18 @@ impl Config {
334334
}
335335
}
336336

337+
pub fn did_save_text_document_dynamic_registration(&self) -> bool {
338+
let caps =
339+
try_or!(self.caps.text_document.as_ref()?.synchronization.clone()?, Default::default());
340+
caps.did_save == Some(true) && caps.dynamic_registration == Some(true)
341+
}
342+
pub fn did_change_watched_files_dynamic_registration(&self) -> bool {
343+
try_or!(
344+
self.caps.workspace.as_ref()?.did_change_watched_files.as_ref()?.dynamic_registration?,
345+
false
346+
)
347+
}
348+
337349
pub fn location_link(&self) -> bool {
338350
try_or!(self.caps.text_document.as_ref()?.definition?.link_support?, false)
339351
}

crates/rust-analyzer/src/main_loop.rs

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -108,38 +108,40 @@ impl GlobalState {
108108
);
109109
};
110110

111-
let save_registration_options = lsp_types::TextDocumentSaveRegistrationOptions {
112-
include_text: Some(false),
113-
text_document_registration_options: lsp_types::TextDocumentRegistrationOptions {
114-
document_selector: Some(vec![
115-
lsp_types::DocumentFilter {
116-
language: None,
117-
scheme: None,
118-
pattern: Some("**/*.rs".into()),
119-
},
120-
lsp_types::DocumentFilter {
121-
language: None,
122-
scheme: None,
123-
pattern: Some("**/Cargo.toml".into()),
124-
},
125-
lsp_types::DocumentFilter {
126-
language: None,
127-
scheme: None,
128-
pattern: Some("**/Cargo.lock".into()),
129-
},
130-
]),
131-
},
132-
};
133-
134-
let registration = lsp_types::Registration {
135-
id: "textDocument/didSave".to_string(),
136-
method: "textDocument/didSave".to_string(),
137-
register_options: Some(serde_json::to_value(save_registration_options).unwrap()),
138-
};
139-
self.send_request::<lsp_types::request::RegisterCapability>(
140-
lsp_types::RegistrationParams { registrations: vec![registration] },
141-
|_, _| (),
142-
);
111+
if self.config.did_save_text_document_dynamic_registration() {
112+
let save_registration_options = lsp_types::TextDocumentSaveRegistrationOptions {
113+
include_text: Some(false),
114+
text_document_registration_options: lsp_types::TextDocumentRegistrationOptions {
115+
document_selector: Some(vec![
116+
lsp_types::DocumentFilter {
117+
language: None,
118+
scheme: None,
119+
pattern: Some("**/*.rs".into()),
120+
},
121+
lsp_types::DocumentFilter {
122+
language: None,
123+
scheme: None,
124+
pattern: Some("**/Cargo.toml".into()),
125+
},
126+
lsp_types::DocumentFilter {
127+
language: None,
128+
scheme: None,
129+
pattern: Some("**/Cargo.lock".into()),
130+
},
131+
]),
132+
},
133+
};
134+
135+
let registration = lsp_types::Registration {
136+
id: "textDocument/didSave".to_string(),
137+
method: "textDocument/didSave".to_string(),
138+
register_options: Some(serde_json::to_value(save_registration_options).unwrap()),
139+
};
140+
self.send_request::<lsp_types::request::RegisterCapability>(
141+
lsp_types::RegistrationParams { registrations: vec![registration] },
142+
|_, _| (),
143+
);
144+
}
143145

144146
self.fetch_workspaces_request();
145147
self.fetch_workspaces_if_needed();

crates/rust-analyzer/src/reload.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -182,26 +182,31 @@ impl GlobalState {
182182
}
183183

184184
if let FilesWatcher::Client = self.config.files().watcher {
185-
let registration_options = lsp_types::DidChangeWatchedFilesRegistrationOptions {
186-
watchers: workspaces
187-
.iter()
188-
.flat_map(ProjectWorkspace::to_roots)
189-
.filter(|it| it.is_member)
190-
.flat_map(|root| {
191-
root.include.into_iter().map(|it| format!("{}/**/*.rs", it.display()))
192-
})
193-
.map(|glob_pattern| lsp_types::FileSystemWatcher { glob_pattern, kind: None })
194-
.collect(),
195-
};
196-
let registration = lsp_types::Registration {
197-
id: "workspace/didChangeWatchedFiles".to_string(),
198-
method: "workspace/didChangeWatchedFiles".to_string(),
199-
register_options: Some(serde_json::to_value(registration_options).unwrap()),
200-
};
201-
self.send_request::<lsp_types::request::RegisterCapability>(
202-
lsp_types::RegistrationParams { registrations: vec![registration] },
203-
|_, _| (),
204-
);
185+
if self.config.did_change_watched_files_dynamic_registration() {
186+
let registration_options = lsp_types::DidChangeWatchedFilesRegistrationOptions {
187+
watchers: workspaces
188+
.iter()
189+
.flat_map(ProjectWorkspace::to_roots)
190+
.filter(|it| it.is_member)
191+
.flat_map(|root| {
192+
root.include.into_iter().map(|it| format!("{}/**/*.rs", it.display()))
193+
})
194+
.map(|glob_pattern| lsp_types::FileSystemWatcher {
195+
glob_pattern,
196+
kind: None,
197+
})
198+
.collect(),
199+
};
200+
let registration = lsp_types::Registration {
201+
id: "workspace/didChangeWatchedFiles".to_string(),
202+
method: "workspace/didChangeWatchedFiles".to_string(),
203+
register_options: Some(serde_json::to_value(registration_options).unwrap()),
204+
};
205+
self.send_request::<lsp_types::request::RegisterCapability>(
206+
lsp_types::RegistrationParams { registrations: vec![registration] },
207+
|_, _| (),
208+
);
209+
}
205210
}
206211

207212
let mut change = Change::new();

0 commit comments

Comments
 (0)