commonEditorConfig.ts 32.7 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

A
Alex Dima 已提交
7
import * as nls from 'vs/nls';
J
Johannes Rieken 已提交
8 9
import Event, { Emitter } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
A
Alex Dima 已提交
10
import * as objects from 'vs/base/common/objects';
A
Alex Dima 已提交
11
import * as platform from 'vs/base/common/platform';
J
Johannes Rieken 已提交
12 13 14
import { Extensions, IConfigurationRegistry, IConfigurationNode } from 'vs/platform/configuration/common/configurationRegistry';
import { Registry } from 'vs/platform/platform';
import { DefaultConfig, DEFAULT_INDENTATION, DEFAULT_TRIM_AUTO_WHITESPACE, GOLDEN_LINE_HEIGHT_RATIO } from 'vs/editor/common/config/defaultConfig';
A
Alex Dima 已提交
15
import * as editorCommon from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
16 17
import { EditorLayoutProvider } from 'vs/editor/common/viewLayout/editorLayoutProvider';
import { ScrollbarVisibility } from 'vs/base/common/scrollable';
E
Erich Gamma 已提交
18

19 20 21 22 23 24 25 26 27 28 29
// TODO@Alex: investigate if it is better to stick to 31 bits (see smi = SMall Integer)
// See https://thibaultlaurens.github.io/javascript/2013/04/29/how-the-v8-engine-works/#tagged-values
/**
 * MAX_INT that fits in 32 bits
 */
const MAX_SAFE_INT = 0x7fffffff;
/**
 * MIN_INT that fits in 32 bits
 */
const MIN_SAFE_INT = -0x80000000;

30
export interface IEditorZoom {
J
Johannes Rieken 已提交
31
	onDidChangeZoomLevel: Event<number>;
32
	getZoomLevel(): number;
J
Johannes Rieken 已提交
33
	setZoomLevel(zoomLevel: number): void;
34 35 36 37 38 39 40
}

export const EditorZoom: IEditorZoom = new class {

	private _zoomLevel: number = 0;

	private _onDidChangeZoomLevel: Emitter<number> = new Emitter<number>();
J
Johannes Rieken 已提交
41
	public onDidChangeZoomLevel: Event<number> = this._onDidChangeZoomLevel.event;
42 43 44 45 46

	public getZoomLevel(): number {
		return this._zoomLevel;
	}

J
Johannes Rieken 已提交
47
	public setZoomLevel(zoomLevel: number): void {
48 49 50 51 52 53 54 55 56
		zoomLevel = Math.min(Math.max(-9, zoomLevel), 9);
		if (this._zoomLevel === zoomLevel) {
			return;
		}

		this._zoomLevel = zoomLevel;
		this._onDidChangeZoomLevel.fire(this._zoomLevel);
	}
};
E
Erich Gamma 已提交
57

58 59 60 61 62 63 64
/**
 * Control what pressing Tab does.
 * If it is false, pressing Tab or Shift-Tab will be handled by the editor.
 * If it is true, pressing Tab or Shift-Tab will move the browser focus.
 * Defaults to false.
 */
export interface ITabFocus {
J
Johannes Rieken 已提交
65
	onDidChangeTabFocus: Event<boolean>;
66
	getTabFocusMode(): boolean;
J
Johannes Rieken 已提交
67
	setTabFocusMode(tabFocusMode: boolean): void;
68 69 70 71 72 73
}

export const TabFocus: ITabFocus = new class {
	private _tabFocus: boolean = false;

	private _onDidChangeTabFocus: Emitter<boolean> = new Emitter<boolean>();
J
Johannes Rieken 已提交
74
	public onDidChangeTabFocus: Event<boolean> = this._onDidChangeTabFocus.event;
75 76 77 78 79

	public getTabFocusMode(): boolean {
		return this._tabFocus;
	}

J
Johannes Rieken 已提交
80
	public setTabFocusMode(tabFocusMode: boolean): void {
81 82 83 84 85 86 87 88 89
		if (this._tabFocus === tabFocusMode) {
			return;
		}

		this._tabFocus = tabFocusMode;
		this._onDidChangeTabFocus.fire(this._tabFocus);
	}
};

90 91 92 93 94 95 96 97 98 99 100 101 102
/**
 * Experimental screen reader support toggle
 */
export class GlobalScreenReaderNVDA {

	private static _value = false;
	private static _onChange = new Emitter<boolean>();
	public static onChange: Event<boolean> = GlobalScreenReaderNVDA._onChange.event;

	public static getValue(): boolean {
		return this._value;
	}

J
Johannes Rieken 已提交
103
	public static setValue(value: boolean): void {
104 105 106 107 108 109 110 111
		if (this._value === value) {
			return;
		}
		this._value = value;
		this._onChange.fire(this._value);
	}
}

E
Erich Gamma 已提交
112 113
export class ConfigurationWithDefaults {

J
Johannes Rieken 已提交
114
	private _editor: editorCommon.IEditorOptions;
E
Erich Gamma 已提交
115

J
Johannes Rieken 已提交
116
	constructor(options: editorCommon.IEditorOptions) {
A
Alex Dima 已提交
117
		this._editor = <editorCommon.IEditorOptions>objects.clone(DefaultConfig.editor);
E
Erich Gamma 已提交
118 119 120 121

		this._mergeOptionsIn(options);
	}

A
Alex Dima 已提交
122
	public getEditorOptions(): editorCommon.IEditorOptions {
E
Erich Gamma 已提交
123 124 125
		return this._editor;
	}

J
Johannes Rieken 已提交
126
	private _mergeOptionsIn(newOptions: editorCommon.IEditorOptions): void {
A
Alex Dima 已提交
127
		this._editor = objects.mixin(this._editor, newOptions || {});
E
Erich Gamma 已提交
128 129
	}

J
Johannes Rieken 已提交
130
	public updateOptions(newOptions: editorCommon.IEditorOptions): void {
E
Erich Gamma 已提交
131 132 133 134 135 136 137 138 139 140 141
		// Apply new options
		this._mergeOptionsIn(newOptions);
	}
}

class InternalEditorOptionsHelper {

	constructor() {
	}

