Skip to content

Commit 642f2a4

Browse files
authored
fix(lsp): don't provide organizeImports action when client provides it (#31530)
1 parent 2353b9b commit 642f2a4

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

cli/lsp/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,14 @@ impl Config {
12081208
})()
12091209
.unwrap_or(false)
12101210
}
1211+
1212+
pub fn client_provided_organize_imports_capable(&self) -> bool {
1213+
(|| {
1214+
let experimental = self.client_capabilities.experimental.as_ref()?;
1215+
experimental.get("clientProvidedOrganizeImports")?.as_bool()
1216+
})()
1217+
.unwrap_or(false)
1218+
}
12111219
}
12121220

12131221
#[derive(Debug, Clone, Copy, PartialEq, Eq)]

cli/lsp/language_server.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,8 +2323,9 @@ impl Inner {
23232323
);
23242324

23252325
// Organize imports
2326-
if kinds.is_empty()
2327-
|| kinds.contains(&CodeActionKind::SOURCE_ORGANIZE_IMPORTS)
2326+
if !self.config.client_provided_organize_imports_capable()
2327+
&& (kinds.is_empty()
2328+
|| kinds.contains(&CodeActionKind::SOURCE_ORGANIZE_IMPORTS))
23282329
{
23292330
let document_has_errors = params.context.diagnostics.iter().any(|d| {
23302331
// Assume diagnostics without a severity are errors

tests/integration/lsp_tests.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8033,6 +8033,47 @@ console.log(other, submodule);
80338033
client.shutdown();
80348034
}
80358035

8036+
#[test]
8037+
#[timeout(300_000)]
8038+
fn lsp_code_actions_organize_imports_client_provided_capability() {
8039+
let context = TestContextBuilder::new().use_temp_cwd().build();
8040+
let temp_dir = context.temp_dir();
8041+
let file = temp_dir.source_file(
8042+
"file.ts",
8043+
r#"import { z, y } from "./z.ts";
8044+
import { c, a, b } from "./b.ts";
8045+
import { d } from "./a.ts";
8046+
import unused from "./c.ts";
8047+
8048+
console.log(b, a, c, d, y, z);
8049+
"#,
8050+
);
8051+
let uri = file.uri();
8052+
let mut client = context.new_lsp_command().build();
8053+
client.initialize(|builder| {
8054+
builder.enable_client_provided_organize_imports();
8055+
});
8056+
client.did_open_file(&file);
8057+
8058+
// Request "Organize Imports" action
8059+
let res = client.write_request(
8060+
"textDocument/codeAction",
8061+
json!({
8062+
"textDocument": { "uri": uri },
8063+
"range": {
8064+
"start": { "line": 0, "character": 0 },
8065+
"end": { "line": 0, "character": 0 }
8066+
},
8067+
"context": {
8068+
"diagnostics": [],
8069+
"only": ["source.organizeImports"]
8070+
}
8071+
}),
8072+
);
8073+
assert_eq!(res, json!(null));
8074+
client.shutdown();
8075+
}
8076+
80368077
#[test]
80378078
#[timeout(300_000)]
80388079
fn lsp_code_actions_refactor_no_disabled_support() {

tests/util/server/src/lsp.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,19 @@ impl InitializeParamsBuilder {
321321
self
322322
}
323323

324+
pub fn enable_client_provided_organize_imports(&mut self) -> &mut Self {
325+
let obj = self
326+
.params
327+
.capabilities
328+
.experimental
329+
.as_mut()
330+
.unwrap()
331+
.as_object_mut()
332+
.unwrap();
333+
obj.insert("clientProvidedOrganizeImports".to_string(), true.into());
334+
self
335+
}
336+
324337
pub fn set_cache(&mut self, value: impl AsRef<str>) -> &mut Self {
325338
let options = self.initialization_options_mut();
326339
options.insert("cache".to_string(), value.as_ref().to_string().into());

0 commit comments

Comments
 (0)