Bring back respecting of cached search term

This commit is contained in:
Daniel Imms
2025-09-26 17:09:54 +09:00
parent ffd6dc6ab7
commit d11f411c17
2 changed files with 21 additions and 12 deletions
+2 -2
View File
@@ -157,7 +157,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
return false;
}
const result = this._engine.findNextWithSelection(term, searchOptions);
const result = this._engine.findNextWithSelection(term, searchOptions, this._state.cachedSearchTerm);
return this._selectResult(result, searchOptions?.decorations, internalSearchOptions?.noScroll);
}
@@ -200,7 +200,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
return false;
}
const result = this._engine.findPreviousWithSelection(term, searchOptions);
const result = this._engine.findPreviousWithSelection(term, searchOptions, this._state.cachedSearchTerm);
return this._selectResult(result, searchOptions?.decorations, internalSearchOptions?.noScroll);
}
+19 -10
View File
@@ -91,9 +91,10 @@ export class SearchEngine {
* Find the next occurrence of a term with wrapping and selection management.
* @param term The search term.
* @param searchOptions Search options.
* @param cachedSearchTerm The cached search term to determine incremental behavior.
* @returns The search result if found, undefined otherwise.
*/
public findNextWithSelection(term: string, searchOptions?: ISearchOptions): ISearchResult | undefined {
public findNextWithSelection(term: string, searchOptions?: ISearchOptions, cachedSearchTerm?: string): ISearchResult | undefined {
if (!term || term.length === 0) {
this._terminal.clearSelection();
return undefined;
@@ -105,8 +106,13 @@ export class SearchEngine {
let startCol = 0;
let startRow = 0;
if (prevSelectedPos) {
startCol = prevSelectedPos.end.x;
startRow = prevSelectedPos.end.y;
if (cachedSearchTerm === term) {
startCol = prevSelectedPos.end.x;
startRow = prevSelectedPos.end.y;
} else {
startCol = prevSelectedPos.start.x;
startRow = prevSelectedPos.start.y;
}
}
this._lineCache.initLinesCache();
@@ -155,9 +161,10 @@ export class SearchEngine {
* Find the previous occurrence of a term with wrapping and selection management.
* @param term The search term.
* @param searchOptions Search options.
* @param cachedSearchTerm The cached search term to determine if expansion should occur.
* @returns The search result if found, undefined otherwise.
*/
public findPreviousWithSelection(term: string, searchOptions?: ISearchOptions): ISearchResult | undefined {
public findPreviousWithSelection(term: string, searchOptions?: ISearchOptions, cachedSearchTerm?: string): ISearchResult | undefined {
if (!term || term.length === 0) {
this._terminal.clearSelection();
return undefined;
@@ -180,12 +187,14 @@ export class SearchEngine {
if (prevSelectedPos) {
searchPosition.startRow = startRow = prevSelectedPos.start.y;
searchPosition.startCol = startCol = prevSelectedPos.start.x;
// Try to expand selection to right first.
result = this._findInLine(term, searchPosition, searchOptions, false);
if (!result) {
// If selection was not able to be expanded to the right, then try reverse search
searchPosition.startRow = startRow = prevSelectedPos.end.y;
searchPosition.startCol = startCol = prevSelectedPos.end.x;
if (cachedSearchTerm !== term) {
// Try to expand selection to right first.
result = this._findInLine(term, searchPosition, searchOptions, false);
if (!result) {
// If selection was not able to be expanded to the right, then try reverse search
searchPosition.startRow = startRow = prevSelectedPos.end.y;
searchPosition.startCol = startCol = prevSelectedPos.end.x;
}
}
}