modelServiceImpl.ts 15.5 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';
S
Sandeep Somavarapu 已提交
8
import network = require('vs/base/common/network');
J
Johannes Rieken 已提交
9 10 11 12
import Event, { Emitter } from 'vs/base/common/event';
import { EmitterEvent } from 'vs/base/common/eventEmitter';
import { MarkedString } from 'vs/base/common/htmlContent';
import { IDisposable } from 'vs/base/common/lifecycle';
A
Alex Dima 已提交
13 14
import Severity from 'vs/base/common/severity';
import URI from 'vs/base/common/uri';
J
Johannes Rieken 已提交
15 16 17
import { TPromise } from 'vs/base/common/winjs.base';
import { IMarker, IMarkerService } from 'vs/platform/markers/common/markers';
import { Range } from 'vs/editor/common/core/range';
A
Alex Dima 已提交
18
import * as editorCommon from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
19
import { Model } from 'vs/editor/common/model/model';
A
Alex Dima 已提交
20
import { IMode, LanguageIdentifier } from 'vs/editor/common/modes';
J
Johannes Rieken 已提交
21
import { IModelService } from 'vs/editor/common/services/modelService';
22
import * as platform from 'vs/base/common/platform';
J
Johannes Rieken 已提交
23 24
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { DEFAULT_INDENTATION, DEFAULT_TRIM_AUTO_WHITESPACE } from 'vs/editor/common/config/defaultConfig';
A
Alex Dima 已提交
25
import { PLAINTEXT_LANGUAGE_IDENTIFIER } from 'vs/editor/common/modes/modesRegistry';
E
Erich Gamma 已提交
26

B
Benjamin Pasero 已提交
27
function MODEL_ID(resource: URI): string {
A
Alex Dima 已提交
28 29 30 31
	return resource.toString();
}

class ModelData implements IDisposable {
A
Alex Dima 已提交
32
	model: editorCommon.IModel;
E
Erich Gamma 已提交
33

A
Alex Dima 已提交
34 35
	private _markerDecorations: string[];
	private _modelEventsListener: IDisposable;
E
Erich Gamma 已提交
36

A
Alex Dima 已提交
37
	constructor(model: editorCommon.IModel, eventsHandler: (modelData: ModelData, events: EmitterEvent[]) => void) {
E
Erich Gamma 已提交
38
		this.model = model;
A
Alex Dima 已提交
39 40

		this._markerDecorations = [];
A
Alex Dima 已提交
41
		this._modelEventsListener = model.addBulkListener((events) => eventsHandler(this, events));
E
Erich Gamma 已提交
42 43 44
	}

	public dispose(): void {
A
Alex Dima 已提交
45 46 47
		this._markerDecorations = this.model.deltaDecorations(this._markerDecorations, []);
		this._modelEventsListener.dispose();
		this._modelEventsListener = null;
E
Erich Gamma 已提交
48
		this.model = null;
A
Alex Dima 已提交
49
	}
E
Erich Gamma 已提交
50

A
Alex Dima 已提交
51
	public getModelId(): string {
52
		return MODEL_ID(this.model.uri);
A
Alex Dima 已提交
53 54
	}

B
Benjamin Pasero 已提交
55
	public acceptMarkerDecorations(newDecorations: editorCommon.IModelDeltaDecoration[]): void {
A
Alex Dima 已提交
56
		this._markerDecorations = this.model.deltaDecorations(this._markerDecorations, newDecorations);
E
Erich Gamma 已提交
57
	}
A
Alex Dima 已提交
58
}
E
Erich Gamma 已提交
59

