从扩展中获取语言服务器信息的API(括号对,函数开始/结束,...)
我目前正在编写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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只能通过VS代码API拥有其中的一半。
使用
vscode.executedocumentsymbolprovider
命令,您可以从文件中收集所有功能,并检查当前位置是否在其中一个功能内部。像这样检索功能的东西:
每个
符号
为您提供range
,它定义了函数声明和主体的开始和结尾。请注意,您可能需要一种递归方法(每个符号都包含其他符号)。可以在我的分离器扩展程序上找到一个完整的样本( https://github.com/alefragnani/vscode-separators/blob/b6d515847bbaccf6395b24f95b24f9f9fddf82c373c373cb24fd7/
不幸的是,没有API,因为VS代码不会暴露语言令牌或AST。因此,您将不得不自己处理,也许是使用正则表达式。
希望这会有所帮助
You can have only half of that via VS Code APIs.
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:
Each
Symbol
provides you with aRange
, 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)
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