From f65c4c2487848934e13dc4600b52f80941f555ab Mon Sep 17 00:00:00 2001 From: Heyward Fann Date: Tue, 23 Mar 2021 17:52:42 +0800 Subject: [PATCH] feat: move item up/down FYI #256 --- README.md | 2 ++ package.json | 10 ++++++++++ src/commands.ts | 32 ++++++++++++++++++++++++++++++++ src/index.ts | 2 ++ src/lsp_ext.ts | 13 +++++++++++++ 5 files changed, 59 insertions(+) diff --git a/README.md b/README.md index 3a58fd3a..19beecc8 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,8 @@ You can use these commands by `:CocCommand XYZ`. | rust-analyzer.joinLines | Join lines | | rust-analyzer.matchingBrace | Find matching brace | | rust-analyzer.memoryUsage | Memory Usage (Clears Database) | +| rust-analyzer.moveItemUp | Move item up | +| rust-analyzer.moveItemDown | Move item down | | rust-analyzer.openDocs | Open docs under cursor | | rust-analyzer.parentModule | Locate parent module | | rust-analyzer.peekTests | Peek related tests | diff --git a/package.json b/package.json index 5fb141e0..dac60136 100644 --- a/package.json +++ b/package.json @@ -615,6 +615,16 @@ "title": "Peek related tests", "category": "Rust Analyzer" }, + { + "command": "rust-analyzer.moveItemUp", + "title": "Move item up", + "category": "Rust Analyzer" + }, + { + "command": "rust-analyzer.moveItemDown", + "title": "Move item down", + "category": "Rust Analyzer" + }, { "command": "rust-analyzer.explainError", "title": "Explain the currently hovered diagnostic", diff --git a/src/commands.ts b/src/commands.ts index 562e51c2..98c089c6 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -624,3 +624,35 @@ export function peekTests(ctx: Ctx): Cmd { await commands.executeCommand('editor.action.showReferences', Uri.parse(document.uri), position, locations); }; } + +function moveItem(ctx: Ctx, direction: ra.Direction): Cmd { + return async () => { + const { document, position } = await workspace.getCurrentState(); + if (!isRustDocument(document)) return; + + let range: Range | null = null; + const mode = (await workspace.nvim.call('visualmode')) as string; + if (mode) { + range = await workspace.getSelectedRange(mode, workspace.getDocument(document.uri)); + } + if (!range) range = Range.create(position, position); + const params: ra.MoveItemParams = { + direction, + textDocument: { uri: document.uri}, + range, + }; + const edit = await ctx.client.sendRequest(ra.moveItem, params); + if (!edit) return; + + await workspace.applyEdit({documentChanges: [edit]}); + }; +} + +export function moveItemUp(ctx: Ctx): Cmd { + return moveItem(ctx, ra.Direction.Up); +} + +export function moveItemDown(ctx: Ctx): Cmd { + return moveItem(ctx, ra.Direction.Down); +} + diff --git a/src/index.ts b/src/index.ts index 9c7122f3..416418db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -56,6 +56,8 @@ export async function activate(context: ExtensionContext): Promise { ctx.registerCommand('syntaxTree', cmds.syntaxTree); ctx.registerCommand('memoryUsage', cmds.memoryUsage); ctx.registerCommand('expandMacro', cmds.expandMacro); + ctx.registerCommand('moveItemUp', cmds.moveItemUp); + ctx.registerCommand('moveItemDown', cmds.moveItemDown); ctx.registerCommand('explainError', cmds.explainError); ctx.registerCommand('parentModule', cmds.parentModule); ctx.registerCommand('matchingBrace', cmds.matchingBrace); diff --git a/src/lsp_ext.ts b/src/lsp_ext.ts index 0f775698..a904f9c0 100644 --- a/src/lsp_ext.ts +++ b/src/lsp_ext.ts @@ -127,3 +127,16 @@ export const openCargoToml = new lc.RequestType('experimental/moveItem'); + +export interface MoveItemParams { + textDocument: lc.TextDocumentIdentifier; + range: lc.Range; + direction: Direction; +} + +export const enum Direction { + Up = 'Up', + Down = 'Down' +}