	public static createInternalEditorOptions(
J
Johannes Rieken 已提交
142 143
		outerWidth: number, outerHeight: number,
		opts: editorCommon.IEditorOptions,
144
		fontInfo: editorCommon.FontInfo,
J
Johannes Rieken 已提交
145 146
		editorClassName: string,
		isDominatedByLongLines: boolean,
147
		maxLineNumber: number,
148
		canUseTranslate3d: boolean
149
	): editorCommon.InternalEditorOptions {
E
Erich Gamma 已提交
150 151

		let wrappingColumn = toInteger(opts.wrappingColumn, -1);
152
		let wordWrap = toBoolean(opts.wordWrap);
E
Erich Gamma 已提交
153

J
Johannes Rieken 已提交
154
		let stopRenderingLineAfter: number;
E
Erich Gamma 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168
		if (typeof opts.stopRenderingLineAfter !== 'undefined') {
			stopRenderingLineAfter = toInteger(opts.stopRenderingLineAfter, -1);
		} else if (wrappingColumn >= 0) {
			stopRenderingLineAfter = -1;
		} else {
			stopRenderingLineAfter = 10000;
		}

		let mouseWheelScrollSensitivity = toFloat(opts.mouseWheelScrollSensitivity, 1);
		let scrollbar = this._sanitizeScrollbarOpts(opts.scrollbar, mouseWheelScrollSensitivity);

		let glyphMargin = toBoolean(opts.glyphMargin);
		let lineNumbers = opts.lineNumbers;
		let lineNumbersMinChars = toInteger(opts.lineNumbersMinChars, 1);
169 170 171 172 173 174 175 176

		let lineDecorationsWidth: number;
		if (typeof opts.lineDecorationsWidth === 'string' && /^\d+(\.\d+)?ch$/.test(opts.lineDecorationsWidth)) {
			let multiple = parseFloat(opts.lineDecorationsWidth.substr(0, opts.lineDecorationsWidth.length - 2));
			lineDecorationsWidth = multiple * fontInfo.typicalHalfwidthCharacterWidth;
		} else {
			lineDecorationsWidth = toInteger(opts.lineDecorationsWidth, 0);
		}
M
Martin Aeschlimann 已提交
177
		if (opts.folding) {
178
			lineDecorationsWidth += 16;
M
Martin Aeschlimann 已提交
179
		}
180
		let renderLineNumbers: boolean;
J
Johannes Rieken 已提交
181
		let renderCustomLineNumbers: (lineNumber: number) => string;
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
		let renderRelativeLineNumbers: boolean;

		// Compatibility with old true or false values
		if (<any>lineNumbers === true) {
			lineNumbers = 'on';
		} else if (<any>lineNumbers === false) {
			lineNumbers = 'off';
		}

		if (typeof lineNumbers === 'function') {
			renderLineNumbers = true;
			renderCustomLineNumbers = lineNumbers;
			renderRelativeLineNumbers = false;
		} else if (lineNumbers === 'relative') {
			renderLineNumbers = true;
			renderCustomLineNumbers = null;
			renderRelativeLineNumbers = true;
		} else if (lineNumbers === 'on') {
			renderLineNumbers = true;
			renderCustomLineNumbers = null;
			renderRelativeLineNumbers = false;
		} else {
			renderLineNumbers = false;
			renderCustomLineNumbers = null;
			renderRelativeLineNumbers = false;
		}

E
Erich Gamma 已提交
209 210 211 212
		let layoutInfo = EditorLayoutProvider.compute({
			outerWidth: outerWidth,
			outerHeight: outerHeight,
			showGlyphMargin: glyphMargin,
213
			lineHeight: fontInfo.lineHeight,
214
			showLineNumbers: renderLineNumbers,
E
Erich Gamma 已提交
215 216
			lineNumbersMinChars: lineNumbersMinChars,
			lineDecorationsWidth: lineDecorationsWidth,
217
			maxDigitWidth: fontInfo.maxDigitWidth,
218
			maxLineNumber: maxLineNumber,
E
Erich Gamma 已提交
219 220 221 222 223 224 225 226 227 228 229
			verticalScrollbarWidth: scrollbar.verticalScrollbarSize,
			horizontalScrollbarHeight: scrollbar.horizontalScrollbarSize,
			scrollbarArrowSize: scrollbar.arrowSize,
			verticalScrollbarHasArrows: scrollbar.verticalHasArrows
		});

		if (isDominatedByLongLines && wrappingColumn > 0) {
			// Force viewport width wrapping if model is dominated by long lines
			wrappingColumn = 0;
		}

230
		let bareWrappingInfo: { isViewportWrapping: boolean; wrappingColumn: number; };
E
Erich Gamma 已提交
231 232
		if (wrappingColumn === 0) {
			// If viewport width wrapping is enabled
233
			bareWrappingInfo = {
E
Erich Gamma 已提交
234
				isViewportWrapping: true,
235
				wrappingColumn: Math.max(1, Math.floor((layoutInfo.contentWidth - layoutInfo.verticalScrollbarWidth) / fontInfo.typicalHalfwidthCharacterWidth))
E
Erich Gamma 已提交
236
			};
237
		} else if (wrappingColumn > 0 && wordWrap === true) {
238 239 240 241 242
			// Enable smart viewport wrapping
			bareWrappingInfo = {
				isViewportWrapping: true,
				wrappingColumn: Math.min(wrappingColumn, Math.floor((layoutInfo.contentWidth - layoutInfo.verticalScrollbarWidth) / fontInfo.typicalHalfwidthCharacterWidth))
			};
E
Erich Gamma 已提交
243 244
		} else if (wrappingColumn > 0) {
			// Wrapping is enabled
245
			bareWrappingInfo = {
E
Erich Gamma 已提交
246 247 248 249
				isViewportWrapping: false,
				wrappingColumn: wrappingColumn
			};
		} else {
250
			bareWrappingInfo = {
E
Erich Gamma 已提交
251 252 253 254
				isViewportWrapping: false,
				wrappingColumn: -1
			};
		}
255 256 257 258 259 260 261 262
		let wrappingInfo = new editorCommon.EditorWrappingInfo({
			isViewportWrapping: bareWrappingInfo.isViewportWrapping,
			wrappingColumn: bareWrappingInfo.wrappingColumn,
			wrappingIndent: wrappingIndentFromString(opts.wrappingIndent),
			wordWrapBreakBeforeCharacters: String(opts.wordWrapBreakBeforeCharacters),
			wordWrapBreakAfterCharacters: String(opts.wordWrapBreakAfterCharacters),
			wordWrapBreakObtrusiveCharacters: String(opts.wordWrapBreakObtrusiveCharacters),
		});
E
Erich Gamma 已提交
263

264 265
		let readOnly = toBoolean(opts.readOnly);

266
		let tabFocusMode = TabFocus.getTabFocusMode();
267 268 269 270
		if (readOnly) {
			tabFocusMode = true;
		}

271

272 273 274 275 276 277 278 279
		let renderWhitespace = opts.renderWhitespace;
		// Compatibility with old true or false values
		if (<any>renderWhitespace === true) {
			renderWhitespace = 'boundary';
		} else if (<any>renderWhitespace === false) {
			renderWhitespace = 'none';
		}

280 281 282 283 284 285 286 287
		let renderLineHighlight = opts.renderLineHighlight;
		// Compatibility with old true or false values
		if (<any>renderLineHighlight === true) {
			renderLineHighlight = 'line';
		} else if (<any>renderLineHighlight === false) {
			renderLineHighlight = 'none';
		}

288
		let viewInfo = new editorCommon.InternalEditorViewOptions({
289
			theme: opts.theme,
290
			canUseTranslate3d: canUseTranslate3d,
291 292 293
			experimentalScreenReader: toBoolean(opts.experimentalScreenReader),
			rulers: toSortedIntegerArray(opts.rulers),
			ariaLabel: String(opts.ariaLabel),
294 295 296
			renderLineNumbers: renderLineNumbers,
			renderCustomLineNumbers: renderCustomLineNumbers,
			renderRelativeLineNumbers: renderRelativeLineNumbers,
E
Erich Gamma 已提交
297 298 299 300 301
			selectOnLineNumbers: toBoolean(opts.selectOnLineNumbers),
			glyphMargin: glyphMargin,
			revealHorizontalRightPadding: toInteger(opts.revealHorizontalRightPadding, 0),
			roundedSelection: toBoolean(opts.roundedSelection),
			overviewRulerLanes: toInteger(opts.overviewRulerLanes, 0, 3),
302
			cursorBlinking: cursorBlinkingStyleFromString(opts.cursorBlinking),
303
			mouseWheelZoom: toBoolean(opts.mouseWheelZoom),
A
Alex Dima 已提交
304
			cursorStyle: cursorStyleFromString(opts.cursorStyle),
E
Erich Gamma 已提交
305 306
			hideCursorInOverviewRuler: toBoolean(opts.hideCursorInOverviewRuler),
			scrollBeyondLastLine: toBoolean(opts.scrollBeyondLastLine),
307 308
			editorClassName: editorClassName,
			stopRenderingLineAfter: stopRenderingLineAfter,
309
			renderWhitespace: renderWhitespace,
310
			renderControlCharacters: toBoolean(opts.renderControlCharacters),
311
			renderIndentGuides: toBoolean(opts.renderIndentGuides),
312
			renderLineHighlight: renderLineHighlight,
313
			scrollbar: scrollbar,
J
Joao Moreno 已提交
314
			fixedOverflowWidgets: toBoolean(opts.fixedOverflowWidgets)
315 316
		});

A
Alex Dima 已提交
317
		let contribInfo = new editorCommon.EditorContribOptions({
318
			selectionClipboard: toBoolean(opts.selectionClipboard),
E
Erich Gamma 已提交
319 320 321 322
			hover: toBoolean(opts.hover),
			contextmenu: toBoolean(opts.contextmenu),
			quickSuggestions: toBoolean(opts.quickSuggestions),
			quickSuggestionsDelay: toInteger(opts.quickSuggestionsDelay),
J
Joao Moreno 已提交
323
			parameterHints: toBoolean(opts.parameterHints),
E
Erich Gamma 已提交
324 325 326
			iconsInSuggestions: toBoolean(opts.iconsInSuggestions),
			formatOnType: toBoolean(opts.formatOnType),
			suggestOnTriggerCharacters: toBoolean(opts.suggestOnTriggerCharacters),
327
			acceptSuggestionOnEnter: toBoolean(opts.acceptSuggestionOnEnter),
328
			snippetSuggestions: opts.snippetSuggestions,
329
			emptySelectionClipboard: opts.emptySelectionClipboard,
330
			tabCompletion: opts.tabCompletion,
331
			wordBasedSuggestions: opts.wordBasedSuggestions,
J
Joao Moreno 已提交
332 333
			suggestFontSize: opts.suggestFontSize,
			suggestLineHeight: opts.suggestLineHeight,
E
Erich Gamma 已提交
334
			selectionHighlight: toBoolean(opts.selectionHighlight),
335
			codeLens: opts.referenceInfos && opts.codeLens,
M
Martin Aeschlimann 已提交
336
			folding: toBoolean(opts.folding),
A
Alex Dima 已提交
337 338
		});

339 340 341 342
		return new editorCommon.InternalEditorOptions({
			lineHeight: fontInfo.lineHeight, // todo -> duplicated in styling
			readOnly: readOnly,
			wordSeparators: String(opts.wordSeparators),
A
Alex Dima 已提交
343
			autoClosingBrackets: toBoolean(opts.autoClosingBrackets),
344
			useTabStops: toBoolean(opts.useTabStops),
345
			tabFocusMode: tabFocusMode,
E
Erich Gamma 已提交
346
			layoutInfo: layoutInfo,
347
			fontInfo: fontInfo,
348
			viewInfo: viewInfo,
E
Erich Gamma 已提交
349
			wrappingInfo: wrappingInfo,
A
Alex Dima 已提交
350
			contribInfo: contribInfo,
351
		});
E
Erich Gamma 已提交
352 353
	}

J
Johannes Rieken 已提交
354
	private static _sanitizeScrollbarOpts(raw: editorCommon.IEditorScrollbarOptions, mouseWheelScrollSensitivity: number): editorCommon.InternalEditorScrollbarOptions {
A
Alex Dima 已提交
355

A
Alex Dima 已提交
356
		let visibilityFromString = (visibility: string) => {
A
Alex Dima 已提交
357 358 359 360 361 362 363 364 365 366
			switch (visibility) {
				case 'hidden':
					return ScrollbarVisibility.Hidden;
				case 'visible':
					return ScrollbarVisibility.Visible;
				default:
					return ScrollbarVisibility.Auto;
			}
		};

A
Alex Dima 已提交
367 368
		let horizontalScrollbarSize = toIntegerWithDefault(raw.horizontalScrollbarSize, 10);
		let verticalScrollbarSize = toIntegerWithDefault(raw.verticalScrollbarSize, 14);
A
Alex Dima 已提交
369 370 371
		return new editorCommon.InternalEditorScrollbarOptions({
			vertical: visibilityFromString(raw.vertical),
			horizontal: visibilityFromString(raw.horizontal),
E
Erich Gamma 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

			arrowSize: toIntegerWithDefault(raw.arrowSize, 11),
			useShadows: toBooleanWithDefault(raw.useShadows, true),

			verticalHasArrows: toBooleanWithDefault(raw.verticalHasArrows, false),
			horizontalHasArrows: toBooleanWithDefault(raw.horizontalHasArrows, false),

			horizontalScrollbarSize: horizontalScrollbarSize,
			horizontalSliderSize: toIntegerWithDefault(raw.horizontalSliderSize, horizontalScrollbarSize),

			verticalScrollbarSize: verticalScrollbarSize,
			verticalSliderSize: toIntegerWithDefault(raw.verticalSliderSize, verticalScrollbarSize),

			handleMouseWheel: toBooleanWithDefault(raw.handleMouseWheel, true),
			mouseWheelScrollSensitivity: mouseWheelScrollSensitivity
A
Alex Dima 已提交
387
		});
E
Erich Gamma 已提交
388 389 390
	}
}