A
Alex Dima 已提交
60 61
class ModelMarkerHandler {

62
	public static setMarkers(modelData: ModelData, markerService: IMarkerService): void {
E
Erich Gamma 已提交
63 64

		// Limit to the first 500 errors/warnings
65
		const markers = markerService.read({ resource: modelData.model.uri, take: 500 });
E
Erich Gamma 已提交
66

B
Benjamin Pasero 已提交
67
		let newModelDecorations: editorCommon.IModelDeltaDecoration[] = markers.map((marker) => {
A
Alex Dima 已提交
68 69
			return {
				range: this._createDecorationRange(modelData.model, marker),
E
Erich Gamma 已提交
70 71 72
				options: this._createDecorationOption(marker)
			};
		});
A
Alex Dima 已提交
73 74

		modelData.acceptMarkerDecorations(newModelDecorations);
E
Erich Gamma 已提交
75 76
	}

77
	private static _createDecorationRange(model: editorCommon.IModel, rawMarker: IMarker): Range {
A
Alex Dima 已提交
78
		let marker = model.validateRange(new Range(rawMarker.startLineNumber, rawMarker.startColumn, rawMarker.endLineNumber, rawMarker.endColumn));
79
		let ret: Range = new Range(marker.startLineNumber, marker.startColumn, marker.endLineNumber, marker.endColumn);
E
Erich Gamma 已提交
80
		if (ret.isEmpty()) {
A
Alex Dima 已提交
81
			let word = model.getWordAtPosition(ret.getStartPosition());
E
Erich Gamma 已提交
82
			if (word) {
83
				ret = new Range(ret.startLineNumber, word.startColumn, ret.endLineNumber, word.endColumn);
E
Erich Gamma 已提交
84
			} else {
A
Alex Dima 已提交
85 86
				let maxColumn = model.getLineLastNonWhitespaceColumn(marker.startLineNumber) ||
					model.getLineMaxColumn(marker.startLineNumber);
E
Erich Gamma 已提交
87 88 89

				if (maxColumn === 1) {
					// empty line
90
					// console.warn('marker on empty line:', marker);
E
Erich Gamma 已提交
91 92
				} else if (ret.endColumn >= maxColumn) {
					// behind eol
93
					ret = new Range(ret.startLineNumber, maxColumn - 1, ret.endLineNumber, maxColumn);
E
Erich Gamma 已提交
94 95
				} else {
					// extend marker to width = 1
96
					ret = new Range(ret.startLineNumber, ret.startColumn, ret.endLineNumber, ret.endColumn + 1);
E
Erich Gamma 已提交
97 98 99
				}
			}
		} else if (rawMarker.endColumn === Number.MAX_VALUE && rawMarker.startColumn === 1 && ret.startLineNumber === ret.endLineNumber) {
A
Alex Dima 已提交
100
			let minColumn = model.getLineFirstNonWhitespaceColumn(rawMarker.startLineNumber);
E
Erich Gamma 已提交
101
			if (minColumn < ret.endColumn) {
102
				ret = new Range(ret.startLineNumber, minColumn, ret.endLineNumber, ret.endColumn);
E
Erich Gamma 已提交
103 104 105 106 107 108
				rawMarker.startColumn = minColumn;
			}
		}
		return ret;
	}

B
Benjamin Pasero 已提交
109
	private static _createDecorationOption(marker: IMarker): editorCommon.IModelDecorationOptions {
E
Erich Gamma 已提交
110 111 112 113 114 115 116 117 118 119 120

		let className: string;
		let color: string;
		let darkColor: string;

		switch (marker.severity) {
			case Severity.Ignore:
				// do something
				break;
			case Severity.Warning:
			case Severity.Info:
A
Alex Dima 已提交
121
				className = editorCommon.ClassName.EditorWarningDecoration;
E
Erich Gamma 已提交
122 123 124 125 126
				color = 'rgba(18,136,18,0.7)';
				darkColor = 'rgba(18,136,18,0.7)';
				break;
			case Severity.Error:
			default:
A
Alex Dima 已提交
127
				className = editorCommon.ClassName.EditorErrorDecoration;
E
Erich Gamma 已提交
128 129 130 131 132
				color = 'rgba(255,18,18,0.7)';
				darkColor = 'rgba(255,18,18,0.7)';
				break;
		}

133 134
		let hoverMessage: MarkedString[] = null;
		let {message, source} = marker;
E
Erich Gamma 已提交
135

136
		if (typeof message === 'string') {
J
Joao Moreno 已提交
137 138
			message = message.trim();

139
			if (source) {
J
Joao Moreno 已提交
140 141 142 143 144
				if (/\n/g.test(message)) {
					message = nls.localize('diagAndSourceMultiline', "[{0}]\n{1}", source, message);
				} else {
					message = nls.localize('diagAndSource', "[{0}] {1}", source, message);
				}
145
			}
J
Joao Moreno 已提交
146

147
			hoverMessage = [{ language: '_', value: message }];
148 149
		}

E
Erich Gamma 已提交
150
		return {
A
Alex Dima 已提交
151
			stickiness: editorCommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges,
E
Erich Gamma 已提交
152
			className,
153
			hoverMessage,
E
Erich Gamma 已提交
154 155 156
			overviewRuler: {
				color,
				darkColor,
A
Alex Dima 已提交
157
				position: editorCommon.OverviewRulerLane.Right
E
Erich Gamma 已提交
158 159 160 161 162
			}
		};
	}
}

