基于为其他语言编译的 JavaScript 创建 IntelliSense?

Creating IntelliSense based on compiled JavaScript for other language?

我考虑过创建一个提供 IntelliSense (i.e. code completion) for CoffeeScript 的扩展,这是一种编译为 JavaScript 的流行语言。我所说的 IntelliSense 主要是指自动完成:属性、函数参数、自动导入。如果可能,还可以使用 Go-To-Definitions。据我所知,尽管它取得了巨大的成功并且年代久远,但在 CS 的任何 IDE 中都不存在。这可以说是因为集成语言服务器、导入扫描、函数签名分析等很复杂,这本身并不是一件容易的事。

然而,CoffeeScript 原生且高效地编译成 JavaScript,VSCode 中的 JS Intellisense 非常出色。对于非常基本但主要是功能性的扩展,以下方法听起来可行:

听起来不难,但我不知道如何进行。这是我想象中如何工作的代码片段:

class Provider {
    provideCompletionItems(document, position, token) {
        const word = document.getText(document.getWordRangeAtPosition(position))
        const text = document.getText().split('\n')
        const currentLineno = position.line
        const textWithoutCurrentLine = text.filter((line, lineno) => lineno != currentLineno)
        const compiled = CoffeeScript.compile(textWithoutCurrentLine.join('\n'), { sourceMap: true })
        const currentLinenoJS = compiled.sourceMap[currentLine].lineno // don't know the syntax yet
        const compiledWithWord = compiled.js.split('\n').splice(currentLinenoJS, 0, word)

        // These methods don't exist, what do I do?
        const JSProvider = vscode.languages.simulateCompletionItemProvider('javascript')
        const completionItems: CompletionItem[] = JSProvider.getCompletionItemsForTextAndLine(textWithoutCurrentLine, currentLinenoJS)

        return completionItems
    }
}
vscode.languages.registerCompletionItemProvider('coffeescript', Provider));

这可能比帮助更令人困惑,但这就是代码的样子。我希望获得有关如何从 CoffeeScript[=33 的完成项提供程序处理程序中访问动态生成代码的 JavaScript 智能感知功能的提示=] 文件。如果这是可行的,它也将允许对类似的编译为 JS 的语言进行简单扩展。

换句话说:JS 具有所有这些惊人的自动完成功能,CS 编译为 JS,包括源映射。我如何利用它来获得 CS 的自动完成功能?

Register completion item provider for type CoffeeScript files

这将遵循 programmatic language features. For a middleware kind of extension there are two main architectures possible, outlined here:语言服务或请求转发。

我最终开始实施语言服务扩展,概述 here

这种方法比编写本机分析器容易得多。

When providing a completion item, take the compiled JavaScript code, get profuse IntelliSense information from the JS/TS language server (how? do I need my own instance?

I don't think VSC even uses an actual JS LSP internally?)

是的,tsserver does not implement LSP, but offers its own APIs, but there are other usable implementations。所以你需要与其中任何一个进行交互。