J
Johannes Rieken 已提交
391
function toBoolean(value: any): boolean {
E
Erich Gamma 已提交
392 393 394
	return value === 'false' ? false : Boolean(value);
}

J
Johannes Rieken 已提交
395
function toBooleanWithDefault(value: any, defaultValue: boolean): boolean {
E
Erich Gamma 已提交
396 397 398 399 400 401 402
	if (typeof value === 'undefined') {
		return defaultValue;
	}
	return toBoolean(value);
}

function toFloat(source: any, defaultValue: number): number {
A
Alex Dima 已提交
403
	let r = parseFloat(source);
E
Erich Gamma 已提交
404 405 406 407 408 409
	if (isNaN(r)) {
		r = defaultValue;
	}
	return r;
}

J
Johannes Rieken 已提交
410
function toInteger(source: any, minimum: number = MIN_SAFE_INT, maximum: number = MAX_SAFE_INT): number {
A
Alex Dima 已提交
411
	let r = parseInt(source, 10);
E
Erich Gamma 已提交
412 413 414
	if (isNaN(r)) {
		r = 0;
	}
415 416 417
	r = Math.max(minimum, r);
	r = Math.min(maximum, r);
	return r | 0;
E
Erich Gamma 已提交
418 419
}

J
Johannes Rieken 已提交
420
function toSortedIntegerArray(source: any): number[] {
421 422 423 424 425 426 427 428 429
	if (!Array.isArray(source)) {
		return [];
	}
	let arrSource = <any[]>source;
	let r = arrSource.map(el => toInteger(el));
	r.sort();
	return r;
}

