Skip to content

Commit 6d86bc4

Browse files
authored
Merge pull request microsoft#49506 from alexr00/replaceHistory
Fixes microsoft#18233: Added replace history for Find view
2 parents e734a01 + cf4ce38 commit 6d86bc4

File tree

4 files changed

+87
-6
lines changed

4 files changed

+87
-6
lines changed

src/vs/workbench/parts/search/browser/searchActions.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,47 @@ export class ShowPreviousSearchTermAction extends Action {
209209
return TPromise.as(null);
210210
}
211211
}
212+
export class ShowNextReplaceTermAction extends Action {
213+
214+
public static readonly ID = 'search.replaceHistory.showNext';
215+
public static readonly LABEL = nls.localize('nextReplaceTerm', "Show Next Search Replace Term");
216+
217+
constructor(id: string, label: string,
218+
@IViewletService private viewletService: IViewletService,
219+
@IContextKeyService private contextKeyService: IContextKeyService,
220+
@IPanelService private panelService: IPanelService
221+
) {
222+
super(id, label);
223+
this.enabled = this.contextKeyService.contextMatchesRules(Constants.SearchViewVisibleKey);
224+
}
225+
226+
public run(): TPromise<any> {
227+
const searchView = getSearchView(this.viewletService, this.panelService);
228+
searchView.searchAndReplaceWidget.showNextReplaceTerm();
229+
return TPromise.as(null);
230+
}
231+
}
232+
233+
export class ShowPreviousReplaceTermAction extends Action {
234+
235+
public static readonly ID = 'search.replaceHistory.showPrevious';
236+
public static readonly LABEL = nls.localize('previousReplaceTerm', "Show Previous Search Replace Term");
237+
238+
constructor(id: string, label: string,
239+
@IViewletService private viewletService: IViewletService,
240+
@IContextKeyService private contextKeyService: IContextKeyService,
241+
@IPanelService private panelService: IPanelService
242+
) {
243+
super(id, label);
244+
this.enabled = this.contextKeyService.contextMatchesRules(Constants.SearchViewVisibleKey);
245+
}
246+
247+
public run(): TPromise<any> {
248+
const searchView = getSearchView(this.viewletService, this.panelService);
249+
searchView.searchAndReplaceWidget.showPreviousReplaceTerm();
250+
return TPromise.as(null);
251+
}
252+
}
212253

