standaloneEditor.ts 14.0 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';

7
import 'vs/css!./standalone-tokens';
A
Alex Dima 已提交
8
import * as editorCommon from 'vs/editor/common/editorCommon';
A
Alex Dima 已提交
9
import { ICodeEditor, ContentWidgetPositionPreference, OverlayWidgetPositionPreference, MouseTargetType } from 'vs/editor/browser/editorBrowser';
10
import { StandaloneEditor, IStandaloneCodeEditor, StandaloneDiffEditor, IStandaloneDiffEditor, IEditorConstructionOptions, IDiffEditorConstructionOptions } from 'vs/editor/standalone/browser/standaloneCodeEditor';
J
Johannes Rieken 已提交
11
import { ScrollbarVisibility } from 'vs/base/common/scrollable';
12
import { IEditorOverrideServices, DynamicStandaloneServices, StaticServices } from 'vs/editor/standalone/browser/standaloneServices';
J
Johannes Rieken 已提交
13
import { IDisposable } from 'vs/base/common/lifecycle';
14
import { URI } from 'vs/base/common/uri';
J
Johannes Rieken 已提交
15
import { TPromise } from 'vs/base/common/winjs.base';
16
import { OpenerService } from 'vs/editor/browser/services/openerService';
J
Johannes Rieken 已提交
17
import { IOpenerService } from 'vs/platform/opener/common/opener';
18
import { Colorizer, IColorizerElementOptions, IColorizerOptions } from 'vs/editor/standalone/browser/colorizer';
19
import { SimpleEditorModelResolverService } from 'vs/editor/standalone/browser/simpleServices';
20
import * as modes from 'vs/editor/common/modes';
J
Johannes Rieken 已提交
21
import { IWebWorkerOptions, MonacoWebWorker, createWebWorker as actualCreateWebWorker } from 'vs/editor/common/services/webWorker';
R
rsafronov 已提交
22
import { IMarkerData, IMarker } from 'vs/platform/markers/common/markers';
23
import { DiffNavigator } from 'vs/editor/browser/widget/diffNavigator';
J
Johannes Rieken 已提交
24 25 26 27 28
import { ICommandService } from 'vs/platform/commands/common/commands';
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
29
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
J
Johannes Rieken 已提交
30
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorkerService';
31
import { ITextModelService } from 'vs/editor/common/services/resolverService';
A
Alex Dima 已提交
32
import { NULL_STATE, nullTokenize } from 'vs/editor/common/modes/nullMode';
33
import { IStandaloneThemeData, IStandaloneThemeService } from 'vs/editor/standalone/common/standaloneThemeService';
A
Alex Dima 已提交
34
import { Token } from 'vs/editor/common/core/token';
35
import { FontInfo, BareFontInfo } from 'vs/editor/common/config/fontInfo';
36 37
import * as editorOptions from 'vs/editor/common/config/editorOptions';
import { CursorChangeReason } from 'vs/editor/common/controller/cursorEvents';
A
Alex Dima 已提交
38
import { ITextModel, OverviewRulerLane, EndOfLinePreference, DefaultEndOfLine, EndOfLineSequence, TrackedRangeStickiness, TextModelResolvedOptions, FindMatch } from 'vs/editor/common/model';
39
import { INotificationService } from 'vs/platform/notification/common/notification';
40
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
41

J
Johannes Rieken 已提交
42
function withAllStandaloneServices<T extends editorCommon.IEditor>(domElement: HTMLElement, override: IEditorOverrideServices, callback: (services: DynamicStandaloneServices) => T): T {
43 44
	let services = new DynamicStandaloneServices(domElement, override);

45
	let simpleEditorModelResolverService: SimpleEditorModelResolverService = null;
46
	if (!services.has(ITextModelService)) {
47
		simpleEditorModelResolverService = new SimpleEditorModelResolverService();
48
		services.set(ITextModelService, simpleEditorModelResolverService);
49 50
	}

51
	if (!services.has(IOpenerService)) {
52
		services.set(IOpenerService, new OpenerService(services.get(ICodeEditorService), services.get(ICommandService)));
53 54
	}

55
	let result = callback(services);
56

57 58 59 60
	if (simpleEditorModelResolverService) {
		simpleEditorModelResolverService.setEditor(result);
	}

61 62 63
	return result;
}

A
Alex Dima 已提交
64
/**
65
 * Create a new editor under `domElement`.
A
Alex Dima 已提交
66 67 68
 * `domElement` should be empty (not contain other dom nodes).
 * The editor will read the size of `domElement`.
 */