J
Johannes Rieken 已提交
430
function wrappingIndentFromString(wrappingIndent: string): editorCommon.WrappingIndent {
A
Alex Dima 已提交
431 432 433 434 435 436 437 438 439
	if (wrappingIndent === 'indent') {
		return editorCommon.WrappingIndent.Indent;
	} else if (wrappingIndent === 'same') {
		return editorCommon.WrappingIndent.Same;
	} else {
		return editorCommon.WrappingIndent.None;
	}
}

J
Johannes Rieken 已提交
440
function cursorStyleFromString(cursorStyle: string): editorCommon.TextEditorCursorStyle {
A
Alex Dima 已提交
441 442 443 444 445 446 447 448 449 450
	if (cursorStyle === 'line') {
		return editorCommon.TextEditorCursorStyle.Line;
	} else if (cursorStyle === 'block') {
		return editorCommon.TextEditorCursorStyle.Block;
	} else if (cursorStyle === 'underline') {
		return editorCommon.TextEditorCursorStyle.Underline;
	}
	return editorCommon.TextEditorCursorStyle.Line;
}

451
function cursorBlinkingStyleFromString(cursorBlinkingStyle: string): editorCommon.TextEditorCursorBlinkingStyle {
452 453 454 455 456 457 458 459 460 461 462 463
	switch (cursorBlinkingStyle) {
		case 'blink':
			return editorCommon.TextEditorCursorBlinkingStyle.Blink;
		case 'smooth':
			return editorCommon.TextEditorCursorBlinkingStyle.Smooth;
		case 'phase':
			return editorCommon.TextEditorCursorBlinkingStyle.Phase;
		case 'expand':
			return editorCommon.TextEditorCursorBlinkingStyle.Expand;
		case 'visible': // maintain compatibility
		case 'solid':
			return editorCommon.TextEditorCursorBlinkingStyle.Solid;
464 465 466 467
	}
	return editorCommon.TextEditorCursorBlinkingStyle.Blink;
}

J
Johannes Rieken 已提交
468
function toIntegerWithDefault(source: any, defaultValue: number): number {
E
Erich Gamma 已提交
469 470 471 472 473 474
	if (typeof source === 'undefined') {
		return defaultValue;
	}
	return toInteger(source);
}

475 476
export interface IElementSizeObserver {
	startObserving(): void;
J
Johannes Rieken 已提交
477
	observe(dimension?: editorCommon.IDimension): void;
478 479 480 481 482
	dispose(): void;
	getWidth(): number;
	getHeight(): number;
}

