preferencesEditor.ts 41.5 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, SETTINGS_EDITOR_COMMAND_CLEAR_SEARCH_RESULTS, SETTINGS_EDITOR_COMMAND_FOCUS_NEXT_SETTING, SETTINGS_EDITOR_COMMAND_FOCUS_PREVIOUS_SETTING
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 144
		this._register(this.searchWidget.onFocus(() => this.lastFocusedWidget = this.searchWidget));
		this.lastFocusedWidget = this.searchWidget;
145

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

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

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

S
Sandeep Somavarapu 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	public clearSearchResults(): void {
		if (this.searchWidget) {
			this.searchWidget.clear();
		}
	}

	public focusNextResult(): void {
		if (this.preferencesRenderers) {
			this.preferencesRenderers.focusNextPreference(true);
		}
	}

	public focusPreviousResult(): void {
		if (this.preferencesRenderers) {
			this.preferencesRenderers.focusNextPreference(false);
		}
	}

176
	public setInput(newInput: PreferencesEditorInput, options?: EditorOptions): TPromise<void> {
S
Sandeep Somavarapu 已提交
177
		this.defaultSettingsEditorContextKey.set(true);
178
		const oldInput = <PreferencesEditorInput>this.input;
179
		return super.setInput(newInput, options).then(() => this.updateInput(oldInput, newInput, options));
180 181 182
	}

	public layout(dimension: Dimension): void {
S
Sandeep Somavarapu 已提交
183 184
		DOM.toggleClass(this.headerContainer, 'vertical-layout', dimension.width < 700);
		this.searchWidget.layout(dimension);
185 186 187 188 189
		const headerHeight = DOM.getTotalHeight(this.headerContainer);
		this.sideBySidePreferencesWidget.layout(new Dimension(dimension.width, dimension.height - headerHeight));
	}

	public getControl(): IEditorControl {
190
		return this.sideBySidePreferencesWidget.getControl();
191 192 193
	}

	public focus(): void {
S
Sandeep Somavarapu 已提交
194 195 196
		if (this.lastFocusedWidget) {
			this.lastFocusedWidget.focus();
		}
197 198
	}

199
	public focusSearch(filter?: string): void {
R
Rob Lourens 已提交
200
		if (filter) {
201 202
			this.searchWidget.setValue(filter);
		}
R
Rob Lourens 已提交
203

S
Sandeep Somavarapu 已提交
204 205 206
		this.searchWidget.focus();
	}

S
Sandeep Somavarapu 已提交
207 208 209 210 211 212
	public focusSettingsFileEditor(): void {
		if (this.sideBySidePreferencesWidget) {
			this.sideBySidePreferencesWidget.focus();
		}
	}

213
	public clearInput(): void {
S
Sandeep Somavarapu 已提交
214
		this.defaultSettingsEditorContextKey.set(false);
215 216 217 218
		this.sideBySidePreferencesWidget.clearInput();
		super.clearInput();
	}

S
Sandeep Somavarapu 已提交
219 220 221 222 223 224 225 226 227 228
	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);
	}

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

233
		return this.sideBySidePreferencesWidget.setInput(<DefaultPreferencesEditorInput>newInput.details, <EditorInput>newInput.master, options).then(({ defaultPreferencesRenderer, editablePreferencesRenderer }) => {
234 235
			this.preferencesRenderers.defaultPreferencesRenderer = defaultPreferencesRenderer;
			this.preferencesRenderers.editablePreferencesRenderer = editablePreferencesRenderer;
S
Sandeep Somavarapu 已提交
236
			this.filterPreferences(this.searchWidget.getValue());
237 238 239
		});
	}

240
	private getSettingsConfigurationTarget(resource: URI): ConfigurationTarget {
241 242 243
		if (this.preferencesService.userSettingsResource.fsPath === resource.fsPath) {
			return ConfigurationTarget.USER;
		}
244 245 246

		const workspaceSettingsResource = this.preferencesService.workspaceSettingsResource;
		if (workspaceSettingsResource && workspaceSettingsResource.fsPath === resource.fsPath) {
247 248
			return ConfigurationTarget.WORKSPACE;
		}
249

S
Sandeep Somavarapu 已提交
250
		if (this.workspaceContextService.getWorkspaceFolder(resource)) {
251 252
			return ConfigurationTarget.FOLDER;
		}
253

254
		return null;
255 256
	}

