Skip to content

Commit 5c170d8

Browse files
authored
Merge pull request #3205 from blueskyonmars/enable-lsp-refactoring-actions-#3204
Enable lsp refactoring actions * Fixes #3204
2 parents de34b05 + 7bf85cc commit 5c170d8

2 files changed

Lines changed: 22 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Changes to Calva.
44

55
## [Unreleased]
6+
- enable new clojure-lsp code actions: inline function, if<->cond, extract selection to function, move to :let
67

78
## [2.0.583] - 2026-05-04
89

src/lsp/commands/lsp-commands.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ type BaseLspCommand = {
1212
category?: string;
1313
};
1414

15-
// If afterCommandFn is defined, then defaultName must be too
15+
const namePositionDefault = 3;
16+
// If afterCommandFn is defined, then defaultName must be too, namePosition defaults to position 3
1617
type ClojureLspCommand = BaseLspCommand &
1718
(
1819
| {
@@ -21,9 +22,11 @@ type ClojureLspCommand = BaseLspCommand &
2122
commandResponse: Record<string, unknown>
2223
) => Thenable<any>;
2324
defaultName: string;
25+
namePosition?: number;
2426
}
2527
| {
2628
defaultName?: string;
29+
namePosition?: number;
2730
afterCommandFn?: never;
2831
}
2932
);
@@ -38,7 +41,7 @@ async function renameAfterRefactor(
3841
commandArgs: Array<string | number>,
3942
commandResponse: vscode_lsp.WorkspaceEdit
4043
) {
41-
if (commandArgs[3] !== this.defaultName) {
44+
if (commandArgs[this.namePosition ?? namePositionDefault] !== this.defaultName) {
4245
return;
4346
}
4447

@@ -106,6 +109,8 @@ const clojureLspCommands: ClojureLspCommand[] = [
106109
{ command: 'drag-param-backward', category: 'clojureLsp' },
107110
{ command: 'drag-param-forward', category: 'clojureLsp' },
108111
{ command: 'expand-let', category: 'clojureLsp.refactor' },
112+
{ command: 'if->cond-refactor', category: 'clojureLsp.refactor' },
113+
{ command: 'cond->if-refactor', category: 'clojureLsp.refactor' },
109114
{ command: 'get-in-all', category: 'clojureLsp.refactor' },
110115
{ command: 'get-in-less', category: 'clojureLsp.refactor' },
111116
{ command: 'get-in-more', category: 'clojureLsp.refactor' },
@@ -114,6 +119,7 @@ const clojureLspCommands: ClojureLspCommand[] = [
114119
{ command: 'move-coll-entry-down', category: 'clojureLsp.refactor' },
115120
{ command: 'move-coll-entry-up', category: 'clojureLsp.refactor' },
116121
{ command: 'move-form', category: 'clojureLsp.refactor' },
122+
{ command: 'move-to-for-let', category: 'clojureLsp.refactor' },
117123
{ command: 'replace-refer-all-with-alias', category: 'clojureLsp.refactor' },
118124
{ command: 'replace-refer-all-with-refer', category: 'clojureLsp.refactor' },
119125
{ command: 'resolve-macro-as', category: 'clojureLsp.refactor' },
@@ -146,6 +152,17 @@ const clojureLspCommands: ClojureLspCommand[] = [
146152
defaultName: 'new-fn',
147153
category: 'clojureLsp.refactor',
148154
},
155+
{
156+
command: 'inline-function',
157+
category: 'clojureLsp.refactor',
158+
},
159+
{
160+
command: 'extract-function-2',
161+
afterCommandFn: renameAfterRefactor,
162+
defaultName: 'new-fn',
163+
namePosition: 5,
164+
category: 'clojureLsp.refactor',
165+
},
149166
{
150167
command: 'extract-to-def',
151168
afterCommandFn: renameAfterRefactor,
@@ -245,18 +262,7 @@ function registerUserspaceLspCommand(
245262
// default rather than letting clojure-lsp pick a name so that when we trigger a rename action
246263
// after the command completes, we know what to rename.
247264
if (command.defaultName) {
248-
params[3] = command.defaultName;
249-
}
250-
251-
// clojure-lsp's `extract-function` always destructures selection-end coords
252-
// from the args so a 4-arg call throws IndexOutOfBoundsException.
253-
// Append the selection end so the call works whether or not a region is
254-
// selected.
255-
// https://github.com/clojure-lsp/clojure-lsp/issues/2118
256-
if (command.command === 'extract-function') {
257-
const selection = calva_utils.getActiveTextEditor().selections[0];
258-
params[4] = selection.end.line;
259-
params[5] = selection.end.character;
265+
params[command.namePosition ?? namePositionDefault] = command.defaultName;
260266
}
261267

262268
sendCommandRequest(clients, command.command, params);
@@ -273,7 +279,7 @@ function registerInternalLspCommand(
273279
// default name. Replace that name with our own default so we know what to look for when we
274280
// trigger a rename action on it afterwards.
275281
if (command.defaultName) {
276-
args[3] = command.defaultName;
282+
args[command.namePosition ?? namePositionDefault] = command.defaultName;
277283
}
278284

279285
sendCommandRequest(clients, command.command, args);

0 commit comments

Comments
 (0)