-
Notifications
You must be signed in to change notification settings - Fork 2k
[ty] Implement workspace/didChangeConfiguration
#24712
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
Open
pierrem964
wants to merge
8
commits into
astral-sh:main
Choose a base branch
from
pierrem964:pgm/workspace-did-change-configuration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
23d5834
Implement did_change_configuration
pierrem964 7c668c0
remove capability for now
pierrem964 54a9482
Fix clippy issues, register workspace/didChangeConfiguration as a cap…
pierrem964 d59bcac
Make function name more clear
pierrem964 6374937
Add more logs
pierrem964 a9ffadb
Fix typos
pierrem964 81fbc69
Do not reinitialize the project database, address review comments
pierrem964 d99f888
Merge branch 'astral-sh:main' into pgm/workspace-did-change-configura…
pierrem964 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
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
91 changes: 91 additions & 0 deletions
91
crates/ty_server/src/server/api/notifications/did_change_configuration.rs
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,91 @@ | ||
| use crate::server::Action; | ||
| use crate::server::Result; | ||
| use crate::server::api::traits::{NotificationHandler, SyncNotificationHandler}; | ||
| use crate::session::client::Client; | ||
| use crate::session::{ClientOptions, Session}; | ||
| use lsp_types::notification as notif; | ||
| use lsp_types::{self as types, ConfigurationParams, Url}; | ||
|
|
||
| pub(crate) struct DidChangeConfiguration; | ||
|
|
||
| impl NotificationHandler for DidChangeConfiguration { | ||
| type NotificationType = notif::DidChangeConfiguration; | ||
| } | ||
|
|
||
| impl SyncNotificationHandler for DidChangeConfiguration { | ||
| fn run( | ||
| session: &mut Session, | ||
| client: &Client, | ||
| params: types::DidChangeConfigurationParams, | ||
| ) -> Result<()> { | ||
| tracing::debug!("Received workspace/didChangeConfiguration"); | ||
| // workspace/didChangeConfiguration is a pull based, meaning the request should be empty, and | ||
| // the server needs to pull the workspace configuration by requesting it from the | ||
| // client. | ||
| // See https://github.com/microsoft/vscode-languageserver-node/issues/380#issuecomment-414691493 | ||
| // See https://github.com/microsoft/language-server-protocol/issues/676 | ||
| assert!(params.settings.is_null()); | ||
|
|
||
| let workspace_urls: Vec<Url> = session | ||
| .workspaces() | ||
| .into_iter() | ||
| .map(|(_, workspace)| workspace.url().clone()) | ||
| .collect(); | ||
|
|
||
| let items: Vec<types::ConfigurationItem> = workspace_urls | ||
| .iter() | ||
| .map(|workspace| types::ConfigurationItem { | ||
| scope_uri: Some(workspace.clone()), | ||
| section: Some("ty".to_string()), | ||
| }) | ||
| .collect(); | ||
|
|
||
| tracing::debug!("Sending workspace/configuration requests to client"); | ||
| client.send_request::<lsp_types::request::WorkspaceConfiguration>( | ||
| session, | ||
| ConfigurationParams { items }, | ||
| |client, result: Vec<serde_json::value::Value>| { | ||
| // This shouldn't fail because, as per the spec, the client needs to provide a | ||
| // `null` value even if it cannot provide a configuration for a workspace. | ||
| assert_eq!( | ||
| result.len(), | ||
| workspace_urls.len(), | ||
| "Mismatch in number of workspace URLs ({}) and configuration results ({})", | ||
| workspace_urls.len(), | ||
| result.len() | ||
| ); | ||
|
|
||
| let workspaces_with_options: Vec<(Url, ClientOptions)> = workspace_urls | ||
| .into_iter() | ||
| .zip(result) | ||
| .map(|(url, value)| { | ||
| if value.is_null() { | ||
| tracing::debug!( | ||
| "No workspace options provided for {url}, using default options" | ||
| ); | ||
| return (url, ClientOptions::default()); | ||
| } | ||
| let options: ClientOptions = | ||
| serde_json::from_value(value).unwrap_or_else(|err| { | ||
| tracing::error!( | ||
| "Failed to deserialize workspace options for {url}: {err}. \ | ||
| Using default options" | ||
| ); | ||
| ClientOptions::default() | ||
| }); | ||
| (url, options) | ||
| }) | ||
| .collect(); | ||
|
|
||
| tracing::debug!( | ||
| "Received new configuration options {:?}", | ||
| workspaces_with_options, | ||
| ); | ||
|
|
||
| client.queue_action(Action::UpdateWorkspaceConfigs(workspaces_with_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
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
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
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.
Cleaning all workspaces just because e.g. the selected interpreter is a bit rough :)
Are there any settings that change the known workspaces? Is there a way we could only update the settings or is this much more complicated?
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.
Yea, initially I had considered some kind of diff checking to see what specific workspace settings had changed, but it seemed quite complicated between the pulled
ClientOptionsand the existingWorkspaceSettingsin terms of implementation. If we could find a good solution for that we could:A. Only update workspaces that we knew had changed
B. Only update the specific settings that had changed
Potentially this is done through manually comparing each setting on the two structs (and maybe returning or modifying the existing
WorkspaceSettings)? Please let me know if you have any ideas 😃 .Uh oh!
There was an error while loading. Please reload this page.
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.
We could also just replace
Arc<WorkspaceSettings>with the pulled settings as well, potentially we don't need to reregister and reinitialize unless the configuration file has changed? (This is the only thing I can see relying onsettingsininitialize_workspace_folder- https://github.com/pharmac1st/ruff/blob/a9ffadb41636a472c5a8a52aedca350ad64a8ac1/crates/ty_server/src/session.rs#L592).We could also move handling updating global settings in
update_workspace_folders, and not rely at all oninitialize_workspace_folders.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.
I'd have to take a closer look, but just overriding the
WorkspaceSettingsseems fine; we don't need to diff them. But we also need to re-resolve the settings passed to theProjectDatabase. It's okay if we replace the options if they don't compare equal, but that we should avoid is creating a newProjectDatabasebecause that clears all cachesThere 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.
Updated - I saw there was
reload()which sort of did want we want, I split this function out into a more specific one that updates the settings.I've also assumed that we want to update the global settings as well, and that should be set from the last workspace received (similar to how it's done in
initialize_workspace_folders).