searchWidget.ts 13.8 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6 7 8
import * as nls from 'vs/nls';
import * as strings from 'vs/base/common/strings';
import * as dom from 'vs/base/browser/dom';
9 10 11
import { TPromise } from 'vs/base/common/winjs.base';
import { Widget } from 'vs/base/browser/ui/widget';
import { Action } from 'vs/base/common/actions';
S
Sandeep Somavarapu 已提交
12
import { ActionBar } from 'vs/base/browser/ui/actionbar/actionbar';
13 14 15 16
import { FindInput, IFindInputOptions } from 'vs/base/browser/ui/findinput/findInput';
import { InputBox } from 'vs/base/browser/ui/inputbox/inputBox';
import { Button } from 'vs/base/browser/ui/button/button';
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
17
import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
18
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
19
import { ContextKeyExpr, IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
20
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
21
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
22 23 24
import Event, { Emitter } from 'vs/base/common/event';
import { Builder } from 'vs/base/browser/builder';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
B
Benjamin Pasero 已提交
25
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
26
import { isSearchViewletFocussed, appendKeyBindingLabel } from 'vs/workbench/parts/search/browser/searchActions';
A
Alex Dima 已提交
27
import { CONTEXT_FIND_WIDGET_NOT_VISIBLE } from 'vs/editor/contrib/find/common/findController';
28
import { HistoryNavigator } from 'vs/base/common/history';
29
import * as Constants from 'vs/workbench/parts/search/common/constants';
30 31

export interface ISearchWidgetOptions {
J
Johannes Rieken 已提交
32 33 34 35
	value?: string;
	isRegex?: boolean;
	isCaseSensitive?: boolean;
	isWholeWords?: boolean;
36 37
}

38 39
class ReplaceAllAction extends Action {

J
Johannes Rieken 已提交
40 41
	private static fgInstance: ReplaceAllAction = null;
	public static ID: string = 'search.action.replaceAll';
42

J
Johannes Rieken 已提交
43
	static get INSTANCE(): ReplaceAllAction {
44
		if (ReplaceAllAction.fgInstance === null) {
J
Johannes Rieken 已提交
45
			ReplaceAllAction.fgInstance = new ReplaceAllAction();
46 47 48 49
		}
		return ReplaceAllAction.fgInstance;
	}

J
Johannes Rieken 已提交
50
	private _searchWidget: SearchWidget = null;
51 52 53 54 55 56

	constructor() {
		super(ReplaceAllAction.ID, '', 'action-replace-all', false);
	}

	set searchWidget(searchWidget: SearchWidget) {
J
Johannes Rieken 已提交
57
		this._searchWidget = searchWidget;
58 59
	}

J
Johannes Rieken 已提交
60
	run(): TPromise<any> {
61 62 63 64 65 66 67
		if (this._searchWidget) {
			return this._searchWidget.triggerReplaceAll();
		}
		return TPromise.as(null);
	}
}

68 69
export class SearchWidget extends Widget {

J
Johannes Rieken 已提交
70 71
	private static REPLACE_ALL_DISABLED_LABEL = nls.localize('search.action.replaceAll.disabled.label', "Replace All (Submit Search to Enable)");
	private static REPLACE_ALL_ENABLED_LABEL = (keyBindingService2: IKeybindingService): string => {
B
Benjamin Pasero 已提交
72 73
		let [kb] = keyBindingService2.lookupKeybindings(ReplaceAllAction.ID);
		return appendKeyBindingLabel(nls.localize('search.action.replaceAll.enabled.label', "Replace All"), kb, keyBindingService2);
E
Erich Gamma 已提交
74
	}
S
Sandeep Somavarapu 已提交
75

76 77
	public domNode: HTMLElement;
	public searchInput: FindInput;
78
	private searchInputBoxFocussed: IContextKey<boolean>;
S
Sandeep Somavarapu 已提交
79
	private replaceInputBoxFocussed: IContextKey<boolean>;
80 81
	private replaceInput: InputBox;

82 83 84
	public searchInputFocusTracker: dom.IFocusTracker;
	public replaceInputFocusTracker: dom.IFocusTracker;

S
Sandeep Somavarapu 已提交
85
	private replaceContainer: HTMLElement;
86
	private toggleReplaceButton: Button;
87
	private replaceAllAction: ReplaceAllAction;
A
Alex Dima 已提交
88
	private replaceActive: IContextKey<boolean>;
S
Sandeep Somavarapu 已提交
89
	private replaceActionBar: ActionBar;
90

91 92
	private searchHistory: HistoryNavigator<string>;

93 94 95 96 97 98 99 100 101
	private _onSearchSubmit = this._register(new Emitter<boolean>());
	public onSearchSubmit: Event<boolean> = this._onSearchSubmit.event;

	private _onSearchCancel = this._register(new Emitter<void>());
	public onSearchCancel: Event<void> = this._onSearchCancel.event;

	private _onReplaceToggled = this._register(new Emitter<void>());
	public onReplaceToggled: Event<void> = this._onReplaceToggled.event;

S
Sandeep Somavarapu 已提交
102 103
	private _onReplaceStateChange = this._register(new Emitter<boolean>());
	public onReplaceStateChange: Event<boolean> = this._onReplaceStateChange.event;
104 105 106 107 108 109 110

	private _onReplaceValueChanged = this._register(new Emitter<string>());
	public onReplaceValueChanged: Event<string> = this._onReplaceValueChanged.event;

	private _onReplaceAll = this._register(new Emitter<void>());
	public onReplaceAll: Event<void> = this._onReplaceAll.event;

111 112
	private hasBeenCleared: boolean;

J
Johannes Rieken 已提交
113 114
	constructor(container: Builder, private contextViewService: IContextViewService, options: ISearchWidgetOptions = Object.create(null),
		private keyBindingService: IContextKeyService, private keyBindingService2: IKeybindingService, private instantiationService: IInstantiationService) {
115
		super();
116
		this.setHasBeenCleared(false);
117
		this.searchHistory = new HistoryNavigator<string>();
118 119
		this.replaceActive = Constants.ReplaceActiveKey.bindTo(this.keyBindingService);
		this.searchInputBoxFocussed = Constants.SearchInputBoxFocussedKey.bindTo(this.keyBindingService);
S
Sandeep Somavarapu 已提交
120
		this.replaceInputBoxFocussed = Constants.ReplaceInputBoxFocussedKey.bindTo(this.keyBindingService);
121 122 123
		this.render(container, options);
	}

J
Johannes Rieken 已提交
124
	public focus(select: boolean = true, focusReplace: boolean = false): void {
125
		if ((!focusReplace && this.searchInput.inputBox.hasFocus())
J
Johannes Rieken 已提交
126
			|| (focusReplace && this.replaceInput.hasFocus())) {
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
			return;
		}

		if (focusReplace && this.isReplaceShown()) {
			this.replaceInput.focus();
			if (select) {
				this.replaceInput.select();
			}
		} else {
			this.searchInput.focus();
			if (select) {
				this.searchInput.select();
			}
		}
	}

W
Wim Spaargaren 已提交
143
	public setHasBeenCleared(cleared: boolean): void {
144 145 146
		this.hasBeenCleared = cleared;
	}

147 148
	public setWidth(width: number) {
		this.searchInput.setWidth(width - 2);
J
Johannes Rieken 已提交
149
		this.replaceInput.width = width - 28;
150 151 152
	}

	public clear() {
153
		this.setHasBeenCleared(true);
154
		this.searchInput.clear();
J
Johannes Rieken 已提交
155
		this.replaceInput.value = '';
S
Sandeep Somavarapu 已提交
156
		this.setReplaceAllActionState(false);
157 158 159
	}

	public isReplaceShown(): boolean {
S
Sandeep Somavarapu 已提交
160
		return !dom.hasClass(this.replaceContainer, 'disabled');
161 162
	}

J
Johannes Rieken 已提交
163
	public getReplaceValue(): string {
S
Sandeep Somavarapu 已提交
164
		return this.replaceInput.value;
165 166
	}

J
Johannes Rieken 已提交
167
	public toggleReplace(show?: boolean): void {
168 169
		if (show === void 0 || show !== this.isReplaceShown()) {
			this.onToggleReplaceButton();
S
Sandeep Somavarapu 已提交
170 171 172
		}
	}

173 174 175 176 177 178 179 180
	public showNextSearchTerm() {
		let next = this.searchHistory.next();
		if (next) {
			this.searchInput.setValue(next);
		}
	}

	public showPreviousSearchTerm() {
181
		let previous;
W
Wim Spaargaren 已提交
182
		if (this.hasBeenCleared) {
183
			this.setHasBeenCleared(false);
W
Wim Spaargaren 已提交
184 185
			previous = this.searchHistory.current();
		} else {
186 187
		 	previous = this.searchHistory.previous();
		}
188 189 190 191 192
		if (previous) {
			this.searchInput.setValue(previous);
		}
	}

193 194 195 196 197 198 199 200
	public searchInputHasFocus(): boolean {
		return this.searchInputBoxFocussed.get();
	}

	public replaceInputHasFocus(): boolean {
		return this.replaceInput.hasFocus();
	}

201 202 203 204 205 206 207 208 209
	private render(container: Builder, options: ISearchWidgetOptions): void {
		this.domNode = container.div({ 'class': 'search-widget' }).style({ position: 'relative' }).getHTMLElement();
		this.renderToggleReplaceButton(this.domNode);

		this.renderSearchInput(this.domNode, options);
		this.renderReplaceInput(this.domNode);
	}

	private renderToggleReplaceButton(parent: HTMLElement): void {
J
Johannes Rieken 已提交
210 211
		this.toggleReplaceButton = this._register(new Button(parent));
		this.toggleReplaceButton.icon = 'toggle-replace-button collapse';
212
		this.toggleReplaceButton.addListener2('click', () => this.onToggleReplaceButton());
J
Johannes Rieken 已提交
213
		this.toggleReplaceButton.getElement().title = nls.localize('search.replace.toggle.button.title', "Toggle Replace");
214 215 216 217 218 219
	}

	private renderSearchInput(parent: HTMLElement, options: ISearchWidgetOptions): void {
		let inputOptions: IFindInputOptions = {
			label: nls.localize('label.Search', 'Search: Type Search Term and press Enter to search or Escape to cancel'),
			validation: (value: string) => this.validatSearchInput(value),
S
Sandeep Somavarapu 已提交
220 221 222 223
			placeholder: nls.localize('search.placeHolder', "Search"),
			appendCaseSensitiveLabel: appendKeyBindingLabel('', this.keyBindingService2.lookupKeybindings(Constants.ToggleCaseSensitiveActionId)[0], this.keyBindingService2),
			appendWholeWordsLabel: appendKeyBindingLabel('', this.keyBindingService2.lookupKeybindings(Constants.ToggleWholeWordActionId)[0], this.keyBindingService2),
			appendRegexLabel: appendKeyBindingLabel('', this.keyBindingService2.lookupKeybindings(Constants.ToggleRegexActionId)[0], this.keyBindingService2)
224 225
		};

J
Johannes Rieken 已提交
226
		let searchInputContainer = dom.append(parent, dom.$('.search-container.input-box'));
227 228 229 230 231 232
		this.searchInput = this._register(new FindInput(searchInputContainer, this.contextViewService, inputOptions));
		this.searchInput.onKeyUp((keyboardEvent: IKeyboardEvent) => this.onSearchInputKeyUp(keyboardEvent));
		this.searchInput.setValue(options.value || '');
		this.searchInput.setRegex(!!options.isRegex);
		this.searchInput.setCaseSensitive(!!options.isCaseSensitive);
		this.searchInput.setWholeWords(!!options.isWholeWords);
233 234 235
		this._register(this.onSearchSubmit(() => {
			this.searchHistory.add(this.searchInput.getValue());
		}));
236

S
Sandeep Somavarapu 已提交
237
		this.searchInputFocusTracker = this._register(dom.trackFocus(this.searchInput.inputBox.inputElement));
238 239 240 241 242 243
		this._register(this.searchInputFocusTracker.addFocusListener(() => {
			this.searchInputBoxFocussed.set(true);
		}));
		this._register(this.searchInputFocusTracker.addBlurListener(() => {
			this.searchInputBoxFocussed.set(false);
		}));
244 245 246
	}

	private renderReplaceInput(parent: HTMLElement): void {
J
Joao Moreno 已提交
247
		this.replaceContainer = dom.append(parent, dom.$('.replace-container.disabled'));
J
Johannes Rieken 已提交
248
		let replaceBox = dom.append(this.replaceContainer, dom.$('.input-box'));
S
Sandeep Somavarapu 已提交
249
		this.replaceInput = this._register(new InputBox(replaceBox, this.contextViewService, {
S
Sandeep Somavarapu 已提交
250
			ariaLabel: nls.localize('label.Replace', 'Replace: Type replace term and press Enter to preview or Escape to cancel'),
S
Sandeep Somavarapu 已提交
251
			placeholder: nls.localize('search.replace.placeHolder', "Replace")
252 253 254
		}));
		this.onkeyup(this.replaceInput.inputElement, (keyboardEvent) => this.onReplaceInputKeyUp(keyboardEvent));
		this.replaceInput.onDidChange(() => this._onReplaceValueChanged.fire());
S
Sandeep Somavarapu 已提交
255
		this.searchInput.inputBox.onDidChange(() => this.onSearchInputChanged());
S
Sandeep Somavarapu 已提交
256 257

		this.replaceAllAction = ReplaceAllAction.INSTANCE;
J
Johannes Rieken 已提交
258
		this.replaceAllAction.searchWidget = this;
S
Sandeep Somavarapu 已提交
259 260 261
		this.replaceAllAction.label = SearchWidget.REPLACE_ALL_DISABLED_LABEL;
		this.replaceActionBar = this._register(new ActionBar(this.replaceContainer));
		this.replaceActionBar.push([this.replaceAllAction], { icon: true, label: false });
262

S
Sandeep Somavarapu 已提交
263 264 265 266 267 268 269
		this.replaceInputFocusTracker = this._register(dom.trackFocus(this.replaceInput.inputElement));
		this._register(this.replaceInputFocusTracker.addFocusListener(() => {
			this.replaceInputBoxFocussed.set(true);
		}));
		this._register(this.replaceInputFocusTracker.addBlurListener(() => {
			this.replaceInputBoxFocussed.set(false);
		}));
270 271
	}

272 273 274 275 276
	triggerReplaceAll(): TPromise<any> {
		this._onReplaceAll.fire();
		return TPromise.as(null);
	}

J
Johannes Rieken 已提交
277
	private onToggleReplaceButton(): void {
S
Sandeep Somavarapu 已提交
278
		dom.toggleClass(this.replaceContainer, 'disabled');
279 280
		dom.toggleClass(this.toggleReplaceButton.getElement(), 'collapse');
		dom.toggleClass(this.toggleReplaceButton.getElement(), 'expand');
281
		this.updateReplaceActiveState();
282
		this._onReplaceToggled.fire();
283 284
	}

J
Johannes Rieken 已提交
285
	public setReplaceAllActionState(enabled: boolean): void {
286
		if (this.replaceAllAction.enabled !== enabled) {
J
Johannes Rieken 已提交
287 288
			this.replaceAllAction.enabled = enabled;
			this.replaceAllAction.label = enabled ? SearchWidget.REPLACE_ALL_ENABLED_LABEL(this.keyBindingService2) : SearchWidget.REPLACE_ALL_DISABLED_LABEL;
289 290 291 292 293
			this.updateReplaceActiveState();
		}
	}

	private isReplaceActive(): boolean {
A
Alex Dima 已提交
294
		return this.replaceActive.get();
295 296 297
	}

	private updateReplaceActiveState(): void {
J
Johannes Rieken 已提交
298 299
		let currentState = this.isReplaceActive();
		let newState = this.isReplaceShown() && this.replaceAllAction.enabled;
300
		if (currentState !== newState) {
S
Sandeep Somavarapu 已提交
301 302
			this.replaceActive.set(newState);
			this._onReplaceStateChange.fire(newState);
303
		}
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
	}

	private validatSearchInput(value: string): any {
		if (value.length === 0) {
			return null;
		}
		if (!this.searchInput.getRegex()) {
			return null;
		}
		let regExp: RegExp;
		try {
			regExp = new RegExp(value);
		} catch (e) {
			return { content: e.message };
		}
		if (strings.regExpLeadsToEndlessLoop(regExp)) {
			return { content: nls.localize('regexp.validationFailure', "Expression matches everything") };
		}
	}

S
Sandeep Somavarapu 已提交
324
	private onSearchInputChanged(): void {
S
Sandeep Somavarapu 已提交
325
		this.setReplaceAllActionState(false);
S
Sandeep Somavarapu 已提交
326 327
	}

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	private onSearchInputKeyUp(keyboardEvent: IKeyboardEvent) {
		switch (keyboardEvent.keyCode) {
			case KeyCode.Enter:
				this.submitSearch();
				return;
			case KeyCode.Escape:
				this._onSearchCancel.fire();
				return;
			default:
				return;
		}
	}

	private onReplaceInputKeyUp(keyboardEvent: IKeyboardEvent) {
		switch (keyboardEvent.keyCode) {
			case KeyCode.Enter:
				this.submitSearch();
				return;
			default:
				return;
		}
	}

J
Johannes Rieken 已提交
351
	private submitSearch(refresh: boolean = true): void {
352 353 354 355 356 357
		if (this.searchInput.getValue()) {
			this._onSearchSubmit.fire(refresh);
		}
	}

	public dispose(): void {
358
		this.setReplaceAllActionState(false);
J
Johannes Rieken 已提交
359
		this.replaceAllAction.searchWidget = null;
S
Sandeep Somavarapu 已提交
360
		this.replaceActionBar = null;
361 362
		super.dispose();
	}
363 364 365
}

export function registerContributions() {
J
Johannes Rieken 已提交
366 367
	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: ReplaceAllAction.ID,
368
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
S
Sandeep Somavarapu 已提交
369
		when: ContextKeyExpr.and(Constants.SearchViewletVisibleKey, Constants.ReplaceActiveKey, CONTEXT_FIND_WIDGET_NOT_VISIBLE),
370 371 372 373 374 375 376
		primary: KeyMod.Alt | KeyMod.CtrlCmd | KeyCode.Enter,
		handler: accessor => {
			if (isSearchViewletFocussed(accessor.get(IViewletService))) {
				ReplaceAllAction.INSTANCE.run();
			}
		}
	});
A
Alex Dima 已提交
377
}