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

import nls = require('vs/nls');
7
import * as dom from 'vs/base/browser/dom';
8
import strings = require('vs/base/common/strings');
9
import collections = require('vs/base/common/collections');
10
import { $ } from 'vs/base/browser/builder';
11
import { Widget } from 'vs/base/browser/ui/widget';
J
Johannes Rieken 已提交
12
import { IExpression, splitGlobAware } from 'vs/base/common/glob';
13 14 15
import { Checkbox } from 'vs/base/browser/ui/checkbox/checkbox';
import { IContextViewProvider } from 'vs/base/browser/ui/contextview/contextview';
import { MessageType, InputBox, IInputValidator } from 'vs/base/browser/ui/inputbox/inputBox';
16 17 18
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { KeyCode } from 'vs/base/common/keyCodes';
import CommonEvent, { Emitter } from 'vs/base/common/event';
B
Benjamin Pasero 已提交
19
import { IThemeService } from 'vs/platform/theme/common/themeService';
B
Benjamin Pasero 已提交
20
import { attachInputBoxStyler, attachCheckboxStyler } from 'vs/platform/theme/common/styler';
21
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
22 23 24 25 26 27 28 29

export interface IOptions {
	placeholder?: string;
	width?: number;
	validation?: IInputValidator;
	ariaLabel?: string;
}

30
export class PatternInputWidget extends Widget {
31 32 33

	static OPTION_CHANGE: string = 'optionChange';

34 35
	public inputFocusTracker: dom.IFocusTracker;

36
	protected onOptionChange: (event: Event) => void;
37 38 39 40 41
	private width: number;
	private placeholder: string;
	private ariaLabel: string;

	private pattern: Checkbox;
42

43
	private domNode: HTMLElement;
44
	private inputNode: HTMLInputElement;
45
	protected inputBox: InputBox;
46

47 48 49
	private _onSubmit = this._register(new Emitter<boolean>());
	public onSubmit: CommonEvent<boolean> = this._onSubmit.event;

B
Benjamin Pasero 已提交
50
	constructor(parent: HTMLElement, private contextViewProvider: IContextViewProvider, protected themeService: IThemeService, options: IOptions = Object.create(null)) {
51
		super();
52 53 54 55 56 57 58 59 60 61
		this.onOptionChange = null;
		this.width = options.width || 100;
		this.placeholder = options.placeholder || '';
		this.ariaLabel = options.ariaLabel || nls.localize('defaultLabel', "input");

		this.pattern = null;
		this.domNode = null;
		this.inputNode = null;
		this.inputBox = null;

62
		this.render();
63

64
		parent.appendChild(this.domNode);
65 66
	}

67 68
	public dispose(): void {
		super.dispose();
69
		this.pattern.dispose();
70 71 72
		if (this.inputFocusTracker) {
			this.inputFocusTracker.dispose();
		}
73 74
	}

75
	public on(eventType: string, handler: (event: Event) => void): PatternInputWidget {
76 77 78 79 80
		switch (eventType) {
			case 'keydown':
			case 'keyup':
				$(this.inputBox.inputElement).on(eventType, handler);
				break;
81
			case PatternInputWidget.OPTION_CHANGE:
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
				this.onOptionChange = handler;
				break;
		}
		return this;
	}

	public setWidth(newWidth: number): void {
		this.width = newWidth;
		this.domNode.style.width = this.width + 'px';
		this.contextViewProvider.layout();
		this.setInputWidth();
	}

	public getValue(): string {
		return this.inputBox.value;
	}

