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

import { TPromise } from 'vs/base/common/winjs.base';
import * as nls from 'vs/nls';
import URI from 'vs/base/common/uri';
S
Sandeep Somavarapu 已提交
9
import * as DOM from 'vs/base/browser/dom';
10
import { Delayer } from 'vs/base/common/async';
S
Sandeep Somavarapu 已提交
11
import { Dimension, Builder } from 'vs/base/browser/builder';
12
import { ArrayNavigator, INavigator } from 'vs/base/common/iterator';
S
Sandeep Somavarapu 已提交
13
import { Disposable, IDisposable, dispose } from 'vs/base/common/lifecycle';
14
import { KeyMod, KeyCode } from 'vs/base/common/keyCodes';
S
Sandeep Somavarapu 已提交
15 16
import { toResource, SideBySideEditorInput, EditorOptions, EditorInput, IEditorRegistry, Extensions as EditorExtensions } from 'vs/workbench/common/editor';
import { BaseEditor, EditorDescriptor } from 'vs/workbench/browser/parts/editor/baseEditor';
17
import { ResourceEditorModel } from 'vs/workbench/common/editor/resourceEditorModel';
A
Alex Dima 已提交
18
import { IEditorControl, Position, Verbosity } from 'vs/platform/editor/common/editor';
19
import { ResourceEditorInput } from 'vs/workbench/common/editor/resourceEditorInput';
S
Sandeep Somavarapu 已提交
20
import * as editorCommon from 'vs/editor/common/editorCommon';
S
Sandeep Somavarapu 已提交
21
import { BaseTextEditor } from 'vs/workbench/browser/parts/editor/textEditor';
S
Sandeep Somavarapu 已提交
22
import { CodeEditor } from 'vs/editor/browser/codeEditor';
23
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
S
Sandeep Somavarapu 已提交
24
import {
25
	IPreferencesService, ISettingsGroup, ISetting, IFilterResult,
S
Sandeep Somavarapu 已提交
26
	CONTEXT_SETTINGS_EDITOR, CONTEXT_SETTINGS_SEARCH_FOCUS, SETTINGS_EDITOR_COMMAND_SEARCH, SETTINGS_EDITOR_COMMAND_FOCUS_FILE, ISettingsEditorModel
S
Sandeep Somavarapu 已提交
27
} from 'vs/workbench/parts/preferences/common/preferences';
28
import { SettingsEditorModel, DefaultSettingsEditorModel } from 'vs/workbench/parts/preferences/common/preferencesModels';
S
Sandeep Somavarapu 已提交
29
import { editorContribution } from 'vs/editor/browser/editorBrowserExtensions';
30
import { ICodeEditor, IEditorContributionCtor } from 'vs/editor/browser/editorBrowser';
31
import { SearchWidget, SettingsTargetsWidget } from 'vs/workbench/parts/preferences/browser/preferencesWidgets';
S
Sandeep Somavarapu 已提交
32
import { ContextKeyExpr, IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
A
Alex Dima 已提交
33
import { Command } from 'vs/editor/common/editorCommonExtensions';
S
Sandeep Somavarapu 已提交
34
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
35
import { IThemeService } from 'vs/platform/theme/common/themeService';
S
Sandeep Somavarapu 已提交
36 37
import { IModelService } from 'vs/editor/common/services/modelService';
import { IModeService } from 'vs/editor/common/services/modeService';
S
Sandeep Somavarapu 已提交
38
import { IStorageService } from 'vs/platform/storage/common/storage';
39
import { ITextResourceConfigurationService } from 'vs/editor/common/services/resourceConfiguration';
S
Sandeep Somavarapu 已提交
40
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
41
import { ITextModelService } from 'vs/editor/common/services/resolverService';
42
import { ConfigurationTarget } from 'vs/workbench/services/configuration/common/configurationEditing';
43 44 45
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { VSash } from 'vs/base/browser/ui/sash/sash';
import { Widget } from 'vs/base/browser/ui/widget';
46
import { IPreferencesRenderer, DefaultSettingsRenderer, UserSettingsRenderer, WorkspaceSettingsRenderer, FolderSettingsRenderer } from 'vs/workbench/parts/preferences/browser/preferencesRenderers';
B
Benjamin Pasero 已提交
47 48
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
49
import { getCodeEditor } from 'vs/editor/common/services/codeEditorService';
50 51

// Ignore following contributions
S
Sandeep Somavarapu 已提交
52
import { FoldingController } from 'vs/editor/contrib/folding/browser/folding';
S
Sandeep Somavarapu 已提交
53 54
import { FindController } from 'vs/editor/contrib/find/browser/find';
import { SelectionHighlighter } from 'vs/editor/contrib/find/common/findController';
55 56
import { IEditorOptions } from 'vs/editor/common/config/editorOptions';
import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
57 58
import { attachStylerCallback } from 'vs/platform/theme/common/styler';
import { scrollbarShadow } from 'vs/platform/theme/common/colorRegistry';
59
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
B
Benjamin Pasero 已提交
60 61
import Event, { Emitter } from 'vs/base/common/event';
import { Registry } from 'vs/platform/registry/common/platform';
S
Sandeep Somavarapu 已提交
62

63 64
export class PreferencesEditorInput extends SideBySideEditorInput {
	public static ID: string = 'workbench.editorinputs.preferencesEditorInput';
S
Sandeep Somavarapu 已提交
65

66 67 68
	getTypeId(): string {
		return PreferencesEditorInput.ID;
	}
S
Sandeep Somavarapu 已提交
69 70 71 72

	public getTitle(verbosity: Verbosity): string {
		return this.master.getTitle(verbosity);
	}
73
}
74

75
export class DefaultPreferencesEditorInput extends ResourceEditorInput {
76
	public static ID = 'workbench.editorinputs.defaultpreferences';
77
	constructor(defaultSettingsResource: URI,
78
		@ITextModelService textModelResolverService: ITextModelService
79 80
	) {
		super(nls.localize('settingsEditorName', "Default Settings"), '', defaultSettingsResource, textModelResolverService);
81 82
	}

S
Sandeep Somavarapu 已提交
83
	getTypeId(): string {
84
		return DefaultPreferencesEditorInput.ID;
S
Sandeep Somavarapu 已提交
85
	}
86

S
Sandeep Somavarapu 已提交
87
	matches(other: any): boolean {
88
		if (!super.matches(other)) {
S
Sandeep Somavarapu 已提交
89
			return false;
90
		}
91
		if (!(other instanceof DefaultPreferencesEditorInput)) {
S
Sandeep Somavarapu 已提交
92 93
			return false;
		}
S
Sandeep Somavarapu 已提交
94 95
		return true;
	}
96
}
97

98 99 100 101
export class PreferencesEditor extends BaseEditor {

	public static ID: string = 'workbench.editor.preferencesEditor';

S
Sandeep Somavarapu 已提交
102
	private defaultSettingsEditorContextKey: IContextKey<boolean>;
S
Sandeep Somavarapu 已提交
103
	private focusSettingsContextKey: IContextKey<boolean>;
104 105
	private headerContainer: HTMLElement;
	private searchWidget: SearchWidget;
106
	private settingsTargetsWidget: SettingsTargetsWidget;
107
	private sideBySidePreferencesWidget: SideBySidePreferencesWidget;
108
	private preferencesRenderers: PreferencesRenderers;
109 110 111

	private delayedFilterLogging: Delayer<void>;

112
	private latestEmptyFilters: string[] = [];
S
Sandeep Somavarapu 已提交
113
	private lastFocusedWidget: SearchWidget | SideBySidePreferencesWidget = null;
114

115 116 117 118 119
	constructor(
		@IPreferencesService private preferencesService: IPreferencesService,
		@IEnvironmentService private environmentService: IEnvironmentService,
		@ITelemetryService telemetryService: ITelemetryService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
S
Sandeep Somavarapu 已提交
120
		@IContextKeyService private contextKeyService: IContextKeyService,
B
Benjamin Pasero 已提交
121
		@IInstantiationService private instantiationService: IInstantiationService,
122 123
		@IThemeService themeService: IThemeService,
		@IWorkspaceContextService private workspaceContextService: IWorkspaceContextService,
124
	) {
B
Benjamin Pasero 已提交
125
		super(PreferencesEditor.ID, telemetryService, themeService);
S
Sandeep Somavarapu 已提交
126
		this.defaultSettingsEditorContextKey = CONTEXT_SETTINGS_EDITOR.bindTo(this.contextKeyService);
S
Sandeep Somavarapu 已提交
127
		this.focusSettingsContextKey = CONTEXT_SETTINGS_SEARCH_FOCUS.bindTo(this.contextKeyService);
128 129 130 131 132 133 134 135 136
		this.delayedFilterLogging = new Delayer<void>(1000);
	}

	public createEditor(parent: Builder): void {
		const parentElement = parent.getHTMLElement();
		DOM.addClass(parentElement, 'preferences-editor');

		this.headerContainer = DOM.append(parentElement, DOM.$('.preferences-header'));

137 138
		this.searchWidget = this._register(this.instantiationService.createInstance(SearchWidget, this.headerContainer, {
			ariaLabel: nls.localize('SearchSettingsWidget.AriaLabel', "Search settings"),
S
Sandeep Somavarapu 已提交
139
			placeholder: nls.localize('SearchSettingsWidget.Placeholder', "Search Settings"),
S
Sandeep Somavarapu 已提交
140
			focusKey: this.focusSettingsContextKey
141
		}));
142
		this._register(this.searchWidget.onDidChange(value => this.filterPreferences(value.trim())));
S
Sandeep Somavarapu 已提交
143
		this._register(this.searchWidget.onNavigate(shift => this.preferencesRenderers.focusNextPreference(!shift)));
S
Sandeep Somavarapu 已提交
144 145
		this._register(this.searchWidget.onFocus(() => this.lastFocusedWidget = this.searchWidget));
		this.lastFocusedWidget = this.searchWidget;
146

147
		this.settingsTargetsWidget = this._register(this.instantiationService.createInstance(SettingsTargetsWidget, this.headerContainer, this.preferencesService.userSettingsResource, ConfigurationTarget.USER));
148
		this._register(this.settingsTargetsWidget.onDidTargetChange(target => this.switchSettings(target)));
149 150 151

		const editorsContainer = DOM.append(parentElement, DOM.$('.preferences-editors-container'));
		this.sideBySidePreferencesWidget = this._register(this.instantiationService.createInstance(SideBySidePreferencesWidget, editorsContainer));
S
Sandeep Somavarapu 已提交
152 153
		this._register(this.sideBySidePreferencesWidget.onFocus(() => this.lastFocusedWidget = this.sideBySidePreferencesWidget));

154
		this.preferencesRenderers = this._register(new PreferencesRenderers());
S
Sandeep Somavarapu 已提交
155
		this._register(this.workspaceContextService.onDidChangeWorkspaceFolders(() => this.onWorkspaceFoldersChanged()));
156 157 158
	}

	public setInput(newInput: PreferencesEditorInput, options?: EditorOptions): TPromise<void> {
S
Sandeep Somavarapu 已提交
159
		this.defaultSettingsEditorContextKey.set(true);
160
		const oldInput = <PreferencesEditorInput>this.input;
161
		return super.setInput(newInput, options).then(() => this.updateInput(oldInput, newInput, options));
162 163 164
	}

	public layout(dimension: Dimension): void {
S
Sandeep Somavarapu 已提交
165 166
		DOM.toggleClass(this.headerContainer, 'vertical-layout', dimension.width < 700);
		this.searchWidget.layout(dimension);
167 168 169 170 171
		const headerHeight = DOM.getTotalHeight(this.headerContainer);
		this.sideBySidePreferencesWidget.layout(new Dimension(dimension.width, dimension.height - headerHeight));
	}

	public getControl(): IEditorControl {
172
		return this.sideBySidePreferencesWidget.getControl();
173 174 175
	}

	public focus(): void {
S
Sandeep Somavarapu 已提交
176 177 178
		if (this.lastFocusedWidget) {
			this.lastFocusedWidget.focus();
		}
179 180
	}

181
	public focusSearch(filter?: string): void {
R
Rob Lourens 已提交
182
		if (filter) {
183 184
			this.searchWidget.setValue(filter);
		}
R
Rob Lourens 已提交
185

S
Sandeep Somavarapu 已提交
186 187 188
		this.searchWidget.focus();
	}

S
Sandeep Somavarapu 已提交
189 190 191 192 193 194
	public focusSettingsFileEditor(): void {
		if (this.sideBySidePreferencesWidget) {
			this.sideBySidePreferencesWidget.focus();
		}
	}

195
	public clearInput(): void {
S
Sandeep Somavarapu 已提交
196
		this.defaultSettingsEditorContextKey.set(false);
197 198 199 200
		this.sideBySidePreferencesWidget.clearInput();
		super.clearInput();
	}

S
Sandeep Somavarapu 已提交
201 202 203 204 205 206 207 208 209 210
	protected setEditorVisible(visible: boolean, position: Position): void {
		this.sideBySidePreferencesWidget.setEditorVisible(visible, position);
		super.setEditorVisible(visible, position);
	}

	public changePosition(position: Position): void {
		this.sideBySidePreferencesWidget.changePosition(position);
		super.changePosition(position);
	}

211
	private updateInput(oldInput: PreferencesEditorInput, newInput: PreferencesEditorInput, options?: EditorOptions): TPromise<void> {
212
		const resource = toResource(newInput.master);
S
Sandeep Somavarapu 已提交
213
		this.settingsTargetsWidget.setTarget(this.getSettingsConfigurationTargetUri(resource), this.getSettingsConfigurationTarget(resource));
214

215
		return this.sideBySidePreferencesWidget.setInput(<DefaultPreferencesEditorInput>newInput.details, <EditorInput>newInput.master, options).then(({ defaultPreferencesRenderer, editablePreferencesRenderer }) => {
216 217
			this.preferencesRenderers.defaultPreferencesRenderer = defaultPreferencesRenderer;
			this.preferencesRenderers.editablePreferencesRenderer = editablePreferencesRenderer;
S
Sandeep Somavarapu 已提交
218
			this.filterPreferences(this.searchWidget.getValue());
219 220 221
		});
	}

222
	private getSettingsConfigurationTarget(resource: URI): ConfigurationTarget {
223 224 225 226 227 228
		if (this.preferencesService.userSettingsResource.fsPath === resource.fsPath) {
			return ConfigurationTarget.USER;
		}
		if (this.preferencesService.workspaceSettingsResource.fsPath === resource.fsPath) {
			return ConfigurationTarget.WORKSPACE;
		}
S
Sandeep Somavarapu 已提交
229
		if (this.workspaceContextService.getWorkspaceFolder(resource)) {
230 231 232
			return ConfigurationTarget.FOLDER;
		}
		return null;
233 234
	}

S
Sandeep Somavarapu 已提交
235 236 237 238 239 240 241 242
	private getSettingsConfigurationTargetUri(resource: URI): URI {
		if (this.preferencesService.userSettingsResource.fsPath === resource.fsPath) {
			return resource;
		}
		if (this.preferencesService.workspaceSettingsResource.fsPath === resource.fsPath) {
			return resource;
		}

243 244
		const workspaceFolder = this.workspaceContextService.getWorkspaceFolder(resource);
		return workspaceFolder ? workspaceFolder.uri : null;
S
Sandeep Somavarapu 已提交
245 246
	}

S
Sandeep Somavarapu 已提交
247
	private onWorkspaceFoldersChanged(): void {
S
Sandeep Somavarapu 已提交
248 249 250 251 252 253 254 255 256
		if (this.input) {
			const settingsResource = toResource((<PreferencesEditorInput>this.input).master);
			const targetResource = this.getSettingsConfigurationTargetUri(settingsResource);
			if (!targetResource) {
				this.switchSettings(this.preferencesService.userSettingsResource);
			}
		}
	}

257
	private switchSettings(resource: URI): void {
258 259 260 261
		// Focus the editor if this editor is not active editor
		if (this.editorService.getActiveEditor() !== this) {
			this.focus();
		}
262
		const promise = this.input.isDirty() ? this.input.save() : TPromise.as(true);
263
		promise.done(value => this.preferencesService.switchSettings(this.getSettingsConfigurationTarget(resource), resource));
264 265 266
	}

	private filterPreferences(filter: string) {
267 268 269 270 271
		const count = this.preferencesRenderers.filterPreferences(filter);
		const message = filter ? this.showSearchResultsMessage(count) : nls.localize('totalSettingsMessage', "Total {0} Settings", count);
		this.searchWidget.showMessage(message, count);
		if (count === 0) {
			this.latestEmptyFilters.push(filter);
272
		}
273
		this.delayedFilterLogging.trigger(() => this.reportFilteringUsed(filter));
274 275 276
	}

	private showSearchResultsMessage(count: number): string {
S
Sandeep Somavarapu 已提交
277
		return count === 0 ? nls.localize('noSettingsFound', "No Results") :
278 279 280 281 282
			count === 1 ? nls.localize('oneSettingFound', "1 Setting matched") :
				nls.localize('settingsFound', "{0} Settings matched", count);
	}

	private reportFilteringUsed(filter: string): void {
283 284 285 286 287 288 289 290
		if (filter) {
			let data = {
				filter,
				emptyFilters: this.getLatestEmptyFiltersForTelemetry()
			};
			this.latestEmptyFilters = [];
			this.telemetryService.publicLog('defaultSettings.filter', data);
		}
291 292
	}

293 294 295 296 297 298 299 300
	/**
	 * Put a rough limit on the size of the telemetry data, since otherwise it could be an unbounded large amount
	 * of data. 8192 is the max size of a property value. This is rough since that probably includes ""s, etc.
	 */
	private getLatestEmptyFiltersForTelemetry(): string[] {
		let cumulativeSize = 0;
		return this.latestEmptyFilters.filter(filterText => (cumulativeSize += filterText.length) <= 8192);
	}
301 302
}

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
class SettingsNavigator implements INavigator<ISetting> {

	private iterator: ArrayNavigator<ISetting>;

	constructor(settings: ISetting[]) {
		this.iterator = new ArrayNavigator<ISetting>(settings);
	}

	public next(): ISetting {
		return this.iterator.next() || this.iterator.first();
	}

	public previous(): ISetting {
		return this.iterator.previous() || this.iterator.last();
	}

	public parent(): ISetting {
		return this.iterator.parent();
	}

	public first(): ISetting {
		return this.iterator.first();
	}

	public last(): ISetting {
		return this.iterator.last();
	}

	public current(): ISetting {
		return this.iterator.current();
	}
}

336
class PreferencesRenderers extends Disposable {
337

338 339
	private _defaultPreferencesRenderer: IPreferencesRenderer<ISetting>;
	private _editablePreferencesRenderer: IPreferencesRenderer<ISetting>;
340
	private _settingsNavigator: SettingsNavigator;
341 342 343 344 345 346 347 348

	private _disposables: IDisposable[] = [];

	public get defaultPreferencesRenderer(): IPreferencesRenderer<ISetting> {
		return this._defaultPreferencesRenderer;
	}

	public set defaultPreferencesRenderer(defaultPreferencesRenderer: IPreferencesRenderer<ISetting>) {
S
Sandeep Somavarapu 已提交
349 350
		if (this._defaultPreferencesRenderer !== defaultPreferencesRenderer) {
			this._defaultPreferencesRenderer = defaultPreferencesRenderer;
351

S
Sandeep Somavarapu 已提交
352
			this._disposables = dispose(this._disposables);
S
Sandeep Somavarapu 已提交
353 354 355 356 357 358

			if (this._defaultPreferencesRenderer) {
				this._defaultPreferencesRenderer.onUpdatePreference(({ key, value, source }) => this._updatePreference(key, value, source, this._editablePreferencesRenderer), this, this._disposables);
				this._defaultPreferencesRenderer.onFocusPreference(preference => this._focusPreference(preference, this._editablePreferencesRenderer), this, this._disposables);
				this._defaultPreferencesRenderer.onClearFocusPreference(preference => this._clearFocus(preference, this._editablePreferencesRenderer), this, this._disposables);
			}
S
Sandeep Somavarapu 已提交
359
		}
360 361 362 363 364 365 366
	}

	public set editablePreferencesRenderer(editableSettingsRenderer: IPreferencesRenderer<ISetting>) {
		this._editablePreferencesRenderer = editableSettingsRenderer;
	}

	public filterPreferences(filter: string): number {
S
Sandeep Somavarapu 已提交
367 368 369 370 371 372
		const defaultPreferencesFilterResult = this._filterPreferences(filter, this._defaultPreferencesRenderer);
		const editablePreferencesFilterResult = this._filterPreferences(filter, this._editablePreferencesRenderer);

		const defaultPreferencesFilteredGroups = defaultPreferencesFilterResult ? defaultPreferencesFilterResult.filteredGroups : this._getAllPreferences(this._defaultPreferencesRenderer);
		const editablePreferencesFilteredGroups = editablePreferencesFilterResult ? editablePreferencesFilterResult.filteredGroups : this._getAllPreferences(this._editablePreferencesRenderer);
		const consolidatedSettings = this._consolidateSettings(editablePreferencesFilteredGroups, defaultPreferencesFilteredGroups);
373
		this._settingsNavigator = new SettingsNavigator(filter ? consolidatedSettings : []);
S
Sandeep Somavarapu 已提交
374

375
		return consolidatedSettings.length;
376 377
	}

378
	public focusNextPreference(forward: boolean = true) {
379
		const setting = forward ? this._settingsNavigator.next() : this._settingsNavigator.previous();
380 381 382 383
		this._focusPreference(setting, this._defaultPreferencesRenderer);
		this._focusPreference(setting, this._editablePreferencesRenderer);
	}

S
Sandeep Somavarapu 已提交
384 385 386 387 388 389
	private _getAllPreferences(preferencesRenderer: IPreferencesRenderer<ISetting>): ISettingsGroup[] {
		return preferencesRenderer ? (<ISettingsEditorModel>preferencesRenderer.preferencesModel).settingsGroups : [];
	}

	private _filterPreferences(filter: string, preferencesRenderer: IPreferencesRenderer<ISetting>): IFilterResult {
		let filterResult = null;
390
		if (preferencesRenderer) {
S
Sandeep Somavarapu 已提交
391
			filterResult = filter ? (<ISettingsEditorModel>preferencesRenderer.preferencesModel).filterSettings(filter) : null;
392 393
			preferencesRenderer.filterPreferences(filterResult);
		}
S
Sandeep Somavarapu 已提交
394
		return filterResult;
395 396 397 398 399 400 401 402 403 404 405 406 407 408
	}

	private _focusPreference(preference: ISetting, preferencesRenderer: IPreferencesRenderer<ISetting>): void {
		if (preference && preferencesRenderer) {
			preferencesRenderer.focusPreference(preference);
		}
	}

	private _clearFocus(preference: ISetting, preferencesRenderer: IPreferencesRenderer<ISetting>): void {
		if (preference && preferencesRenderer) {
			preferencesRenderer.clearFocus(preference);
		}
	}

S
Sandeep Somavarapu 已提交
409 410 411 412 413 414
	private _updatePreference(key: string, value: any, source: ISetting, preferencesRenderer: IPreferencesRenderer<ISetting>): void {
		if (preferencesRenderer) {
			preferencesRenderer.updatePreference(key, value, source);
		}
	}

415 416 417 418 419 420 421 422
	private _consolidateSettings(editableSettingsGroups: ISettingsGroup[], defaultSettingsGroups: ISettingsGroup[]): ISetting[] {
		const editableSettings = this._flatten(editableSettingsGroups);
		const defaultSettings = this._flatten(defaultSettingsGroups).filter(secondarySetting => !editableSettings.some(primarySetting => primarySetting.key === secondarySetting.key));
		return [...editableSettings, ...defaultSettings];
	}

	private _flatten(settingsGroups: ISettingsGroup[]): ISetting[] {
		const settings: ISetting[] = [];
423 424
		for (const group of settingsGroups) {
			for (const section of group.sections) {
425
				settings.push(...section.settings);
426 427
			}
		}
428
		return settings;
429
	}
430 431 432 433 434

	public dispose(): void {
		dispose(this._disposables);
		super.dispose();
	}
435 436
}

437
class SideBySidePreferencesWidget extends Widget {
438 439 440 441

	private dimension: Dimension;

	private defaultPreferencesEditor: DefaultPreferencesEditor;
S
Sandeep Somavarapu 已提交
442
	private editablePreferencesEditor: BaseEditor;
443 444 445
	private defaultPreferencesEditorContainer: HTMLElement;
	private editablePreferencesEditorContainer: HTMLElement;

S
Sandeep Somavarapu 已提交
446 447 448 449 450
	private _onFocus: Emitter<void> = new Emitter<void>();
	readonly onFocus: Event<void> = this._onFocus.event;

	private lastFocusedEditor: BaseEditor;

451 452
	private sash: VSash;

453
	constructor(parent: HTMLElement, @IInstantiationService private instantiationService: IInstantiationService, @IThemeService private themeService: IThemeService) {
454 455 456 457 458 459 460 461 462 463
		super();
		this.create(parent);
	}

	private create(parentElement: HTMLElement): void {
		DOM.addClass(parentElement, 'side-by-side-preferences-editor');
		this.createSash(parentElement);

		this.defaultPreferencesEditorContainer = DOM.append(parentElement, DOM.$('.default-preferences-editor-container'));
		this.defaultPreferencesEditorContainer.style.position = 'absolute';
464
		this.defaultPreferencesEditor = this._register(this.instantiationService.createInstance(DefaultPreferencesEditor));
465
		this.defaultPreferencesEditor.create(new Builder(this.defaultPreferencesEditorContainer));
S
Sandeep Somavarapu 已提交
466
		this.defaultPreferencesEditor.setVisible(true);
S
Sandeep Somavarapu 已提交
467
		(<CodeEditor>this.defaultPreferencesEditor.getControl()).onDidFocusEditor(() => this.lastFocusedEditor = this.defaultPreferencesEditor);
468 469 470

		this.editablePreferencesEditorContainer = DOM.append(parentElement, DOM.$('.editable-preferences-editor-container'));
		this.editablePreferencesEditorContainer.style.position = 'absolute';
471

472 473 474 475 476 477 478 479 480
		this._register(attachStylerCallback(this.themeService, { scrollbarShadow }, colors => {
			const shadow = colors.scrollbarShadow ? colors.scrollbarShadow.toString() : null;

			if (shadow) {
				this.editablePreferencesEditorContainer.style.boxShadow = `-6px 0 5px -5px ${shadow}`;
			} else {
				this.editablePreferencesEditorContainer.style.boxShadow = null;
			}
		}));
S
Sandeep Somavarapu 已提交
481 482 483

		const focusTracker = this._register(DOM.trackFocus(parentElement));
		this._register(focusTracker.addFocusListener(() => this._onFocus.fire()));
484 485
	}

486
	public setInput(defaultPreferencesEditorInput: DefaultPreferencesEditorInput, editablePreferencesEditorInput: EditorInput, options?: EditorOptions): TPromise<{ defaultPreferencesRenderer: IPreferencesRenderer<ISetting>, editablePreferencesRenderer: IPreferencesRenderer<ISetting> }> {
S
Sandeep Somavarapu 已提交
487 488 489 490 491 492 493
		return this.getOrCreateEditablePreferencesEditor(editablePreferencesEditorInput)
			.then(() => {
				this.dolayout(this.sash.getVerticalSashLeft());
				return TPromise.join([this.updateInput(this.defaultPreferencesEditor, defaultPreferencesEditorInput, DefaultSettingsEditorContribution.ID, toResource(editablePreferencesEditorInput), options),
				this.updateInput(this.editablePreferencesEditor, editablePreferencesEditorInput, SettingsEditorContribution.ID, defaultPreferencesEditorInput.getResource(), options)])
					.then(([defaultPreferencesRenderer, editablePreferencesRenderer]) => ({ defaultPreferencesRenderer, editablePreferencesRenderer }));
			});
494 495 496 497 498 499 500
	}

	public layout(dimension: Dimension): void {
		this.dimension = dimension;
		this.sash.setDimenesion(this.dimension);
	}

S
Sandeep Somavarapu 已提交
501
	public focus(): void {
S
Sandeep Somavarapu 已提交
502 503
		if (this.lastFocusedEditor) {
			this.lastFocusedEditor.focus();
S
Sandeep Somavarapu 已提交
504 505 506
		}
	}

507 508
	public getControl(): IEditorControl {
		return this.editablePreferencesEditor ? this.editablePreferencesEditor.getControl() : null;
509 510
	}

511 512 513 514 515 516
	public clearInput(): void {
		if (this.editablePreferencesEditor) {
			this.editablePreferencesEditor.clearInput();
		}
	}

S
Sandeep Somavarapu 已提交
517 518 519 520 521 522 523 524 525 526 527 528
	public setEditorVisible(visible: boolean, position: Position): void {
		if (this.editablePreferencesEditor) {
			this.editablePreferencesEditor.setVisible(visible, position);
		}
	}

	public changePosition(position: Position): void {
		if (this.editablePreferencesEditor) {
			this.editablePreferencesEditor.changePosition(position);
		}
	}

S
Sandeep Somavarapu 已提交
529 530 531 532 533 534 535 536 537 538 539
	private getOrCreateEditablePreferencesEditor(editorInput: EditorInput): TPromise<BaseEditor> {
		if (this.editablePreferencesEditor) {
			return TPromise.as(this.editablePreferencesEditor);
		}
		const descriptor = Registry.as<IEditorRegistry>(EditorExtensions.Editors).getEditor(editorInput);
		return this.instantiationService.createInstance(<EditorDescriptor>descriptor)
			.then((editor: BaseEditor) => {
				this.editablePreferencesEditor = editor;
				this.editablePreferencesEditor.create(new Builder(this.editablePreferencesEditorContainer));
				this.editablePreferencesEditor.setVisible(true);
				(<CodeEditor>this.editablePreferencesEditor.getControl()).onDidFocusEditor(() => this.lastFocusedEditor = this.editablePreferencesEditor);
S
Sandeep Somavarapu 已提交
540
				this.lastFocusedEditor = this.editablePreferencesEditor;
S
Sandeep Somavarapu 已提交
541 542 543 544
				return editor;
			});
	}

545
	private updateInput(editor: BaseEditor, input: EditorInput, editorContributionId: string, associatedPreferencesModelUri: URI, options: EditorOptions): TPromise<IPreferencesRenderer<ISetting>> {
S
Sandeep Somavarapu 已提交
546
		return editor.setInput(input, options)
S
Sandeep Somavarapu 已提交
547
			.then(() => (<CodeEditor>editor.getControl()).getContribution<ISettingsEditorContribution>(editorContributionId).updatePreferencesRenderer(associatedPreferencesModelUri));
S
Sandeep Somavarapu 已提交
548 549
	}

550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
	private createSash(parentElement: HTMLElement): void {
		this.sash = this._register(new VSash(parentElement, 220));
		this._register(this.sash.onPositionChange(position => this.dolayout(position)));
	}

	private dolayout(splitPoint: number): void {
		if (!this.editablePreferencesEditor || !this.dimension) {
			return;
		}
		const masterEditorWidth = this.dimension.width - splitPoint;
		const detailsEditorWidth = this.dimension.width - masterEditorWidth;

		this.defaultPreferencesEditorContainer.style.width = `${detailsEditorWidth}px`;
		this.defaultPreferencesEditorContainer.style.height = `${this.dimension.height}px`;
		this.defaultPreferencesEditorContainer.style.left = '0px';

		this.editablePreferencesEditorContainer.style.width = `${masterEditorWidth}px`;
		this.editablePreferencesEditorContainer.style.height = `${this.dimension.height}px`;
		this.editablePreferencesEditorContainer.style.left = `${splitPoint}px`;

		this.defaultPreferencesEditor.layout(new Dimension(detailsEditorWidth, this.dimension.height));
		this.editablePreferencesEditor.layout(new Dimension(masterEditorWidth, this.dimension.height));
	}

	private disposeEditors(): void {
		if (this.defaultPreferencesEditor) {
			this.defaultPreferencesEditor.dispose();
			this.defaultPreferencesEditor = null;
		}
		if (this.editablePreferencesEditor) {
			this.editablePreferencesEditor.dispose();
			this.editablePreferencesEditor = null;
		}
	}

	public dispose(): void {
		this.disposeEditors();
S
Sandeep Somavarapu 已提交
587
		super.dispose();
588 589 590
	}
}

591 592 593 594 595
export class EditableSettingsEditor extends BaseTextEditor {

	public static ID: string = 'workbench.editor.settingsEditor';

	private modelDisposables: IDisposable[] = [];
S
Sandeep Somavarapu 已提交
596
	private saveDelayer: Delayer<void>;
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612

	constructor(
		@ITelemetryService telemetryService: ITelemetryService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IInstantiationService instantiationService: IInstantiationService,
		@IStorageService storageService: IStorageService,
		@ITextResourceConfigurationService configurationService: ITextResourceConfigurationService,
		@IThemeService themeService: IThemeService,
		@IPreferencesService private preferencesService: IPreferencesService,
		@IModelService private modelService: IModelService,
		@IModeService modeService: IModeService,
		@ITextFileService textFileService: ITextFileService,
		@IEditorGroupService editorGroupService: IEditorGroupService
	) {
		super(EditableSettingsEditor.ID, telemetryService, instantiationService, storageService, configurationService, themeService, modeService, textFileService, editorGroupService);
		this._register({ dispose: () => dispose(this.modelDisposables) });
613
		this.saveDelayer = new Delayer<void>(1000);
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
	}

	protected createEditor(parent: Builder): void {
		super.createEditor(parent);

		const codeEditor = getCodeEditor(this);
		if (codeEditor) {
			this._register(codeEditor.onDidChangeModel(() => this.onDidModelChange()));
		}
	}

	protected getAriaLabel(): string {
		const input = this.input;
		const inputName = input && input.getName();

		let ariaLabel: string;
		if (inputName) {
			ariaLabel = nls.localize('fileEditorWithInputAriaLabel', "{0}. Text file editor.", inputName);
		} else {
			ariaLabel = nls.localize('fileEditorAriaLabel', "Text file editor.");
		}

		return ariaLabel;
	}

	setInput(input: EditorInput, options: EditorOptions): TPromise<void> {
		return super.setInput(input, options)
			.then(() => this.input.resolve()
				.then(editorModel => editorModel.load())
				.then(editorModel => this.getControl().setModel((<ResourceEditorModel>editorModel).textEditorModel)));
	}

646
	clearInput(): void {
647
		this.modelDisposables = dispose(this.modelDisposables);
648
		super.clearInput();
649 650
	}

651 652 653 654 655 656 657 658
	private onDidModelChange(): void {
		this.modelDisposables = dispose(this.modelDisposables);
		const model = getCodeEditor(this).getModel();
		if (model) {
			this.preferencesService.createPreferencesEditorModel(model.uri)
				.then(preferencesEditorModel => {
					const settingsEditorModel = <SettingsEditorModel>preferencesEditorModel;
					this.modelDisposables.push(settingsEditorModel);
S
Sandeep Somavarapu 已提交
659
					this.modelDisposables.push(model.onDidChangeContent(() => this.saveDelayer.trigger(() => settingsEditorModel.save())));
660 661
				});
		}
662 663 664
	}
}

S
Sandeep Somavarapu 已提交
665
export class DefaultPreferencesEditor extends BaseTextEditor {
666

S
Sandeep Somavarapu 已提交
667
	public static ID: string = 'workbench.editor.defaultPreferences';
668

S
Sandeep Somavarapu 已提交
669 670
	constructor(
		@ITelemetryService telemetryService: ITelemetryService,
S
Sandeep Somavarapu 已提交
671
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
S
Sandeep Somavarapu 已提交
672 673
		@IInstantiationService instantiationService: IInstantiationService,
		@IStorageService storageService: IStorageService,
674
		@ITextResourceConfigurationService configurationService: ITextResourceConfigurationService,
675
		@IThemeService themeService: IThemeService,
S
Sandeep Somavarapu 已提交
676 677
		@IPreferencesService private preferencesService: IPreferencesService,
		@IModelService private modelService: IModelService,
678
		@IModeService modeService: IModeService,
B
Benjamin Pasero 已提交
679 680
		@ITextFileService textFileService: ITextFileService,
		@IEditorGroupService editorGroupService: IEditorGroupService
S
Sandeep Somavarapu 已提交
681
	) {
B
Benjamin Pasero 已提交
682
		super(DefaultPreferencesEditor.ID, telemetryService, instantiationService, storageService, configurationService, themeService, modeService, textFileService, editorGroupService);
S
Sandeep Somavarapu 已提交
683 684
	}

685
	public createEditorControl(parent: Builder, configuration: IEditorOptions): editorCommon.IEditor {
686
		return this.instantiationService.createInstance(DefaultPreferencesCodeEditor, parent.getHTMLElement(), configuration);
687 688
	}

689
	protected getConfigurationOverrides(): IEditorOptions {
690
		const options = super.getConfigurationOverrides();
691
		options.readOnly = true;
692
		if (this.input) {
S
Sandeep Somavarapu 已提交
693 694 695 696 697
			options.lineNumbers = 'off';
			options.renderLineHighlight = 'none';
			options.scrollBeyondLastLine = false;
			options.folding = false;
			options.renderWhitespace = 'none';
698
			options.wordWrap = 'on';
S
Sandeep Somavarapu 已提交
699
			options.renderIndentGuides = false;
S
Sandeep Somavarapu 已提交
700
			options.rulers = [];
S
Sandeep Somavarapu 已提交
701
			options.glyphMargin = true;
S
Sandeep Somavarapu 已提交
702 703 704
			options.minimap = {
				enabled: false
			};
S
Sandeep Somavarapu 已提交
705
		}
S
Sandeep Somavarapu 已提交
706 707 708
		return options;
	}

S
Sandeep Somavarapu 已提交
709 710
	setInput(input: DefaultPreferencesEditorInput, options: EditorOptions): TPromise<void> {
		return super.setInput(input, options)
711
			.then(() => this.input.resolve()
S
Sandeep Somavarapu 已提交
712 713
				.then(editorModel => editorModel.load())
				.then(editorModel => this.getControl().setModel((<ResourceEditorModel>editorModel).textEditorModel)));
S
Sandeep Somavarapu 已提交
714 715 716
	}

	public layout(dimension: Dimension) {
717
		this.getControl().layout(dimension);
S
Sandeep Somavarapu 已提交
718 719
	}

720 721 722
	protected getAriaLabel(): string {
		return nls.localize('preferencesAriaLabel', "Default preferences. Readonly text editor.");
	}
S
Sandeep Somavarapu 已提交
723 724
}

S
Sandeep Somavarapu 已提交
725
class DefaultPreferencesCodeEditor extends CodeEditor {
S
Sandeep Somavarapu 已提交
726 727 728

	protected _getContributions(): IEditorContributionCtor[] {
		let contributions = super._getContributions();
S
Sandeep Somavarapu 已提交
729
		let skipContributions = [FoldingController.prototype, SelectionHighlighter.prototype, FindController.prototype];
S
Sandeep Somavarapu 已提交
730
		contributions = contributions.filter(c => skipContributions.indexOf(c.prototype) === -1);
731 732
		contributions.push(DefaultSettingsEditorContribution);
		return contributions;
S
Sandeep Somavarapu 已提交
733
	}
734

S
Sandeep Somavarapu 已提交
735 736 737 738
}

interface ISettingsEditorContribution extends editorCommon.IEditorContribution {

S
Sandeep Somavarapu 已提交
739
	updatePreferencesRenderer(associatedPreferencesModelUri: URI): TPromise<IPreferencesRenderer<ISetting>>;
S
Sandeep Somavarapu 已提交
740

S
Sandeep Somavarapu 已提交
741 742
}

S
Sandeep Somavarapu 已提交
743
abstract class AbstractSettingsEditorContribution extends Disposable {
S
Sandeep Somavarapu 已提交
744

S
Sandeep Somavarapu 已提交
745
	private preferencesRendererCreationPromise: TPromise<IPreferencesRenderer<ISetting>>;
S
Sandeep Somavarapu 已提交
746

S
Sandeep Somavarapu 已提交
747 748
	constructor(protected editor: ICodeEditor,
		@IInstantiationService protected instantiationService: IInstantiationService,
S
Sandeep Somavarapu 已提交
749 750
		@IPreferencesService protected preferencesService: IPreferencesService,
		@IWorkspaceContextService protected workspaceContextService: IWorkspaceContextService
S
Sandeep Somavarapu 已提交
751 752
	) {
		super();
753
		this._register(this.editor.onDidChangeModel(() => this._onModelChanged()));
S
Sandeep Somavarapu 已提交
754 755
	}

S
Sandeep Somavarapu 已提交
756
	updatePreferencesRenderer(associatedPreferencesModelUri: URI): TPromise<IPreferencesRenderer<ISetting>> {
S
Sandeep Somavarapu 已提交
757 758
		if (!this.preferencesRendererCreationPromise) {
			this.preferencesRendererCreationPromise = this._createPreferencesRenderer();
759 760
		}

S
Sandeep Somavarapu 已提交
761 762 763 764 765 766
		if (this.preferencesRendererCreationPromise) {
			return this._hasAssociatedPreferencesModelChanged(associatedPreferencesModelUri)
				.then(changed => changed ? this._updatePreferencesRenderer(associatedPreferencesModelUri) : this.preferencesRendererCreationPromise);
		}

		return TPromise.as(null);
767 768
	}

769 770
	private _onModelChanged(): void {
		const model = this.editor.getModel();
S
Sandeep Somavarapu 已提交
771
		this.disposePreferencesRenderer();
772
		if (model) {
S
Sandeep Somavarapu 已提交
773
			this.preferencesRendererCreationPromise = this._createPreferencesRenderer();
774 775 776
		}
	}

777
	private _hasAssociatedPreferencesModelChanged(associatedPreferencesModelUri: URI): TPromise<boolean> {
S
Sandeep Somavarapu 已提交
778
		return this.preferencesRendererCreationPromise.then(preferencesRenderer => {
779 780 781 782 783 784 785
			return !(preferencesRenderer && preferencesRenderer.associatedPreferencesModel && preferencesRenderer.associatedPreferencesModel.uri.fsPath === associatedPreferencesModelUri.fsPath);
		});
	}

	private _updatePreferencesRenderer(associatedPreferencesModelUri: URI): TPromise<IPreferencesRenderer<ISetting>> {
		return this.preferencesService.createPreferencesEditorModel<ISetting>(associatedPreferencesModelUri)
			.then(associatedPreferencesEditorModel => {
S
Sandeep Somavarapu 已提交
786
				return this.preferencesRendererCreationPromise.then(preferencesRenderer => {
787 788 789 790 791 792 793 794 795
					if (preferencesRenderer) {
						if (preferencesRenderer.associatedPreferencesModel) {
							preferencesRenderer.associatedPreferencesModel.dispose();
						}
						preferencesRenderer.associatedPreferencesModel = associatedPreferencesEditorModel;
					}
					return preferencesRenderer;
				});
			});
S
Sandeep Somavarapu 已提交
796 797
	}

798
	private disposePreferencesRenderer(): void {
S
Sandeep Somavarapu 已提交
799 800
		if (this.preferencesRendererCreationPromise) {
			this.preferencesRendererCreationPromise.then(preferencesRenderer => {
S
Sandeep Somavarapu 已提交
801 802 803 804 805 806 807
				if (preferencesRenderer) {
					if (preferencesRenderer.associatedPreferencesModel) {
						preferencesRenderer.associatedPreferencesModel.dispose();
					}
					preferencesRenderer.dispose();
				}
			});
S
Sandeep Somavarapu 已提交
808
			this.preferencesRendererCreationPromise = TPromise.as(null);
S
Sandeep Somavarapu 已提交
809
		}
810 811 812 813
	}

	dispose() {
		this.disposePreferencesRenderer();
S
Sandeep Somavarapu 已提交
814
		super.dispose();
815
	}
S
Sandeep Somavarapu 已提交
816 817

	protected abstract _createPreferencesRenderer(): TPromise<IPreferencesRenderer<ISetting>>;
818 819
}

S
Sandeep Somavarapu 已提交
820
class DefaultSettingsEditorContribution extends AbstractSettingsEditorContribution implements ISettingsEditorContribution {
821

S
Sandeep Somavarapu 已提交
822
	static ID: string = 'editor.contrib.defaultsettings';
823

S
Sandeep Somavarapu 已提交
824 825 826
	getId(): string {
		return DefaultSettingsEditorContribution.ID;
	}
S
Sandeep Somavarapu 已提交
827

S
Sandeep Somavarapu 已提交
828 829 830
	protected _createPreferencesRenderer(): TPromise<IPreferencesRenderer<ISetting>> {
		return this.preferencesService.createPreferencesEditorModel(this.editor.getModel().uri)
			.then(editorModel => {
S
Sandeep Somavarapu 已提交
831
				if (editorModel instanceof DefaultSettingsEditorModel && this.editor.getModel()) {
S
Sandeep Somavarapu 已提交
832 833 834 835 836 837
					const preferencesRenderer = this.instantiationService.createInstance(DefaultSettingsRenderer, this.editor, editorModel);
					preferencesRenderer.render();
					return preferencesRenderer;
				}
				return null;
			});
S
Sandeep Somavarapu 已提交
838
	}
S
Sandeep Somavarapu 已提交
839 840 841 842 843 844
}

@editorContribution
class SettingsEditorContribution extends AbstractSettingsEditorContribution implements ISettingsEditorContribution {

	static ID: string = 'editor.contrib.settings';
S
Sandeep Somavarapu 已提交
845

846 847 848 849
	getId(): string {
		return SettingsEditorContribution.ID;
	}

S
Sandeep Somavarapu 已提交
850
	protected _createPreferencesRenderer(): TPromise<IPreferencesRenderer<ISetting>> {
S
Sandeep Somavarapu 已提交
851 852 853
		if (this.isSettingsModel()) {
			return TPromise.join<any>([this.preferencesService.createPreferencesEditorModel(this.preferencesService.defaultSettingsResource), this.preferencesService.createPreferencesEditorModel(this.editor.getModel().uri)])
				.then(([defaultSettingsModel, settingsModel]) => {
S
Sandeep Somavarapu 已提交
854
					if (settingsModel instanceof SettingsEditorModel && this.editor.getModel()) {
S
Sandeep Somavarapu 已提交
855 856 857 858 859 860 861 862
						switch (settingsModel.configurationTarget) {
							case ConfigurationTarget.USER:
								return this.instantiationService.createInstance(UserSettingsRenderer, this.editor, settingsModel, defaultSettingsModel);
							case ConfigurationTarget.WORKSPACE:
								return this.instantiationService.createInstance(WorkspaceSettingsRenderer, this.editor, settingsModel, defaultSettingsModel);
							case ConfigurationTarget.FOLDER:
								return this.instantiationService.createInstance(FolderSettingsRenderer, this.editor, settingsModel, defaultSettingsModel);
						}
863
					}
S
Sandeep Somavarapu 已提交
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
					return null;
				})
				.then(preferencesRenderer => {
					if (preferencesRenderer) {
						preferencesRenderer.render();
					}
					return preferencesRenderer;
				});
		}
		return null;
	}

	private isSettingsModel(): boolean {
		const model = this.editor.getModel();
		if (!model) {
			return false;
		}

		if (this.preferencesService.userSettingsResource && this.preferencesService.userSettingsResource.fsPath === model.uri.fsPath) {
			return true;
		}

		if (this.preferencesService.workspaceSettingsResource && this.preferencesService.workspaceSettingsResource.fsPath === model.uri.fsPath) {
			return true;
		}

S
Sandeep Somavarapu 已提交
890
		for (const folder of this.workspaceContextService.getWorkspace().folders) {
891
			const folderSettingsResource = this.preferencesService.getFolderSettingsResource(folder.uri);
892 893
			if (folderSettingsResource && folderSettingsResource.fsPath === model.uri.fsPath) {
				return true;
S
Sandeep Somavarapu 已提交
894 895 896 897
			}
		}

		return false;
S
Sandeep Somavarapu 已提交
898 899
	}

900 901
}

S
Sandeep Somavarapu 已提交
902
abstract class SettingsCommand extends Command {
903

S
Sandeep Somavarapu 已提交
904
	protected getPreferencesEditor(accessor: ServicesAccessor): PreferencesEditor {
905
		const activeEditor = accessor.get(IWorkbenchEditorService).getActiveEditor();
S
Sandeep Somavarapu 已提交
906 907
		if (activeEditor instanceof PreferencesEditor) {
			return activeEditor;
908 909
		}
		return null;
S
Sandeep Somavarapu 已提交
910

911
	}
S
Sandeep Somavarapu 已提交
912

913
}
S
Sandeep Somavarapu 已提交
914 915 916 917 918 919 920 921
class StartSearchDefaultSettingsCommand extends SettingsCommand {

	public runCommand(accessor: ServicesAccessor, args: any): void {
		const preferencesEditor = this.getPreferencesEditor(accessor);
		if (preferencesEditor) {
			preferencesEditor.focusSearch();
		}
	}
922

S
Sandeep Somavarapu 已提交
923
}
A
Alex Dima 已提交
924
const command = new StartSearchDefaultSettingsCommand({
S
Sandeep Somavarapu 已提交
925 926
	id: SETTINGS_EDITOR_COMMAND_SEARCH,
	precondition: ContextKeyExpr.and(CONTEXT_SETTINGS_EDITOR),
927
	kbOpts: { primary: KeyMod.CtrlCmd | KeyCode.KEY_F }
A
Alex Dima 已提交
928 929
});
KeybindingsRegistry.registerCommandAndKeybindingRule(command.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));
S
Sandeep Somavarapu 已提交
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946

class FocusSettingsFileEditorCommand extends SettingsCommand {

	public runCommand(accessor: ServicesAccessor, args: any): void {
		const preferencesEditor = this.getPreferencesEditor(accessor);
		if (preferencesEditor) {
			preferencesEditor.focusSettingsFileEditor();
		}
	}

}
const focusSettingsFileEditorCommand = new FocusSettingsFileEditorCommand({
	id: SETTINGS_EDITOR_COMMAND_FOCUS_FILE,
	precondition: CONTEXT_SETTINGS_SEARCH_FOCUS,
	kbOpts: { primary: KeyCode.DownArrow }
});
KeybindingsRegistry.registerCommandAndKeybindingRule(focusSettingsFileEditorCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));