如何在 vscode 中启用嵌入式语言智能感知和自动完成?

Pra*_*avi 7 javascript typescript visual-studio-code vscode-extensions

我有一个用例,我有一种语言 X,其中我支持 HTML。由于我在 VScode 中将文件扩展名关联为“.x”,因此我没有获得 X 内 HTML 的自动完成和属性建议。如何解决这个问题。

我尝试过的方法,

  • 创建了语言 X 的扩展。在扩展中,我获取了vscode.html并在 activate 方法中激活了它。
const htmlPluginId = 'vscode.html';

export const activate = () => {
  const htmlPlugin = vscode.extensions.getExtension(htmlPluginId);
  const ht = await htmlPlugin.activate();
}
Run Code Online (Sandbox Code Playgroud)

这不起作用。我猜想,HTML 插件与 X 语言运行没有关联。

  • 创建了语言服务器和客户端,我在其中使用 HTML 语言服务来执行完成和其他任务。这有效。但是,我觉得我在重复已经存在的东西vscode-html-languageserver
import {getLanguageService} from 'vscode-html-languageservice';

let connection = createConnection();

let documents = new TextDocuments(TextDocument);
const htmlLanguageService = getLanguageService();

connection.onCompletion((textDocumentPosition) => {
  const document = documents.get(textDocumentPosition.textDocument.uri);
  const htmlDocument = htmlLanguageService.parseHTMLDocument(document!);
  if (htmlDocument.roots.length > 0) {
    return htmlLanguageService.doComplete(
        document!, textDocumentPosition.position, htmlDocument);
  }
});

documents.listen(connection);
connection.listen();
Run Code Online (Sandbox Code Playgroud)

是否有另一种简单的方法可以在我的语言中使用 HTML 及其 VSCode 功能?