gpt4 book ai didi

typescript - 如何从 monaco 编辑器获取转译代码?

转载 作者:行者123 更新时间:2023-12-03 23:21:18 27 4
gpt4 key购买 nike

如何从 monaco 编辑器中获取转译的 TypeScript 代码?另外,是否可以访问 TypeScript 语言服务?我试过这个:

monaco.languages.typescript.getTypeScriptWorker();

但它返回 Promise<any>我不知道该怎么办!

最佳答案

经过大量研究,它出现了 TypeScript LanguageService接口(interface)通过工作代理对象部分公开。这可能是因为服务在另一个线程中并且消息被推送到它,因此需要使用 promise 。

为了使事情变得更容易,我查看了返回对象上的公开函数并创建了这个定义;希望它可以帮助某人:

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转换为字符串(摩纳哥术语中的 model 是编辑器中的单个文件),因此:
tsProxy.getEmitOutput(editor.getModel().uri.toString())
.then((r) => { console.log(r.outputFiles[0].text); });

返回转译的 JavaScript,以及所有其他文件(如果可用)!别客气。 :)

关于typescript - 如何从 monaco 编辑器获取转译代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55132485/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com