Theia 进阶技巧
在本节中,我们将概述一些高级提示和技巧,以充分利用基于 Eclipse Theia 的开发工具。
在 Eclipse Theia 中为 VS Code 扩展提供自定义 API
Theia 允许通过提供兼容的 API 来运行 VS Code 扩展(有关详细信息,请参阅此概述 https://theia-ide.org/docs/extensions/)。 与在 VS Code 中运行时相比,可以扩展此 API ,从而允许在 Theia 中运行的 VS Code 扩展访问额外的功能。 这允许您提供一个功能作为针对 VS Code 和 Theia 的 VS Code 扩展。 但是,在 Theia 中运行时,可以使用仅在 Theia 中可用的自定义 API 来增强该功能。
以下代码示例显示了仅在基于 Theia 的应用程序中运行时调用的使用自定义 API。 这是通过应用程序名称确定的。 API 是异步导入的,以避免 VS Code 中的运行时错误。
if (vscode.env.appName === MY_THEIA_APP_NAME) { // Implement Theia API const api = await import('@mytheiaextension/mycustomapi'); // After importing the custom API, it can be used as any other API. The following lines are using an example API. api.host.getMessageHandler(() => getMessage()); api.host.onRequestMessage((actor: string) => { const message = getMessage(actor); api.host.showMessage(message); }); }
提供自定义 API 的替代方法是定义自定义命令。 同样,这些命令只有在 Theia 中运行 VS Code 扩展时才可用(参见以下代码示例)。
if (vscode.env.appName === MY_THEIA_APP_NAME) { // Execute Theia custom command const commands = await vscode.commands.getCommands(); if (commands.indexOf(MY_THEIA_CUSTOM_COMMAND) > -1) { vscode.commands.executeCommand(MY_THEIA_CUSTOM_COMMAND); } }
可以在这里查看示例:https://github.com/thegecko/vscode-theia-extension