163 164 165 166 167 168 169 170
interface IRawConfig {
	files?: {
		eol?: any;
	};
	editor?: {
		tabSize?: any;
		insertSpaces?: any;
		detectIndentation?: any;
171
		trimAutoWhitespace?: any;
172 173 174
	};
}

E
Erich Gamma 已提交
175
export class ModelServiceImpl implements IModelService {
176
	public _serviceBrand: any;
E
Erich Gamma 已提交
177 178 179

	private _markerService: IMarkerService;
	private _markerServiceSubscription: IDisposable;
180 181
	private _configurationService: IConfigurationService;
	private _configurationServiceSubscription: IDisposable;
E
Erich Gamma 已提交
182

A
Alex Dima 已提交
183 184 185
	private _onModelAdded: Emitter<editorCommon.IModel>;
	private _onModelRemoved: Emitter<editorCommon.IModel>;
	private _onModelModeChanged: Emitter<{ model: editorCommon.IModel; oldModeId: string; }>;
186 187

	private _modelCreationOptions: editorCommon.ITextModelCreationOptions;
A
Alex Dima 已提交
188 189 190 191

	/**
	 * All the models known in the system.
	 */
B
Benjamin Pasero 已提交
192
	private _models: { [modelId: string]: ModelData; };
E
Erich Gamma 已提交
193

194
	constructor(
195 196
		@IMarkerService markerService: IMarkerService,
		@IConfigurationService configurationService: IConfigurationService,
197
	) {
198
		this._modelCreationOptions = {
199 200 201
			tabSize: DEFAULT_INDENTATION.tabSize,
			insertSpaces: DEFAULT_INDENTATION.insertSpaces,
			detectIndentation: DEFAULT_INDENTATION.detectIndentation,
202
			defaultEOL: (platform.isLinux || platform.isMacintosh) ? editorCommon.DefaultEndOfLine.LF : editorCommon.DefaultEndOfLine.CRLF,
203
			trimAutoWhitespace: DEFAULT_TRIM_AUTO_WHITESPACE
204
		};
E
Erich Gamma 已提交
205
		this._markerService = markerService;
206
		this._configurationService = configurationService;
B
Benjamin Pasero 已提交
207 208 209 210 211 212 213 214 215 216
		this._models = {};


		this._onModelAdded = new Emitter<editorCommon.IModel>();
		this._onModelRemoved = new Emitter<editorCommon.IModel>();
		this._onModelModeChanged = new Emitter<{ model: editorCommon.IModel; oldModeId: string; }>();

		if (this._markerService) {
			this._markerServiceSubscription = this._markerService.onMarkerChanged(this._handleMarkerChange, this);
		}
217

B
Benjamin Pasero 已提交
218
		let readConfig = (config: IRawConfig) => {
J
Joao Moreno 已提交
219

220
			let tabSize = DEFAULT_INDENTATION.tabSize;
221 222 223
			if (config.editor && typeof config.editor.tabSize !== 'undefined') {
				let parsedTabSize = parseInt(config.editor.tabSize, 10);
				if (!isNaN(parsedTabSize)) {
224
					tabSize = parsedTabSize;
225 226 227
				}
			}

228
			let insertSpaces = DEFAULT_INDENTATION.insertSpaces;
229
			if (config.editor && typeof config.editor.insertSpaces !== 'undefined') {
230
				insertSpaces = (config.editor.insertSpaces === 'false' ? false : Boolean(config.editor.insertSpaces));
231 232 233
			}

			let newDefaultEOL = this._modelCreationOptions.defaultEOL;
234
			const eol = config.files && config.files.eol;
J
Joao Moreno 已提交
235
			if (eol === '\r\n') {
236
				newDefaultEOL = editorCommon.DefaultEndOfLine.CRLF;
J
Joao Moreno 已提交
237
			} else if (eol === '\n') {
238
				newDefaultEOL = editorCommon.DefaultEndOfLine.LF;
239
			}
240

241 242 243 244 245
			let trimAutoWhitespace = this._modelCreationOptions.trimAutoWhitespace;
			if (config.editor && typeof config.editor.trimAutoWhitespace !== 'undefined') {
				trimAutoWhitespace = (config.editor.trimAutoWhitespace === 'false' ? false : Boolean(config.editor.trimAutoWhitespace));
			}

246
			let detectIndentation = DEFAULT_INDENTATION.detectIndentation;
247 248 249
			if (config.editor && typeof config.editor.detectIndentation !== 'undefined') {
				detectIndentation = (config.editor.detectIndentation === 'false' ? false : Boolean(config.editor.detectIndentation));
			}
250

251 252 253
			this._setModelOptions({
				tabSize: tabSize,
				insertSpaces: insertSpaces,
254
				detectIndentation: detectIndentation,
255 256
				defaultEOL: newDefaultEOL,
				trimAutoWhitespace: trimAutoWhitespace
257
			});
258
		};
259 260

		this._configurationServiceSubscription = this._configurationService.onDidUpdateConfiguration(e => {
261
			readConfig(e.config);
262
		});
263
		readConfig(this._configurationService.getConfiguration());
E
Erich Gamma 已提交
264 265
	}

266 267 268 269 270 271 272 273 274
	public getCreationOptions(): editorCommon.ITextModelCreationOptions {
		return this._modelCreationOptions;
	}

