Skip to content

Commit

Permalink
Add move-form refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
svdo committed Jan 25, 2023
1 parent 4e05952 commit 08a45ec
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
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
31 changes: 28 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,19 @@ 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',
});
return uris?.length > 0 ? uris[0].path : undefined;
};
}

const clojureLspCommands: ClojureLspCommand[] = [
{
command: 'clean-ns',
Expand Down Expand Up @@ -255,6 +269,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 +303,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') {
vscode.window.showErrorMessage('This function only works on local files');
} else {
let 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

0 comments on commit 08a45ec

Please sign in to comment.