preferencesEditor.ts 25.9 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';
S
Sandeep Somavarapu 已提交
12
import { Disposable, IDisposable, dispose } from 'vs/base/common/lifecycle';
13
import { KeyMod, KeyCode } from 'vs/base/common/keyCodes';
S
Sandeep Somavarapu 已提交
14
import { Registry } from 'vs/platform/platform';
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';
18
import { IEditorControl, IEditor } 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, IPreferencesEditorModel,
S
Sandeep Somavarapu 已提交
26
	CONTEXT_SETTINGS_EDITOR, SETTINGS_EDITOR_COMMAND_SEARCH, 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 31
import { ICodeEditor, IEditorContributionCtor } from 'vs/editor/browser/editorBrowser';
import { SearchWidget, SettingsTabsWidget } from 'vs/workbench/parts/preferences/browser/preferencesWidgets';
S
Sandeep Somavarapu 已提交
32 33
import { ContextKeyExpr, IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
import { CommonEditorRegistry, Command } from 'vs/editor/common/editorCommonExtensions';
S
Sandeep Somavarapu 已提交
34 35 36 37
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IThemeService } from 'vs/workbench/services/themes/common/themeService';
import { IModelService } from 'vs/editor/common/services/modelService';
import { IModeService } from 'vs/editor/common/services/modeService';
S
Sandeep Somavarapu 已提交
38 39 40 41
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
42
import { ITextModelResolverService } from 'vs/editor/common/services/resolverService';
43
import { ConfigurationTarget } from 'vs/workbench/services/configuration/common/configurationEditing';
44 45 46
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';
47
import { IPreferencesRenderer, DefaultSettingsRenderer, UserSettingsRenderer, WorkspaceSettingsRenderer } from 'vs/workbench/parts/preferences/browser/preferencesRenderers';
B
Benjamin Pasero 已提交
48 49
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
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';
S
Sandeep Somavarapu 已提交
55

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

59 60 61 62
	getTypeId(): string {
		return PreferencesEditorInput.ID;
	}
}
63

64
export class DefaultPreferencesEditorInput extends ResourceEditorInput {
65
	public static ID = 'workbench.editorinputs.defaultpreferences';
66 67 68 69
	constructor(defaultSettingsResource: URI,
		@ITextModelResolverService textModelResolverService: ITextModelResolverService
	) {
		super(nls.localize('settingsEditorName', "Default Settings"), '', defaultSettingsResource, textModelResolverService);
70 71
	}

S
Sandeep Somavarapu 已提交
72
	getTypeId(): string {
73
		return DefaultPreferencesEditorInput.ID;
S
Sandeep Somavarapu 已提交
74
	}
75

S
Sandeep Somavarapu 已提交
76
	matches(other: any): boolean {
77
		if (!super.matches(other)) {
S
Sandeep Somavarapu 已提交
78
			return false;
79
		}
80
		if (!(other instanceof DefaultPreferencesEditorInput)) {
S
Sandeep Somavarapu 已提交
81 82
			return false;
		}
S
Sandeep Somavarapu 已提交
83 84
		return true;
	}
85
}
86

87 88 89 90
export class PreferencesEditor extends BaseEditor {

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

S
Sandeep Somavarapu 已提交
91
	private defaultSettingsEditorContextKey: IContextKey<boolean>;
92 93 94 95
	private headerContainer: HTMLElement;
	private searchWidget: SearchWidget;
	private settingsTabsWidget: SettingsTabsWidget;
	private sideBySidePreferencesWidget: SideBySidePreferencesWidget;
96
	private preferencesRenderers: PreferencesRenderers;
97 98 99

