fix(lsp): don't provide organizeImports action when client provides it (#31530)
Some checks are pending
ci / test debug windows-x86_64 (push) Blocked by required conditions
ci / test release windows-x86_64 (push) Blocked by required conditions
ci / pre-build (push) Waiting to run
ci / test debug linux-aarch64 (push) Blocked by required conditions
ci / test release linux-aarch64 (push) Blocked by required conditions
ci / test debug macos-aarch64 (push) Blocked by required conditions
ci / test release macos-aarch64 (push) Blocked by required conditions
ci / bench release linux-x86_64 (push) Blocked by required conditions
ci / test debug linux-x86_64 (push) Blocked by required conditions
ci / test release linux-x86_64 (push) Blocked by required conditions
ci / test debug macos-x86_64 (push) Blocked by required conditions
ci / test release macos-x86_64 (push) Blocked by required conditions
ci / lint debug linux-x86_64 (push) Blocked by required conditions
ci / lint debug macos-x86_64 (push) Blocked by required conditions
ci / lint debug windows-x86_64 (push) Blocked by required conditions
ci / build libs (push) Blocked by required conditions
ci / publish canary (push) Blocked by required conditions

This commit is contained in:
Nayeem Rahman 2025-12-08 10:46:22 +00:00 committed by GitHub
parent 2353b9ba6a
commit 642f2a46a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 65 additions and 2 deletions

View file

@ -1208,6 +1208,14 @@ impl Config {
})()
.unwrap_or(false)
}
pub fn client_provided_organize_imports_capable(&self) -> bool {
(|| {
let experimental = self.client_capabilities.experimental.as_ref()?;
experimental.get("clientProvidedOrganizeImports")?.as_bool()
})()
.unwrap_or(false)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]

View file

@ -2323,8 +2323,9 @@ impl Inner {
);
// Organize imports
if kinds.is_empty()
|| kinds.contains(&CodeActionKind::SOURCE_ORGANIZE_IMPORTS)
if !self.config.client_provided_organize_imports_capable()
&& (kinds.is_empty()
|| kinds.contains(&CodeActionKind::SOURCE_ORGANIZE_IMPORTS))
{
let document_has_errors = params.context.diagnostics.iter().any(|d| {
// Assume diagnostics without a severity are errors

View file

@ -8033,6 +8033,47 @@ console.log(other, submodule);
client.shutdown();
}
#[test]
#[timeout(300_000)]
fn lsp_code_actions_organize_imports_client_provided_capability() {
let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir();
let file = temp_dir.source_file(
"file.ts",
r#"import { z, y } from "./z.ts";
import { c, a, b } from "./b.ts";
import { d } from "./a.ts";
import unused from "./c.ts";
console.log(b, a, c, d, y, z);
"#,
);
let uri = file.uri();
let mut client = context.new_lsp_command().build();
client.initialize(|builder| {
builder.enable_client_provided_organize_imports();
});
client.did_open_file(&file);
// Request "Organize Imports" action
let res = client.write_request(
"textDocument/codeAction",
json!({
"textDocument": { "uri": uri },
"range": {
"start": { "line": 0, "character": 0 },
"end": { "line": 0, "character": 0 }
},
"context": {
"diagnostics": [],
"only": ["source.organizeImports"]
}
}),
);
assert_eq!(res, json!(null));
client.shutdown();
}
#[test]
#[timeout(300_000)]
fn lsp_code_actions_refactor_no_disabled_support() {

View file

@ -321,6 +321,19 @@ impl InitializeParamsBuilder {
self
}
pub fn enable_client_provided_organize_imports(&mut self) -> &mut Self {
let obj = self
.params
.capabilities
.experimental
.as_mut()
.unwrap()
.as_object_mut()
.unwrap();
obj.insert("clientProvidedOrganizeImports".to_string(), true.into());
self
}
pub fn set_cache(&mut self, value: impl AsRef<str>) -> &mut Self {
let options = self.initialization_options_mut();
options.insert("cache".to_string(), value.as_ref().to_string().into());