2.2.4 • Published 7 days ago

@volar/monaco v2.2.4

Weekly downloads
-
License
MIT
Repository
github
Last release
7 days ago

@volar/monaco

@volar/monaco is used to bridge the language capabilities implemented based on Volar.js to Monaco Editor, you can expect:

  • Support IntelliSense, Diagnosis, Formatting
  • Language behavior is consistent with regular IDEs
  • Optimized Performance
  • Missing package types are automatically fetched from CDN

It should be noted that this package does not participate in syntax highlighting support and language configuration.

We assume you already know:

  • How to create a Monaco Editor
  • How to work with Web Worker

Usage

Setup worker

// my-lang.worker.ts
import * as worker from 'monaco-editor-core/esm/vs/editor/editor.worker';
import type * as monaco from 'monaco-editor-core';
import { createSimpleWorkerService, ServiceEnvironment } from '@volar/monaco/worker';

self.onmessage = () => {
	worker.initialize((ctx: monaco.worker.IWorkerContext) => {
		const env: ServiceEnvironment = {
			workspaceFolder: 'file:///',
		};
		return createSimpleWorkerService({
			workerContext: ctx,
			env,
			languagePlugins: [
				// ...
			],
			servicePlugins: [
				// ...
			],
		});
	});
};

Add TypeScript Support

import * as worker from 'monaco-editor-core/esm/vs/editor/editor.worker';
import type * as monaco from 'monaco-editor-core';
-import { createSimpleWorkerService, ServiceEnvironment } from '@volar/monaco/worker';
+import {
+	createTypeScriptWorkerService,
+	ServiceEnvironment,
+} from '@volar/monaco/worker';
+import * as ts from 'typescript';
+import { create as createTypeScriptService } from 'volar-service-typescript';

