mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-07 00:50:46 +00:00
Item up and down movers
This commit is contained in:
parent
d704750ba9
commit
7d60458495
11 changed files with 536 additions and 1 deletions
|
@ -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;
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue