-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Register formatting capabilities dynamically to exclude TOML files #27332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+214
−15
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ use crossbeam::select; | |
| use lsp_server::Message; | ||
| use lsp_types::{ | ||
| self as types, DidChangeWatchedFilesRegistrationOptions, FileSystemWatcher, Notification as _, | ||
| Request as _, | ||
| }; | ||
|
|
||
| use crate::{ | ||
|
|
@@ -135,20 +136,27 @@ impl Server { | |
| } | ||
|
|
||
| fn initialize(&mut self, client: &Client) { | ||
| let dynamic_registration = self | ||
| let supports_watched_files = self | ||
| .client_capabilities | ||
| .workspace | ||
| .as_ref() | ||
| .and_then(|workspace| workspace.did_change_watched_files) | ||
| .and_then(|watched_files| watched_files.dynamic_registration) | ||
| .unwrap_or_default(); | ||
| let supports_formatting = Self::supports_dynamic_formatting(&self.client_capabilities); | ||
| let supports_range_formatting = | ||
| Self::supports_dynamic_range_formatting(&self.client_capabilities); | ||
| let dynamic_registration = | ||
| supports_watched_files || supports_formatting || supports_range_formatting; | ||
|
|
||
| if dynamic_registration { | ||
| // Register all dynamic capabilities here | ||
| let mut registrations = vec![]; | ||
|
|
||
| // `workspace/didChangeWatchedFiles` | ||
| // (this registers the configuration file watcher) | ||
| let params = lsp_types::RegistrationParams { | ||
| registrations: vec![lsp_types::Registration { | ||
| if supports_watched_files { | ||
| // `workspace/didChangeWatchedFiles` | ||
| // (this registers the configuration file watcher) | ||
| registrations.push(lsp_types::Registration { | ||
| id: "ruff-server-watch".into(), | ||
| method: "workspace/didChangeWatchedFiles".into(), | ||
| register_options: Some( | ||
|
|
@@ -176,11 +184,81 @@ impl Server { | |
| }) | ||
| .unwrap(), | ||
| ), | ||
| }], | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| if supports_formatting || supports_range_formatting { | ||
| let mut document_selector: types::DocumentSelector = ["python", "markdown"] | ||
| .into_iter() | ||
| .flat_map(|language| { | ||
| ["file", "untitled"].into_iter().map(move |scheme| { | ||
| types::TextDocumentFilter::Language(types::TextDocumentFilterLanguage { | ||
| language: language.to_string(), | ||
| scheme: Some(scheme.to_string()), | ||
| pattern: None, | ||
| }) | ||
| .into() | ||
| }) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit. The |
||
| }) | ||
| .collect(); | ||
|
|
||
| document_selector.push( | ||
| types::TextDocumentFilter::Language(types::TextDocumentFilterLanguage { | ||
| language: "python".into(), | ||
| scheme: Some("vscode-notebook".into()), | ||
| pattern: None, | ||
| }) | ||
| .into(), | ||
| ); | ||
| document_selector.push( | ||
| types::NotebookCellTextDocumentFilter { | ||
| notebook: "*".into(), | ||
| language: Some("python".into()), | ||
| } | ||
| .into(), | ||
| ); | ||
|
|
||
| let text_document_registration_options = types::TextDocumentRegistrationOptions { | ||
| document_selector: Some(document_selector), | ||
| }; | ||
|
|
||
| if supports_formatting { | ||
| registrations.push(types::Registration { | ||
| id: "ruff-server-format".into(), | ||
| method: types::DocumentFormattingRequest::METHOD.to_string(), | ||
| register_options: Some( | ||
| serde_json::to_value(types::DocumentFormattingRegistrationOptions { | ||
| text_document_registration_options: | ||
| text_document_registration_options.clone(), | ||
| document_formatting_options: | ||
| types::DocumentFormattingOptions::default(), | ||
| }) | ||
| .unwrap(), | ||
| ), | ||
| }); | ||
| } | ||
|
|
||
| if supports_range_formatting { | ||
| registrations.push(types::Registration { | ||
| id: "ruff-server-format-range".into(), | ||
| method: types::DocumentRangeFormattingRequest::METHOD.to_string(), | ||
| register_options: Some( | ||
| serde_json::to_value( | ||
| types::DocumentRangeFormattingRegistrationOptions { | ||
| text_document_registration_options, | ||
| document_range_formatting_options: | ||
| types::DocumentRangeFormattingOptions::default(), | ||
| }, | ||
| ) | ||
| .unwrap(), | ||
| ), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| let params = types::RegistrationParams { registrations }; | ||
| let response_handler = |_: &Client, ()| { | ||
| tracing::info!("Configuration file watcher successfully registered"); | ||
| tracing::info!("Dynamic capabilities successfully registered"); | ||
| }; | ||
|
|
||
| if let Err(err) = client.send_request::<lsp_types::RegistrationRequest>( | ||
|
|
@@ -189,7 +267,7 @@ impl Server { | |
| response_handler, | ||
| ) { | ||
| tracing::error!( | ||
| "An error occurred when trying to register the configuration file watcher: {err}" | ||
| "An error occurred when trying to register dynamic capabilities: {err}" | ||
| ); | ||
| } | ||
| } else { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| use anyhow::Result; | ||
| use lsp_types::Request as _; | ||
| use lsp_types::{DocumentFormattingRequest, DocumentRangeFormattingRequest, RegistrationRequest}; | ||
|
|
||
| use crate::TestServerBuilder; | ||
|
|
||
| #[test] | ||
| fn statically_registers_formatting_when_dynamic_registration_is_unsupported() -> Result<()> { | ||
| let server = TestServerBuilder::new()?.build(); | ||
| let capabilities = &server | ||
| .initialization_result() | ||
| .expect("Server should return initialization capabilities") | ||
| .capabilities; | ||
|
|
||
| assert_eq!(capabilities.document_formatting_provider, Some(true.into())); | ||
| assert_eq!( | ||
| capabilities.document_range_formatting_provider, | ||
| Some(true.into()) | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn dynamically_registers_formatting_and_range_formatting_for_python_and_markdown() -> Result<()> { | ||
| let mut server = TestServerBuilder::new()? | ||
| .enable_formatting_dynamic_registration(true) | ||
| .enable_range_formatting_dynamic_registration(true) | ||
| .build(); | ||
| let capabilities = &server | ||
| .initialization_result() | ||
| .expect("Server should return initialization capabilities") | ||
| .capabilities; | ||
|
|
||
| assert_eq!(capabilities.document_formatting_provider, None); | ||
| assert_eq!(capabilities.document_range_formatting_provider, None); | ||
|
|
||
| let (_, params) = server.await_request::<RegistrationRequest>(); | ||
| let [formatting, range_formatting] = params.registrations.as_slice() else { | ||
| panic!("Expected both dynamic formatting registrations"); | ||
| }; | ||
|
|
||
| assert_eq!( | ||
| formatting.method, | ||
| DocumentFormattingRequest::METHOD.as_str() | ||
| ); | ||
| assert_eq!( | ||
| range_formatting.method, | ||
| DocumentRangeFormattingRequest::METHOD.as_str() | ||
| ); | ||
| assert_eq!( | ||
| formatting.register_options, | ||
| Some(serde_json::json!({ | ||
| "documentSelector": [ | ||
| { "language": "python", "scheme": "file" }, | ||
| { "language": "python", "scheme": "untitled" }, | ||
| { "language": "markdown", "scheme": "file" }, | ||
| { "language": "markdown", "scheme": "untitled" }, | ||
| { "language": "python", "scheme": "vscode-notebook" }, | ||
| { "notebook": "*", "language": "python" } | ||
| ] | ||
| })) | ||
| ); | ||
| assert_eq!( | ||
| range_formatting.register_options, | ||
| formatting.register_options | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to set the
schema? what about clients with custom schemas?I think the smallest change here is to omit the schema, which matches our existing behavior
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, yeah this also helped to get rid of all the
into_iterstuff.