Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in flash, ignorecase = true, When the label is the same as the next search character, but with a different case, the jump position is incorrect #19

Open
wants to merge 3 commits into
base: flash
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/actions/plugins/flash/flashAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 === '';
Expand All @@ -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) {
Expand All @@ -95,7 +97,7 @@ class FlashSearchInProgressCommand extends BaseCommand {
);

if (firstMarker) {
this.changeCursorPosition(firstMarker, vimState);
await this.changeCursorPosition(firstMarker, vimState);
}
}

Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
}
}

Expand All @@ -169,7 +171,7 @@ class FlashSearchInProgressCommand extends BaseCommand {
vimState.cursorStopPosition = marker.getJumpPosition();
}

exitFlashMode(vimState);
await exitFlashMode(vimState);
vimState.flash.recordSearchString();
}

Expand All @@ -183,7 +185,7 @@ class CommandEscFlashSearchInProgressMode extends BaseCommand {
keys = [['<Esc>'], ['<C-c>'], ['<C-[>']];

public override async exec(position: Position, vimState: VimState): Promise<void> {
exitFlashMode(vimState);
await exitFlashMode(vimState);
}
}

Expand Down
20 changes: 14 additions & 6 deletions src/actions/plugins/flash/flashMarker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,24 +180,32 @@ 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 { 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);
});
}
Expand All @@ -207,7 +215,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<string, Marker[]> = {};
Expand Down
23 changes: 13 additions & 10 deletions src/actions/plugins/flash/flashMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) => {
Expand All @@ -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<number, Match[]> = {};

matches.forEach((match) => { const key = match.range.start.line;
matches.forEach((match) => {
const key = match.range.start.line;
if (!matchesMap[key]) {
matchesMap[key] = [];
}
Expand All @@ -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]) {
Expand All @@ -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;
}
79 changes: 79 additions & 0 deletions test/plugins/flash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Configuration } from '../testConfiguration';
import { newTest } 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);

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'],
});

newTest({
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|',
});

newTest({
title: 'logging jump points',
start: ['|the flash', 'the flash', 'the flash'],
keysPressed: 'fhf' + '<C-o>',
end: ['|the flash', 'the flash', 'the flash'],
});

newTest({
title: 'with d operator',
start: ['|the flash', 'the flash', 'the flash'],
keysPressed: 'dffh',
end: ['|lash', 'the flash', 'the flash'],
});

newTest({
title: 'with y operator',
start: ['|the flash', 'the flash', 'the flash'],
keysPressed: 'yfhk$p',
end: ['the flashthe flas|h', 'the flash', 'the flash'],
});

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'],
});

newTest({
title: 'last search',
start: ['|the flash', 'the flash', 'the flash'],
keysPressed: 'fhhf\n\n',
end: ['the flas|h', 'the flash', 'the flash'],
});

newTest({
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',
});

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'],
keysPressed: 'fhhy',
end: ['the flash', 't|hH flash', 'the flash'],
});
});