A
Alex Dima 已提交
483
export abstract class CommonEditorConfiguration extends Disposable implements editorCommon.IConfiguration {
E
Erich Gamma 已提交
484

J
Johannes Rieken 已提交
485 486
	public editor: editorCommon.InternalEditorOptions;
	public editorClone: editorCommon.InternalEditorOptions;
E
Erich Gamma 已提交
487

J
Johannes Rieken 已提交
488
	protected _configWithDefaults: ConfigurationWithDefaults;
489
	protected _elementSizeObserver: IElementSizeObserver;
J
Johannes Rieken 已提交
490 491
	private _isDominatedByLongLines: boolean;
	private _maxLineNumber: number;
E
Erich Gamma 已提交
492

A
Alex Dima 已提交
493 494
	private _onDidChange = this._register(new Emitter<editorCommon.IConfigurationChangedEvent>());
	public onDidChange: Event<editorCommon.IConfigurationChangedEvent> = this._onDidChange.event;
A
Alex Dima 已提交
495

J
Johannes Rieken 已提交
496
	constructor(options: editorCommon.IEditorOptions, elementSizeObserver: IElementSizeObserver = null) {
A
Alex Dima 已提交
497
		super();
E
Erich Gamma 已提交
498
		this._configWithDefaults = new ConfigurationWithDefaults(options);
499
		this._elementSizeObserver = elementSizeObserver;
E
Erich Gamma 已提交
500
		this._isDominatedByLongLines = false;
501
		this._maxLineNumber = 1;
E
Erich Gamma 已提交
502
		this.editor = this._computeInternalOptions();
503
		this.editorClone = this.editor.clone();
504
		this._register(EditorZoom.onDidChangeZoomLevel(_ => this._recomputeOptions()));
505
		this._register(TabFocus.onDidChangeTabFocus(_ => this._recomputeOptions()));
E
Erich Gamma 已提交
506 507 508 509 510 511 512
	}

	public dispose(): void {
		super.dispose();
	}

	protected _recomputeOptions(): void {
513 514
		this._setOptions(this._computeInternalOptions());
	}
515

J
Johannes Rieken 已提交
516
	private _setOptions(newOptions: editorCommon.InternalEditorOptions): void {
517 518
		if (this.editor && this.editor.equals(newOptions)) {
			return;
E
Erich Gamma 已提交
519 520
		}

521 522 523 524
		let changeEvent = this.editor.createChangeEvent(newOptions);
		this.editor = newOptions;
		this.editorClone = this.editor.clone();
		this._onDidChange.fire(changeEvent);
E
Erich Gamma 已提交
525 526
	}

A
Alex Dima 已提交
527
	public getRawOptions(): editorCommon.IEditorOptions {
E
Erich Gamma 已提交
528 529 530
		return this._configWithDefaults.getEditorOptions();
	}

531
	private _computeInternalOptions(): editorCommon.InternalEditorOptions {
E
Erich Gamma 已提交
532 533
		let opts = this._configWithDefaults.getEditorOptions();

534
		let editorClassName = this._getEditorClassName(opts.theme, toBoolean(opts.fontLigatures));
535
		let fontFamily = String(opts.fontFamily) || DefaultConfig.editor.fontFamily;
536
		let fontWeight = String(opts.fontWeight) || DefaultConfig.editor.fontWeight;
537 538 539
		let fontSize = toFloat(opts.fontSize, DefaultConfig.editor.fontSize);
		fontSize = Math.max(0, fontSize);
		fontSize = Math.min(100, fontSize);
D
Daniel Imms 已提交
540 541 542
		if (fontSize === 0) {
			fontSize = DefaultConfig.editor.fontSize;
		}
E
Erich Gamma 已提交
543

544 545
		let lineHeight = toInteger(opts.lineHeight, 0, 150);
		if (lineHeight === 0) {
546
			lineHeight = Math.round(GOLDEN_LINE_HEIGHT_RATIO * fontSize);
E
Erich Gamma 已提交
547
		}
548
		let editorZoomLevelMultiplier = 1 + (EditorZoom.getZoomLevel() * 0.1);
549 550
		fontSize *= editorZoomLevelMultiplier;
		lineHeight *= editorZoomLevelMultiplier;
E
Erich Gamma 已提交
551

552 553 554 555 556 557
		let disableTranslate3d = toBoolean(opts.disableTranslate3d);
		let canUseTranslate3d = this._getCanUseTranslate3d();
		if (disableTranslate3d) {
			canUseTranslate3d = false;
		}

558
		return InternalEditorOptionsHelper.createInternalEditorOptions(
E
Erich Gamma 已提交
559 560 561
			this.getOuterWidth(),
			this.getOuterHeight(),
			opts,
A
Alex Dima 已提交
562 563
			this.readConfiguration(new editorCommon.BareFontInfo({
				fontFamily: fontFamily,
564
				fontWeight: fontWeight,
A
Alex Dima 已提交
565 566 567
				fontSize: fontSize,
				lineHeight: lineHeight
			})),
568
			editorClassName,
E
Erich Gamma 已提交
569
			this._isDominatedByLongLines,
570
			this._maxLineNumber,
571
			canUseTranslate3d
E
Erich Gamma 已提交
572 573 574
		);
	}

J
Johannes Rieken 已提交
575
	public updateOptions(newOptions: editorCommon.IEditorOptions): void {
E
Erich Gamma 已提交
576 577 578 579
		this._configWithDefaults.updateOptions(newOptions);
		this._recomputeOptions();
	}

J
Johannes Rieken 已提交
580
	public setIsDominatedByLongLines(isDominatedByLongLines: boolean): void {
E
Erich Gamma 已提交
581 582 583 584
		this._isDominatedByLongLines = isDominatedByLongLines;
		this._recomputeOptions();
	}

J
Johannes Rieken 已提交
585
	public setMaxLineNumber(maxLineNumber: number): void {
586
		this._maxLineNumber = maxLineNumber;
E
Erich Gamma 已提交
587 588 589
		this._recomputeOptions();
	}

J
Johannes Rieken 已提交
590
	protected abstract _getEditorClassName(theme: string, fontLigatures: boolean): string;
E
Erich Gamma 已提交
591

592
	protected abstract getOuterWidth(): number;
E
Erich Gamma 已提交
593

594
	protected abstract getOuterHeight(): number;
E
Erich Gamma 已提交
595

596 597
	protected abstract _getCanUseTranslate3d(): boolean;

598
	protected abstract readConfiguration(styling: editorCommon.BareFontInfo): editorCommon.FontInfo;
E
Erich Gamma 已提交
599 600
}

