From 7628aff5cb55d809cba523d44c27dd869dc893e3 Mon Sep 17 00:00:00 2001 From: Stefan van den Oord Date: Tue, 10 Jan 2023 07:58:26 +0100 Subject: [PATCH 1/3] Add move-form refactoring --- package.json | 6 ++++++ src/lsp/main.ts | 32 +++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index c377fe786..551d1301a 100644 --- a/package.json +++ b/package.json @@ -1770,6 +1770,12 @@ "category": "clojure-lsp Refactor", "enablement": "editorLangId == clojure && clojureLsp:active" }, + { + "command": "clojureLsp.refactor.moveForm", + "title": "Move form", + "category": "clojure-lsp Refactor", + "enablement": "editorLangId == clojure && clojureLsp:active" + }, { "command": "clojureLsp.refactor.threadFirst", "title": "Thread First", diff --git a/src/lsp/main.ts b/src/lsp/main.ts index 56c499ecd..bf6176001 100644 --- a/src/lsp/main.ts +++ b/src/lsp/main.ts @@ -185,6 +185,7 @@ type ClojureLspCommand = { command: string; extraParamFn?: () => Thenable; category?: string; + requireLocalFile?: boolean; }; function makePromptForInput(placeHolder: string) { @@ -197,6 +198,20 @@ function makePromptForInput(placeHolder: string) { }; } +function makeQuickPickForInput() { + return async () => { + const uris = await vscode.window.showOpenDialog({ + canSelectFolders: false, + canSelectFiles: true, + canSelectMany: false, + openLabel: 'Select destination', + title: 'Select destination', + filters: { 'Clojure(Script)': ['clj', 'cljc', 'cljs', 'cljx'] }, + }); + return uris?.length > 0 ? uris[0].path : undefined; + }; +} + const clojureLspCommands: ClojureLspCommand[] = [ { command: 'clean-ns', @@ -255,6 +270,11 @@ const clojureLspCommands: ClojureLspCommand[] = [ command: 'extract-function', extraParamFn: makePromptForInput('Function name'), }, + { + command: 'move-form', + extraParamFn: makeQuickPickForInput(), + requireLocalFile: true, + }, ]; function sendCommandRequest(command: string, args: (number | string)[]): void { @@ -284,9 +304,15 @@ function registerLspCommand(command: ClojureLspCommand): vscode.Disposable { const column = editor.selection.start.character; const docUri = `${document.uri.scheme}://${document.uri.path}`; const params = [docUri, line, column]; - const extraParam = command.extraParamFn ? await command.extraParamFn() : undefined; - if (!command.extraParamFn || (command.extraParamFn && extraParam)) { - sendCommandRequest(command.command, extraParam ? [...params, extraParam] : params); + if (command.requireLocalFile === true && document.uri.scheme !== 'file') { + await vscode.window.showErrorMessage('This function only works on local files'); + } else { + const extraParam = command.extraParamFn ? await command.extraParamFn() : undefined; + if (command.command === 'execute-lsp-command' && command.extraParamFn && extraParam) { + sendCommandRequest(extraParam, params); + } else if (!command.extraParamFn || (command.extraParamFn && extraParam)) { + sendCommandRequest(command.command, extraParam ? [...params, extraParam] : params); + } } } }); From 99d5acd74caf242a8b6596f686b8e5c239358115 Mon Sep 17 00:00:00 2001 From: Stefan van den Oord Date: Wed, 25 Jan 2023 10:47:23 +0100 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49cecc5ac..e72318a1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Changes to Calva. ## [Unreleased] +- [The move-form clojure-lsp refactoring is missing in Calva](https://github.com/BetterThanTomorrow/calva/issues/2015) + ## [2.0.326] - 2023-01-24 - Fix: [`afterCLJReplJackInCode` fails if no editor is open](https://github.com/BetterThanTomorrow/calva/issues/2025) From 71bf46c4d11e206b455680a8c36cbfd213a62350 Mon Sep 17 00:00:00 2001 From: Stefan van den Oord Date: Wed, 25 Jan 2023 10:55:52 +0100 Subject: [PATCH 3/3] Update docs. Fixes #2015 --- docs/site/refactoring.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/site/refactoring.md b/docs/site/refactoring.md index 954d54691..6713e235c 100644 --- a/docs/site/refactoring.md +++ b/docs/site/refactoring.md @@ -31,6 +31,7 @@ Thread Last | `clojureLsp.refactor.threadLast` | ![](images/refactoring/threadLa Thread Last All | `clojureLsp.refactor.threadLastAll` | ![](images/refactoring/threadLastAll.gif) Unwind All | `clojureLsp.refactor.unwindAll` | ![](images/refactoring/unwindAll.gif) Unwind Thread | `clojureLsp.refactor.unwindThread` | ![](images/refactoring/unwindThread.gif) +Move Form | `clojureLsp.refactor.moveForm` | Opens a file picker to select the file to move a given form to. After selecting the destination, the form is moved and references to the form are updated. !!! Note "Formatting" The way that some of the refactorings are applied to the document, makes it difficult for Calva to format the results. So, sometimes you'll need to navigate the cursor to the enclosing form and hit `tab` to tidy up the formatting after a refactoring. See also [Formatting](formatting.md).