英文:
What is the correct approach to create a custom GoTo Definition behavior in VS Code extension?
问题
我正在创建一个支持小型专有语言的VS Code扩展。其中一个最低要求是仅支持过程的“转到定义”功能。由于我对JavaScript/TypeScript非常陌生,我在使用VSCode的API来解决问题方面感到困难。
我大致了解了创建VS Code扩展所需的内容,即package.json的目的,extesnion.ts中的activate函数以及对TypeScript的一般了解(来自Python)。我尝试参考了一些实现了更高级版本的扩展的开源代码(vscode-go),但作为TypeScript初学者,那里的代码让我感到不知所措。
基本上,我的问题可以归结为:是否有一种简单的方法(也许是一组抽象的步骤?)来实现一个非常基本的“转到同一文件中的函数定义”行为,而无需创建自定义语言服务器?
英文:
I am creating a VS Code extension that supports a small proprietary language. One of the minimal requirements for it is to have a "Go To Definition" functionality for procedures only. Being very new to JavaScript/TypeScript i'm struggling with using VSCode's API to actually solve the problem.
I have a grasp on what is required to make a VS Code extension, i.e the purpose of package.json, activate function in extesnion.ts and a general understanding of TypeScript (coming from Python). I have tried to refer to some open-source code that implements a much more advanced version of my extension (vscode-go), but the code there is overwhelming to go through as a TS beginner.
Essentially my question boils down to: Is there a simple way (may be an abstract set of steps?) to implement a very basic "go-to function definition within the same file" behavior, without resorting to creating a custom language server?
答案1
得分: 1
为提供“转到定义”功能,就像其他扩展一样,您只需注册并实现一个“DefinitionProvider”。
您不需要为此使用语言服务器,也不需要任何复杂的工具。一个简单的延迟解析器就足够了 :-).
我制作了一个 Pascal 扩展(https://github.com/alefragnani/vscode-language-pascal),提供了一些这些功能,而且不使用语言服务器。我使用全局/ctags来解析和生成源代码的标签。然后我加载和解析其输出以识别符号。
您可以自由查看(https://github.com/alefragnani/vscode-language-pascal/blob/master/src/providers/definitionProvider.ts)。源代码是开放的,相当简单,因为这是我开发的第一个扩展之一。
希望这对您有所帮助。
英文:
To provide a Go To Definition
feature, just like other extensions, you just have to register and implement a DefinitionProvider
.
You don’t need a language server for that, nor any complex tooling. A simple lazy parser would work just fine :-).
I’ve made a Pascal (https://github.com/alefragnani/vscode-language-pascal) extension that provides some of these features, and don’t use a Language Server. I’m using global/ctags to parse and generate tags for the sources. Then I load and parse its output to identify the symbols.
You are free to take a look (https://github.com/alefragnani/vscode-language-pascal/blob/master/src/providers/definitionProvider.ts). The source code is open and fairly simple, as it was one of the first extensions that I have developed.
Hope this helps
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论