J
Johannes Rieken 已提交
69
export function create(domElement: HTMLElement, options?: IEditorConstructionOptions, override?: IEditorOverrideServices): IStandaloneCodeEditor {
70 71 72 73 74 75 76 77 78 79
	return withAllStandaloneServices(domElement, override, (services) => {
		return new StandaloneEditor(
			domElement,
			options,
			services,
			services.get(IInstantiationService),
			services.get(ICodeEditorService),
			services.get(ICommandService),
			services.get(IContextKeyService),
			services.get(IKeybindingService),
A
Alex Dima 已提交
80
			services.get(IContextViewService),
81
			services.get(IStandaloneThemeService),
82 83
			services.get(INotificationService),
			services.get(IConfigurationService),
84 85 86
		);
	});
}
87

88 89 90 91 92 93 94 95 96 97 98
/**
 * Emitted when an editor is created.
 * Creating a diff editor might cause this listener to be invoked with the two editors.
 * @event
 */
export function onDidCreateEditor(listener: (codeEditor: ICodeEditor) => void): IDisposable {
	return StaticServices.codeEditorService.get().onCodeEditorAdd((editor) => {
		listener(<ICodeEditor>editor);
	});
}

99 100 101 102 103
/**
 * Create a new diff editor under `domElement`.
 * `domElement` should be empty (not contain other dom nodes).
 * The editor will read the size of `domElement`.
 */
J
Johannes Rieken 已提交
104
export function createDiffEditor(domElement: HTMLElement, options?: IDiffEditorConstructionOptions, override?: IEditorOverrideServices): IStandaloneDiffEditor {
105 106 107 108 109 110 111 112 113
	return withAllStandaloneServices(domElement, override, (services) => {
		return new StandaloneDiffEditor(
			domElement,
			options,
			services,
			services.get(IInstantiationService),
			services.get(IContextKeyService),
			services.get(IKeybindingService),
			services.get(IContextViewService),
114
			services.get(IEditorWorkerService),
115
			services.get(ICodeEditorService),
116
			services.get(IStandaloneThemeService),
117 118
			services.get(INotificationService),
			services.get(IConfigurationService),
119 120
		);
	});
121 122
}

A
Alex Dima 已提交
123
export interface IDiffNavigator {
J
Johannes Rieken 已提交
124 125 126 127
	canNavigate(): boolean;
	next(): void;
	previous(): void;
	dispose(): void;
A
Alex Dima 已提交
128 129 130
}

export interface IDiffNavigatorOptions {
131 132 133
	readonly followsCaret?: boolean;
	readonly ignoreCharChanges?: boolean;
	readonly alwaysRevealFirst?: boolean;
A
Alex Dima 已提交
134 135
}

J
Johannes Rieken 已提交
136
export function createDiffNavigator(diffEditor: IStandaloneDiffEditor, opts?: IDiffNavigatorOptions): IDiffNavigator {
A
Alex Dima 已提交
137 138 139
	return new DiffNavigator(diffEditor, opts);
}

A
Alex Dima 已提交
140
function doCreateModel(value: string, mode: TPromise<modes.IMode>, uri?: URI): ITextModel {
141
	return StaticServices.modelService.get().createModel(value, mode, uri);
142 143
}

A
Alex Dima 已提交
144 145 146 147
/**
 * Create a new editor model.
 * You can specify the language that should be set for this model or let the language be inferred from the `uri`.
 */
A
Alex Dima 已提交
148
export function createModel(value: string, language?: string, uri?: URI): ITextModel {
A
Alex Dima 已提交
149
	value = value || '';
150

A
Alex Dima 已提交
151 152 153 154 155 156 157 158 159
	if (!language) {
		let path = uri ? uri.path : null;

		let firstLF = value.indexOf('\n');
		let firstLine = value;
		if (firstLF !== -1) {
			firstLine = value.substring(0, firstLF);
		}

B
Benjamin Pasero 已提交
160
		return doCreateModel(value, StaticServices.modeService.get().getOrCreateModeByFilepathOrFirstLine(path, firstLine), uri);
161
	}
162
	return doCreateModel(value, StaticServices.modeService.get().getOrCreateMode(language), uri);
A
Alex Dima 已提交
163 164
}

A
Alex Dima 已提交
165 166 167
/**
 * Change the language for a model.
 */
M
Martin Aeschlimann 已提交
168 169
export function setModelLanguage(model: ITextModel, languageId: string): void {
	StaticServices.modelService.get().setMode(model, StaticServices.modeService.get().getOrCreateMode(languageId));
170 171
}

A
Alex Dima 已提交
172 173 174
/**
 * Set the markers for a model.
 */
A
Alex Dima 已提交
175
export function setModelMarkers(model: ITextModel, owner: string, markers: IMarkerData[]): void {
176 177 178
	if (model) {
		StaticServices.markerService.get().changeOne(owner, model.uri, markers);
	}
179 180
}

R
Roman Safronov 已提交
181
/**
R
rebornix 已提交
182
 * Get markers for owner and/or resource
R
rebornix 已提交
183
 * @returns {IMarker[]} list of markers
R
Roman Safronov 已提交
184 185
 * @param filter
 */
