Skip to content

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented May 20, 2025

Summary

PR adding support for it in the VS Code extension: astral-sh/ty-vscode#36

This PR adds support for python.ty.disableLanguageServices to the ty language server by accepting this as server setting.

This has the same issue as astral-sh/ty#282 in that it only works when configured globally. Fixing that requires support for multiple workspaces in the server itself.

I also went ahead and did a similar refactor as the Ruff server to use "Options" and "Settings" to keep the code consistent although the combine functionality doesn't exists yet because workspace settings isn't supported in the ty server.

Test Plan

Refer to astral-sh/ty-vscode#36 for the test demo.

@dhruvmanila dhruvmanila added server Related to the LSP server ty Multi-file analysis & type inference labels May 20, 2025
Copy link
Contributor

github-actions bot commented May 20, 2025

mypy_primer results

No ecosystem changes detected ✅

@dhruvmanila dhruvmanila force-pushed the dhruv/disable-language-services branch from c0db4f2 to f335a4d Compare May 20, 2025 19:48
Comment on lines 14 to 27
#[derive(Debug, Deserialize, Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
#[serde(rename_all = "camelCase")]
struct Python {
ty: Option<Ty>,
}

#[derive(Debug, Deserialize, Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
#[serde(rename_all = "camelCase")]
struct Ty {
disable_language_services: Option<bool>,
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative here is to avoid introducing "Python" struct and directly use disableLanguageServices in ClientSettings like:

struct ClientSettings {
	disable_language_services: Option<bool>
}

The disadvantage for this is that in VS Code the setting would be python.ty.disableLanguageServices while in other editors it would just be disableLanguageServices.

This PR keeps it consistent across editors and this is what we want as well because once workspace/didChangeConfiguration notification support is added, we'd directly query the client (editor) to get this setting value instead of mirroring the structure here.

Comment on lines +31 to +36
if snapshot.client_settings().is_language_services_disabled() {
return Ok(None);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried doing this centrally in background_request_task but that didn't work:

diff --git a/crates/ty_server/src/server/api.rs b/crates/ty_server/src/server/api.rs
index a9dd9da1d0..9899184fd4 100644
--- a/crates/ty_server/src/server/api.rs
+++ b/crates/ty_server/src/server/api.rs
@@ -143,7 +143,18 @@ fn background_request_task<'a, R: traits::BackgroundDocumentRequestHandler>(
 
         Box::new(move |notifier, responder| {
             let _span = tracing::trace_span!("request", %id, method = R::METHOD).entered();
-            let result = R::run_with_snapshot(&db, snapshot, notifier, params);
+            let result = if snapshot.client_settings().is_language_services_disabled()
+                && matches!(
+                    R::METHOD,
+                    request::GotoTypeDefinitionRequestHandler::METHOD
+                        | request::CompletionRequestHandler::METHOD
+                        | request::InlayHintRequestHandler::METHOD
+                        | request::HoverRequestHandler::METHOD
+                ) {
+                // Return an empty response here ...
+            } else {
+                R::run_with_snapshot(&db, snapshot, notifier, params)
+            };
             respond::<R>(id, result, &responder);
         })
     }))

But, then we'd need to somehow be able to get an "empty" response from those handlers which could be None or something else.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer having this in the specific handlers. It's less surprising, and someone copying an existing handler (which I always do as a starting point) is then also more likely to think about checking this option.

I guess, it's somewhat wasteful to still have the client send us requests but I suspect that unregistering the providers is more complicated and not supported by all clients?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, it's somewhat wasteful to still have the client send us requests but I suspect that unregistering the providers is more complicated and not supported by all clients?

I think unregistering only works for capabilities that support dynamic registration and I'm not sure if all language capabilities supports that. And, this would also require that we go through the dynamic registration path (via registering each capabilities dynamically) to un-register the capability later as it requires the id that the server used to perform the registration.

@dhruvmanila dhruvmanila force-pushed the dhruv/disable-language-services branch from f335a4d to f041dc0 Compare June 16, 2025 09:30
@dhruvmanila dhruvmanila changed the base branch from main to ag/stabilize-completions June 16, 2025 09:31
Copy link

codspeed-hq bot commented Jun 16, 2025

CodSpeed Performance Report

Merging #18230 will not alter performance

Comparing dhruv/disable-language-services (e6cca67) with main (c22f809)

Summary

✅ 34 untouched benchmarks

@dhruvmanila
Copy link
Member Author

This PR is based on top of #18650 because it's just easier to not have to deal with the (now removed) experimental section.

@dhruvmanila dhruvmanila marked this pull request as ready for review June 16, 2025 10:39
Base automatically changed from ag/stabilize-completions to main June 16, 2025 11:44
@AlexWaygood AlexWaygood removed their request for review June 16, 2025 22:35
@carljm carljm removed their request for review June 17, 2025 01:53
Comment on lines +31 to +36
if snapshot.client_settings().is_language_services_disabled() {
return Ok(None);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer having this in the specific handlers. It's less surprising, and someone copying an existing handler (which I always do as a starting point) is then also more likely to think about checking this option.

I guess, it's somewhat wasteful to still have the client send us requests but I suspect that unregistering the providers is more complicated and not supported by all clients?

@sharkdp sharkdp removed their request for review June 17, 2025 06:38
@dhruvmanila dhruvmanila force-pushed the dhruv/disable-language-services branch from f041dc0 to e6cca67 Compare June 17, 2025 06:56
@dhruvmanila dhruvmanila reopened this Jun 17, 2025
@dhruvmanila dhruvmanila merged commit 390918e into main Jun 17, 2025
35 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/disable-language-services branch June 17, 2025 08:20
dhruvmanila added a commit to astral-sh/ty-vscode that referenced this pull request Jun 17, 2025
## Summary

PR adding support for it in the language server:
astral-sh/ruff#18230

This PR adds support for `python.ty.disableLanguageServices` in the
extension to extract the value and pass it as server setting.

Closes: #20 

## Test Plan

The video starts by the commented out
`python.ty.disableLanguageServices` and you can see that there are inlay
hints, hover support, diagnostics, etc.

The hover content for `x` is `Literal[1]`.

Once it's set to true, the server restarts and inlay hints are gone,
hover content is gone but the diagnostics still remain as expected.


https://github.com/user-attachments/assets/9280e7b0-bf89-4d99-bfa9-d10a6d3368f7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
server Related to the LSP server ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants