如何从 Monaco 编辑器获取转译后的 TypeScript 代码?同时,是否可以访问 TypeScript 语言服务?我尝试了以下代码:
monaco.languages.typescript.getTypeScriptWorker();
但是它返回了Promise<any>
,我不知道该怎么办!
monaco.languages.typescript.getTypeScriptWorker();
但是它返回了Promise<any>
,我不知道该怎么办!
namespace ts {
export interface IMonacoTypeScriptServiceProxy {
_getModel(uri: string): Promise<{ _eol: string, _lineStarts: any, _Lines: string[], length: number, _uri: Uri, _versionId: number }>;
findRenameLocations(uri: string, position: number, findInStrings: boolean, findInComments: boolean, providePrefixAndSuffixTextForRename?: boolean): Promise<readonly RenameLocation[] | undefined>;
getCodeFixesAtPosition(uri: string, start: number, end: number, errorCodes: readonly number[], formatOptions: FormatCodeSettings, preferences: UserPreferences): Promise<readonly CodeFixAction[]>;
getCompilationSettings(): Promise<CompilerOptions>;
getCompilerOptionsDiagnostics(): Promise<Diagnostic[]>;
getCompletionEntryDetails(uri: string, position: number, name: string, formatOptions: FormatCodeOptions | FormatCodeSettings | undefined, source: string | undefined, preferences: UserPreferences | undefined): Promise<CompletionEntryDetails | undefined>;
getCompletionsAtPosition(uri: string, position: number, options: GetCompletionsAtPositionOptions | undefined): Promise<WithMetadata<CompletionInfo> | undefined>;
getCurrentDirectory(): Promise<string>;
getDefaultLibFileName(options: CompilerOptions): Promise<string>;
getDefinitionAtPosition(uri: string, position: number): Promise<ReadonlyArray<DefinitionInfo> | undefined>;
getEmitOutput(uri: string, emitOnlyDtsFiles?: boolean): Promise<EmitOutput>;
getFormattingEditsAfterKeystroke(uri: string, position: number, key: string, options: FormatCodeOptions | FormatCodeSettings): Promise<TextChange[]>;
getFormattingEditsForDocument(uri: string, options: FormatCodeOptions | FormatCodeSettings): Promise<TextChange[]>;
getFormattingEditsForRange(uri: string, start: number, end: number, options: FormatCodeOptions | FormatCodeSettings): Promise<TextChange[]>;
getNavigationBarItems(uri: string): Promise<NavigationBarItem[]>;
getOccurrencesAtPosition(uri: string, position: number): Promise<ReadonlyArray<ReferenceEntry> | undefined>;
getQuickInfoAtPosition(uri: string, position: number): Promise<QuickInfo | undefined>;
getReferencesAtPosition(uri: string, position: number): Promise<ReferenceEntry[] | undefined>;
getRenameInfo(uri: string, position: number, options?: RenameInfoOptions): Promise<RenameInfo>;
getScriptFileNames(): Promise<string[]>;
getScriptKind(uri: string): Promise<ScriptKind>;
getScriptSnapshot(uri: string): Promise<IScriptSnapshot | undefined>;
getScriptVersion(uri: string): Promise<string>;
getSemanticDiagnostics(uri: string): Promise<Diagnostic[]>;
getSignatureHelpItems(uri: string, position: number, options: SignatureHelpItemsOptions | undefined): Promise<SignatureHelpItems | undefined>;
getSuggestionDiagnostics(uri: string): Promise<DiagnosticWithLocation[]>;
getSyntacticDiagnostics(uri: string): Promise<DiagnosticWithLocation[]>;
isDefaultLibFileName(uri: string): Promise<boolean>;
}
}
使用方法如下:
var editor = monaco.editor.create(...etc...);
var tsProxy: ts.IMonacoTypeScriptServiceProxy;
monaco.languages.typescript.getTypeScriptWorker()
.then(function(worker: (v: monaco.Uri) => Promise<ts.IMonacoTypeScriptServiceProxy>) {
worker(editor.getModel().uri)
.then(function(proxy) {
tsProxy = proxy;
});
});
getEmitOutput(uri: string, emitOnlyDtsFiles?: boolean)
。其中uri
是文件名,它是一个monaco.Uri
转换成的字符串(在monaco术语中,model
是编辑器中的单个文件),所以:tsProxy.getEmitOutput(editor.getModel().uri.toString())
.then((r) => { console.log(r.outputFiles[0].text); });
getTypeScriptWorker
和 getJavaScriptWorker
返回有效的类型: export const getTypeScriptWorker: () => Promise<(...uris: Uri[]) => Promise<TypeScriptWorker>>;
export const getJavaScriptWorker: () => Promise<(...uris: Uri[]) => Promise<TypeScriptWorker>>;
同时,Promise参数也已经发生了变化。
随着这一变化,不再需要维护自己的工作线程(代理)类型。
import ts from "typescript";
this.worker.postMessage({ code: ts.transpile(context.code) });