aboutsummaryrefslogtreecommitdiff
path: root/editors/code
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code')
-rw-r--r--editors/code/package.json10
-rw-r--r--editors/code/src/commands.ts45
-rw-r--r--editors/code/src/lsp_ext.ts13
-rw-r--r--editors/code/src/main.ts2
4 files changed, 70 insertions, 0 deletions
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 @@
208 "command": "rust-analyzer.peekTests", 208 "command": "rust-analyzer.peekTests",
209 "title": "Peek related tests", 209 "title": "Peek related tests",
210 "category": "Rust Analyzer" 210 "category": "Rust Analyzer"
211 },
212 {
213 "command": "rust-analyzer.moveItemUp",
214 "title": "Move item up",
215 "category": "Rust Analyzer"
216 },
217 {
218 "command": "rust-analyzer.moveItemDown",
219 "title": "Move item down",
220 "category": "Rust Analyzer"
211 } 221 }
212 ], 222 ],
213 "keybindings": [ 223 "keybindings": [
diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts
index bed1f0116..1a0805bd3 100644
--- a/editors/code/src/commands.ts
+++ b/editors/code/src/commands.ts
@@ -134,6 +134,51 @@ export function joinLines(ctx: Ctx): Cmd {
134 }; 134 };
135} 135}
136 136
137export function moveItemUp(ctx: Ctx): Cmd {
138 return moveItem(ctx, ra.Direction.Up);
139}
140
141export function moveItemDown(ctx: Ctx): Cmd {
142 return moveItem(ctx, ra.Direction.Down);
143}
144
145export function moveItem(ctx: Ctx, direction: ra.Direction): Cmd {
146 return async () => {
147 const editor = ctx.activeRustEditor;
148 const client = ctx.client;
149 if (!editor || !client) return;
150
151 const edit = await client.sendRequest(ra.moveItem, {
152 range: client.code2ProtocolConverter.asRange(editor.selection),
153 textDocument: ctx.client.code2ProtocolConverter.asTextDocumentIdentifier(editor.document),
154 direction
155 });
156
157 if (!edit) return;
158
159 let cursor: vscode.Position | null = null;
160
161 await editor.edit((builder) => {
162 client.protocol2CodeConverter.asTextEdits(edit.edits).forEach((edit: any) => {
163 builder.replace(edit.range, edit.newText);
164
165 if (direction === ra.Direction.Up) {
166 if (!cursor || edit.range.end.isBeforeOrEqual(cursor)) {
167 cursor = edit.range.end;
168 }
169 } else {
170 if (!cursor || edit.range.end.isAfterOrEqual(cursor)) {
171 cursor = edit.range.end;
172 }
173 }
174 });
175 }).then(() => {
176 const newPosition = cursor ?? editor.selection.start;
177 editor.selection = new vscode.Selection(newPosition, newPosition);
178 });
179 };
180}
181
137export function onEnter(ctx: Ctx): Cmd { 182export function onEnter(ctx: Ctx): Cmd {
138 async function handleKeypress() { 183 async function handleKeypress() {
139 const editor = ctx.activeRustEditor; 184 const editor = ctx.activeRustEditor;
diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts
index 52de29e04..00e128b8c 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<OpenCargoTomlParams, lc.Location
127export interface OpenCargoTomlParams { 127export interface OpenCargoTomlParams {
128 textDocument: lc.TextDocumentIdentifier; 128 textDocument: lc.TextDocumentIdentifier;
129} 129}
130
131export const moveItem = new lc.RequestType<MoveItemParams, lc.TextDocumentEdit | void, void>("experimental/moveItem");
132
133export interface MoveItemParams {
134 textDocument: lc.TextDocumentIdentifier;
135 range: lc.Range;
136 direction: Direction;
137}
138
139export const enum Direction {
140 Up = "Up",
141 Down = "Down"
142}
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) {
114 ctx.registerCommand('openDocs', commands.openDocs); 114 ctx.registerCommand('openDocs', commands.openDocs);
115 ctx.registerCommand('openCargoToml', commands.openCargoToml); 115 ctx.registerCommand('openCargoToml', commands.openCargoToml);
116 ctx.registerCommand('peekTests', commands.peekTests); 116 ctx.registerCommand('peekTests', commands.peekTests);
117 ctx.registerCommand('moveItemUp', commands.moveItemUp);
118 ctx.registerCommand('moveItemDown', commands.moveItemDown);
117 119
118 defaultOnEnter.dispose(); 120 defaultOnEnter.dispose();
119 ctx.registerCommand('onEnter', commands.onEnter); 121 ctx.registerCommand('onEnter', commands.onEnter);