213254
export class FocusNextInputAction extends Action {
214255

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,15 @@ export class SearchView extends Viewlet implements IViewlet, IPanel {
331331
let isWholeWords = this.viewletSettings['query.wholeWords'] === true;
332332
let isCaseSensitive = this.viewletSettings['query.caseSensitive'] === true;
333333
let searchHistory = this.viewletSettings['query.searchHistory'] || [];
334+
let replaceHistory = this.viewletSettings['query.replaceHistory'] || [];
334335

335336
this.searchWidget = this.instantiationService.createInstance(SearchWidget, builder, <ISearchWidgetOptions>{
336337
value: contentPattern,
337338
isRegex: isRegex,
338339
isCaseSensitive: isCaseSensitive,
339340
isWholeWords: isWholeWords,
340-
history: searchHistory,
341+
searchHistory: searchHistory,
342+
replaceHistory: replaceHistory,
341343
historyLimit: SearchView.MAX_HISTORY_ITEMS
342344
});
343345

@@ -1519,13 +1521,15 @@ export class SearchView extends Viewlet implements IViewlet, IPanel {
15191521
const patternExcludes = this.inputPatternExcludes.getValue().trim();
15201522
const patternIncludes = this.inputPatternIncludes.getValue().trim();
15211523
const useExcludesAndIgnoreFiles = this.inputPatternExcludes.useExcludesAndIgnoreFiles();
1522-
const searchHistory = this.searchWidget.getHistory();
1524+
const searchHistory = this.searchWidget.getSearchHistory();
1525+
const replaceHistory = this.searchWidget.getReplaceHistory();
15231526
const patternExcludesHistory = this.inputPatternExcludes.getHistory();
15241527
const patternIncludesHistory = this.inputPatternIncludes.getHistory();
15251528

15261529
// store memento
15271530
this.viewletSettings['query.contentPattern'] = contentPattern;
15281531
this.viewletSettings['query.searchHistory'] = searchHistory;
1532+
this.viewletSettings['query.replaceHistory'] = replaceHistory;
15291533
this.viewletSettings['query.regex'] = isRegex;
15301534
this.viewletSettings['query.wholeWords'] = isWholeWords;
15311535
this.viewletSettings['query.caseSensitive'] = isCaseSensitive;

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ export interface ISearchWidgetOptions {
3838
isRegex?: boolean;
3939
isCaseSensitive?: boolean;
4040
isWholeWords?: boolean;
41-
history?: string[];
41+
searchHistory?: string[];
4242
historyLimit?: number;
43+
replaceHistory?: string[];
4344
}
4445

4546
class ReplaceAllAction extends Action {
@@ -96,6 +97,7 @@ export class SearchWidget extends Widget {
9697
private replaceActionBar: ActionBar;
9798

9899
private searchHistory: HistoryNavigator<string>;
100+
private replaceHistory: HistoryNavigator<string>;
99101
private ignoreGlobalFindBufferOnNextFocus = false;
100102
private previousGlobalFindBufferValue: string;
101103

@@ -128,7 +130,8 @@ export class SearchWidget extends Widget {
128130
@IConfigurationService private configurationService: IConfigurationService
129131
) {
130132
super();
131-
this.searchHistory = new HistoryNavigator<string>(options.history, options.historyLimit);
133+
this.searchHistory = new HistoryNavigator<string>(options.searchHistory, options.historyLimit);
134+
this.replaceHistory = new HistoryNavigator<string>(options.replaceHistory, options.historyLimit);
132135
this.replaceActive = Constants.ReplaceActiveKey.bindTo(this.keyBindingService);
133136
this.searchInputBoxFocused = Constants.SearchInputBoxFocusedKey.bindTo(this.keyBindingService);
134137
this.replaceInputBoxFocused = Constants.ReplaceInputBoxFocusedKey.bindTo(this.keyBindingService);
@@ -176,10 +179,14 @@ export class SearchWidget extends Widget {
176179
}
177180
}
178181

179-
public getHistory(): string[] {
182+
public getSearchHistory(): string[] {
180183
return this.searchHistory.getHistory();
181184
}
182185

186+
public getReplaceHistory(): string[] {
187+
return this.replaceHistory.getHistory();
188+
}
189+
183190
public clearHistory(): void {
184191
this.searchHistory.clear();
185192
}
@@ -204,6 +211,26 @@ export class SearchWidget extends Widget {
204211
}
205212
}
206213

214+
public showNextReplaceTerm() {
215+
let next = this.replaceHistory.next();
216+
if (next) {
217+
this.replaceInput.value = next;
218+
}
219+
}
220+
221+
public showPreviousReplaceTerm() {
222+
let previous;
223+
if (this.replaceInput.value.length === 0) {
224+
previous = this.replaceHistory.current();
225+
} else {
226+
this.replaceHistory.addIfNotPresent(this.replaceInput.value);
227+
previous = this.replaceHistory.previous();
228+
}
229+
if (previous) {
230+
this.replaceInput.value = previous;
231+
}
232+
}
233+
207234
public searchInputHasFocus(): boolean {
208235
return this.searchInputBoxFocused.get();
209236
}
@@ -256,6 +283,12 @@ export class SearchWidget extends Widget {
256283
this.searchHistory.add(this.searchInput.getValue());
257284
}));
258285

286+
this._register(this.onReplaceValueChanged(() => {
287+
if ((this.replaceHistory.current() !== this.replaceInput.value) && (this.replaceInput.value)) {
288+
this.replaceHistory.add(this.replaceInput.value);
289+
}
290+
}));
291+
259292
this.searchInputFocusTracker = this._register(dom.trackFocus(this.searchInput.inputBox.inputElement));
260293
this._register(this.searchInputFocusTracker.onDidFocus(() => {
261294
this.searchInputBoxFocused.set(true);

src/vs/workbench/parts/search/electron-browser/search.contribution.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import { getMultiSelectedResources } from 'vs/workbench/parts/files/browser/file
5353
import { Schemas } from 'vs/base/common/network';
5454
import { PanelRegistry, Extensions as PanelExtensions, PanelDescriptor } from 'vs/workbench/browser/panel';
5555
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
56-
import { openSearchView, getSearchView, ReplaceAllInFolderAction, ReplaceAllAction, CloseReplaceAction, FocusNextInputAction, FocusPreviousInputAction, FocusNextSearchResultAction, FocusPreviousSearchResultAction, ReplaceInFilesAction, FindInFilesAction, FocusActiveEditorCommand, toggleCaseSensitiveCommand, ShowNextSearchTermAction, ShowPreviousSearchTermAction, toggleRegexCommand, ShowPreviousSearchIncludeAction, ShowNextSearchIncludeAction, CollapseDeepestExpandedLevelAction, toggleWholeWordCommand, RemoveAction, ReplaceAction, ClearSearchResultsAction, copyPathCommand, copyMatchCommand, copyAllCommand, ShowNextSearchExcludeAction, ShowPreviousSearchExcludeAction, clearHistoryCommand } from 'vs/workbench/parts/search/browser/searchActions';
56+
import { openSearchView, getSearchView, ReplaceAllInFolderAction, ReplaceAllAction, CloseReplaceAction, FocusNextInputAction, FocusPreviousInputAction, FocusNextSearchResultAction, FocusPreviousSearchResultAction, ReplaceInFilesAction, FindInFilesAction, FocusActiveEditorCommand, toggleCaseSensitiveCommand, ShowNextSearchTermAction, ShowPreviousSearchTermAction, toggleRegexCommand, ShowPreviousSearchIncludeAction, ShowNextSearchIncludeAction, CollapseDeepestExpandedLevelAction, toggleWholeWordCommand, RemoveAction, ReplaceAction, ClearSearchResultsAction, copyPathCommand, copyMatchCommand, copyAllCommand, ShowNextSearchExcludeAction, ShowPreviousSearchExcludeAction, clearHistoryCommand, ShowNextReplaceTermAction, ShowPreviousReplaceTermAction } from 'vs/workbench/parts/search/browser/searchActions';
5757
import { VIEW_ID, ISearchConfigurationProperties } from 'vs/platform/search/common/search';
5858
import { IWorkbenchContributionsRegistry, Extensions as WorkbenchExtensions } from 'vs/workbench/common/contributions';
5959
import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
@@ -493,6 +493,9 @@ KeybindingsRegistry.registerCommandAndKeybindingRule(objects.assign({
493493
registry.registerWorkbenchAction(new SyncActionDescriptor(ShowNextSearchTermAction, ShowNextSearchTermAction.ID, ShowNextSearchTermAction.LABEL, ShowNextFindTermKeybinding, ContextKeyExpr.and(Constants.SearchViewVisibleKey, Constants.SearchInputBoxFocusedKey)), 'Search: Show Next Search Term', category);
494494
registry.registerWorkbenchAction(new SyncActionDescriptor(ShowPreviousSearchTermAction, ShowPreviousSearchTermAction.ID, ShowPreviousSearchTermAction.LABEL, ShowPreviousFindTermKeybinding, ContextKeyExpr.and(Constants.SearchViewVisibleKey, Constants.SearchInputBoxFocusedKey)), 'Search: Show Previous Search Term', category);
495495

496+
registry.registerWorkbenchAction(new SyncActionDescriptor(ShowNextReplaceTermAction, ShowNextReplaceTermAction.ID, ShowNextReplaceTermAction.LABEL, ShowNextFindTermKeybinding, ContextKeyExpr.and(Constants.SearchViewVisibleKey, Constants.ReplaceInputBoxFocusedKey)), 'Search: Show Next Search Replace Term', category);
497+
registry.registerWorkbenchAction(new SyncActionDescriptor(ShowPreviousReplaceTermAction, ShowPreviousReplaceTermAction.ID, ShowPreviousReplaceTermAction.LABEL, ShowPreviousFindTermKeybinding, ContextKeyExpr.and(Constants.SearchViewVisibleKey, Constants.ReplaceInputBoxFocusedKey)), 'Search: Show Previous Search Replace Term', category);
498+
496499
registry.registerWorkbenchAction(new SyncActionDescriptor(ShowNextSearchIncludeAction, ShowNextSearchIncludeAction.ID, ShowNextSearchIncludeAction.LABEL, ShowNextFindTermKeybinding, ContextKeyExpr.and(Constants.SearchViewVisibleKey, Constants.PatternIncludesFocusedKey)), 'Search: Show Next Search Include Pattern', category);
497500
registry.registerWorkbenchAction(new SyncActionDescriptor(ShowPreviousSearchIncludeAction, ShowPreviousSearchIncludeAction.ID, ShowPreviousSearchIncludeAction.LABEL, ShowPreviousFindTermKeybinding, ContextKeyExpr.and(Constants.SearchViewVisibleKey, Constants.PatternIncludesFocusedKey)), 'Search: Show Previous Search Include Pattern', category);
498501

0 commit comments

Comments
 (0)