standaloneEditor.ts 13.4 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!./media/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';
J
Johannes Rieken 已提交
10 11 12 13
import { StandaloneEditor, IStandaloneCodeEditor, StandaloneDiffEditor, IStandaloneDiffEditor, IEditorConstructionOptions, IDiffEditorConstructionOptions } from 'vs/editor/browser/standalone/standaloneCodeEditor';
import { ScrollbarVisibility } from 'vs/base/common/scrollable';
import { IEditorOverrideServices, DynamicStandaloneServices, StaticServices } from 'vs/editor/browser/standalone/standaloneServices';
import { IDisposable } from 'vs/base/common/lifecycle';
14
import URI from 'vs/base/common/uri';
J
Johannes Rieken 已提交
15 16 17 18
import { TPromise } from 'vs/base/common/winjs.base';
import { OpenerService } from 'vs/platform/opener/browser/openerService';
import { IOpenerService } from 'vs/platform/opener/common/opener';
import { Colorizer, IColorizerElementOptions, IColorizerOptions } from 'vs/editor/browser/standalone/colorizer';
19
import { SimpleEditorService, SimpleEditorModelResolverService } from 'vs/editor/browser/standalone/simpleServices';
20
import * as modes from 'vs/editor/common/modes';
J
Johannes Rieken 已提交
21 22 23 24 25 26 27 28 29 30 31
import { IWebWorkerOptions, MonacoWebWorker, createWebWorker as actualCreateWebWorker } from 'vs/editor/common/services/webWorker';
import { IMarkerData } from 'vs/platform/markers/common/markers';
import { DiffNavigator } from 'vs/editor/contrib/diffNavigator/common/diffNavigator';
import { IEditorService } from 'vs/platform/editor/common/editor';
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';
import { ICodeEditorService } from 'vs/editor/common/services/codeEditorService';
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorkerService';
32
import { ITextModelService } from 'vs/editor/common/services/resolverService';
A
Alex Dima 已提交
33
import { NULL_STATE, nullTokenize } from 'vs/editor/common/modes/nullMode';
34
import { IStandaloneThemeData, IStandaloneThemeService } from 'vs/editor/common/services/standaloneThemeService';
A
Alex Dima 已提交
35
import { Token } from 'vs/editor/common/core/token';
36
import { FontInfo, BareFontInfo } from 'vs/editor/common/config/fontInfo';
37 38
import * as editorOptions from 'vs/editor/common/config/editorOptions';
import { CursorChangeReason } from 'vs/editor/common/controller/cursorEvents';
39

40 41 42
/**
 * @internal
 */
43 44
export function setupServices(overrides: IEditorOverrideServices): any {
	return StaticServices.init(overrides);
45 46
}

J
Johannes Rieken 已提交
47
function withAllStandaloneServices<T extends editorCommon.IEditor>(domElement: HTMLElement, override: IEditorOverrideServices, callback: (services: DynamicStandaloneServices) => T): T {
48 49 50 51 52 53 54
	let services = new DynamicStandaloneServices(domElement, override);

	// The editorService is a lovely beast. It needs to point back to the code editor instance...
	let simpleEditorService: SimpleEditorService = null;
	if (!services.has(IEditorService)) {
		simpleEditorService = new SimpleEditorService();
		services.set(IEditorService, simpleEditorService);
55 56
	}

57
	let simpleEditorModelResolverService: SimpleEditorModelResolverService = null;
58
	if (!services.has(ITextModelService)) {
59
		simpleEditorModelResolverService = new SimpleEditorModelResolverService();
60
		services.set(ITextModelService, simpleEditorModelResolverService);
61 62
	}

63 64
	if (!services.has(IOpenerService)) {
		services.set(IOpenerService, new OpenerService(services.get(IEditorService), services.get(ICommandService)));
65 66
	}

67
	let result = callback(services);
68

69 70
	if (simpleEditorService) {
		simpleEditorService.setEditor(result);
71 72
	}

73 74 75 76
	if (simpleEditorModelResolverService) {
		simpleEditorModelResolverService.setEditor(result);
	}

77 78 79
	return result;
}

A
Alex Dima 已提交
80
/**
81
 * Create a new editor under `domElement`.
A
Alex Dima 已提交
82 83 84
 * `domElement` should be empty (not contain other dom nodes).
 * The editor will read the size of `domElement`.
 */