S
Sandeep Somavarapu 已提交
257 258 259 260 261 262 263 264
	private getSettingsConfigurationTargetUri(resource: URI): URI {
		if (this.preferencesService.userSettingsResource.fsPath === resource.fsPath) {
			return resource;
		}
		if (this.preferencesService.workspaceSettingsResource.fsPath === resource.fsPath) {
			return resource;
		}

265 266
		const workspaceFolder = this.workspaceContextService.getWorkspaceFolder(resource);
		return workspaceFolder ? workspaceFolder.uri : null;
S
Sandeep Somavarapu 已提交
267 268
	}

S
Sandeep Somavarapu 已提交
269
	private onWorkspaceFoldersChanged(): void {
S
Sandeep Somavarapu 已提交
270 271 272 273 274 275 276 277 278
		if (this.input) {
			const settingsResource = toResource((<PreferencesEditorInput>this.input).master);
			const targetResource = this.getSettingsConfigurationTargetUri(settingsResource);
			if (!targetResource) {
				this.switchSettings(this.preferencesService.userSettingsResource);
			}
		}
	}

279 280 281 282 283 284 285 286 287 288
	private onWorkbenchStateChanged(): void {
		if (this.input) {
			const settingsResource = toResource((<PreferencesEditorInput>this.input).master);
			const target = this.getSettingsConfigurationTarget(settingsResource);
			if (target !== ConfigurationTarget.USER) {
				this.switchSettings(this.preferencesService.userSettingsResource);
			}
		}
	}

289
	private switchSettings(resource: URI): void {
290 291 292 293
		// Focus the editor if this editor is not active editor
		if (this.editorService.getActiveEditor() !== this) {
			this.focus();
		}
294
		const promise = this.input.isDirty() ? this.input.save() : TPromise.as(true);
295
		promise.done(value => this.preferencesService.switchSettings(this.getSettingsConfigurationTarget(resource), resource));
296 297 298
	}

	private filterPreferences(filter: string) {
299 300 301 302 303
		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);
304
		}
305
		this.delayedFilterLogging.trigger(() => this.reportFilteringUsed(filter));
306 307 308
	}

	private showSearchResultsMessage(count: number): string {
S
Sandeep Somavarapu 已提交
309
		return count === 0 ? nls.localize('noSettingsFound', "No Results") :
310 311 312 313 314
			count === 1 ? nls.localize('oneSettingFound', "1 Setting matched") :
				nls.localize('settingsFound', "{0} Settings matched", count);
	}

	private reportFilteringUsed(filter: string): void {
315 316 317 318 319 320 321 322
		if (filter) {
			let data = {
				filter,
				emptyFilters: this.getLatestEmptyFiltersForTelemetry()
			};
			this.latestEmptyFilters = [];
			this.telemetryService.publicLog('defaultSettings.filter', data);
		}
323 324
	}