	private _setModelOptions(newOpts: editorCommon.ITextModelCreationOptions): void {
		if (
			(this._modelCreationOptions.detectIndentation === newOpts.detectIndentation)
			&& (this._modelCreationOptions.insertSpaces === newOpts.insertSpaces)
			&& (this._modelCreationOptions.tabSize === newOpts.tabSize)
275
			&& (this._modelCreationOptions.trimAutoWhitespace === newOpts.trimAutoWhitespace)
276 277 278 279 280 281 282
		) {
			// Same indent opts, no need to touch created models
			this._modelCreationOptions = newOpts;
			return;
		}
		this._modelCreationOptions = newOpts;

283
		// Update options on all models
A
Alex Dima 已提交
284 285 286 287
		let keys = Object.keys(this._models);
		for (let i = 0, len = keys.length; i < len; i++) {
			let modelId = keys[i];
			let modelData = this._models[modelId];
288

A
Alex Dima 已提交
289 290
			if (this._modelCreationOptions.detectIndentation) {
				modelData.model.detectIndentation(this._modelCreationOptions.insertSpaces, this._modelCreationOptions.tabSize);
291 292 293
				modelData.model.updateOptions({
					trimAutoWhitespace: this._modelCreationOptions.trimAutoWhitespace
				});
A
Alex Dima 已提交
294 295 296
			} else {
				modelData.model.updateOptions({
					insertSpaces: this._modelCreationOptions.insertSpaces,
297 298
					tabSize: this._modelCreationOptions.tabSize,
					trimAutoWhitespace: this._modelCreationOptions.trimAutoWhitespace
A
Alex Dima 已提交
299
				});
300 301
			}
		}
302 303
	}

E
Erich Gamma 已提交
304
	public dispose(): void {
B
Benjamin Pasero 已提交
305
		if (this._markerServiceSubscription) {
E
Erich Gamma 已提交
306 307
			this._markerServiceSubscription.dispose();
		}
308
		this._configurationServiceSubscription.dispose();
E
Erich Gamma 已提交
309 310 311
	}

