Item up and down movers

This commit is contained in:
ivan770 2021-03-16 14:37:00 +02:00
parent d704750ba9
commit 7d60458495
No known key found for this signature in database
GPG key ID: D8C4BD5AE4D9CC4D
11 changed files with 536 additions and 1 deletions

View file

@ -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;

View file

@ -127,3 +127,16 @@ export const openCargoToml = new lc.RequestType<OpenCargoTomlParams, lc.Location
export interface OpenCargoTomlParams {
textDocument: lc.TextDocumentIdentifier;
}
export const moveItem = new lc.RequestType<MoveItemParams, lc.TextDocumentEdit, void>("experimental/moveItem");
export interface MoveItemParams {
textDocument: lc.TextDocumentIdentifier,
range: lc.Range,
direction: Direction
}
export const enum Direction {
Up = "Up",
Down = "Down"
}

View file

@ -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);