	private delayedFilterLogging: Delayer<void>;

100 101
	private latestEmptyFilters: string[] = [];

102 103 104 105 106
	constructor(
		@IPreferencesService private preferencesService: IPreferencesService,
		@IEnvironmentService private environmentService: IEnvironmentService,
		@ITelemetryService telemetryService: ITelemetryService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
S
Sandeep Somavarapu 已提交
107
		@IContextKeyService private contextKeyService: IContextKeyService,
108 109 110
		@IInstantiationService private instantiationService: IInstantiationService
	) {
		super(PreferencesEditor.ID, telemetryService);
S
Sandeep Somavarapu 已提交
111
		this.defaultSettingsEditorContextKey = CONTEXT_SETTINGS_EDITOR.bindTo(this.contextKeyService);
112 113 114 115 116 117 118 119 120 121 122
		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'));

		this.searchWidget = this._register(this.instantiationService.createInstance(SearchWidget, this.headerContainer));
		this._register(this.searchWidget.onDidChange(value => this.filterPreferences(value.trim())));
123
		this._register(this.searchWidget.onEnter(value => this.preferencesRenderers.focusNextPreference()));
124 125 126 127 128 129

		this.settingsTabsWidget = this._register(this.instantiationService.createInstance(SettingsTabsWidget, this.headerContainer));
		this._register(this.settingsTabsWidget.onSwitch(() => this.switchSettings()));

		const editorsContainer = DOM.append(parentElement, DOM.$('.preferences-editors-container'));
		this.sideBySidePreferencesWidget = this._register(this.instantiationService.createInstance(SideBySidePreferencesWidget, editorsContainer));
130
		this.preferencesRenderers = this._register(new PreferencesRenderers());
131 132 133
	}

	public setInput(newInput: PreferencesEditorInput, options?: EditorOptions): TPromise<void> {
S
Sandeep Somavarapu 已提交
134
		this.defaultSettingsEditorContextKey.set(true);
135
		const oldInput = <PreferencesEditorInput>this.input;
136
		return super.setInput(newInput, options).then(() => this.updateInput(oldInput, newInput, options));
137 138 139
	}

	public layout(dimension: Dimension): void {
S
Sandeep Somavarapu 已提交
140 141
		DOM.toggleClass(this.headerContainer, 'vertical-layout', dimension.width < 700);
		this.searchWidget.layout(dimension);
142 143 144 145 146
		const headerHeight = DOM.getTotalHeight(this.headerContainer);
		this.sideBySidePreferencesWidget.layout(new Dimension(dimension.width, dimension.height - headerHeight));
	}

	public getControl(): IEditorControl {
147
		return this.sideBySidePreferencesWidget.getControl();
148 149 150
	}

	public focus(): void {
S
Sandeep Somavarapu 已提交
151
		this.sideBySidePreferencesWidget.focus();
152 153
	}

S
Sandeep Somavarapu 已提交
154 155 156 157
	public focusSearch(): void {
		this.searchWidget.focus();
	}

158
	public clearInput(): void {
S
Sandeep Somavarapu 已提交
159
		this.defaultSettingsEditorContextKey.set(false);
160 161 162 163
		this.sideBySidePreferencesWidget.clearInput();
		super.clearInput();
	}

164 165 166 167
	private updateInput(oldInput: PreferencesEditorInput, newInput: PreferencesEditorInput, options?: EditorOptions): TPromise<void> {
		const editablePreferencesUri = toResource(newInput.master);
		this.settingsTabsWidget.show(editablePreferencesUri.toString() === this.preferencesService.userSettingsResource.toString() ? ConfigurationTarget.USER : ConfigurationTarget.WORKSPACE);

168 169 170
		return this.sideBySidePreferencesWidget.setInput(<DefaultPreferencesEditorInput>newInput.details, newInput.master, options).then(({ defaultPreferencesRenderer, editablePreferencesRenderer }) => {
			this.preferencesRenderers.defaultPreferencesRenderer = defaultPreferencesRenderer;
			this.preferencesRenderers.editablePreferencesRenderer = editablePreferencesRenderer;
171 172 173 174 175
			this.filterPreferences(this.searchWidget.value());
		});
	}

	private switchSettings(): void {
176 177 178 179
		// Focus the editor if this editor is not active editor
		if (this.editorService.getActiveEditor() !== this) {
			this.focus();
		}
180 181 182 183 184
		const promise = this.input.isDirty() ? this.input.save() : TPromise.as(true);
		promise.done(value => this.preferencesService.switchSettings());
	}