	private _handleMarkerChange(changedResources: URI[]): void {
A
Alex Dima 已提交
312 313 314 315
		changedResources.forEach((resource) => {
			let modelId = MODEL_ID(resource);
			let modelData = this._models[modelId];
			if (!modelData) {
E
Erich Gamma 已提交
316 317
				return;
			}
318
			ModelMarkerHandler.setMarkers(modelData, this._markerService);
E
Erich Gamma 已提交
319 320 321
		});
	}

S
Sandeep Somavarapu 已提交
322 323 324
	private _cleanUp(model: editorCommon.IModel): void {
		// clean up markers for internal, transient models
		if (model.uri.scheme === network.Schemas.inMemory
B
Benjamin Pasero 已提交
325 326 327 328 329
			|| model.uri.scheme === network.Schemas.internal
			|| model.uri.scheme === network.Schemas.vscode) {
			if (this._markerService) {
				this._markerService.read({ resource: model.uri }).map(marker => marker.owner).forEach(owner => this._markerService.remove(owner, [model.uri]));
			}
S
Sandeep Somavarapu 已提交
330 331 332
		}
	}

E
Erich Gamma 已提交
333 334
	// --- begin IModelService

A
Alex Dima 已提交
335
	private _createModelData(value: string | editorCommon.IRawText, languageIdentifier: LanguageIdentifier, resource: URI): ModelData {
A
Alex Dima 已提交
336
		// create & save the model
J
Johannes Rieken 已提交
337
		let model: Model;
338
		if (typeof value === 'string') {
A
Alex Dima 已提交
339
			model = Model.createFromString(value, this._modelCreationOptions, languageIdentifier, resource);
340
		} else {
A
Alex Dima 已提交
341
			model = new Model(value, languageIdentifier, resource);
342
		}
343
		let modelId = MODEL_ID(model.uri);
E
Erich Gamma 已提交
344 345 346

		if (this._models[modelId]) {
			// There already exists a model with this id => this is a programmer error
347
			throw new Error('ModelService: Cannot add model because it already exists!');
E
Erich Gamma 已提交
348 349
		}

A
Alex Dima 已提交
350 351
		let modelData = new ModelData(model, (modelData, events) => this._onModelEvents(modelData, events));
		this._models[modelId] = modelData;
E
Erich Gamma 已提交
352

A
Alex Dima 已提交
353
		return modelData;
E
Erich Gamma 已提交
354 355
	}

356
	public createModel(value: string | editorCommon.IRawText, modeOrPromise: TPromise<IMode> | IMode, resource: URI): editorCommon.IModel {
357 358 359
		let modelData: ModelData;

		if (!modeOrPromise || TPromise.is(modeOrPromise)) {
A
Alex Dima 已提交
360
			modelData = this._createModelData(value, PLAINTEXT_LANGUAGE_IDENTIFIER, resource);
361 362
			this.setMode(modelData.model, modeOrPromise);
		} else {
A
Alex Dima 已提交
363
			modelData = this._createModelData(value, modeOrPromise.getLanguageIdentifier(), resource);
364
		}
E
Erich Gamma 已提交
365

A
Alex Dima 已提交
366 367
		// handle markers (marker service => model)
		if (this._markerService) {
368
			ModelMarkerHandler.setMarkers(modelData, this._markerService);
E
Erich Gamma 已提交
369 370
		}

A
Alex Dima 已提交
371
		this._onModelAdded.fire(modelData.model);
E
Erich Gamma 已提交
372

A
Alex Dima 已提交
373
		return modelData.model;
E
Erich Gamma 已提交
374 375
	}

J
Johannes Rieken 已提交
376
	public setMode(model: editorCommon.IModel, modeOrPromise: TPromise<IMode> | IMode): void {
377 378 379 380 381 382
		if (!modeOrPromise) {
			return;
		}
		if (TPromise.is(modeOrPromise)) {
			modeOrPromise.then((mode) => {
				if (!model.isDisposed()) {
A
Alex Dima 已提交
383
					model.setMode(mode.getLanguageIdentifier());
384 385 386
				}
			});
		} else {
A
Alex Dima 已提交
387
			model.setMode(modeOrPromise.getLanguageIdentifier());
388 389 390
		}
	}

J
Johannes Rieken 已提交
391
	public destroyModel(resource: URI): void {
A
Alex Dima 已提交
392 393 394 395
		// We need to support that not all models get disposed through this service (i.e. model.dispose() should work!)
		let modelData = this._models[MODEL_ID(resource)];
		if (!modelData) {
			return;
E
Erich Gamma 已提交
396
		}
A
Alex Dima 已提交
397
		modelData.model.dispose();
E
Erich Gamma 已提交
398 399
	}

A
Alex Dima 已提交
400 401
	public getModels(): editorCommon.IModel[] {
		let ret: editorCommon.IModel[] = [];
A
Alex Dima 已提交
402 403 404 405 406

		let keys = Object.keys(this._models);
		for (let i = 0, len = keys.length; i < len; i++) {
			let modelId = keys[i];
			ret.push(this._models[modelId].model);
E
Erich Gamma 已提交
407
		}
A
Alex Dima 已提交
408

E
Erich Gamma 已提交
409 410 411
		return ret;
	}

A
Alex Dima 已提交
412
	public getModel(resource: URI): editorCommon.IModel {
A
Alex Dima 已提交
413 414 415 416
		let modelId = MODEL_ID(resource);
		let modelData = this._models[modelId];
		if (!modelData) {
			return null;
E
Erich Gamma 已提交
417
		}
A
Alex Dima 已提交
418
		return modelData.model;
E
Erich Gamma 已提交
419 420
	}

A
Alex Dima 已提交
421
	public get onModelAdded(): Event<editorCommon.IModel> {
422
		return this._onModelAdded ? this._onModelAdded.event : null;
E
Erich Gamma 已提交
423 424
	}

A
Alex Dima 已提交
425
	public get onModelRemoved(): Event<editorCommon.IModel> {
426
		return this._onModelRemoved ? this._onModelRemoved.event : null;
E
Erich Gamma 已提交
427 428
	}

A
Alex Dima 已提交
429
	public get onModelModeChanged(): Event<{ model: editorCommon.IModel; oldModeId: string; }> {
430
		return this._onModelModeChanged ? this._onModelModeChanged.event : null;
E
Erich Gamma 已提交
431 432 433 434
	}