601 602
const configurationRegistry = <IConfigurationRegistry>Registry.as(Extensions.Configuration);
const editorConfiguration: IConfigurationNode = {
E
Erich Gamma 已提交
603 604 605
	'id': 'editor',
	'order': 5,
	'type': 'object',
606
	'title': nls.localize('editorConfigurationTitle', "Editor"),
J
Johannes Rieken 已提交
607 608
	'properties': {
		'editor.fontFamily': {
E
Erich Gamma 已提交
609 610 611 612
			'type': 'string',
			'default': DefaultConfig.editor.fontFamily,
			'description': nls.localize('fontFamily', "Controls the font family.")
		},
J
Johannes Rieken 已提交
613
		'editor.fontWeight': {
614
			'type': 'string',
615
			'enum': ['normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900'],
616 617 618
			'default': DefaultConfig.editor.fontWeight,
			'description': nls.localize('fontWeight', "Controls the font weight.")
		},
J
Johannes Rieken 已提交
619
		'editor.fontSize': {
E
Erich Gamma 已提交
620 621
			'type': 'number',
			'default': DefaultConfig.editor.fontSize,
622
			'description': nls.localize('fontSize', "Controls the font size in pixels.")
E
Erich Gamma 已提交
623
		},
J
Johannes Rieken 已提交
624
		'editor.lineHeight': {
E
Erich Gamma 已提交
625 626
			'type': 'number',
			'default': DefaultConfig.editor.lineHeight,
627
			'description': nls.localize('lineHeight', "Controls the line height. Use 0 to compute the lineHeight from the fontSize.")
E
Erich Gamma 已提交
628
		},
J
Johannes Rieken 已提交
629
		'editor.lineNumbers': {
630 631
			'type': 'string',
			'enum': ['off', 'on', 'relative'],
E
Erich Gamma 已提交
632
			'default': DefaultConfig.editor.lineNumbers,
633
			'description': nls.localize('lineNumbers', "Controls the display of line numbers. Possible values are 'on', 'off', and 'relative'. 'relative' shows the line count from the current cursor position.")
E
Erich Gamma 已提交
634
		},
J
Johannes Rieken 已提交
635
		'editor.rulers': {
636 637 638 639 640 641 642
			'type': 'array',
			'items': {
				'type': 'number'
			},
			'default': DefaultConfig.editor.rulers,
			'description': nls.localize('rulers', "Columns at which to show vertical rulers")
		},
J
Johannes Rieken 已提交
643
		'editor.wordSeparators': {
A
Alex Dima 已提交
644 645 646 647
			'type': 'string',
			'default': DefaultConfig.editor.wordSeparators,
			'description': nls.localize('wordSeparators', "Characters that will be used as word separators when doing word related navigations or operations")
		},
J
Johannes Rieken 已提交
648
		'editor.tabSize': {
649 650
			'type': 'number',
			'default': DEFAULT_INDENTATION.tabSize,
E
Erich Gamma 已提交
651
			'minimum': 1,
652
			'description': nls.localize('tabSize', "The number of spaces a tab is equal to. This setting is overriden based on the file contents when `editor.detectIndentation` is on."),
653
			'errorMessage': nls.localize('tabSize.errorMessage', "Expected 'number'. Note that the value \"auto\" has been replaced by the `editor.detectIndentation` setting.")
E
Erich Gamma 已提交
654
		},
J
Johannes Rieken 已提交
655
		'editor.insertSpaces': {
656 657
			'type': 'boolean',
			'default': DEFAULT_INDENTATION.insertSpaces,
658
			'description': nls.localize('insertSpaces', "Insert spaces when pressing Tab. This setting is overriden based on the file contents when `editor.detectIndentation` is on."),
659
			'errorMessage': nls.localize('insertSpaces.errorMessage', "Expected 'boolean'. Note that the value \"auto\" has been replaced by the `editor.detectIndentation` setting.")
E
Erich Gamma 已提交
660
		},
J
Johannes Rieken 已提交
661
		'editor.detectIndentation': {
662 663
			'type': 'boolean',
			'default': DEFAULT_INDENTATION.detectIndentation,
A
Alex Dima 已提交
664
			'description': nls.localize('detectIndentation', "When opening a file, `editor.tabSize` and `editor.insertSpaces` will be detected based on the file contents.")
665
		},
J
Johannes Rieken 已提交
666
		'editor.roundedSelection': {
E
Erich Gamma 已提交
667 668 669 670
			'type': 'boolean',
			'default': DefaultConfig.editor.roundedSelection,
			'description': nls.localize('roundedSelection', "Controls if selections have rounded corners")
		},
J
Johannes Rieken 已提交
671
		'editor.scrollBeyondLastLine': {
E
Erich Gamma 已提交
672 673 674 675
			'type': 'boolean',
			'default': DefaultConfig.editor.scrollBeyondLastLine,
			'description': nls.localize('scrollBeyondLastLine', "Controls if the editor will scroll beyond the last line")
		},
J
Johannes Rieken 已提交
676
		'editor.wrappingColumn': {
E
Erich Gamma 已提交
677 678 679
			'type': 'integer',
			'default': DefaultConfig.editor.wrappingColumn,
			'minimum': -1,
680
			'description': nls.localize('wrappingColumn', "Controls after how many characters the editor will wrap to the next line. Setting this to 0 turns on viewport width wrapping (word wrapping). Setting this to -1 forces the editor to never wrap.")
E
Erich Gamma 已提交
681
		},
J
Johannes Rieken 已提交
682
		'editor.wordWrap': {
683
			'type': 'boolean',
684 685
			'default': DefaultConfig.editor.wordWrap,
			'description': nls.localize('wordWrap', "Controls if lines should wrap. The lines will wrap at min(editor.wrappingColumn, viewportWidthInColumns).")
686
		},
J
Johannes Rieken 已提交
687
		'editor.wrappingIndent': {
E
Erich Gamma 已提交
688 689 690 691 692
			'type': 'string',
			'enum': ['none', 'same', 'indent'],
			'default': DefaultConfig.editor.wrappingIndent,
			'description': nls.localize('wrappingIndent', "Controls the indentation of wrapped lines. Can be one of 'none', 'same' or 'indent'.")
		},
J
Johannes Rieken 已提交
693
		'editor.mouseWheelScrollSensitivity': {
E
Erich Gamma 已提交
694 695 696 697
			'type': 'number',
			'default': DefaultConfig.editor.mouseWheelScrollSensitivity,
			'description': nls.localize('mouseWheelScrollSensitivity', "A multiplier to be used on the `deltaX` and `deltaY` of mouse wheel scroll events")
		},
J
Johannes Rieken 已提交
698
		'editor.quickSuggestions': {
E
Erich Gamma 已提交
699 700 701 702
			'type': 'boolean',
			'default': DefaultConfig.editor.quickSuggestions,
			'description': nls.localize('quickSuggestions', "Controls if quick suggestions should show up or not while typing")
		},
J
Johannes Rieken 已提交
703
		'editor.quickSuggestionsDelay': {
E
Erich Gamma 已提交
704 705 706 707 708
			'type': 'integer',
			'default': DefaultConfig.editor.quickSuggestionsDelay,
			'minimum': 0,
			'description': nls.localize('quickSuggestionsDelay', "Controls the delay in ms after which quick suggestions will show up")
		},
J
Johannes Rieken 已提交
709
		'editor.parameterHints': {
J
Joao Moreno 已提交
710 711 712 713
			'type': 'boolean',
			'default': DefaultConfig.editor.parameterHints,
			'description': nls.localize('parameterHints', "Enables parameter hints")
		},
J
Johannes Rieken 已提交
714
		'editor.autoClosingBrackets': {
E
Erich Gamma 已提交
715 716 717 718
			'type': 'boolean',
			'default': DefaultConfig.editor.autoClosingBrackets,
			'description': nls.localize('autoClosingBrackets', "Controls if the editor should automatically close brackets after opening them")
		},
J
Johannes Rieken 已提交
719
		'editor.formatOnType': {
E
Erich Gamma 已提交
720 721 722 723
			'type': 'boolean',
			'default': DefaultConfig.editor.formatOnType,
			'description': nls.localize('formatOnType', "Controls if the editor should automatically format the line after typing")
		},
J
Johannes Rieken 已提交
724
		'editor.suggestOnTriggerCharacters': {
E
Erich Gamma 已提交
725 726 727 728
			'type': 'boolean',
			'default': DefaultConfig.editor.suggestOnTriggerCharacters,
			'description': nls.localize('suggestOnTriggerCharacters', "Controls if suggestions should automatically show up when typing trigger characters")
		},
J
Johannes Rieken 已提交
729
		'editor.acceptSuggestionOnEnter': {
730 731 732 733
			'type': 'boolean',
			'default': DefaultConfig.editor.acceptSuggestionOnEnter,
			'description': nls.localize('acceptSuggestionOnEnter', "Controls if suggestions should be accepted 'Enter' - in addition to 'Tab'. Helps to avoid ambiguity between inserting new lines or accepting suggestions.")
		},
734
		'editor.snippetSuggestions': {
735
			'type': 'string',
736 737 738
			'enum': ['top', 'bottom', 'inline', 'none'],
			'default': DefaultConfig.editor.snippetSuggestions,
			'description': nls.localize('snippetSuggestions', "Controls whether snippets are shown with other suggestions and how they are sorted.")
739
		},
740 741 742 743 744
		'editor.emptySelectionClipboard': {
			'type': 'boolean',
			'default': DefaultConfig.editor.emptySelectionClipboard,
			'description': nls.localize('emptySelectionClipboard', "Controls whether copying without a selection copies the current line.")
		},
745 746 747 748 749
		'editor.wordBasedSuggestions': {
			'type': 'boolean',
			'default': DefaultConfig.editor.wordBasedSuggestions,
			'description': nls.localize('wordBasedSuggestions', "Enable word based suggestions.")
		},
J
Johannes Rieken 已提交
750
		'editor.suggestFontSize': {
J
Joao Moreno 已提交
751 752 753 754 755
			'type': 'integer',
			'default': 0,
			'minimum': 0,
			'description': nls.localize('suggestFontSize', "Font size for the suggest widget")
		},
J
Johannes Rieken 已提交
756
		'editor.suggestLineHeight': {
J
Joao Moreno 已提交
757 758 759 760 761
			'type': 'integer',
			'default': 0,
			'minimum': 0,
			'description': nls.localize('suggestLineHeight', "Line height for the suggest widget")
		},
762 763 764
		'editor.tabCompletion': {
			'type': 'boolean',
			'default': DefaultConfig.editor.tabCompletion,
765
			'description': nls.localize('tabCompletion', "Insert snippets when their prefix matches. Works best when 'quickSuggestions' aren't enabled.")
766
		},
J
Johannes Rieken 已提交
767
		'editor.selectionHighlight': {
E
Erich Gamma 已提交
768 769 770 771
			'type': 'boolean',
			'default': DefaultConfig.editor.selectionHighlight,
			'description': nls.localize('selectionHighlight', "Controls whether the editor should highlight similar matches to the selection")
		},
J
Johannes Rieken 已提交
772
		'editor.overviewRulerLanes': {
E
Erich Gamma 已提交
773 774 775 776
			'type': 'integer',
			'default': 3,
			'description': nls.localize('overviewRulerLanes', "Controls the number of decorations that can show up at the same position in the overview ruler")
		},
J
Johannes Rieken 已提交
777
		'editor.cursorBlinking': {
778
			'type': 'string',
779
			'enum': ['blink', 'smooth', 'phase', 'expand', 'solid'],
780
			'default': DefaultConfig.editor.cursorBlinking,
781
			'description': nls.localize('cursorBlinking', "Control the cursor animation style, possible values are 'blink', 'smooth', 'phase', 'expand' and 'solid'")
782
		},
783 784 785 786 787
		'editor.mouseWheelZoom': {
			'type': 'boolean',
			'default': DefaultConfig.editor.mouseWheelZoom,
			'description': nls.localize('mouseWheelZoom', "Zoom the font of the editor when using mouse wheel and holding Ctrl")
		},
J
Johannes Rieken 已提交
788
		'editor.cursorStyle': {
M
markrendle 已提交
789
			'type': 'string',
790
			'enum': ['block', 'line', 'underline'],
M
markrendle 已提交
791
			'default': DefaultConfig.editor.cursorStyle,
792
			'description': nls.localize('cursorStyle', "Controls the cursor style, accepted values are 'block', 'line' and 'underline'")
M
markrendle 已提交
793
		},
J
Johannes Rieken 已提交
794
		'editor.fontLigatures': {
795 796 797 798
			'type': 'boolean',
			'default': DefaultConfig.editor.fontLigatures,
			'description': nls.localize('fontLigatures', "Enables font ligatures")
		},
J
Johannes Rieken 已提交
799
		'editor.hideCursorInOverviewRuler': {
E
Erich Gamma 已提交
800 801 802 803 804
			'type': 'boolean',
			'default': DefaultConfig.editor.hideCursorInOverviewRuler,
			'description': nls.localize('hideCursorInOverviewRuler', "Controls if the cursor should be hidden in the overview ruler.")
		},
		'editor.renderWhitespace': {
805 806
			'type': 'string',
			'enum': ['none', 'boundary', 'all'],
E
Erich Gamma 已提交
807
			default: DefaultConfig.editor.renderWhitespace,
808
			description: nls.localize('renderWhitespace', "Controls how the editor should render whitespace characters, possibilities are 'none', 'boundary', and 'all'. The 'boundary' option does not render single spaces between words.")
E
Erich Gamma 已提交
809
		},
810 811 812 813 814
		'editor.renderControlCharacters': {
			'type': 'boolean',
			default: DefaultConfig.editor.renderControlCharacters,
			description: nls.localize('renderControlCharacters', "Controls whether the editor should render control characters")
		},
815 816 817 818 819
		'editor.renderIndentGuides': {
			'type': 'boolean',
			default: DefaultConfig.editor.renderIndentGuides,
			description: nls.localize('renderIndentGuides', "Controls whether the editor should render indent guides")
		},
820
		'editor.renderLineHighlight': {
821 822
			'type': 'string',
			'enum': ['none', 'gutter', 'line', 'all'],
823
			default: DefaultConfig.editor.renderLineHighlight,
824
			description: nls.localize('renderLineHighlight', "Controls how the editor should render the current line highlight, possibilities are 'none', 'gutter', 'line', and 'all'.")
825
		},
J
Johannes Rieken 已提交
826
		'editor.codeLens': {
E
Erich Gamma 已提交
827
			'type': 'boolean',
828 829
			'default': DefaultConfig.editor.codeLens,
			'description': nls.localize('codeLens', "Controls if the editor shows code lenses")
E
Erich Gamma 已提交
830
		},
J
Johannes Rieken 已提交
831
		'editor.folding': {
M
Martin Aeschlimann 已提交
832 833
			'type': 'boolean',
			'default': DefaultConfig.editor.folding,
834
			'description': nls.localize('folding', "Controls whether the editor has code folding enabled")
M
Martin Aeschlimann 已提交
835
		},
I
isidor 已提交
836 837 838 839 840
		'editor.glyphMargin': {
			'type': 'boolean',
			'default': DefaultConfig.editor.glyphMargin,
			'description': nls.localize('glyphMargin', "Controls whether the editor should render the vertical glyph margin. Glyph margin is mostly used for debugging.")
		},
J
Johannes Rieken 已提交
841
		'editor.useTabStops': {
842 843 844 845
			'type': 'boolean',
			'default': DefaultConfig.editor.useTabStops,
			'description': nls.localize('useTabStops', "Inserting and deleting whitespace follows tab stops")
		},
J
Johannes Rieken 已提交
846
		'editor.trimAutoWhitespace': {
847
			'type': 'boolean',
848
			'default': DEFAULT_TRIM_AUTO_WHITESPACE,
849 850
			'description': nls.localize('trimAutoWhitespace', "Remove trailing auto inserted whitespace")
		},
J
Johannes Rieken 已提交
851
		'editor.stablePeek': {
852
			'type': 'boolean',
853
			'default': false,
854
			'description': nls.localize('stablePeek', "Keep peek editors open even when double clicking their content or when hitting Escape.")
855
		},
J
Johannes Rieken 已提交
856
		'diffEditor.renderSideBySide': {
E
Erich Gamma 已提交
857 858 859 860
			'type': 'boolean',
			'default': true,
			'description': nls.localize('sideBySide', "Controls if the diff editor shows the diff side by side or inline")
		},
J
Johannes Rieken 已提交
861
		'diffEditor.ignoreTrimWhitespace': {
E
Erich Gamma 已提交
862 863 864
			'type': 'boolean',
			'default': true,
			'description': nls.localize('ignoreTrimWhitespace', "Controls if the diff editor shows changes in leading or trailing whitespace as diffs")
865 866 867 868 869
		},
		'diffEditor.renderIndicators': {
			'type': 'boolean',
			'default': true,
			'description': nls.localize('renderIndicators', "Controls if the diff editor shows +/- indicators for added/removed changes")
E
Erich Gamma 已提交
870 871
		}
	}
A
Alex Dima 已提交
872 873 874 875 876 877 878 879 880 881
};

if (platform.isLinux) {
	editorConfiguration['properties']['editor.selectionClipboard'] = {
		'type': 'boolean',
		'default': DefaultConfig.editor.selectionClipboard,
		'description': nls.localize('selectionClipboard', "Controls if the Linux primary clipboard should be supported.")
	};
}

882
configurationRegistry.registerConfiguration(editorConfiguration);