Skip to content

Commit 199fd02

Browse files
authored
add roaming changes to search history (microsoft#187330)
1 parent ff358d7 commit 199fd02

File tree

5 files changed

+61
-8
lines changed

5 files changed

+61
-8
lines changed

src/vs/base/browser/ui/inputbox/inputBox.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,19 @@ export class HistoryInputBox extends InputBox implements IHistoryNavigationWidge
685685
}
686686
}
687687

688+
public prependHistory(restoredHistory: string[]): void {
689+
const newHistory = this.getHistory();
690+
this.clearHistory();
691+
692+
restoredHistory.forEach((item) => {
693+
this.history.add(item);
694+
});
695+
696+
newHistory.forEach(item => {
697+
this.history.add(item);
698+
});
699+
}
700+
688701
public getHistory(): string[] {
689702
return this.history.getHistory();
690703
}

src/vs/workbench/contrib/search/browser/patternInputWidget.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ export class PatternInputWidget extends Widget {
116116
this.inputBox.clearHistory();
117117
}
118118

119+
prependHistory(history: string[]): void {
120+
this.inputBox.prependHistory(history);
121+
}
122+
119123
clear(): void {
120124
this.setValue('');
121125
}

src/vs/workbench/contrib/search/browser/searchView.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ import { SearchWidget } from 'vs/workbench/contrib/search/browser/searchWidget';
7070
import * as Constants from 'vs/workbench/contrib/search/common/constants';
7171
import { IReplaceService } from 'vs/workbench/contrib/search/browser/replace';
7272
import { getOutOfWorkspaceEditorResources, SearchStateKey, SearchUIState } from 'vs/workbench/contrib/search/common/search';
73-
import { ISearchHistoryService, ISearchHistoryValues } from 'vs/workbench/contrib/search/common/searchHistoryService';
73+
import { ISearchHistoryService, ISearchHistoryValues, SearchHistoryService } from 'vs/workbench/contrib/search/common/searchHistoryService';
7474
import { FileMatch, FileMatchOrMatch, FolderMatch, FolderMatchWithResource, IChangeEvent, ISearchWorkbenchService, Match, MatchInNotebook, RenderableMatch, searchMatchComparer, SearchModel, SearchResult } from 'vs/workbench/contrib/search/browser/searchModel';
7575
import { createEditorFromSearchResult } from 'vs/workbench/contrib/searchEditor/browser/searchEditorActions';
7676
import { ACTIVE_GROUP, IEditorService, SIDE_GROUP } from 'vs/workbench/services/editor/common/editorService';
@@ -185,7 +185,7 @@ export class SearchView extends ViewPane {
185185
@IContextMenuService contextMenuService: IContextMenuService,
186186
@IAccessibilityService private readonly accessibilityService: IAccessibilityService,
187187
@IKeybindingService keybindingService: IKeybindingService,
188-
@IStorageService storageService: IStorageService,
188+
@IStorageService private readonly storageService: IStorageService,
189189
@IOpenerService openerService: IOpenerService,
190190
@ITelemetryService telemetryService: ITelemetryService,
191191
@INotebookService private readonly notebookService: INotebookService,
@@ -257,6 +257,30 @@ export class SearchView extends ViewPane {
257257
this.isTreeLayoutViewVisible = this.viewletState['view.treeLayout'] ?? (this.searchConfig.defaultViewMode === ViewMode.Tree);
258258

259259
this._refreshResultsScheduler = this._register(new RunOnceScheduler(this._updateResults.bind(this), 80));
260+
261+
// storage service listener for for roaming changes
262+
this._register(this.storageService.onWillSaveState(() => {
263+
this._saveSearchHistoryService();
264+
}));
265+
266+
this._register(this.storageService.onDidChangeValue((v) => {
267+
if (v.key === SearchHistoryService.SEARCH_HISTORY_KEY) {
268+
const restoredHistory = this.searchHistoryService.load();
269+
270+
if (restoredHistory.include) {
271+
this.inputPatternIncludes.prependHistory(restoredHistory.include);
272+
}
273+
if (restoredHistory.exclude) {
274+
this.inputPatternExcludes.prependHistory(restoredHistory.exclude);
275+
}
276+
if (restoredHistory.search) {
277+
this.searchWidget.prependSearchHistory(restoredHistory.search);
278+
}
279+
if (restoredHistory.replace) {
280+
this.searchWidget.prependReplaceHistory(restoredHistory.replace);
281+
}
282+
}
283+
}));
260284
}
261285

262286
get isTreeLayoutViewVisible(): boolean {
@@ -2038,6 +2062,14 @@ export class SearchView extends ViewPane {
20382062
this.viewletState['view.treeLayout'] = this.isTreeLayoutViewVisible;
20392063
this.viewletState['query.replaceText'] = isReplaceShown && this.searchWidget.getReplaceValue();
20402064

2065+
this._saveSearchHistoryService();
2066+
2067+
this.memento.saveMemento();
2068+
2069+
super.saveState();
2070+
}
2071+
2072+
private _saveSearchHistoryService() {
20412073
const history: ISearchHistoryValues = Object.create(null);
20422074

20432075
const searchHistory = this.searchWidget.getSearchHistory();
@@ -2061,10 +2093,6 @@ export class SearchView extends ViewPane {
20612093
}
20622094

20632095
this.searchHistoryService.save(history);
2064-
2065-
this.memento.saveMemento();
2066-
2067-
super.saveState();
20682096
}
20692097

20702098
private async retrieveFileStats(): Promise<void> {

src/vs/workbench/contrib/search/browser/searchWidget.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ export class SearchWidget extends Widget {
300300
return this.replaceInput?.inputBox.getHistory() ?? [];
301301
}
302302

303+
prependSearchHistory(history: string[]): void {
304+
this.searchInput?.inputBox.prependHistory(history);
305+
}
306+
307+
prependReplaceHistory(history: string[]): void {
308+
this.replaceInput?.inputBox.prependHistory(history);
309+
}
310+
303311
clearHistory(): void {
304312
this.searchInput?.inputBox.clearHistory();
305313
this.replaceInput?.inputBox.clearHistory();

src/vs/workbench/contrib/search/common/searchHistoryService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface ISearchHistoryValues {
2828
export class SearchHistoryService implements ISearchHistoryService {
2929
declare readonly _serviceBrand: undefined;
3030

31-
private static readonly SEARCH_HISTORY_KEY = 'workbench.search.history';
31+
public static readonly SEARCH_HISTORY_KEY = 'workbench.search.history';
3232

3333
private readonly _onDidClearHistory = new Emitter<void>();
3434
readonly onDidClearHistory: Event<void> = this._onDidClearHistory.event;
@@ -61,7 +61,7 @@ export class SearchHistoryService implements ISearchHistoryService {
6161
if (isEmptyObject(history)) {
6262
this.storageService.remove(SearchHistoryService.SEARCH_HISTORY_KEY, StorageScope.WORKSPACE);
6363
} else {
64-
this.storageService.store(SearchHistoryService.SEARCH_HISTORY_KEY, JSON.stringify(history), StorageScope.WORKSPACE, StorageTarget.MACHINE);
64+
this.storageService.store(SearchHistoryService.SEARCH_HISTORY_KEY, JSON.stringify(history), StorageScope.WORKSPACE, StorageTarget.USER);
6565
}
6666
}
6767
}

0 commit comments

Comments
 (0)