	// --- end IModelService

B
Benjamin Pasero 已提交
435
	private _onModelDisposing(model: editorCommon.IModel): void {
436
		let modelId = MODEL_ID(model.uri);
A
Alex Dima 已提交
437 438 439 440 441
		let modelData = this._models[modelId];

		delete this._models[modelId];
		modelData.dispose();

442
		this._cleanUp(model);
A
Alex Dima 已提交
443 444 445
		this._onModelRemoved.fire(model);
	}

A
Alex Dima 已提交
446
	private _onModelEvents(modelData: ModelData, events: EmitterEvent[]): void {
E
Erich Gamma 已提交
447

448
		// First look for dispose
A
Alex Dima 已提交
449 450
		for (let i = 0, len = events.length; i < len; i++) {
			let e = events[i];
A
Alex Dima 已提交
451
			if (e.getType() === editorCommon.EventType.ModelDispose) {
452 453 454 455 456
				this._onModelDisposing(modelData.model);
				// no more processing since model got disposed
				return;
			}
		}
E
Erich Gamma 已提交
457

458 459 460
		// Second, look for mode change
		for (let i = 0, len = events.length; i < len; i++) {
			let e = events[i];
A
Alex Dima 已提交
461
			if (e.getType() === editorCommon.EventType.ModelLanguageChanged) {
462 463
				this._onModelModeChanged.fire({
					model: modelData.model,
464
					oldModeId: (<editorCommon.IModelLanguageChangedEvent>e.getData()).oldLanguage
465
				});
E
Erich Gamma 已提交
466 467 468 469
			}
		}
	}
}