325 326 327 328 329 330 331 332
	/**
	 * 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);
	}
333 334
}

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
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();
	}
}

368
class PreferencesRenderers extends Disposable {
369

370 371
	private _defaultPreferencesRenderer: IPreferencesRenderer<ISetting>;
	private _editablePreferencesRenderer: IPreferencesRenderer<ISetting>;
372
	private _settingsNavigator: SettingsNavigator;
373 374 375 376 377 378 379 380

	private _disposables: IDisposable[] = [];

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

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

S
Sandeep Somavarapu 已提交
384
			this._disposables = dispose(this._disposables);
S
Sandeep Somavarapu 已提交
385 386 387 388 389 390

			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 已提交
391
		}
392 393 394 395 396 397 398
	}

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

	public filterPreferences(filter: string): number {
S
Sandeep Somavarapu 已提交
399 400 401 402 403 404
		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);
405
		this._settingsNavigator = new SettingsNavigator(filter ? consolidatedSettings : []);
S
Sandeep Somavarapu 已提交
406

407
		return consolidatedSettings.length;
408 409
	}

410
	public focusNextPreference(forward: boolean = true) {
411
		const setting = forward ? this._settingsNavigator.next() : this._settingsNavigator.previous();
412 413 414 415
		this._focusPreference(setting, this._defaultPreferencesRenderer);
		this._focusPreference(setting, this._editablePreferencesRenderer);
	}

S
Sandeep Somavarapu 已提交
416 417 418 419 420 421
	private _getAllPreferences(preferencesRenderer: IPreferencesRenderer<ISetting>): ISettingsGroup[] {
		return preferencesRenderer ? (<ISettingsEditorModel>preferencesRenderer.preferencesModel).settingsGroups : [];
	}

	private _filterPreferences(filter: string, preferencesRenderer: IPreferencesRenderer<ISetting>): IFilterResult {
		let filterResult = null;
422
		if (preferencesRenderer) {
S
Sandeep Somavarapu 已提交
423
			filterResult = filter ? (<ISettingsEditorModel>preferencesRenderer.preferencesModel).filterSettings(filter) : null;
424 425
			preferencesRenderer.filterPreferences(filterResult);
		}
S
Sandeep Somavarapu 已提交
426
		return filterResult;
427 428 429 430 431 432 433 434 435 436 437 438 439 440
	}

	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 已提交
441 442 443 444 445 446
	private _updatePreference(key: string, value: any, source: ISetting, preferencesRenderer: IPreferencesRenderer<ISetting>): void {
		if (preferencesRenderer) {
			preferencesRenderer.updatePreference(key, value, source);
		}
	}

447 448 449 450 451 452 453 454
	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[] = [];
455 456
		for (const group of settingsGroups) {
			for (const section of group.sections) {
457
				settings.push(...section.settings);
458 459
			}
		}
460
		return settings;
461
	}
462 463 464 465 466

	public dispose(): void {
		dispose(this._disposables);
		super.dispose();
	}
467 468
}

469
class SideBySidePreferencesWidget extends Widget {
470 471 472 473

	private dimension: Dimension;

	private defaultPreferencesEditor: DefaultPreferencesEditor;
S
Sandeep Somavarapu 已提交
474
	private editablePreferencesEditor: BaseEditor;
475 476 477
	private defaultPreferencesEditorContainer: HTMLElement;
	private editablePreferencesEditorContainer: HTMLElement;

S
Sandeep Somavarapu 已提交
478 479 480 481 482
	private _onFocus: Emitter<void> = new Emitter<void>();
	readonly onFocus: Event<void> = this._onFocus.event;

	private lastFocusedEditor: BaseEditor;

483 484
	private sash: VSash;

485
	constructor(parent: HTMLElement, @IInstantiationService private instantiationService: IInstantiationService, @IThemeService private themeService: IThemeService) {
486 487 488 489 490 491 492 493 494 495
		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';
496
		this.defaultPreferencesEditor = this._register(this.instantiationService.createInstance(DefaultPreferencesEditor));
497
		this.defaultPreferencesEditor.create(new Builder(this.defaultPreferencesEditorContainer));
S
Sandeep Somavarapu 已提交
498
		this.defaultPreferencesEditor.setVisible(true);
S
Sandeep Somavarapu 已提交
499
		(<CodeEditor>this.defaultPreferencesEditor.getControl()).onDidFocusEditor(() => this.lastFocusedEditor = this.defaultPreferencesEditor);
500 501 502

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

504 505 506 507 508 509 510 511 512
		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 已提交
513 514 515

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

518
	public setInput(defaultPreferencesEditorInput: DefaultPreferencesEditorInput, editablePreferencesEditorInput: EditorInput, options?: EditorOptions): TPromise<{ defaultPreferencesRenderer: IPreferencesRenderer<ISetting>, editablePreferencesRenderer: IPreferencesRenderer<ISetting> }> {
S
Sandeep Somavarapu 已提交
519 520 521 522 523 524 525
		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 }));
			});
526 527 528 529 530 531 532
	}

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

S
Sandeep Somavarapu 已提交
533
	public focus(): void {
S
Sandeep Somavarapu 已提交
534 535
		if (this.lastFocusedEditor) {
			this.lastFocusedEditor.focus();
S
Sandeep Somavarapu 已提交
536 537 538
		}
	}

539 540
	public getControl(): IEditorControl {
		return this.editablePreferencesEditor ? this.editablePreferencesEditor.getControl() : null;
541 542
	}

543 544 545 546 547 548
	public clearInput(): void {
		if (this.editablePreferencesEditor) {
			this.editablePreferencesEditor.clearInput();
		}
	}

S
Sandeep Somavarapu 已提交
549 550 551 552 553 554 555 556 557 558 559 560
	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 已提交
561 562 563 564 565 566 567 568 569 570 571
	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 已提交
572
				this.lastFocusedEditor = this.editablePreferencesEditor;
S
Sandeep Somavarapu 已提交
573 574 575 576
				return editor;
			});
	}

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

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
	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 已提交
619
		super.dispose();
620 621 622
	}
}

623 624 625 626 627
export class EditableSettingsEditor extends BaseTextEditor {

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

	private modelDisposables: IDisposable[] = [];
S
Sandeep Somavarapu 已提交
628
	private saveDelayer: Delayer<void>;
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644

	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) });
645
		this.saveDelayer = new Delayer<void>(1000);
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
	}

	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)));
	}

678
	clearInput(): void {
679
		this.modelDisposables = dispose(this.modelDisposables);
680
		super.clearInput();
681 682
	}

683 684 685 686 687 688 689 690
	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 已提交
691
					this.modelDisposables.push(model.onDidChangeContent(() => this.saveDelayer.trigger(() => settingsEditorModel.save())));
692 693
				});
		}
694 695 696
	}
}

S
Sandeep Somavarapu 已提交
697
export class DefaultPreferencesEditor extends BaseTextEditor {
698

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

S
Sandeep Somavarapu 已提交
701 702
	constructor(
		@ITelemetryService telemetryService: ITelemetryService,
S
Sandeep Somavarapu 已提交
703
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
S
Sandeep Somavarapu 已提交
704 705
		@IInstantiationService instantiationService: IInstantiationService,
		@IStorageService storageService: IStorageService,
706
		@ITextResourceConfigurationService configurationService: ITextResourceConfigurationService,
707
		@IThemeService themeService: IThemeService,
S
Sandeep Somavarapu 已提交
708 709
		@IPreferencesService private preferencesService: IPreferencesService,
		@IModelService private modelService: IModelService,
710
		@IModeService modeService: IModeService,
B
Benjamin Pasero 已提交
711 712
		@ITextFileService textFileService: ITextFileService,
		@IEditorGroupService editorGroupService: IEditorGroupService
S
Sandeep Somavarapu 已提交
713
	) {
B
Benjamin Pasero 已提交
714
		super(DefaultPreferencesEditor.ID, telemetryService, instantiationService, storageService, configurationService, themeService, modeService, textFileService, editorGroupService);
S
Sandeep Somavarapu 已提交
715 716
	}

717
	public createEditorControl(parent: Builder, configuration: IEditorOptions): editorCommon.IEditor {
718
		return this.instantiationService.createInstance(DefaultPreferencesCodeEditor, parent.getHTMLElement(), configuration);
719 720
	}

721
	protected getConfigurationOverrides(): IEditorOptions {
722
		const options = super.getConfigurationOverrides();
723
		options.readOnly = true;
724
		if (this.input) {
S
Sandeep Somavarapu 已提交
725 726 727 728 729
			options.lineNumbers = 'off';
			options.renderLineHighlight = 'none';
			options.scrollBeyondLastLine = false;
			options.folding = false;
			options.renderWhitespace = 'none';
730
			options.wordWrap = 'on';
S
Sandeep Somavarapu 已提交
731
			options.renderIndentGuides = false;
S
Sandeep Somavarapu 已提交
732
			options.rulers = [];
S
Sandeep Somavarapu 已提交
733
			options.glyphMargin = true;
S
Sandeep Somavarapu 已提交
734 735 736
			options.minimap = {
				enabled: false
			};
S
Sandeep Somavarapu 已提交
737
		}
S
Sandeep Somavarapu 已提交
738 739 740
		return options;
	}

S
Sandeep Somavarapu 已提交
741 742
	setInput(input: DefaultPreferencesEditorInput, options: EditorOptions): TPromise<void> {
		return super.setInput(input, options)
743
			.then(() => this.input.resolve()
S
Sandeep Somavarapu 已提交
744 745
				.then(editorModel => editorModel.load())
				.then(editorModel => this.getControl().setModel((<ResourceEditorModel>editorModel).textEditorModel)));
S
Sandeep Somavarapu 已提交
746 747 748
	}

	public layout(dimension: Dimension) {
749
		this.getControl().layout(dimension);
S
Sandeep Somavarapu 已提交
750 751
	}

752 753 754
	protected getAriaLabel(): string {
		return nls.localize('preferencesAriaLabel', "Default preferences. Readonly text editor.");
	}
S
Sandeep Somavarapu 已提交
755 756
}

S
Sandeep Somavarapu 已提交
757
class DefaultPreferencesCodeEditor extends CodeEditor {
S
Sandeep Somavarapu 已提交
758 759 760

	protected _getContributions(): IEditorContributionCtor[] {
		let contributions = super._getContributions();
S
Sandeep Somavarapu 已提交
761
		let skipContributions = [FoldingController.prototype, SelectionHighlighter.prototype, FindController.prototype];
S
Sandeep Somavarapu 已提交
762
		contributions = contributions.filter(c => skipContributions.indexOf(c.prototype) === -1);
763 764
		contributions.push(DefaultSettingsEditorContribution);
		return contributions;
S
Sandeep Somavarapu 已提交
765
	}
766

S
Sandeep Somavarapu 已提交
767 768 769 770
}

interface ISettingsEditorContribution extends editorCommon.IEditorContribution {

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

S
Sandeep Somavarapu 已提交
773 774
}

S
Sandeep Somavarapu 已提交
775
abstract class AbstractSettingsEditorContribution extends Disposable {
S
Sandeep Somavarapu 已提交
776

S
Sandeep Somavarapu 已提交
777
	private preferencesRendererCreationPromise: TPromise<IPreferencesRenderer<ISetting>>;
S
Sandeep Somavarapu 已提交
778

S
Sandeep Somavarapu 已提交
779 780
	constructor(protected editor: ICodeEditor,
		@IInstantiationService protected instantiationService: IInstantiationService,
S
Sandeep Somavarapu 已提交
781 782
		@IPreferencesService protected preferencesService: IPreferencesService,
		@IWorkspaceContextService protected workspaceContextService: IWorkspaceContextService
S
Sandeep Somavarapu 已提交
783 784
	) {
		super();
785
		this._register(this.editor.onDidChangeModel(() => this._onModelChanged()));
S
Sandeep Somavarapu 已提交
786 787
	}

S
Sandeep Somavarapu 已提交
788
	updatePreferencesRenderer(associatedPreferencesModelUri: URI): TPromise<IPreferencesRenderer<ISetting>> {
S
Sandeep Somavarapu 已提交
789 790
		if (!this.preferencesRendererCreationPromise) {
			this.preferencesRendererCreationPromise = this._createPreferencesRenderer();
791 792
		}

S
Sandeep Somavarapu 已提交
793 794 795 796 797 798
		if (this.preferencesRendererCreationPromise) {
			return this._hasAssociatedPreferencesModelChanged(associatedPreferencesModelUri)
				.then(changed => changed ? this._updatePreferencesRenderer(associatedPreferencesModelUri) : this.preferencesRendererCreationPromise);
		}

		return TPromise.as(null);
799 800
	}

801 802
	private _onModelChanged(): void {
		const model = this.editor.getModel();
S
Sandeep Somavarapu 已提交
803
		this.disposePreferencesRenderer();
804
		if (model) {
S
Sandeep Somavarapu 已提交
805
			this.preferencesRendererCreationPromise = this._createPreferencesRenderer();
806 807 808
		}
	}

809
	private _hasAssociatedPreferencesModelChanged(associatedPreferencesModelUri: URI): TPromise<boolean> {
S
Sandeep Somavarapu 已提交
810
		return this.preferencesRendererCreationPromise.then(preferencesRenderer => {
811
			return !(preferencesRenderer && preferencesRenderer.associatedPreferencesModel && preferencesRenderer.associatedPreferencesModel.uri.toString() === associatedPreferencesModelUri.toString());
812 813 814 815 816 817
		});
	}

	private _updatePreferencesRenderer(associatedPreferencesModelUri: URI): TPromise<IPreferencesRenderer<ISetting>> {
		return this.preferencesService.createPreferencesEditorModel<ISetting>(associatedPreferencesModelUri)
			.then(associatedPreferencesEditorModel => {
S
Sandeep Somavarapu 已提交
818
				return this.preferencesRendererCreationPromise.then(preferencesRenderer => {
819 820 821 822 823 824 825 826 827
					if (preferencesRenderer) {
						if (preferencesRenderer.associatedPreferencesModel) {
							preferencesRenderer.associatedPreferencesModel.dispose();
						}
						preferencesRenderer.associatedPreferencesModel = associatedPreferencesEditorModel;
					}
					return preferencesRenderer;
				});
			});
S
Sandeep Somavarapu 已提交
828 829
	}

830
	private disposePreferencesRenderer(): void {
S
Sandeep Somavarapu 已提交
831 832
		if (this.preferencesRendererCreationPromise) {
			this.preferencesRendererCreationPromise.then(preferencesRenderer => {
S
Sandeep Somavarapu 已提交
833 834 835 836 837 838 839
				if (preferencesRenderer) {
					if (preferencesRenderer.associatedPreferencesModel) {
						preferencesRenderer.associatedPreferencesModel.dispose();
					}
					preferencesRenderer.dispose();
				}
			});
S
Sandeep Somavarapu 已提交
840
			this.preferencesRendererCreationPromise = TPromise.as(null);
S
Sandeep Somavarapu 已提交
841
		}
842 843 844 845
	}

	dispose() {
		this.disposePreferencesRenderer();
S
Sandeep Somavarapu 已提交
846
		super.dispose();
847
	}
S
Sandeep Somavarapu 已提交
848 849

	protected abstract _createPreferencesRenderer(): TPromise<IPreferencesRenderer<ISetting>>;
850 851
}

S
Sandeep Somavarapu 已提交
852
class DefaultSettingsEditorContribution extends AbstractSettingsEditorContribution implements ISettingsEditorContribution {
853

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

S
Sandeep Somavarapu 已提交
856 857 858
	getId(): string {
		return DefaultSettingsEditorContribution.ID;
	}
S
Sandeep Somavarapu 已提交
859

S
Sandeep Somavarapu 已提交
860 861 862
	protected _createPreferencesRenderer(): TPromise<IPreferencesRenderer<ISetting>> {
		return this.preferencesService.createPreferencesEditorModel(this.editor.getModel().uri)
			.then(editorModel => {
S
Sandeep Somavarapu 已提交
863
				if (editorModel instanceof DefaultSettingsEditorModel && this.editor.getModel()) {
S
Sandeep Somavarapu 已提交
864 865 866 867 868 869
					const preferencesRenderer = this.instantiationService.createInstance(DefaultSettingsRenderer, this.editor, editorModel);
					preferencesRenderer.render();
					return preferencesRenderer;
				}
				return null;
			});
S
Sandeep Somavarapu 已提交
870
	}
S
Sandeep Somavarapu 已提交
871 872 873 874 875 876
}

@editorContribution
class SettingsEditorContribution extends AbstractSettingsEditorContribution implements ISettingsEditorContribution {

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

878 879 880 881
	getId(): string {
		return SettingsEditorContribution.ID;
	}

S
Sandeep Somavarapu 已提交
882
	protected _createPreferencesRenderer(): TPromise<IPreferencesRenderer<ISetting>> {
S
Sandeep Somavarapu 已提交
883 884 885
		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 已提交
886
					if (settingsModel instanceof SettingsEditorModel && this.editor.getModel()) {
S
Sandeep Somavarapu 已提交
887 888 889 890 891 892 893 894
						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);
						}
895
					}
S
Sandeep Somavarapu 已提交
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
					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 已提交
922
		for (const folder of this.workspaceContextService.getWorkspace().folders) {
923
			const folderSettingsResource = this.preferencesService.getFolderSettingsResource(folder.uri);
924 925
			if (folderSettingsResource && folderSettingsResource.fsPath === model.uri.fsPath) {
				return true;
S
Sandeep Somavarapu 已提交
926 927 928 929
			}
		}

		return false;
S
Sandeep Somavarapu 已提交
930 931
	}

932 933
}

S
Sandeep Somavarapu 已提交
934
abstract class SettingsCommand extends Command {
935

S
Sandeep Somavarapu 已提交
936
	protected getPreferencesEditor(accessor: ServicesAccessor): PreferencesEditor {
937
		const activeEditor = accessor.get(IWorkbenchEditorService).getActiveEditor();
S
Sandeep Somavarapu 已提交
938 939
		if (activeEditor instanceof PreferencesEditor) {
			return activeEditor;
940 941
		}
		return null;
S
Sandeep Somavarapu 已提交
942

943
	}
S
Sandeep Somavarapu 已提交
944

945
}
S
Sandeep Somavarapu 已提交
946 947 948 949 950 951 952 953
class StartSearchDefaultSettingsCommand extends SettingsCommand {

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

S
Sandeep Somavarapu 已提交
955
}
A
Alex Dima 已提交
956
const command = new StartSearchDefaultSettingsCommand({
S
Sandeep Somavarapu 已提交
957 958
	id: SETTINGS_EDITOR_COMMAND_SEARCH,
	precondition: ContextKeyExpr.and(CONTEXT_SETTINGS_EDITOR),
959
	kbOpts: { primary: KeyMod.CtrlCmd | KeyCode.KEY_F }
A
Alex Dima 已提交
960 961
});
KeybindingsRegistry.registerCommandAndKeybindingRule(command.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));
S
Sandeep Somavarapu 已提交
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978

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()));
S
Sandeep Somavarapu 已提交
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029

class ClearSearchResultsCommand extends SettingsCommand {

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

}
const clearSearchResultsCommand = new ClearSearchResultsCommand({
	id: SETTINGS_EDITOR_COMMAND_CLEAR_SEARCH_RESULTS,
	precondition: CONTEXT_SETTINGS_SEARCH_FOCUS,
	kbOpts: { primary: KeyCode.Escape }
});
KeybindingsRegistry.registerCommandAndKeybindingRule(clearSearchResultsCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));

class FocusNextSearchResultCommand extends SettingsCommand {

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

}
const focusNextSearchResultCommand = new FocusNextSearchResultCommand({
	id: SETTINGS_EDITOR_COMMAND_FOCUS_NEXT_SETTING,
	precondition: CONTEXT_SETTINGS_SEARCH_FOCUS,
	kbOpts: { primary: KeyCode.Enter }
});
KeybindingsRegistry.registerCommandAndKeybindingRule(focusNextSearchResultCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));

class FocusPreviousSearchResultCommand extends SettingsCommand {

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

}
const focusPreviousSearchResultCommand = new FocusPreviousSearchResultCommand({
	id: SETTINGS_EDITOR_COMMAND_FOCUS_PREVIOUS_SETTING,
	precondition: CONTEXT_SETTINGS_SEARCH_FOCUS,
	kbOpts: { primary: KeyMod.Shift | KeyCode.Enter }
});
KeybindingsRegistry.registerCommandAndKeybindingRule(focusPreviousSearchResultCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));