J
Johannes Rieken 已提交
85
export function create(domElement: HTMLElement, options?: IEditorConstructionOptions, override?: IEditorOverrideServices): IStandaloneCodeEditor {
86 87 88 89 90 91 92 93 94 95
	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 已提交
96
			services.get(IContextViewService),
97
			services.get(IStandaloneThemeService)
98 99 100
		);
	});
}
101

102 103 104 105 106 107 108 109 110 111 112
/**
 * 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);
	});
}

113 114 115 116 117
/**
 * 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 已提交
118
export function createDiffEditor(domElement: HTMLElement, options?: IDiffEditorConstructionOptions, override?: IEditorOverrideServices): IStandaloneDiffEditor {
119 120 121 122 123 124 125 126 127
	return withAllStandaloneServices(domElement, override, (services) => {
		return new StandaloneDiffEditor(
			domElement,
			options,
			services,
			services.get(IInstantiationService),
			services.get(IContextKeyService),
			services.get(IKeybindingService),
			services.get(IContextViewService),
128
			services.get(IEditorWorkerService),
129 130
			services.get(ICodeEditorService),
			services.get(IStandaloneThemeService)
131 132
		);
	});
133 134
}

A
Alex Dima 已提交
135
export interface IDiffNavigator {
A
Alex Dima 已提交
136 137
	revealFirst: boolean;

J
Johannes Rieken 已提交
138 139 140 141
	canNavigate(): boolean;
	next(): void;
	previous(): void;
	dispose(): void;
A
Alex Dima 已提交
142 143 144
}

export interface IDiffNavigatorOptions {
145 146 147
	readonly followsCaret?: boolean;
	readonly ignoreCharChanges?: boolean;
	readonly alwaysRevealFirst?: boolean;
A
Alex Dima 已提交
148 149
}

J
Johannes Rieken 已提交
150
export function createDiffNavigator(diffEditor: IStandaloneDiffEditor, opts?: IDiffNavigatorOptions): IDiffNavigator {
A
Alex Dima 已提交
151 152 153
	return new DiffNavigator(diffEditor, opts);
}

A
Alex Dima 已提交
154
function doCreateModel(value: string, mode: TPromise<modes.IMode>, uri?: URI): editorCommon.IModel {
155
	return StaticServices.modelService.get().createModel(value, mode, uri);
156 157
}

A
Alex Dima 已提交
158 159 160 161
/**
 * 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 已提交
162
export function createModel(value: string, language?: string, uri?: URI): editorCommon.IModel {
A
Alex Dima 已提交
163
	value = value || '';
164

A
Alex Dima 已提交
165 166 167 168 169 170 171 172 173
	if (!language) {
		let path = uri ? uri.path : null;

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

174
		return doCreateModel(value, StaticServices.modeService.get().getOrCreateModeByFilenameOrFirstLine(path, firstLine), uri);
175
	}
176
	return doCreateModel(value, StaticServices.modeService.get().getOrCreateMode(language), uri);
A
Alex Dima 已提交
177 178
}

A
Alex Dima 已提交
179 180 181
/**
 * Change the language for a model.
 */
A
Alex Dima 已提交
182
export function setModelLanguage(model: editorCommon.IModel, language: string): void {
183
	StaticServices.modelService.get().setMode(model, StaticServices.modeService.get().getOrCreateMode(language));
184 185
}

A
Alex Dima 已提交
186 187 188
/**
 * Set the markers for a model.
 */
A
Alex Dima 已提交
189
export function setModelMarkers(model: editorCommon.IModel, owner: string, markers: IMarkerData[]): void {
190 191 192
	if (model) {
		StaticServices.markerService.get().changeOne(owner, model.uri, markers);
	}
193 194
}

A
Alex Dima 已提交
195 196 197
/**
 * Get the model that has `uri` if it exists.
 */
A
Alex Dima 已提交
198
export function getModel(uri: URI): editorCommon.IModel {
199
	return StaticServices.modelService.get().getModel(uri);
200 201
}

A
Alex Dima 已提交
202 203 204
/**
 * Get all the created models.
 */
A
Alex Dima 已提交
205
export function getModels(): editorCommon.IModel[] {
206
	return StaticServices.modelService.get().getModels();
A
Alex Dima 已提交
207 208
}

A
Alex Dima 已提交
209 210
/**
 * Emitted when a model is created.
A
Alex Dima 已提交
211
 * @event
A
Alex Dima 已提交
212
 */
A
Alex Dima 已提交
213
export function onDidCreateModel(listener: (model: editorCommon.IModel) => void): IDisposable {
214
	return StaticServices.modelService.get().onModelAdded(listener);
215 216
}