self.onmessage = () => {
	worker.initialize((ctx: monaco.worker.IWorkerContext) => {
		const env: ServiceEnvironment = {
			workspaceFolder: 'file:///',
+			typescript: {
+				uriToFileName: uri => uri.substring('file://'.length),
+				fileNameToUri: fileName => 'file://' + fileName,
+			},
		};
-		return createSimpleWorkerService({
+		return createTypeScriptWorkerService({
+			typescript: ts,
+			compilerOptions: {
+				// ...
+			},
			workerContext: ctx,
			env,
			languagePlugins: [
				// ...
			],
			servicePlugins: [
				// ...
+				createTypeScriptService(ts),
			],
		});
	});
};

Add ATA Support for TypeScript

import * as worker from 'monaco-editor-core/esm/vs/editor/editor.worker';
import type * as monaco from 'monaco-editor-core';
import {
	createTypeScriptWorkerService,
	ServiceEnvironment,
+	activateAutomaticTypeAcquisition,
} from '@volar/monaco/worker';
import * as ts from 'typescript';
import { create as createTypeScriptService } from 'volar-service-typescript';

self.onmessage = () => {
	worker.initialize((ctx: monaco.worker.IWorkerContext) => {
		const env: ServiceEnvironment = {
			workspaceFolder: 'file:///',
			typescript: {
				uriToFileName: uri => uri.substring('file://'.length),
				fileNameToUri: fileName => 'file://' + fileName,
			},
		};
+		activateAutomaticTypeAcquisition(env);
		return createTypeScriptWorkerService({
			typescript: ts,
			compilerOptions: {
				// ...
			},
			workerContext: ctx,
			env,
			languagePlugins: [
				// ...
			],
			servicePlugins: [
				// ...
				createTypeScriptService(ts),
			],
		});
	});
};

Add worker loader to global env

import editorWorker from 'monaco-editor-core/esm/vs/editor/editor.worker?worker';
import myWorker from './my-lang.worker?worker';

(self as any).MonacoEnvironment = {
	getWorker(_: any, label: string) {
		if (label === 'my-lang') {
			return new myWorker();
		}
		return new editorWorker();
	}
}

Setup Language Features and Diagnostics

import type { LanguageService } from '@volar/language-service';
import { editor, languages, Uri } from 'monaco-editor-core';
import { activateMarkers, activateAutoInsertion, registerProviders } from '@volar/monaco';

languages.register({ id: 'my-lang', extensions: ['.my-lang'] });

languages.onLanguage('my-lang', () => {
	const worker = editor.createWebWorker<LanguageService>({
		moduleId: 'vs/language/my-lang/myLangWorker',
		label: 'my-lang',
	});
	activateMarkers(
		worker,
		['my-lang'],
		'my-lang-markers-owner',
		// sync files
		() => [Uri.file('/Foo.my-lang'), Uri.file('/Bar.my-lang')],
		editor
	);
	// auto close tags
	activateAutoInsertion(
		worker,
		['my-lang'],
		// sync files
		() => [Uri.file('/Foo.my-lang'), Uri.file('/Bar.my-lang')],
		editor
	);
	registerProviders(worker, ['my-lang'], languages)
});

Samples

2.2.3

7 days ago

2.2.4

7 days ago

2.2.2

12 days ago

2.2.1

16 days ago

2.2.0

21 days ago

2.2.0-alpha.11

22 days ago

2.2.0-alpha.12

22 days ago

2.2.0-alpha.10

1 month ago

2.2.0-alpha.9

1 month ago

2.2.0-alpha.8

1 month ago

2.2.0-alpha.7

1 month ago

2.2.0-alpha.6

1 month ago

2.2.0-alpha.5

2 months ago

2.2.0-alpha.4

2 months ago

2.2.0-alpha.3

2 months ago

2.2.0-alpha.2

2 months ago

2.1.6

2 months ago

2.2.0-alpha.1

2 months ago

2.2.0-alpha.0

2 months ago

2.1.5

2 months ago

2.1.4

2 months ago

2.1.3

2 months ago

2.1.2

3 months ago

2.1.1

3 months ago

2.1.0

3 months ago

2.0.4

3 months ago

2.0.3

3 months ago

2.0.2

3 months ago

2.0.1

4 months ago

2.0.0

4 months ago

2.0.0-alpha.14

4 months ago

2.0.0-alpha.13

5 months ago

2.0.0-alpha.11

5 months ago

2.0.0-alpha.12

5 months ago

2.0.0-alpha.8

5 months ago

2.0.0-alpha.9

5 months ago

2.0.0-alpha.10

5 months ago

2.0.0-alpha.7

5 months ago

2.0.0-alpha.6

5 months ago

2.0.0-alpha.5

5 months ago

2.0.0-alpha.4

5 months ago

2.0.0-alpha.3

5 months ago

2.0.0-alpha.1

6 months ago

2.0.0-alpha.2

6 months ago

2.0.0-alpha.0

6 months ago

1.10.5

7 months ago

1.10.4

7 months ago

1.10.3

8 months ago

1.10.2

8 months ago

1.10.9

7 months ago

1.10.8

7 months ago

1.10.7

7 months ago

1.10.6

7 months ago

1.8.2

10 months ago

1.8.1

11 months ago

1.8.0

11 months ago

1.9.2

10 months ago

1.11.0

7 months ago

1.11.1

7 months ago

1.9.1

10 months ago

1.9.0

10 months ago

1.10.10-alpha.1

7 months ago

1.10.10-alpha.0

7 months ago

1.10.10

7 months ago

1.8.3

10 months ago

1.10.1

9 months ago

1.10.0

10 months ago

1.7.10

11 months ago

1.7.9

11 months ago

1.6.5-patch.1

1 year ago

1.6.5-patch.2

1 year ago

1.6.4

1 year ago

1.6.3

1 year ago

1.7.8

11 months ago

1.7.7

11 months ago

1.7.6

11 months ago

1.7.5

11 months ago

1.7.4

12 months ago

1.7.3

12 months ago

1.7.2

12 months ago

1.7.1

12 months ago

1.7.0

12 months ago

1.6.9

1 year ago

1.6.8

1 year ago

1.6.7

1 year ago

1.6.6

1 year ago

1.6.5

1 year ago

1.6.2

1 year ago

1.6.1

1 year ago

1.6.0

1 year ago

1.5.4

1 year ago

1.5.3

1 year ago

1.5.2

1 year ago

1.5.1

1 year ago

1.5.0

1 year ago

1.5.0-alpha.0

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.4.0-alpha.12

1 year ago

1.4.0-alpha.11

1 year ago

1.4.0-alpha.10

1 year ago

1.4.0-alpha.9

1 year ago

1.4.0-alpha.8

1 year ago

1.4.0-alpha.7

1 year ago

1.4.0-alpha.6

1 year ago

1.4.0-alpha.5

1 year ago

1.4.0-alpha.4

1 year ago

1.4.0-alpha.3

1 year ago

1.4.0-alpha.2

1 year ago

1.4.0-alpha.1

1 year ago

1.4.0-alpha.0

1 year ago

1.3.0-alpha.3

1 year ago

1.3.0-alpha.2

1 year ago

1.3.0-alpha.1

1 year ago

1.3.0-alpha.0

1 year ago