	public setValue(value: string): void {
		if (this.inputBox.value !== value) {
			this.inputBox.value = value;
		}
	}

105
	public getGlob(): { expression?: IExpression, searchPaths?: string[] } {
R
💄  
Rob Lourens 已提交
106 107
		const pattern = this.getValue();
		const isGlobPattern = this.isGlobPattern();
108 109

		if (!pattern) {
110
			return {};
111 112
		}

113 114
		let exprSegments: string[];
		let searchPaths: string[];
115
		if (isGlobPattern) {
116 117 118 119
			const segments = splitGlobAware(pattern, ',')
				.map(s => s.trim())
				.filter(s => !!s.length);

120 121 122
			const groups = this.groupByPathsAndExprSegments(segments);
			searchPaths = groups.searchPaths;
			exprSegments = groups.exprSegments;
123
		} else {
124 125 126 127
			const segments = pattern.split(',')
				.map(s => strings.trim(s.trim(), '/'))
				.filter(s => !!s.length);

128 129 130
			const groups = this.groupByPathsAndExprSegments(segments);
			searchPaths = groups.searchPaths;
			exprSegments = groups.exprSegments
131 132 133 134 135 136 137
				.map(p => {
					if (p[0] === '.') {
						p = '*' + p; // convert ".js" to "*.js"
					}

					return strings.format('{{0}/**,**/{1}}', p, p); // convert foo to {foo/**,**/foo} to cover files and folders
				});
138 139
		}

140 141
		const expression = exprSegments.reduce((glob, cur) => { glob[cur] = true; return glob; }, Object.create(null));
		return { expression, searchPaths };
142 143
	}

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	private groupByPathsAndExprSegments(segments: string[]) {
		const isSearchPath = segment => segment.match(/^\.\//);

		const groups = collections.groupBy(segments,
			segment => isSearchPath(segment) ? 'searchPaths' : 'exprSegments');
		groups.searchPaths = groups.searchPaths || [];
		groups.exprSegments = groups.exprSegments || [];

		// If a ./searchPath has a glob character, remove ./ and use it as an expression segment
		groups.searchPaths = groups.searchPaths.filter(searchPath => {
			if (searchPath.match(/[\*\{\}\(\)\[\]\?]/)) {
				groups.exprSegments.push(strings.ltrim(searchPath, './'));
				return false;
			}

			return true;
		});

		return groups;
	}

165 166 167 168 169 170 171 172
	public select(): void {
		this.inputBox.select();
	}

	public focus(): void {
		this.inputBox.focus();
	}

173 174 175 176
	public inputHasFocus(): boolean {
		return this.inputBox.hasFocus();
	}

177 178 179 180 181 182 183 184 185
	public isGlobPattern(): boolean {
		return this.pattern.checked;
	}

	public setIsGlobPattern(value: boolean): void {
		this.pattern.checked = value;
	}

	private setInputWidth(): void {
186 187 188 189 190
		this.inputBox.width = this.width - this.getSubcontrolsWidth();
	}

	protected getSubcontrolsWidth(): number {
		return this.pattern.width();
191 192
	}

193
	private render(): void {
194 195 196 197 198 199 200 201 202 203 204 205
		this.domNode = document.createElement('div');
		this.domNode.style.width = this.width + 'px';
		$(this.domNode).addClass('monaco-findInput');

		this.inputBox = new InputBox(this.domNode, this.contextViewProvider, {
			placeholder: this.placeholder || '',
			ariaLabel: this.ariaLabel || '',
			validationOptions: {
				validation: null,
				showMessage: true
			}
		});
B
Benjamin Pasero 已提交
206
		this._register(attachInputBoxStyler(this.inputBox, this.themeService));
207
		this.inputFocusTracker = dom.trackFocus(this.inputBox.inputElement);
208 209
		this.onkeyup(this.inputBox.inputElement, (keyboardEvent) => this.onInputKeyUp(keyboardEvent));

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
		this.pattern = new Checkbox({
			actionClassName: 'pattern',
			title: nls.localize('patternDescription', "Use Glob Patterns"),
			isChecked: false,
			onChange: (viaKeyboard) => {
				this.onOptionChange(null);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}

				if (this.isGlobPattern()) {
					this.showGlobHelp();
				} else {
					this.inputBox.hideMessage();
				}
			}
		});
B
Benjamin Pasero 已提交
227
		this._register(attachCheckboxStyler(this.pattern, this.themeService));
228 229 230 231 232 233 234 235 236 237 238 239 240

		$(this.pattern.domNode).on('mouseover', () => {
			if (this.isGlobPattern()) {
				this.showGlobHelp();
			}
		});

		$(this.pattern.domNode).on(['mouseleave', 'mouseout'], () => {
			this.inputBox.hideMessage();
		});

		let controls = document.createElement('div');
		controls.className = 'controls';
241
		this.renderSubcontrols(controls);
242 243

		this.domNode.appendChild(controls);
244 245 246 247 248
		this.setInputWidth();
	}

