From 09b14ce338b3ef6b7991490bb7b084dc7255c651 Mon Sep 17 00:00:00 2001 From: wongxy Date: Mon, 20 May 2024 01:17:02 +0800 Subject: [PATCH 1/3] fix(highlight): fix eol highlight not displaying --- src/utils/text.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/utils/text.ts b/src/utils/text.ts index 9b6c779f6..08f8644d7 100644 --- a/src/utils/text.ts +++ b/src/utils/text.ts @@ -17,6 +17,9 @@ export function expandTabs(line: string, tabWidth: number): string { return expanded; } +/** + * Note: This function is for use by the highlight module only. + */ export function calculateEditorColFromVimScreenCol(line: string, screenCol: number, tabSize: number): number { if (screenCol === 0 || !line) { return 0; @@ -33,7 +36,18 @@ export function calculateEditorColFromVimScreenCol(line: string, screenCol: numb } if (currentCharIdx >= line.length) { - return currentCharIdx; + // The basic logic of highlighting is to store them according + // to the editor column. For EOL highlights, the end-of-line position + // is always used as the decoration position, use CSS + // position offset for display. + + // Typically, the starting vim column in the grid_line event only exceeds the + // line text length when there are EOL highlights, so the offset + // needs to be added directly here. Otherwise, the parts exceeding the + // line text length will use the same editor column, causing EOL highlights + // to be incorrectly covered. + const eolCol = Math.max(screenCol - currentVimCol, 0); + return currentCharIdx + eolCol; } } return currentCharIdx; From 3308c5264883724b52a7e9385bbbe08b5dc2f818 Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sun, 19 May 2024 12:34:01 -0400 Subject: [PATCH 2/3] test: add test --- .../unit/highlights/highlight_grid.test.ts | 20 +++++++++++++++++++ src/utils/text.ts | 1 + 2 files changed, 21 insertions(+) diff --git a/src/test/unit/highlights/highlight_grid.test.ts b/src/test/unit/highlights/highlight_grid.test.ts index 30e1ed0f4..9497ffe48 100644 --- a/src/test/unit/highlights/highlight_grid.test.ts +++ b/src/test/unit/highlights/highlight_grid.test.ts @@ -630,6 +630,26 @@ describe("processHighlightCellsEvent", () => { }, ], }, + { + testName: "virtual highlights can exceed line length", + events: [ + { + row: 2, + vimCol: 6, + validCells: [{ hlId: 2, text: "!" }], + lineText: "hello", + tabSize: 4, + }, + ], + expectedRanges: [ + { + textType: "virtual" as const, + highlights: [{ hlId: 2, text: "!", virtText: "!" }], + line: 12, + col: 6, + }, + ], + }, ].forEach( ({ testName, diff --git a/src/utils/text.ts b/src/utils/text.ts index 08f8644d7..9fefc5a8b 100644 --- a/src/utils/text.ts +++ b/src/utils/text.ts @@ -24,6 +24,7 @@ export function calculateEditorColFromVimScreenCol(line: string, screenCol: numb if (screenCol === 0 || !line) { return 0; } + let currentCharIdx = 0; let currentVimCol = 0; while (currentVimCol < screenCol) { From db38cd128e212ab2cb53636f2ad4151515149a22 Mon Sep 17 00:00:00 2001 From: wongxy Date: Mon, 20 May 2024 02:05:08 +0800 Subject: [PATCH 3/3] test: eolCol >= 0 --- .../unit/highlights/highlight_grid.test.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/test/unit/highlights/highlight_grid.test.ts b/src/test/unit/highlights/highlight_grid.test.ts index 9497ffe48..364b403f9 100644 --- a/src/test/unit/highlights/highlight_grid.test.ts +++ b/src/test/unit/highlights/highlight_grid.test.ts @@ -650,6 +650,26 @@ describe("processHighlightCellsEvent", () => { }, ], }, + { + testName: "calculateEditorColFromVimScreenCol eolCol >= 0", + events: [ + { + row: 2, + vimCol: 3, + validCells: [{ hlId: 2, text: "!" }], + lineText: "\t你", + tabSize: 2, + }, + ], + expectedRanges: [ + { + textType: "virtual" as const, + highlights: [{ hlId: 2, text: "!", virtText: "!" }], + line: 12, + col: 1, + }, + ], + }, ].forEach( ({ testName,