	private filterPreferences(filter: string) {
185 186 187 188 189
		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);
190
		}
191
		this.delayedFilterLogging.trigger(() => this.reportFilteringUsed(filter));
192 193 194
	}

	private showSearchResultsMessage(count: number): string {
S
Sandeep Somavarapu 已提交
195
		return count === 0 ? nls.localize('noSettingsFound', "No Results") :
196 197 198 199 200
			count === 1 ? nls.localize('oneSettingFound', "1 Setting matched") :
				nls.localize('settingsFound', "{0} Settings matched", count);
	}

	private reportFilteringUsed(filter: string): void {
201 202 203 204 205 206 207 208
		if (filter) {
			let data = {
				filter,
				emptyFilters: this.getLatestEmptyFiltersForTelemetry()
			};
			this.latestEmptyFilters = [];
			this.telemetryService.publicLog('defaultSettings.filter', data);
		}
209 210
	}

211 212 213 214 215 216 217 218
	/**
	 * 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);
	}
219 220 221
}

class PreferencesRenderers extends Disposable {
222

223 224 225 226 227 228 229 230 231 232
	private _defaultPreferencesRenderer: IPreferencesRenderer<ISetting>;
	private _editablePreferencesRenderer: IPreferencesRenderer<ISetting>;

	private _disposables: IDisposable[] = [];

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

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

S
Sandeep Somavarapu 已提交
236 237 238 239 240
			this._disposables = dispose(this._disposables);
			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);
		}
241 242 243 244 245 246 247
	}

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

	public filterPreferences(filter: string): number {
S
Sandeep Somavarapu 已提交
248
		const filterResult = filter ? (<ISettingsEditorModel>this._defaultPreferencesRenderer.preferencesModel).filterSettings(filter) : null;
249 250
		this._filterPreferences(filterResult, this._defaultPreferencesRenderer);
		this._filterPreferences(filterResult, this._editablePreferencesRenderer);
S
Sandeep Somavarapu 已提交
251
		return this._getCount(filterResult ? filterResult.filteredGroups : (this._defaultPreferencesRenderer ? (<ISettingsEditorModel>this._defaultPreferencesRenderer.preferencesModel).settingsGroups : []));
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	}

	public focusNextPreference() {
		const setting = this._defaultPreferencesRenderer.iterator.next();
		this._focusPreference(setting, this._defaultPreferencesRenderer);
		this._focusPreference(setting, this._editablePreferencesRenderer);
	}

	private _filterPreferences(filterResult: IFilterResult, preferencesRenderer: IPreferencesRenderer<ISetting>): void {
		if (preferencesRenderer) {
			preferencesRenderer.filterPreferences(filterResult);
		}
	}

	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 已提交
278 279 280 281 282 283
	private _updatePreference(key: string, value: any, source: ISetting, preferencesRenderer: IPreferencesRenderer<ISetting>): void {
		if (preferencesRenderer) {
			preferencesRenderer.updatePreference(key, value, source);
		}
	}

284
	private _getCount(settingsGroups: ISettingsGroup[]): number {
285 286 287 288 289 290 291 292
		let count = 0;
		for (const group of settingsGroups) {
			for (const section of group.sections) {
				count += section.settings.length;
			}
		}
		return count;
	}
293 294 295 296 297

	public dispose(): void {
		dispose(this._disposables);
		super.dispose();
	}
298 299
}

300
class SideBySidePreferencesWidget extends Widget {
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

	private dimension: Dimension;

	private defaultPreferencesEditor: DefaultPreferencesEditor;
	private defaultPreferencesEditorContainer: HTMLElement;
	private editablePreferencesEditor: BaseEditor;
	private editablePreferencesEditorContainer: HTMLElement;

	private sash: VSash;

	constructor(parent: HTMLElement, @IInstantiationService private instantiationService: IInstantiationService) {
		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';
		this.defaultPreferencesEditor = this.instantiationService.createInstance(DefaultPreferencesEditor);
		this.defaultPreferencesEditor.create(new Builder(this.defaultPreferencesEditorContainer));
S
Sandeep Somavarapu 已提交
324
		this.defaultPreferencesEditor.setVisible(true);
325 326 327 328 329

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

330
	public setInput(defaultPreferencesEditorInput: DefaultPreferencesEditorInput, editablePreferencesEditorInput: EditorInput, options?: EditorOptions): TPromise<{ defaultPreferencesRenderer: IPreferencesRenderer<ISetting>, editablePreferencesRenderer: IPreferencesRenderer<ISetting> }> {
331 332 333
		return this.getOrCreateEditablePreferencesEditor(editablePreferencesEditorInput)
			.then(() => {
				this.dolayout(this.sash.getVerticalSashLeft());
S
Sandeep Somavarapu 已提交
334
				return TPromise.join([this.defaultPreferencesEditor.updateInput(defaultPreferencesEditorInput, options, toResource(editablePreferencesEditorInput)),
335
				this.editablePreferencesEditor.setInput(editablePreferencesEditorInput, options)])
336 337 338 339 340 341
					.then(() => {
						return {
							defaultPreferencesRenderer: (<CodeEditor>this.defaultPreferencesEditor.getControl()).getContribution<DefaultSettingsEditorContribution>(DefaultSettingsEditorContribution.ID).getPreferencesRenderer(),
							editablePreferencesRenderer: (<CodeEditor>this.editablePreferencesEditor.getControl()).getContribution<SettingsEditorContribution>(SettingsEditorContribution.ID).getPreferencesRenderer()
						};
					});
342 343 344 345 346 347 348 349
			});
	}

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

S
Sandeep Somavarapu 已提交
350 351 352 353 354 355
	public focus(): void {
		if (this.editablePreferencesEditor) {
			this.editablePreferencesEditor.focus();
		}
	}

356 357
	public getControl(): IEditorControl {
		return this.editablePreferencesEditor ? this.editablePreferencesEditor.getControl() : null;
358 359
	}

360 361 362 363 364 365
	public clearInput(): void {
		if (this.editablePreferencesEditor) {
			this.editablePreferencesEditor.clearInput();
		}
	}

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
	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);
				return editor;
			});
	}

	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 已提交
417
		super.dispose();
418 419 420
	}
}

S
Sandeep Somavarapu 已提交
421
export class DefaultPreferencesEditor extends BaseTextEditor {
422

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

S
Sandeep Somavarapu 已提交
425 426
	constructor(
		@ITelemetryService telemetryService: ITelemetryService,
S
Sandeep Somavarapu 已提交
427
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
S
Sandeep Somavarapu 已提交
428 429 430 431 432
		@IInstantiationService instantiationService: IInstantiationService,
		@IStorageService storageService: IStorageService,
		@IConfigurationService configurationService: IConfigurationService,
		@IThemeService themeService: IThemeService,
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
S
Sandeep Somavarapu 已提交
433 434
		@IPreferencesService private preferencesService: IPreferencesService,
		@IModelService private modelService: IModelService,
435
		@IModeService modeService: IModeService,
B
Benjamin Pasero 已提交
436 437
		@ITextFileService textFileService: ITextFileService,
		@IEditorGroupService editorGroupService: IEditorGroupService
S
Sandeep Somavarapu 已提交
438
	) {
B
Benjamin Pasero 已提交
439
		super(DefaultPreferencesEditor.ID, telemetryService, instantiationService, storageService, configurationService, themeService, modeService, textFileService, editorGroupService);
S
Sandeep Somavarapu 已提交
440 441
	}

442
	public createEditorControl(parent: Builder, configuration: editorCommon.IEditorOptions): editorCommon.IEditor {
443
		return this.instantiationService.createInstance(DefaultPreferencesCodeEditor, parent.getHTMLElement(), configuration);
444 445
	}

446 447
	protected getConfigurationOverrides(): editorCommon.IEditorOptions {
		const options = super.getConfigurationOverrides();
448
		options.readOnly = true;
449
		if (this.input) {
S
Sandeep Somavarapu 已提交
450 451 452 453 454
			options.lineNumbers = 'off';
			options.renderLineHighlight = 'none';
			options.scrollBeyondLastLine = false;
			options.folding = false;
			options.renderWhitespace = 'none';
455
			options.wordWrap = 'on';
S
Sandeep Somavarapu 已提交
456
			options.renderIndentGuides = false;
S
Sandeep Somavarapu 已提交
457
			options.rulers = [];
S
Sandeep Somavarapu 已提交
458
			options.glyphMargin = true;
S
Sandeep Somavarapu 已提交
459
		}
S
Sandeep Somavarapu 已提交
460 461 462
		return options;
	}

S
Sandeep Somavarapu 已提交
463
	updateInput(input: DefaultPreferencesEditorInput, options: EditorOptions, editablePreferencesUri: URI): TPromise<void> {
464 465 466 467 468 469
		return this.setInput(input, options)
			.then(() => this.input.resolve()
				.then(editorModel => TPromise.join<any>([
					editorModel.load(),
					this.preferencesService.resolvePreferencesEditorModel(editablePreferencesUri)
				]))
S
Sandeep Somavarapu 已提交
470
				.then(([editorModel, preferencesModel]) => (<DefaultPreferencesCodeEditor>this.getControl()).setModels((<ResourceEditorModel>editorModel).textEditorModel, <SettingsEditorModel>preferencesModel)));
S
Sandeep Somavarapu 已提交
471 472 473
	}

	public layout(dimension: Dimension) {
474
		this.getControl().layout(dimension);
S
Sandeep Somavarapu 已提交
475 476
	}

477
	public clearInput(): void {
S
Sandeep Somavarapu 已提交
478
		this.getControl().setModel(null);
479 480
		super.clearInput();
	}
481 482 483 484

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

S
Sandeep Somavarapu 已提交
487
class DefaultPreferencesCodeEditor extends CodeEditor {
S
Sandeep Somavarapu 已提交
488

489
	public settingsModel: IPreferencesEditorModel<ISetting>;
490

S
Sandeep Somavarapu 已提交
491 492
	protected _getContributions(): IEditorContributionCtor[] {
		let contributions = super._getContributions();
S
Sandeep Somavarapu 已提交
493
		let skipContributions = [FoldingController.prototype, SelectionHighlighter.prototype, FindController.prototype];
S
Sandeep Somavarapu 已提交
494
		contributions = contributions.filter(c => skipContributions.indexOf(c.prototype) === -1);
495 496
		contributions.push(DefaultSettingsEditorContribution);
		return contributions;
S
Sandeep Somavarapu 已提交
497
	}
498

S
Sandeep Somavarapu 已提交
499
	setModels(model: editorCommon.IModel, settingsModel: SettingsEditorModel): void {
500 501 502 503 504 505
		this.settingsModel = settingsModel;
		super.setModel(model);
		const renderer = this.getContribution<DefaultSettingsEditorContribution>(DefaultSettingsEditorContribution.ID).getPreferencesRenderer();
		if (renderer) {
			renderer.associatedPreferencesModel = this.settingsModel;
		}
506
	}
S
Sandeep Somavarapu 已提交
507 508
}

509
export abstract class PreferencesEditorContribution<T> extends Disposable implements editorCommon.IEditorContribution {
S
Sandeep Somavarapu 已提交
510

511
	private preferencesRenderer: IPreferencesRenderer<T>;
S
Sandeep Somavarapu 已提交
512

513 514 515
	constructor(protected editor: ICodeEditor,
		@IInstantiationService protected instantiationService: IInstantiationService,
		@IPreferencesService protected preferencesService: IPreferencesService
S
Sandeep Somavarapu 已提交
516 517 518 519 520 521 522
	) {
		super();
		this._register(editor.onDidChangeModel(() => this.onModelChanged()));
	}

	private onModelChanged(): void {
		const model = this.editor.getModel();
523 524
		this.disposePreferencesRenderer();
		if (model) {
525 526 527 528 529
			this.createPreferencesRenderer()
				.then(preferencesRenderer => {
					this.preferencesRenderer = preferencesRenderer;
					if (this.preferencesRenderer) {
						this.preferencesRenderer.render();
530 531 532 533
					}
				});
		}
	}
S
Sandeep Somavarapu 已提交
534

535
	getPreferencesRenderer(): IPreferencesRenderer<T> {
S
Sandeep Somavarapu 已提交
536
		return this.preferencesRenderer;
537 538
	}

539
	protected abstract createPreferencesRenderer(): TPromise<IPreferencesRenderer<T>>
540
	abstract getId(): string;
541 542 543 544 545 546 547 548 549 550 551 552 553 554

	private disposePreferencesRenderer() {
		if (this.preferencesRenderer) {
			this.preferencesRenderer.dispose();
			this.preferencesRenderer = null;
		}
	}

	public dispose() {
		this.disposePreferencesRenderer();
		super.dispose();
	}
}

555
export class DefaultSettingsEditorContribution extends PreferencesEditorContribution<ISetting> implements editorCommon.IEditorContribution {
556 557 558

	static ID: string = 'editor.contrib.defaultsettings';

559 560 561 562
	protected createPreferencesRenderer(): TPromise<IPreferencesRenderer<ISetting>> {
		return this.preferencesService.resolvePreferencesEditorModel(this.editor.getModel().uri)
			.then(editorModel => {
				if (editorModel instanceof DefaultSettingsEditorModel) {
563
					return this.instantiationService.createInstance(DefaultSettingsRenderer, this.editor, editorModel, (<DefaultPreferencesCodeEditor>this.editor).settingsModel);
564 565 566
				}
				return null;
			});
567
	}
568 569 570 571

	getId(): string {
		return DefaultSettingsEditorContribution.ID;
	}
572 573 574
}

@editorContribution
575
export class SettingsEditorContribution extends PreferencesEditorContribution<ISetting> implements editorCommon.IEditorContribution {
576 577 578 579 580 581 582

	static ID: string = 'editor.contrib.settings';

	getId(): string {
		return SettingsEditorContribution.ID;
	}

583 584 585 586 587 588 589 590 591 592 593
	protected createPreferencesRenderer(): TPromise<IPreferencesRenderer<ISetting>> {
		return TPromise.join<any>([this.preferencesService.createDefaultPreferencesEditorModel(this.preferencesService.defaultSettingsResource), this.preferencesService.resolvePreferencesEditorModel(this.editor.getModel().uri)])
			.then(([defaultSettingsModel, settingsModel]) => {
				if (settingsModel instanceof SettingsEditorModel) {
					if (ConfigurationTarget.USER === settingsModel.configurationTarget) {
						return this.instantiationService.createInstance(UserSettingsRenderer, this.editor, settingsModel, defaultSettingsModel);
					}
					return this.instantiationService.createInstance(WorkspaceSettingsRenderer, this.editor, settingsModel, defaultSettingsModel);
				}
				return null;
			});
594 595 596
	}
}

597 598 599
class StartSearchDefaultSettingsCommand extends Command {

	public runCommand(accessor: ServicesAccessor, args: any): void {
S
Sandeep Somavarapu 已提交
600 601 602
		const preferencesEditor = this.getPreferencesEditor(accessor);
		if (preferencesEditor) {
			preferencesEditor.focusSearch();
603 604 605
		}
	}

S
Sandeep Somavarapu 已提交
606
	private getPreferencesEditor(accessor: ServicesAccessor): PreferencesEditor {
607
		const activeEditor = accessor.get(IWorkbenchEditorService).getActiveEditor();
S
Sandeep Somavarapu 已提交
608 609
		if (activeEditor instanceof PreferencesEditor) {
			return activeEditor;
610 611 612 613 614 615
		}
		return null;
	}
}

CommonEditorRegistry.registerEditorCommand(new StartSearchDefaultSettingsCommand({
S
Sandeep Somavarapu 已提交
616 617
	id: SETTINGS_EDITOR_COMMAND_SEARCH,
	precondition: ContextKeyExpr.and(CONTEXT_SETTINGS_EDITOR),
618
	kbOpts: { primary: KeyMod.CtrlCmd | KeyCode.KEY_F }
S
Sandeep Somavarapu 已提交
619
}));