Skip to content

Commit

Permalink
fix(cursor): avoid unnecessary selections updates (#1507)
Browse files Browse the repository at this point in the history
* fix(cursor): avoid unnecessary selections updates

* fix: ensure complete consistency of selections
  • Loading branch information
xiyaowong committed Oct 8, 2023
1 parent 2515e46 commit bb0faed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/cursor_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,20 +274,27 @@ export class CursorManager implements Disposable, NeovimRedrawProcessable, Neovi
}
const active = convertVimPositionToEditorPosition(editor, bytePos);

const prev = editor.selection;
let selections;

let selections: Selection[];
const mode = this.main.modeManager.currentMode;
if (mode.isVisual) {
selections = await this.createVisualSelection(editor, mode, active, undefined);
} else {
logger.debug(`Updating cursor in editor pos: [${active.line}, ${active.character}]`);
selections = [new Selection(active, active)];
}

const { selections: prevSelections } = editor;
if (
// Avoid unnecessary selections updates, or it will disrupt cursor movement related features in vscode
selections.length !== prevSelections.length ||
selections.some(
(s, idx) =>
!(s.active.isEqual(prevSelections[idx].active) && s.anchor.isEqual(prevSelections[idx].anchor)),
)
) {
editor.selections = selections;
}
this.neovimCursorPosition.set(editor, selections[0]);
editor.selections = selections; // always update to clear visual selections
if (!selections[0].isEqual(prev)) {
if (!selections[0].isEqual(prevSelections[0])) {
logger.debug(`The selection was changed, scroll view`);
this.triggerMovementFunctions(editor, active);
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/suite/vscode-integration-specific.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
openTextDocument,
sendInsertKey,
sendVSCodeCommand,
sendNeovimKeys,
} from "../utils";

describe("VSCode integration specific stuff", () => {
Expand Down Expand Up @@ -519,4 +520,15 @@ describe("VSCode integration specific stuff", () => {
client,
);
});

it("cursorMove with wrappedLine should works #1498", async () => {
await openTextDocument({ content: " hello\n\nworld" });
await wait(200);
await sendNeovimKeys(client, "ll", 200);
vscode.commands.executeCommand("cursorMove", { to: "down", by: "wrappedLine" });
await wait(200);
vscode.commands.executeCommand("cursorMove", { to: "down", by: "wrappedLine" });
await wait(200);
await assertContent({ cursor: [2, 2] }, client);
});
});

0 comments on commit bb0faed

Please sign in to comment.