add roaming changes to search history (#187330)

This commit is contained in:
Andrea Mah 2023-07-10 12:55:02 -07:00 committed by GitHub
parent ff358d75cd
commit 199fd02fc1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 8 deletions

View file

@ -685,6 +685,19 @@ export class HistoryInputBox extends InputBox implements IHistoryNavigationWidge
}
}
public prependHistory(restoredHistory: string[]): void {
const newHistory = this.getHistory();
this.clearHistory();
restoredHistory.forEach((item) => {
this.history.add(item);
});
newHistory.forEach(item => {
this.history.add(item);
});
}
public getHistory(): string[] {
return this.history.getHistory();
}

View file

@ -116,6 +116,10 @@ export class PatternInputWidget extends Widget {
this.inputBox.clearHistory();
}
prependHistory(history: string[]): void {
this.inputBox.prependHistory(history);
}
clear(): void {
this.setValue('');
}

View file

@ -70,7 +70,7 @@ import { SearchWidget } from 'vs/workbench/contrib/search/browser/searchWidget';
import * as Constants from 'vs/workbench/contrib/search/common/constants';
import { IReplaceService } from 'vs/workbench/contrib/search/browser/replace';
import { getOutOfWorkspaceEditorResources, SearchStateKey, SearchUIState } from 'vs/workbench/contrib/search/common/search';
import { ISearchHistoryService, ISearchHistoryValues } from 'vs/workbench/contrib/search/common/searchHistoryService';
import { ISearchHistoryService, ISearchHistoryValues, SearchHistoryService } from 'vs/workbench/contrib/search/common/searchHistoryService';
import { FileMatch, FileMatchOrMatch, FolderMatch, FolderMatchWithResource, IChangeEvent, ISearchWorkbenchService, Match, MatchInNotebook, RenderableMatch, searchMatchComparer, SearchModel, SearchResult } from 'vs/workbench/contrib/search/browser/searchModel';
import { createEditorFromSearchResult } from 'vs/workbench/contrib/searchEditor/browser/searchEditorActions';
import { ACTIVE_GROUP, IEditorService, SIDE_GROUP } from 'vs/workbench/services/editor/common/editorService';
@ -185,7 +185,7 @@ export class SearchView extends ViewPane {
@IContextMenuService contextMenuService: IContextMenuService,
@IAccessibilityService private readonly accessibilityService: IAccessibilityService,
@IKeybindingService keybindingService: IKeybindingService,
@IStorageService storageService: IStorageService,
@IStorageService private readonly storageService: IStorageService,
@IOpenerService openerService: IOpenerService,
@ITelemetryService telemetryService: ITelemetryService,
@INotebookService private readonly notebookService: INotebookService,
@ -257,6 +257,30 @@ export class SearchView extends ViewPane {
this.isTreeLayoutViewVisible = this.viewletState['view.treeLayout'] ?? (this.searchConfig.defaultViewMode === ViewMode.Tree);
this._refreshResultsScheduler = this._register(new RunOnceScheduler(this._updateResults.bind(this), 80));
// storage service listener for for roaming changes
this._register(this.storageService.onWillSaveState(() => {
this._saveSearchHistoryService();
}));
this._register(this.storageService.onDidChangeValue((v) => {
if (v.key === SearchHistoryService.SEARCH_HISTORY_KEY) {
const restoredHistory = this.searchHistoryService.load();
if (restoredHistory.include) {
this.inputPatternIncludes.prependHistory(restoredHistory.include);
}
if (restoredHistory.exclude) {
this.inputPatternExcludes.prependHistory(restoredHistory.exclude);
}
if (restoredHistory.search) {
this.searchWidget.prependSearchHistory(restoredHistory.search);
}
if (restoredHistory.replace) {
this.searchWidget.prependReplaceHistory(restoredHistory.replace);
}
}
}));
}
get isTreeLayoutViewVisible(): boolean {
@ -2038,6 +2062,14 @@ export class SearchView extends ViewPane {
this.viewletState['view.treeLayout'] = this.isTreeLayoutViewVisible;
this.viewletState['query.replaceText'] = isReplaceShown && this.searchWidget.getReplaceValue();
this._saveSearchHistoryService();
this.memento.saveMemento();
super.saveState();
}
private _saveSearchHistoryService() {
const history: ISearchHistoryValues = Object.create(null);
const searchHistory = this.searchWidget.getSearchHistory();
@ -2061,10 +2093,6 @@ export class SearchView extends ViewPane {
}
this.searchHistoryService.save(history);
this.memento.saveMemento();
super.saveState();
}
private async retrieveFileStats(): Promise<void> {

View file

@ -300,6 +300,14 @@ export class SearchWidget extends Widget {
return this.replaceInput?.inputBox.getHistory() ?? [];
}
prependSearchHistory(history: string[]): void {
this.searchInput?.inputBox.prependHistory(history);
}
prependReplaceHistory(history: string[]): void {
this.replaceInput?.inputBox.prependHistory(history);
}
clearHistory(): void {
this.searchInput?.inputBox.clearHistory();
this.replaceInput?.inputBox.clearHistory();

View file

@ -28,7 +28,7 @@ export interface ISearchHistoryValues {
export class SearchHistoryService implements ISearchHistoryService {
declare readonly _serviceBrand: undefined;
private static readonly SEARCH_HISTORY_KEY = 'workbench.search.history';
public static readonly SEARCH_HISTORY_KEY = 'workbench.search.history';
private readonly _onDidClearHistory = new Emitter<void>();
readonly onDidClearHistory: Event<void> = this._onDidClearHistory.event;
@ -61,7 +61,7 @@ export class SearchHistoryService implements ISearchHistoryService {
if (isEmptyObject(history)) {
this.storageService.remove(SearchHistoryService.SEARCH_HISTORY_KEY, StorageScope.WORKSPACE);
} else {
this.storageService.store(SearchHistoryService.SEARCH_HISTORY_KEY, JSON.stringify(history), StorageScope.WORKSPACE, StorageTarget.MACHINE);
this.storageService.store(SearchHistoryService.SEARCH_HISTORY_KEY, JSON.stringify(history), StorageScope.WORKSPACE, StorageTarget.USER);
}
}
}