]> git.lizzy.rs Git - rust.git/commitdiff
Improve cursor positioning after moving
authorivan770 <leshenko.ivan770@gmail.com>
Thu, 18 Mar 2021 09:21:23 +0000 (11:21 +0200)
committerivan770 <leshenko.ivan770@gmail.com>
Thu, 18 Mar 2021 09:22:28 +0000 (11:22 +0200)
editors/code/src/commands.ts

index 59ef98ecf978817c9403a57b905fd6db618898c9..1a0805bd3782f288ef5978991b798f7e3683aeb7 100644 (file)
@@ -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);
         });
     };
 }