mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-23 00:22:56 +01:00
Export apis
This commit is contained in:
parent
83339b4d97
commit
73295879a4
8 changed files with 256 additions and 110 deletions
|
|
@ -6,8 +6,136 @@
|
|||
|
||||
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 _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.setCompilerOptions(compilerOptions);
|
||||
this.setDiagnosticsOptions(diagnosticsOptions);
|
||||
}
|
||||
|
||||
get onDidChange(): IEvent<monaco.languages.typescript.LanguageServiceDefaults>{
|
||||
return this._onDidChange.event;
|
||||
}
|
||||
|
||||
get extraLibs(): { [path: string]: string; } {
|
||||
return Object.freeze(this._extraLibs);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
get compilerOptions(): monaco.languages.typescript.CompilerOptions {
|
||||
return this._compilerOptions;
|
||||
}
|
||||
|
||||
setCompilerOptions(options: monaco.languages.typescript.CompilerOptions): void {
|
||||
this._compilerOptions = options || Object.create(null);
|
||||
this._onDidChange.fire(this);
|
||||
}
|
||||
|
||||
get diagnosticsOptions(): monaco.languages.typescript.DiagnosticsOptions {
|
||||
return this._diagnosticsOptions;
|
||||
}
|
||||
|
||||
setDiagnosticsOptions(options: monaco.languages.typescript.DiagnosticsOptions): void {
|
||||
this._diagnosticsOptions = options || Object.create(null);
|
||||
this._onDidChange.fire(this);
|
||||
}
|
||||
}
|
||||
|
||||
// --- BEGIN enums copied from typescript to prevent loading the entire typescriptServices ---
|
||||
|
||||
enum ModuleKind {
|
||||
None = 0,
|
||||
CommonJS = 1,
|
||||
AMD = 2,
|
||||
UMD = 3,
|
||||
System = 4,
|
||||
ES6 = 5,
|
||||
ES2015 = 5,
|
||||
}
|
||||
|
||||
enum JsxEmit {
|
||||
None = 0,
|
||||
Preserve = 1,
|
||||
React = 2,
|
||||
}
|
||||
|
||||
enum NewLineKind {
|
||||
CarriageReturnLineFeed = 0,
|
||||
LineFeed = 1,
|
||||
}
|
||||
|
||||
enum ScriptTarget {
|
||||
ES3 = 0,
|
||||
ES5 = 1,
|
||||
ES6 = 2,
|
||||
ES2015 = 2,
|
||||
Latest = 2,
|
||||
}
|
||||
|
||||
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 createAPI(): typeof monaco.languages.typescript {
|
||||
return {
|
||||
ModuleKind: ModuleKind,
|
||||
JsxEmit: JsxEmit,
|
||||
NewLineKind: NewLineKind,
|
||||
ScriptTarget: ScriptTarget,
|
||||
ModuleResolutionKind: ModuleResolutionKind,
|
||||
typescriptDefaults: typescriptDefaults,
|
||||
javascriptDefaults: javascriptDefaults
|
||||
}
|
||||
}
|
||||
|
||||
// --- Registration to monaco editor ---
|
||||
|
||||
function withMode(callback:(module:typeof mode)=>void): void {
|
||||
require<typeof mode>(['vs/language/typescript/src/mode'], callback);
|
||||
}
|
||||
|
|
@ -19,7 +147,7 @@ monaco.languages.register({
|
|||
mimetypes: ['text/typescript']
|
||||
});
|
||||
monaco.languages.onLanguage('typescript', () => {
|
||||
withMode((mode) => mode.setupTypeScript());
|
||||
withMode((mode) => mode.setupTypeScript(typescriptDefaults));
|
||||
});
|
||||
|
||||
monaco.languages.register({
|
||||
|
|
@ -31,5 +159,5 @@ monaco.languages.register({
|
|||
mimetypes: ['text/javascript'],
|
||||
});
|
||||
monaco.languages.onLanguage('javascript', () => {
|
||||
withMode((mode) => mode.setupJavaScript());
|
||||
withMode((mode) => mode.setupJavaScript(javascriptDefaults));
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue