Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing move-form clojure-lsp refactoring #2016

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/site/refactoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 29 additions & 3 deletions src/lsp/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ type ClojureLspCommand = {
command: string;
extraParamFn?: () => Thenable<string>;
category?: string;
requireLocalFile?: boolean;
};

function makePromptForInput(placeHolder: string) {
Expand All @@ -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',
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
}
});
Expand Down