	protected renderSubcontrols(controlsDiv: HTMLDivElement): void {
		controlsDiv.appendChild(this.pattern.domNode);
249 250 251 252 253 254 255 256 257 258 259
	}

	private showGlobHelp(): void {
		this.inputBox.showMessage({
			type: MessageType.INFO,
			formatContent: true,
			content: nls.localize('patternHelpInclude',
				"The pattern to match. e.g. **\\*\\*/*.js** to match all JavaScript files or **myFolder/\\*\\*** to match that folder with all children.\n\n**Reference**:\n**\\*** matches 0 or more characters\n**?** matches 1 character\n**\\*\\*** matches zero or more directories\n**[a-z]** matches a range of characters\n**{a,b}** matches any of the patterns)"
			)
		}, true);
	}
260 261 262 263 264 265 266 267 268 269

	private onInputKeyUp(keyboardEvent: IKeyboardEvent) {
		switch (keyboardEvent.keyCode) {
			case KeyCode.Enter:
				this._onSubmit.fire();
				return;
			default:
				return;
		}
	}
270 271 272 273
}

export class ExcludePatternInputWidget extends PatternInputWidget {

274 275 276 277
	constructor(parent: HTMLElement, contextViewProvider: IContextViewProvider, themeService: IThemeService, private telemetryService: ITelemetryService, options: IOptions = Object.create(null)) {
		super(parent, contextViewProvider, themeService, options);
	}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
	private useIgnoreFilesBox: Checkbox;
	private useExcludeSettingsBox: Checkbox;

	public dispose(): void {
		super.dispose();
		this.useIgnoreFilesBox.dispose();
		this.useExcludeSettingsBox.dispose();
	}

	public useExcludeSettings(): boolean {
		return this.useExcludeSettingsBox.checked;
	}

	public setUseExcludeSettings(value: boolean) {
		this.useExcludeSettingsBox.checked = value;
	}

	public useIgnoreFiles(): boolean {
		return this.useIgnoreFilesBox.checked;
	}

	public setUseIgnoreFiles(value: boolean): void {
		this.useIgnoreFilesBox.checked = value;
	}

	protected getSubcontrolsWidth(): number {
		return super.getSubcontrolsWidth() + this.useIgnoreFilesBox.width() + this.useExcludeSettingsBox.width();
	}

	protected renderSubcontrols(controlsDiv: HTMLDivElement): void {
		this.useIgnoreFilesBox = new Checkbox({
			actionClassName: 'useIgnoreFiles',
			title: nls.localize('useIgnoreFilesDescription', "Use Ignore Files"),
			isChecked: false,
			onChange: (viaKeyboard) => {
313
				this.telemetryService.publicLog('search.useIgnoreFiles.toggled');
314 315 316 317 318 319
				this.onOptionChange(null);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}
			}
		});
B
Benjamin Pasero 已提交
320
		this._register(attachCheckboxStyler(this.useIgnoreFilesBox, this.themeService));
321 322 323 324 325 326

		this.useExcludeSettingsBox = new Checkbox({
			actionClassName: 'useExcludeSettings',
			title: nls.localize('useExcludeSettingsDescription', "Use Exclude Settings"),
			isChecked: false,
			onChange: (viaKeyboard) => {
327
				this.telemetryService.publicLog('search.useExcludeSettings.toggled');
328 329 330 331 332 333
				this.onOptionChange(null);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}
			}
		});
B
Benjamin Pasero 已提交
334
		this._register(attachCheckboxStyler(this.useExcludeSettingsBox, this.themeService));
335 336 337 338 339

		controlsDiv.appendChild(this.useIgnoreFilesBox.domNode);
		controlsDiv.appendChild(this.useExcludeSettingsBox.domNode);
		super.renderSubcontrols(controlsDiv);
	}
340
}