Skip to content

Commit d11f411

Browse files
committed
Bring back respecting of cached search term
1 parent ffd6dc6 commit d11f411

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

addons/addon-search/src/SearchAddon.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
157157
return false;
158158
}
159159

160-
const result = this._engine.findNextWithSelection(term, searchOptions);
160+
const result = this._engine.findNextWithSelection(term, searchOptions, this._state.cachedSearchTerm);
161161
return this._selectResult(result, searchOptions?.decorations, internalSearchOptions?.noScroll);
162162
}
163163

@@ -200,7 +200,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
200200
return false;
201201
}
202202

203-
const result = this._engine.findPreviousWithSelection(term, searchOptions);
203+
const result = this._engine.findPreviousWithSelection(term, searchOptions, this._state.cachedSearchTerm);
204204
return this._selectResult(result, searchOptions?.decorations, internalSearchOptions?.noScroll);
205205
}
206206

addons/addon-search/src/SearchEngine.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@ export class SearchEngine {
9191
* Find the next occurrence of a term with wrapping and selection management.
9292
* @param term The search term.
9393
* @param searchOptions Search options.
94+
* @param cachedSearchTerm The cached search term to determine incremental behavior.
9495
* @returns The search result if found, undefined otherwise.
9596
*/
96-
public findNextWithSelection(term: string, searchOptions?: ISearchOptions): ISearchResult | undefined {
97+
public findNextWithSelection(term: string, searchOptions?: ISearchOptions, cachedSearchTerm?: string): ISearchResult | undefined {
9798
if (!term || term.length === 0) {
9899
this._terminal.clearSelection();
99100
return undefined;
@@ -105,8 +106,13 @@ export class SearchEngine {
105106
let startCol = 0;
106107
let startRow = 0;
107108
if (prevSelectedPos) {
108-
startCol = prevSelectedPos.end.x;
109-
startRow = prevSelectedPos.end.y;
109+
if (cachedSearchTerm === term) {
110+
startCol = prevSelectedPos.end.x;
111+
startRow = prevSelectedPos.end.y;
112+
} else {
113+
startCol = prevSelectedPos.start.x;
114+
startRow = prevSelectedPos.start.y;
115+
}
110116
}
111117

112118
this._lineCache.initLinesCache();
@@ -155,9 +161,10 @@ export class SearchEngine {
155161
* Find the previous occurrence of a term with wrapping and selection management.
156162
* @param term The search term.
157163
* @param searchOptions Search options.
164+
* @param cachedSearchTerm The cached search term to determine if expansion should occur.
158165
* @returns The search result if found, undefined otherwise.
159166
*/
160-
public findPreviousWithSelection(term: string, searchOptions?: ISearchOptions): ISearchResult | undefined {
167+
public findPreviousWithSelection(term: string, searchOptions?: ISearchOptions, cachedSearchTerm?: string): ISearchResult | undefined {
161168
if (!term || term.length === 0) {
162169
this._terminal.clearSelection();
163170
return undefined;
@@ -180,12 +187,14 @@ export class SearchEngine {
180187
if (prevSelectedPos) {
181188
searchPosition.startRow = startRow = prevSelectedPos.start.y;
182189
searchPosition.startCol = startCol = prevSelectedPos.start.x;
183-
// Try to expand selection to right first.
184-
result = this._findInLine(term, searchPosition, searchOptions, false);
185-
if (!result) {
186-
// If selection was not able to be expanded to the right, then try reverse search
187-
searchPosition.startRow = startRow = prevSelectedPos.end.y;
188-
searchPosition.startCol = startCol = prevSelectedPos.end.x;
190+
if (cachedSearchTerm !== term) {
191+
// Try to expand selection to right first.
192+
result = this._findInLine(term, searchPosition, searchOptions, false);
193+
if (!result) {
194+
// If selection was not able to be expanded to the right, then try reverse search
195+
searchPosition.startRow = startRow = prevSelectedPos.end.y;
196+
searchPosition.startCol = startCol = prevSelectedPos.end.x;
197+
}
189198
}
190199
}
191200

0 commit comments

Comments
 (0)