A
Alex Dima 已提交
217 218
/**
 * Emitted right before a model is disposed.
A
Alex Dima 已提交
219
 * @event
A
Alex Dima 已提交
220
 */
A
Alex Dima 已提交
221
export function onWillDisposeModel(listener: (model: editorCommon.IModel) => void): IDisposable {
222
	return StaticServices.modelService.get().onModelRemoved(listener);
223 224
}

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

A
Alex Dima 已提交
238 239 240 241
/**
 * 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 已提交
242
export function createWebWorker<T>(opts: IWebWorkerOptions): MonacoWebWorker<T> {
243
	return actualCreateWebWorker<T>(StaticServices.modelService.get(), opts);
244 245
}

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

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

A
Alex Dima 已提交
260 261 262
/**
 * Colorize a line in a model.
 */
A
Alex Dima 已提交
263
export function colorizeModelLine(model: editorCommon.IModel, lineNumber: number, tabSize: number = 4): string {
264 265 266
	return Colorizer.colorizeModelLine(model, lineNumber, tabSize);
}

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

/**
 * 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 已提交
298
		result[i] = tokenizationResult.tokens;
A
Alex Dima 已提交
299 300 301 302 303
		state = tokenizationResult.endState;
	}
	return result;
}

304 305 306
/**
 * Define a new theme.
 */
307
export function defineTheme(themeName: string, themeData: IStandaloneThemeData): void {
308
	StaticServices.standaloneThemeService.get().defineTheme(themeName, themeData);
309 310
}

311 312 313 314 315 316 317
/**
 * Switches to a theme.
 */
export function setTheme(themeName: string): void {
	StaticServices.standaloneThemeService.get().setTheme(themeName);
}

318 319 320
/**
 * @internal
 */
321
export function createMonacoEditorAPI(): typeof monaco.editor {
A
Alex Dima 已提交
322
	return {
323
		// methods
A
Alex Dima 已提交
324
		create: <any>create,
325
		onDidCreateEditor: <any>onDidCreateEditor,
326 327
		createDiffEditor: <any>createDiffEditor,
		createDiffNavigator: <any>createDiffNavigator,
328 329

		createModel: createModel,
A
Alex Dima 已提交
330 331
		setModelLanguage: setModelLanguage,
		setModelMarkers: setModelMarkers,
332 333 334 335
		getModels: getModels,
		getModel: getModel,
		onDidCreateModel: onDidCreateModel,
		onWillDisposeModel: onWillDisposeModel,
A
Alex Dima 已提交
336
		onDidChangeModelLanguage: onDidChangeModelLanguage,
337 338 339 340 341 342


		createWebWorker: createWebWorker,
		colorizeElement: colorizeElement,
		colorize: colorize,
		colorizeModelLine: colorizeModelLine,
A
Alex Dima 已提交
343
		tokenize: tokenize,
344
		defineTheme: defineTheme,
345
		setTheme: setTheme,
346 347 348

		// enums
		ScrollbarVisibility: ScrollbarVisibility,
349
		WrappingIndent: editorOptions.WrappingIndent,
350 351 352 353 354
		OverviewRulerLane: editorCommon.OverviewRulerLane,
		EndOfLinePreference: editorCommon.EndOfLinePreference,
		DefaultEndOfLine: editorCommon.DefaultEndOfLine,
		EndOfLineSequence: editorCommon.EndOfLineSequence,
		TrackedRangeStickiness: editorCommon.TrackedRangeStickiness,
355
		CursorChangeReason: CursorChangeReason,
A
Alex Dima 已提交
356
		MouseTargetType: MouseTargetType,
357 358
		TextEditorCursorStyle: editorOptions.TextEditorCursorStyle,
		TextEditorCursorBlinkingStyle: editorOptions.TextEditorCursorBlinkingStyle,
359 360
		ContentWidgetPositionPreference: ContentWidgetPositionPreference,
		OverlayWidgetPositionPreference: OverlayWidgetPositionPreference,
361
		RenderMinimap: editorOptions.RenderMinimap,
362 363

		// classes
364
		InternalEditorOptions: <any>editorOptions.InternalEditorOptions,
365 366
		BareFontInfo: <any>BareFontInfo,
		FontInfo: <any>FontInfo,
367
		TextModelResolvedOptions: <any>editorCommon.TextModelResolvedOptions,
A
Alex Dima 已提交
368
		FindMatch: <any>editorCommon.FindMatch,
369 370

		// vars
A
Alex Dima 已提交
371
		EditorType: editorCommon.EditorType
A
Alex Dima 已提交
372
	};
373
}