Skip to content

Commit

Permalink
feat: move item up/down
Browse files Browse the repository at this point in the history
FYI #256
  • Loading branch information
fannheyward committed Mar 23, 2021
1 parent dd832ca commit f65c4c2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 32 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export async function activate(context: ExtensionContext): Promise<void> {
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);
Expand Down
13 changes: 13 additions & 0 deletions src/lsp_ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, void>('experimental/moveItem');

export interface MoveItemParams {
textDocument: lc.TextDocumentIdentifier;
range: lc.Range;
direction: Direction;
}

export const enum Direction {
Up = 'Up',
Down = 'Down'
}

0 comments on commit f65c4c2

Please sign in to comment.