This repository has been archived by the owner on Nov 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
/
monaco.contribution.ts
222 lines (190 loc) · 6.09 KB
/
monaco.contribution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as mode from './mode';
import Emitter = monaco.Emitter;
import IEvent = monaco.IEvent;
import IDisposable = monaco.IDisposable;
declare var require: <T>(moduleId: [string], callback: (module: T) => void) => void;
// --- TypeScript configuration and defaults ---------
export class LanguageServiceDefaultsImpl implements monaco.languages.typescript.LanguageServiceDefaults {
private _onDidChange = new Emitter<monaco.languages.typescript.LanguageServiceDefaults>();
private _extraLibs: { [path: string]: string };
private _workerMaxIdleTime: number;
private _eagerModelSync: boolean;
private _compilerOptions: monaco.languages.typescript.CompilerOptions;
private _diagnosticsOptions: monaco.languages.typescript.DiagnosticsOptions;
constructor(compilerOptions: monaco.languages.typescript.CompilerOptions, diagnosticsOptions: monaco.languages.typescript.DiagnosticsOptions) {
this._extraLibs = Object.create(null);
this._workerMaxIdleTime = 2 * 60 * 1000;
this.setCompilerOptions(compilerOptions);
this.setDiagnosticsOptions(diagnosticsOptions);
}
get onDidChange(): IEvent<monaco.languages.typescript.LanguageServiceDefaults> {
return this._onDidChange.event;
}
getExtraLibs(): { [path: string]: string; } {
const result = Object.create(null);
for (var key in this._extraLibs) {
result[key] = this._extraLibs[key];
}
return Object.freeze(result);
}
addExtraLib(content: string, filePath?: string): IDisposable {
if (typeof filePath === 'undefined') {
filePath = `ts:extralib-${Date.now()}`;
}
if (this._extraLibs[filePath]) {
throw new Error(`${filePath} already a extra lib`);
}
this._extraLibs[filePath] = content;
this._onDidChange.fire(this);
return {
dispose: () => {
if (delete this._extraLibs[filePath]) {
this._onDidChange.fire(this);
}
}
};
}
getCompilerOptions(): monaco.languages.typescript.CompilerOptions {
return this._compilerOptions;
}
setCompilerOptions(options: monaco.languages.typescript.CompilerOptions): void {
this._compilerOptions = options || Object.create(null);
this._onDidChange.fire(this);
}
getDiagnosticsOptions(): monaco.languages.typescript.DiagnosticsOptions {
return this._diagnosticsOptions;
}
setDiagnosticsOptions(options: monaco.languages.typescript.DiagnosticsOptions): void {
this._diagnosticsOptions = options || Object.create(null);
this._onDidChange.fire(this);
}
setMaximunWorkerIdleTime(value: number): void {
// doesn't fire an event since no
// worker restart is required here
this._workerMaxIdleTime = value;
}
getWorkerMaxIdleTime() {
return this._workerMaxIdleTime;
}
setEagerModelSync(value: boolean) {
// doesn't fire an event since no
// worker restart is required here
this._eagerModelSync = value;
}
getEagerModelSync() {
return this._eagerModelSync;
}
}
// --- BEGIN enums copied from typescript to prevent loading the entire typescriptServices ---
enum ModuleKind {
None = 0,
CommonJS = 1,
AMD = 2,
UMD = 3,
System = 4,
ES2015 = 5,
}
enum JsxEmit {
None = 0,
Preserve = 1,
React = 2,
}
enum NewLineKind {
CarriageReturnLineFeed = 0,
LineFeed = 1,
}
interface LineAndCharacter {
line: number;
character: number;
}
enum ScriptKind {
Unknown = 0,
JS = 1,
JSX = 2,
TS = 3,
TSX = 4,
}
enum ScriptTarget {
ES3 = 0,
ES5 = 1,
ES2015 = 2,
ES2016 = 3,
ES2017 = 4,
ESNext = 5,
Latest = 5,
}
enum LanguageVariant {
Standard = 0,
JSX = 1,
}
enum ModuleResolutionKind {
Classic = 1,
NodeJs = 2,
}
// --- END enums copied from typescript to prevent loading the entire typescriptServices ---
const typescriptDefaults = new LanguageServiceDefaultsImpl(
{ allowNonTsExtensions: true, target: ScriptTarget.Latest },
{ noSemanticValidation: false, noSyntaxValidation: false });
const javascriptDefaults = new LanguageServiceDefaultsImpl(
{ allowNonTsExtensions: true, allowJs: true, target: ScriptTarget.Latest },
{ noSemanticValidation: true, noSyntaxValidation: false });
function getTypeScriptWorker(): monaco.Promise<any> {
return new monaco.Promise((resolve, reject) => {
withMode((mode) => {
mode.getTypeScriptWorker()
.then(resolve, reject);
});
});
}
function getJavaScriptWorker(): monaco.Promise<any> {
return new monaco.Promise((resolve, reject) => {
withMode((mode) => {
mode.getJavaScriptWorker()
.then(resolve, reject);
});
});
}
// Export API
function createAPI(): typeof monaco.languages.typescript {
return {
ModuleKind: ModuleKind,
JsxEmit: JsxEmit,
NewLineKind: NewLineKind,
ScriptTarget: ScriptTarget,
ModuleResolutionKind: ModuleResolutionKind,
typescriptDefaults: typescriptDefaults,
javascriptDefaults: javascriptDefaults,
getTypeScriptWorker: getTypeScriptWorker,
getJavaScriptWorker: getJavaScriptWorker
}
}
monaco.languages.typescript = createAPI();
// --- Registration to monaco editor ---
function withMode(callback: (module: typeof mode) => void): void {
require<typeof mode>(['vs/language/typescript/src/mode'], callback);
}
monaco.languages.register({
id: 'typescript',
extensions: ['.ts', '.tsx'],
aliases: ['TypeScript', 'ts', 'typescript'],
mimetypes: ['text/typescript']
});
monaco.languages.onLanguage('typescript', () => {
withMode((mode) => mode.setupTypeScript(typescriptDefaults));
});
monaco.languages.register({
id: 'javascript',
extensions: ['.js', '.es6', '.jsx'],
firstLine: '^#!.*\\bnode',
filenames: ['jakefile'],
aliases: ['JavaScript', 'javascript', 'js'],
mimetypes: ['text/javascript'],
});
monaco.languages.onLanguage('javascript', () => {
withMode((mode) => mode.setupJavaScript(javascriptDefaults));
});