From 7d604584954660d255ad0929d3be8ce03f879d0c Mon Sep 17 00:00:00 2001 From: ivan770 Date: Tue, 16 Mar 2021 14:37:00 +0200 Subject: Item up and down movers --- editors/code/package.json | 10 ++++++++++ editors/code/src/commands.ts | 28 ++++++++++++++++++++++++++++ editors/code/src/lsp_ext.ts | 13 +++++++++++++ editors/code/src/main.ts | 2 ++ 4 files changed, 53 insertions(+) (limited to 'editors') diff --git a/editors/code/package.json b/editors/code/package.json index a2ed9b2d5..faec45276 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -208,6 +208,16 @@ "command": "rust-analyzer.peekTests", "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" } ], "keybindings": [ diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index bed1f0116..cc90fe889 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts @@ -134,6 +134,34 @@ export function joinLines(ctx: Ctx): Cmd { }; } +export function moveItemUp(ctx: Ctx): Cmd { + return moveItem(ctx, ra.Direction.Up); +} + +export function moveItemDown(ctx: Ctx): Cmd { + return moveItem(ctx, ra.Direction.Down); +} + +export function moveItem(ctx: Ctx, direction: ra.Direction): Cmd { + return async () => { + const editor = ctx.activeRustEditor; + const client = ctx.client; + if (!editor || !client) return; + + const edit: lc.TextDocumentEdit = await client.sendRequest(ra.moveItem, { + range: client.code2ProtocolConverter.asRange(editor.selection), + textDocument: ctx.client.code2ProtocolConverter.asTextDocumentIdentifier(editor.document), + direction + }); + + await editor.edit((builder) => { + client.protocol2CodeConverter.asTextEdits(edit.edits).forEach((edit: any) => { + builder.replace(edit.range, edit.newText); + }); + }); + }; +} + export function onEnter(ctx: Ctx): Cmd { async function handleKeypress() { const editor = ctx.activeRustEditor; diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index 52de29e04..9af30cfdb 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/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" +} diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 925103f56..643fb643f 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -114,6 +114,8 @@ async function tryActivate(context: vscode.ExtensionContext) { ctx.registerCommand('openDocs', commands.openDocs); ctx.registerCommand('openCargoToml', commands.openCargoToml); ctx.registerCommand('peekTests', commands.peekTests); + ctx.registerCommand('moveItemUp', commands.moveItemUp); + ctx.registerCommand('moveItemDown', commands.moveItemDown); defaultOnEnter.dispose(); ctx.registerCommand('onEnter', commands.onEnter); -- cgit v1.2.3