从扩展中获取语言服务器信息的API(括号对,函数开始/结束,...)

发布于 2025-01-29 17:02:34 字数 317 浏览 2 评论 0原文

我目前正在编写VSCODE的扩展名,该扩展名需要对编辑器中当前显示的代码有一些良好的了解,我想知道是否有一些可用的API可以为我提供所需的信息(例如,来自当前语言服务器) 必要的代码解析等来进行繁重的举重等。

我需要详细的内容:

  • 给定是代码中的位置(行 + col no)
  • 或者,如果我必须通过实施所有 位置:
    • 是函数内部的pos,如果是,则该函数在哪里开始&结束?
    • 是字符串中的pos,如果是这样,字符串启动&结束?

该扩展名将提供某种“ VIM选择灯”。

I'm currently writing an extension for VSCode which needs to have some good knowledge about the currently shown code in the editor and I'm wondering if there is some API available which can give me the needed information (e.g. from the current language server) or if I have to do the heavy lifting myself by implementing all the needed code parsing etc.

What I need in detail is the following:

  • Given is a position in code (line + col no)
  • What I'd like to know about the given position:
    • Is pos inside a function and if so, where does the function start & end?
    • Is pos inside a string and if so, where does the string start & end?

The extension is going to provide some kind of "vim selection light".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各自安好 2025-02-05 17:02:34

您只能通过VS代码API拥有其中的一半。

是函数内部的pos,如果是,则该函数在哪里启动&结束?

使用vscode.executedocumentsymbolprovider命令,您可以从文件中收集所有功能,并检查当前位置是否在其中一个功能内部。

像这样检索功能的东西:


    const symbolsToFind = [SymbolKind.Function, SymbolKind.Method, SymbolKind.Constructor];

    const docSymbols = await commands.executeCommand(
        'vscode.executeDocumentSymbolProvider',
        window.activeTextEditor.document.uri
    ) as DocumentSymbol[];

    const docSymbolsFunctionsMethods = docSymbols
        ? docSymbols.filter(symbol => symbolsToFind.includes(symbol.kind))
        : undefined;

每个符号为您提供range,它定义了函数声明和主体的开始和结尾。

请注意,您可能需要一种递归方法(每个符号都包含其他符号)。可以在我的分离器扩展程序上找到一个完整的样本( https://github.com/alefragnani/vscode-separators/blob/b6d515847bbaccf6395b24f95b24f9f9fddf82c373c373cb24fd7/

是字符串中的pos,如果是这样,字符串启动&结束?

不幸的是,没有API,因为VS代码不会暴露语言令牌或AST。因此,您将不得不自己处理,也许是使用正则表达式。

希望这会有所帮助

You can have only half of that via VS Code APIs.

Is pos inside a function and if so, where does the function start & end?

Using the vscode.executeDocumentSymbolProvider command, you can gather all functions from a file and check if the current position is inside one of the functions.

Something like this to retrieve the functions:


    const symbolsToFind = [SymbolKind.Function, SymbolKind.Method, SymbolKind.Constructor];

    const docSymbols = await commands.executeCommand(
        'vscode.executeDocumentSymbolProvider',
        window.activeTextEditor.document.uri
    ) as DocumentSymbol[];

    const docSymbolsFunctionsMethods = docSymbols
        ? docSymbols.filter(symbol => symbolsToFind.includes(symbol.kind))
        : undefined;

Each Symbol provides you with a Range, which defines the start and end of the function declaration and body.

Be aware that you will probably need a recursive approach (each Symbol can contain other Symbols). A complete sample is available on my Separators extension (https://github.com/alefragnani/vscode-separators/blob/b6d515847bbaccf6395b24f9fdf82c373cb24fd7/src/symbols.ts#L51)

Is pos inside a string and if so, where does the string start & end?

Unfortunately, there is no API for that, as VS Code does not expose language tokens or the AST. So, you will have to deal with it yourself, maybe using regex.

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文