From 10c2b613bfffea174e7fe28d131461c2f0b4d2b9 Mon Sep 17 00:00:00 2001 From: ko-shin-ryo Date: Wed, 11 Sep 2024 17:29:45 +0900 Subject: [PATCH] Fix RangeError when executing surround command with backward selection (#234) --- main.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/main.ts b/main.ts index e09a63f..bf1dd3d 100644 --- a/main.ts +++ b/main.ts @@ -494,12 +494,10 @@ export default class VimrcPlugin extends Plugin { chosenSelection = {anchor: wordAt.from, head: wordAt.to}; } } - let currText; - if (editor.posToOffset(chosenSelection.anchor) > editor.posToOffset(chosenSelection.head)) { - currText = editor.getRange(chosenSelection.head, chosenSelection.anchor); - } else { - currText = editor.getRange(chosenSelection.anchor, chosenSelection.head); - } + if (editor.posToOffset(chosenSelection.anchor) > editor.posToOffset(chosenSelection.head)) { + [chosenSelection.anchor, chosenSelection.head] = [chosenSelection.head, chosenSelection.anchor]; + } + let currText = editor.getRange(chosenSelection.anchor, chosenSelection.head); editor.replaceRange(beginning + currText + ending, chosenSelection.anchor, chosenSelection.head); // If no selection, place cursor between beginning and ending if (editor.posToOffset(chosenSelection.anchor) === editor.posToOffset(chosenSelection.head)) {