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/code') 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 From a154ef7ca1010b890e7dbe913d00140ed029945c Mon Sep 17 00:00:00 2001 From: ivan770 Date: Tue, 16 Mar 2021 16:57:14 +0200 Subject: Remove movable array, improve client code --- editors/code/src/commands.ts | 6 +++++- editors/code/src/lsp_ext.ts | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index cc90fe889..ad0b533b9 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts @@ -148,16 +148,20 @@ export function moveItem(ctx: Ctx, direction: ra.Direction): Cmd { const client = ctx.client; if (!editor || !client) return; - const edit: lc.TextDocumentEdit = await client.sendRequest(ra.moveItem, { + const edit = await client.sendRequest(ra.moveItem, { range: client.code2ProtocolConverter.asRange(editor.selection), textDocument: ctx.client.code2ProtocolConverter.asTextDocumentIdentifier(editor.document), direction }); + if(!edit) return; + await editor.edit((builder) => { client.protocol2CodeConverter.asTextEdits(edit.edits).forEach((edit: any) => { builder.replace(edit.range, edit.newText); }); + }).then(() => { + editor.selection = new vscode.Selection(editor.selection.end, editor.selection.end); }); }; } diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index 9af30cfdb..4c070beb0 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts @@ -128,7 +128,7 @@ export interface OpenCargoTomlParams { textDocument: lc.TextDocumentIdentifier; } -export const moveItem = new lc.RequestType("experimental/moveItem"); +export const moveItem = new lc.RequestType("experimental/moveItem"); export interface MoveItemParams { textDocument: lc.TextDocumentIdentifier, -- cgit v1.2.3 From 5f5a3e6eaecbe63916756c6706aef17792e46c06 Mon Sep 17 00:00:00 2001 From: ivan770 Date: Tue, 16 Mar 2021 17:11:50 +0200 Subject: Fix tsfmt and eslint warnings --- editors/code/src/commands.ts | 2 +- editors/code/src/lsp_ext.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index ad0b533b9..59ef98ecf 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts @@ -154,7 +154,7 @@ export function moveItem(ctx: Ctx, direction: ra.Direction): Cmd { direction }); - if(!edit) return; + if (!edit) return; await editor.edit((builder) => { client.protocol2CodeConverter.asTextEdits(edit.edits).forEach((edit: any) => { diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index 4c070beb0..00e128b8c 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts @@ -131,9 +131,9 @@ export interface OpenCargoTomlParams { export const moveItem = new lc.RequestType("experimental/moveItem"); export interface MoveItemParams { - textDocument: lc.TextDocumentIdentifier, - range: lc.Range, - direction: Direction + textDocument: lc.TextDocumentIdentifier; + range: lc.Range; + direction: Direction; } export const enum Direction { -- cgit v1.2.3 From 236abe2e60efd4b50ffe0bd0a9a40d9716c192d5 Mon Sep 17 00:00:00 2001 From: ivan770 Date: Thu, 18 Mar 2021 11:21:23 +0200 Subject: Improve cursor positioning after moving --- editors/code/src/commands.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'editors/code') diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index 59ef98ecf..1a0805bd3 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts @@ -156,12 +156,25 @@ export function moveItem(ctx: Ctx, direction: ra.Direction): Cmd { if (!edit) return; + let cursor: vscode.Position | null = null; + await editor.edit((builder) => { client.protocol2CodeConverter.asTextEdits(edit.edits).forEach((edit: any) => { builder.replace(edit.range, edit.newText); + + if (direction === ra.Direction.Up) { + if (!cursor || edit.range.end.isBeforeOrEqual(cursor)) { + cursor = edit.range.end; + } + } else { + if (!cursor || edit.range.end.isAfterOrEqual(cursor)) { + cursor = edit.range.end; + } + } }); }).then(() => { - editor.selection = new vscode.Selection(editor.selection.end, editor.selection.end); + const newPosition = cursor ?? editor.selection.start; + editor.selection = new vscode.Selection(newPosition, newPosition); }); }; } -- cgit v1.2.3