From dedc976895a49d92b88fb75c7ec30dafc94a25eb Mon Sep 17 00:00:00 2001 From: HenryTSZ Date: Sat, 20 Jul 2024 21:43:41 +0800 Subject: [PATCH 1/3] fixes eslint rules --- src/actions/plugins/flash/flashAction.ts | 30 +++++++++++++----------- src/actions/plugins/flash/flashMarker.ts | 11 +++++---- src/actions/plugins/flash/flashMatch.ts | 23 ++++++++++-------- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/actions/plugins/flash/flashAction.ts b/src/actions/plugins/flash/flashAction.ts index 8f9075d9..dbb57e2c 100644 --- a/src/actions/plugins/flash/flashAction.ts +++ b/src/actions/plugins/flash/flashAction.ts @@ -58,18 +58,20 @@ class FlashSearchInProgressCommand extends BaseCommand { const chat = this.keysPressed[0]; if (this.isTriggerLastSearch(chat, vimState)) { - this.handleLastSearch(vimState); + await this.handleLastSearch(vimState); return; } if (this.isPressEnter(chat)) { - this.handleEnterJump(vimState); + await this.handleEnterJump(vimState); return; } - findMarkerByLabel(getCacheMarker(vimState.flash.searchString), chat) - ? this.handleJump(chat, vimState) - : this.handleSearch(chat, vimState); + if (findMarkerByLabel(getCacheMarker(vimState.flash.searchString), chat)) { + await this.handleJump(chat, vimState); + } else { + await this.handleSearch(chat, vimState); + } } private isTriggerLastSearch(chat: string, vimState: VimState) { return this.isPressEnter(chat) && vimState.flash.searchString === ''; @@ -81,7 +83,7 @@ class FlashSearchInProgressCommand extends BaseCommand { await vimState.setCurrentMode(vimState.flash.previousMode!); return; } - this.handleSearch(vimState.flash.previousSearchString, vimState, true); + await this.handleSearch(vimState.flash.previousSearchString, vimState, true); } private isPressEnter(chat: string) { @@ -95,7 +97,7 @@ class FlashSearchInProgressCommand extends BaseCommand { ); if (firstMarker) { - this.changeCursorPosition(firstMarker, vimState); + await this.changeCursorPosition(firstMarker, vimState); } } @@ -107,7 +109,7 @@ class FlashSearchInProgressCommand extends BaseCommand { vimState.flash.deleteSearchString(); if (vimState.flash.searchString.length === 0) { - exitFlashMode(vimState); + await exitFlashMode(vimState); } else { this.deleteSearchString(vimState); } @@ -123,14 +125,14 @@ class FlashSearchInProgressCommand extends BaseCommand { } } - private async deleteSearchString(vimState: VimState) { + private deleteSearchString(vimState: VimState) { const markers = getCacheMarker(vimState.flash.searchString); showMarkers(markers); updateMarkerLabel(markers, vimState); updateNextMatchMarker(markers, vimState.cursorStopPosition); } - private async handleFirstSearchString(vimState: VimState) { + private handleFirstSearchString(vimState: VimState) { const matches = createSearchMatches(vimState.flash.searchString, vimState.document, vimState); if (matches.length === 0) return; const labels = createMarkerLabels(matches, vimState); @@ -140,7 +142,7 @@ class FlashSearchInProgressCommand extends BaseCommand { showMarkers(markers); } - private async handleAppendSearchString(chat: string, vimState: VimState) { + private handleAppendSearchString(chat: string, vimState: VimState) { const preMarkers = getPreMarkers(vimState.flash.searchString); let matchedMarkers = getCacheMarker(vimState.flash.searchString); if (!matchedMarkers) { @@ -157,7 +159,7 @@ class FlashSearchInProgressCommand extends BaseCommand { private async handleJump(key: string, vimState: VimState) { const markerDecoration = findMarkerByLabel(getCacheMarker(vimState.flash.searchString), key); if (markerDecoration) { - this.changeCursorPosition(markerDecoration, vimState); + await this.changeCursorPosition(markerDecoration, vimState); } } @@ -169,7 +171,7 @@ class FlashSearchInProgressCommand extends BaseCommand { vimState.cursorStopPosition = marker.getJumpPosition(); } - exitFlashMode(vimState); + await exitFlashMode(vimState); vimState.flash.recordSearchString(); } @@ -183,7 +185,7 @@ class CommandEscFlashSearchInProgressMode extends BaseCommand { keys = [[''], [''], ['']]; public override async exec(position: Position, vimState: VimState): Promise { - exitFlashMode(vimState); + await exitFlashMode(vimState); } } diff --git a/src/actions/plugins/flash/flashMarker.ts b/src/actions/plugins/flash/flashMarker.ts index e67cd314..fd4215ac 100644 --- a/src/actions/plugins/flash/flashMarker.ts +++ b/src/actions/plugins/flash/flashMarker.ts @@ -180,15 +180,18 @@ export function getNextMatchMarker(searchString: string, position: vscode.Positi return markers[0]; } -let id = 0; +let _id = 0; export function createMarkers(matches: Match[], labels: string[], editor: vscode.TextEditor) { return matches.map(({ range }, index) => { const label = labels[index] || ''; - return new Marker(range, label, editor, id++); + return new Marker(range, label, editor, _id++); }); } -export function createMarkerLabels(matchRanges: Array<{ range: vscode.Range }>, vimState: VimState) { +export function createMarkerLabels( + matchRanges: Array<{ range: vscode.Range }>, + vimState: VimState, +) { const nextSearchChatList = Array.from( new Set( matchRanges.map(({ range }) => { @@ -207,7 +210,7 @@ export function getNextSearchChat(range: vscode.Range, vimState: VimState) { range.end, new vscode.Position(range.end.line, range.end.character + 1), ); - return vimState.document.getText(nextRange) + return vimState.document.getText(nextRange); } const markersMap: Record = {}; diff --git a/src/actions/plugins/flash/flashMatch.ts b/src/actions/plugins/flash/flashMatch.ts index c980723b..d4bfbae6 100644 --- a/src/actions/plugins/flash/flashMatch.ts +++ b/src/actions/plugins/flash/flashMatch.ts @@ -10,9 +10,9 @@ export interface Match { export function createSearchMatches( rawSearchString: string, document: vscode.TextDocument, - vimState: VimState + vimState: VimState, ): Match[] { - let matches: Match[] = []; + const matches: Match[] = []; if (!rawSearchString.length) return matches; const documentText = document.getText(); const flags = configuration.flash.ignorecase ? 'gi' : 'g'; @@ -44,14 +44,14 @@ function filteredVisibleRange(matches: Match[], vimState: VimState) { match.range.start.line >= visibleRange.start.line && match.range.start.line <= visibleRange.end.line ); - }) + }), ); return prev; }, [] as Match[]); } function sortMatches(matches: Match[], vimState: VimState) { - function getMiddleIndex() { + const getMiddleIndex = () => { const currentLine = vimState.cursorStartPosition.line; return lineKeys .map((lineNumber, index) => { @@ -60,13 +60,15 @@ function sortMatches(matches: Match[], vimState: VimState) { index, }; }) - .sort((a, b) => a.diffValue - b.diffValue)[0].index; } + .sort((a, b) => a.diffValue - b.diffValue)[0].index; + }; - let result: Match[] = []; + const result: Match[] = []; const matchesMap: Record = {}; - matches.forEach((match) => { const key = match.range.start.line; + matches.forEach((match) => { + const key = match.range.start.line; if (!matchesMap[key]) { matchesMap[key] = []; } @@ -85,8 +87,8 @@ function sortMatches(matches: Match[], vimState: VimState) { result.push(...matchesMap[middleKey]); } - let max = lineKeys.length; - let min = 0; + const max = lineKeys.length; + const min = 0; while (i < max || j >= min) { const nextKey = Number(lineKeys[i]); if (matchesMap[nextKey]) { @@ -105,6 +107,7 @@ function sortMatches(matches: Match[], vimState: VimState) { return result; } -const needEscapeStrings: string = '$()*+.[]?\\^{}|'; function escapeString(str: string) { +const needEscapeStrings: string = '$()*+.[]?\\^{}|'; +function escapeString(str: string) { return needEscapeStrings.includes(str) ? '\\' + str : str; } From 6acbe87bb70295a20e7b6c862d4a0e9f03196581 Mon Sep 17 00:00:00 2001 From: HenryTSZ Date: Mon, 22 Jul 2024 11:26:27 +0800 Subject: [PATCH 2/3] fixes ignorecase = true, the case of the label is different from that of the next search character & add test --- src/actions/plugins/flash/flashMarker.ts | 9 ++- test/plugins/flash.test.ts | 79 ++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 test/plugins/flash.test.ts diff --git a/src/actions/plugins/flash/flashMarker.ts b/src/actions/plugins/flash/flashMarker.ts index fd4215ac..695dbea4 100644 --- a/src/actions/plugins/flash/flashMarker.ts +++ b/src/actions/plugins/flash/flashMarker.ts @@ -192,15 +192,20 @@ export function createMarkerLabels( matchRanges: Array<{ range: vscode.Range }>, vimState: VimState, ) { + const { ignorecase, labels } = configuration.flash; + const nextSearchChatList = Array.from( new Set( matchRanges.map(({ range }) => { - return getNextSearchChat(range, vimState); + const nextSearchChat = getNextSearchChat(range, vimState); + return ignorecase ? nextSearchChat.toLocaleLowerCase() : nextSearchChat; }), ), ); - return configuration.flash.labels.split('').filter((s) => { + const labelList = ignorecase ? labels.toLocaleLowerCase().split('') : labels.split(''); + + return labelList.filter((s) => { return !nextSearchChatList.includes(s); }); } diff --git a/test/plugins/flash.test.ts b/test/plugins/flash.test.ts new file mode 100644 index 00000000..12134ce9 --- /dev/null +++ b/test/plugins/flash.test.ts @@ -0,0 +1,79 @@ +import { Configuration } from '../testConfiguration'; +import { newTest, newTestOnly } from '../testSimplifier'; +import { cleanUpWorkspace, setupWorkspace } from '../testUtils'; + +suite('flash plugin', () => { + suiteSetup(async () => { + const configuration = new Configuration(); + configuration.flash.enable = true; + + await setupWorkspace(configuration); + }); + suiteTeardown(cleanUpWorkspace); + + newTestOnly({ + title: 'f + the character to search for + the marker label', + start: ['|the flash', 'the flash', 'the flash'], + keysPressed: 'fhh', + end: ['t|he flash', 'the flash', 'the flash'], + }); + + newTestOnly({ + title: 'displaying the currently typed search character in the statusBar', + start: ['|the flash', 'the flash', 'the flash'], + keysPressed: 'fflash', + end: ['|the flash', 'the flash', 'the flash'], + statusBar: 'flash:flash|', + }); + + newTestOnly({ + title: 'logging jump points', + start: ['|the flash', 'the flash', 'the flash'], + keysPressed: 'fhf' + '', + end: ['|the flash', 'the flash', 'the flash'], + }); + + newTestOnly({ + title: 'with d operator', + start: ['|the flash', 'the flash', 'the flash'], + keysPressed: 'dffh', + end: ['|lash', 'the flash', 'the flash'], + }); + + newTestOnly({ + title: 'with y operator', + start: ['|the flash', 'the flash', 'the flash'], + keysPressed: 'yfhk$p', + end: ['the flashthe flas|h', 'the flash', 'the flash'], + }); + + newTestOnly({ + title: 'carriage returns to jump to the next marker', + start: ['|the flash', 'the flash', 'the flash'], + keysPressed: 'fh\n', + end: ['t|he flash', 'the flash', 'the flash'], + }); + + newTestOnly({ + title: 'last search', + start: ['|the flash', 'the flash', 'the flash'], + keysPressed: 'fhhf\n\n', + end: ['the flas|h', 'the flash', 'the flash'], + }); + + newTestOnly({ + title: 'no last search', + start: ['|the flash', 'the flash', 'the flash'], + keysPressed: 'f\n', + end: ['|the flash', 'the flash', 'the flash'], + statusBar: 'E888: No last search', + }); + + newTestOnly({ + title: + 'ignorecase = true, the case of the label is different from that of the next search character', + start: ['|the flash', 'thH flash', 'the flash'], + keysPressed: 'fhhy', + end: ['the flash', 't|hH flash', 'the flash'], + }); +}); From b95ea8b800ef6d218b5473170c7e6b4aaf8cab32 Mon Sep 17 00:00:00 2001 From: Tennie Henry <55341525+HenryTSZ@users.noreply.github.com> Date: Fri, 6 Sep 2024 12:33:43 +0800 Subject: [PATCH 3/3] Update flash.test.ts: newTestOnly to newTest --- test/plugins/flash.test.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/plugins/flash.test.ts b/test/plugins/flash.test.ts index 12134ce9..57e2af59 100644 --- a/test/plugins/flash.test.ts +++ b/test/plugins/flash.test.ts @@ -1,5 +1,5 @@ import { Configuration } from '../testConfiguration'; -import { newTest, newTestOnly } from '../testSimplifier'; +import { newTest } from '../testSimplifier'; import { cleanUpWorkspace, setupWorkspace } from '../testUtils'; suite('flash plugin', () => { @@ -11,14 +11,14 @@ suite('flash plugin', () => { }); suiteTeardown(cleanUpWorkspace); - newTestOnly({ + newTest({ title: 'f + the character to search for + the marker label', start: ['|the flash', 'the flash', 'the flash'], keysPressed: 'fhh', end: ['t|he flash', 'the flash', 'the flash'], }); - newTestOnly({ + newTest({ title: 'displaying the currently typed search character in the statusBar', start: ['|the flash', 'the flash', 'the flash'], keysPressed: 'fflash', @@ -26,42 +26,42 @@ suite('flash plugin', () => { statusBar: 'flash:flash|', }); - newTestOnly({ + newTest({ title: 'logging jump points', start: ['|the flash', 'the flash', 'the flash'], keysPressed: 'fhf' + '', end: ['|the flash', 'the flash', 'the flash'], }); - newTestOnly({ + newTest({ title: 'with d operator', start: ['|the flash', 'the flash', 'the flash'], keysPressed: 'dffh', end: ['|lash', 'the flash', 'the flash'], }); - newTestOnly({ + newTest({ title: 'with y operator', start: ['|the flash', 'the flash', 'the flash'], keysPressed: 'yfhk$p', end: ['the flashthe flas|h', 'the flash', 'the flash'], }); - newTestOnly({ + newTest({ title: 'carriage returns to jump to the next marker', start: ['|the flash', 'the flash', 'the flash'], keysPressed: 'fh\n', end: ['t|he flash', 'the flash', 'the flash'], }); - newTestOnly({ + newTest({ title: 'last search', start: ['|the flash', 'the flash', 'the flash'], keysPressed: 'fhhf\n\n', end: ['the flas|h', 'the flash', 'the flash'], }); - newTestOnly({ + newTest({ title: 'no last search', start: ['|the flash', 'the flash', 'the flash'], keysPressed: 'f\n', @@ -69,7 +69,7 @@ suite('flash plugin', () => { statusBar: 'E888: No last search', }); - newTestOnly({ + newTest({ title: 'ignorecase = true, the case of the label is different from that of the next search character', start: ['|the flash', 'thH flash', 'the flash'],