standaloneEditor.ts 13.3 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 { ITextModelResolverService } 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 58 59 60 61 62
	let simpleEditorModelResolverService: SimpleEditorModelResolverService = null;
	if (!services.has(ITextModelResolverService)) {
		simpleEditorModelResolverService = new SimpleEditorModelResolverService();
		services.set(ITextModelResolverService, simpleEditorModelResolverService);
	}

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(IStandaloneThemeService),
129
			services.get(IEditorWorkerService),
130 131
			services.get(ICodeEditorService),
			services.get(IStandaloneThemeService)
132 133
		);
	});
134 135
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

312 313 314
/**
 * @internal
 */
315
export function createMonacoEditorAPI(): typeof monaco.editor {
A
Alex Dima 已提交
316
	return {
317
		// methods
A
Alex Dima 已提交
318
		create: <any>create,
319
		onDidCreateEditor: <any>onDidCreateEditor,
320 321
		createDiffEditor: <any>createDiffEditor,
		createDiffNavigator: <any>createDiffNavigator,
322 323

		createModel: createModel,
A
Alex Dima 已提交
324 325
		setModelLanguage: setModelLanguage,
		setModelMarkers: setModelMarkers,
326 327 328 329
		getModels: getModels,
		getModel: getModel,
		onDidCreateModel: onDidCreateModel,
		onWillDisposeModel: onWillDisposeModel,
A
Alex Dima 已提交
330
		onDidChangeModelLanguage: onDidChangeModelLanguage,
331 332 333 334 335 336


		createWebWorker: createWebWorker,
		colorizeElement: colorizeElement,
		colorize: colorize,
		colorizeModelLine: colorizeModelLine,
A
Alex Dima 已提交
337
		tokenize: tokenize,
338
		defineTheme: defineTheme,
339 340 341

		// enums
		ScrollbarVisibility: ScrollbarVisibility,
342
		WrappingIndent: editorOptions.WrappingIndent,
343 344 345 346 347
		OverviewRulerLane: editorCommon.OverviewRulerLane,
		EndOfLinePreference: editorCommon.EndOfLinePreference,
		DefaultEndOfLine: editorCommon.DefaultEndOfLine,
		EndOfLineSequence: editorCommon.EndOfLineSequence,
		TrackedRangeStickiness: editorCommon.TrackedRangeStickiness,
348
		CursorChangeReason: CursorChangeReason,
A
Alex Dima 已提交
349
		MouseTargetType: MouseTargetType,
350 351
		TextEditorCursorStyle: editorOptions.TextEditorCursorStyle,
		TextEditorCursorBlinkingStyle: editorOptions.TextEditorCursorBlinkingStyle,
352 353
		ContentWidgetPositionPreference: ContentWidgetPositionPreference,
		OverlayWidgetPositionPreference: OverlayWidgetPositionPreference,
354
		RenderMinimap: editorOptions.RenderMinimap,
355 356

		// classes
357
		InternalEditorOptions: <any>editorOptions.InternalEditorOptions,
358 359
		BareFontInfo: <any>BareFontInfo,
		FontInfo: <any>FontInfo,
360
		TextModelResolvedOptions: <any>editorCommon.TextModelResolvedOptions,
A
Alex Dima 已提交
361
		FindMatch: <any>editorCommon.FindMatch,
362 363

		// vars
A
Alex Dima 已提交
364
		EditorType: editorCommon.EditorType
A
Alex Dima 已提交
365
	};
366
}