R
rsafronov 已提交
186
export function getModelMarkers(filter: { owner?: string, resource?: URI, take?: number }): IMarker[] {
R
Roman Safronov 已提交
187 188 189
	return StaticServices.markerService.get().read(filter);
}

A
Alex Dima 已提交
190 191 192
/**
 * Get the model that has `uri` if it exists.
 */
A
Alex Dima 已提交
193
export function getModel(uri: URI): ITextModel {
194
	return StaticServices.modelService.get().getModel(uri);
195 196
}

A
Alex Dima 已提交
197 198 199
/**
 * Get all the created models.
 */
A
Alex Dima 已提交
200
export function getModels(): ITextModel[] {
201
	return StaticServices.modelService.get().getModels();
A
Alex Dima 已提交
202 203
}

A
Alex Dima 已提交
204 205
/**
 * Emitted when a model is created.
A
Alex Dima 已提交
206
 * @event
A
Alex Dima 已提交
207
 */
A
Alex Dima 已提交
208
export function onDidCreateModel(listener: (model: ITextModel) => void): IDisposable {
209
	return StaticServices.modelService.get().onModelAdded(listener);
210 211
}

A
Alex Dima 已提交
212 213
/**
 * Emitted right before a model is disposed.
A
Alex Dima 已提交
214
 * @event
A
Alex Dima 已提交
215
 */
A
Alex Dima 已提交
216
export function onWillDisposeModel(listener: (model: ITextModel) => void): IDisposable {
217
	return StaticServices.modelService.get().onModelRemoved(listener);
218 219
}

A
Alex Dima 已提交
220 221
/**
 * Emitted when a different language is set to a model.
A
Alex Dima 已提交
222
 * @event
A
Alex Dima 已提交
223
 */
A
Alex Dima 已提交
224
export function onDidChangeModelLanguage(listener: (e: { readonly model: ITextModel; readonly oldLanguage: string; }) => void): IDisposable {
225
	return StaticServices.modelService.get().onModelModeChanged((e) => {
A
Alex Dima 已提交
226 227 228 229 230
		listener({
			model: e.model,
			oldLanguage: e.oldModeId
		});
	});
231 232
}

A
Alex Dima 已提交
233 234 235 236
/**
 * Create a new web worker that has model syncing capabilities built in.
 * Specify an AMD module to load that will `create` an object that will be proxied.
 */
J
Johannes Rieken 已提交
237
export function createWebWorker<T>(opts: IWebWorkerOptions): MonacoWebWorker<T> {
238
	return actualCreateWebWorker<T>(StaticServices.modelService.get(), opts);
239 240
}

A
Alex Dima 已提交
241 242 243
/**
 * Colorize the contents of `domNode` using attribute `data-lang`.
 */
J
Johannes Rieken 已提交
244
export function colorizeElement(domNode: HTMLElement, options: IColorizerElementOptions): TPromise<void> {
245
	return Colorizer.colorizeElement(StaticServices.standaloneThemeService.get(), StaticServices.modeService.get(), domNode, options);
246 247
}

A
Alex Dima 已提交
248 249 250
/**
 * Colorize `text` using language `languageId`.
 */
J
Johannes Rieken 已提交
251
export function colorize(text: string, languageId: string, options: IColorizerOptions): TPromise<string> {
252
	return Colorizer.colorize(StaticServices.modeService.get(), text, languageId, options);
253 254
}

A
Alex Dima 已提交
255 256 257
/**
 * Colorize a line in a model.
 */
A
Alex Dima 已提交
258
export function colorizeModelLine(model: ITextModel, lineNumber: number, tabSize: number = 4): string {
259 260 261
	return Colorizer.colorizeModelLine(model, lineNumber, tabSize);
}

A
Alex Dima 已提交
262 263 264
/**
 * @internal
 */
M
Martin Aeschlimann 已提交
265 266
function getSafeTokenizationSupport(language: string): modes.ITokenizationSupport {
	let tokenizationSupport = modes.TokenizationRegistry.get(language);
A
Alex Dima 已提交
267 268 269 270 271
	if (tokenizationSupport) {
		return tokenizationSupport;
	}
	return {
		getInitialState: () => NULL_STATE,
M
Martin Aeschlimann 已提交
272
		tokenize: (line: string, state: modes.IState, deltaOffset: number) => nullTokenize(language, line, state, deltaOffset),
A
Tweaks  
Alex Dima 已提交
273
		tokenize2: undefined,
A
Alex Dima 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
	};
}

/**
 * Tokenize `text` using language `languageId`
 */
export function tokenize(text: string, languageId: string): Token[][] {
	let modeService = StaticServices.modeService.get();
	// Needed in order to get the mode registered for subsequent look-ups
	modeService.getOrCreateMode(languageId);

	let tokenizationSupport = getSafeTokenizationSupport(languageId);
	let lines = text.split(/\r\n|\r|\n/);
	let result: Token[][] = [];
	let state = tokenizationSupport.getInitialState();
	for (let i = 0, len = lines.length; i < len; i++) {
		let line = lines[i];
		let tokenizationResult = tokenizationSupport.tokenize(line, state, 0);

A
Alex Dima 已提交
293
		result[i] = tokenizationResult.tokens;
A
Alex Dima 已提交
294 295 296 297 298
		state = tokenizationResult.endState;
	}
	return result;
}

299
/**
M
Martin Aeschlimann 已提交
300
 * Define a new theme or updte an existing theme.
301
 */
302
export function defineTheme(themeName: string, themeData: IStandaloneThemeData): void {
303
	StaticServices.standaloneThemeService.get().defineTheme(themeName, themeData);
304 305
}

306 307 308 309 310 311 312
/**
 * Switches to a theme.
 */
export function setTheme(themeName: string): void {
	StaticServices.standaloneThemeService.get().setTheme(themeName);
}

313 314 315 316 317 318 319 320 321 322 323 324
/**
 * @internal
 * --------------------------------------------
 * This is repeated here so it can be exported
 * because TS inlines const enums
 * --------------------------------------------
 */
enum ScrollType {
	Smooth = 0,
	Immediate = 1,
}

325 326 327 328 329 330 331 332 333 334 335
/**
 * @internal
 * --------------------------------------------
 * This is repeated here so it can be exported
 * because TS inlines const enums
 * --------------------------------------------
 */
enum RenderLineNumbersType {
	Off = 0,
	On = 1,
	Relative = 2,
336 337
	Interval = 3,
	Custom = 4
338 339
}

340 341 342
/**
 * @internal
 */
343
export function createMonacoEditorAPI(): typeof monaco.editor {
A
Alex Dima 已提交
344
	return {
345
		// methods
A
Alex Dima 已提交
346
		create: <any>create,
347
		onDidCreateEditor: <any>onDidCreateEditor,
348 349
		createDiffEditor: <any>createDiffEditor,
		createDiffNavigator: <any>createDiffNavigator,
350 351

		createModel: createModel,
A
Alex Dima 已提交
352 353
		setModelLanguage: setModelLanguage,
		setModelMarkers: setModelMarkers,
R
Roman Safronov 已提交
354
		getModelMarkers: getModelMarkers,
355 356 357 358
		getModels: getModels,
		getModel: getModel,
		onDidCreateModel: onDidCreateModel,
		onWillDisposeModel: onWillDisposeModel,
A
Alex Dima 已提交
359
		onDidChangeModelLanguage: onDidChangeModelLanguage,
360 361 362 363 364 365


		createWebWorker: createWebWorker,
		colorizeElement: colorizeElement,
		colorize: colorize,
		colorizeModelLine: colorizeModelLine,
A
Alex Dima 已提交
366
		tokenize: tokenize,
367
		defineTheme: defineTheme,
368
		setTheme: setTheme,
369 370 371

		// enums
		ScrollbarVisibility: ScrollbarVisibility,
372
		WrappingIndent: editorOptions.WrappingIndent,
373 374 375 376 377
		OverviewRulerLane: OverviewRulerLane,
		EndOfLinePreference: EndOfLinePreference,
		DefaultEndOfLine: DefaultEndOfLine,
		EndOfLineSequence: EndOfLineSequence,
		TrackedRangeStickiness: TrackedRangeStickiness,
378
		CursorChangeReason: CursorChangeReason,
A
Alex Dima 已提交
379
		MouseTargetType: MouseTargetType,
380 381
		TextEditorCursorStyle: editorOptions.TextEditorCursorStyle,
		TextEditorCursorBlinkingStyle: editorOptions.TextEditorCursorBlinkingStyle,
382 383
		ContentWidgetPositionPreference: ContentWidgetPositionPreference,
		OverlayWidgetPositionPreference: OverlayWidgetPositionPreference,
384
		RenderMinimap: editorOptions.RenderMinimap,
385
		ScrollType: <any>ScrollType,
386
		RenderLineNumbersType: <any>RenderLineNumbersType,
387 388

		// classes
389
		InternalEditorOptions: <any>editorOptions.InternalEditorOptions,
390 391
		BareFontInfo: <any>BareFontInfo,
		FontInfo: <any>FontInfo,
392 393
		TextModelResolvedOptions: <any>TextModelResolvedOptions,
		FindMatch: <any>FindMatch,
394 395

		// vars
A
Alex Dima 已提交
396
		EditorType: editorCommon.EditorType
M
Martin Aeschlimann 已提交
397

A
Alex Dima 已提交
398
	};
399
}