简体   繁体   English

如何在vscode扩展中获取对象?

[英]How to get the object in a vscode extension?

When developing a new extension for Visual Studio Code, how can I get the current class where my cursor is pointed to? 为Visual Studio Code开发新的扩展时,如何获取指向光标的当前类?

For example: 例如:

export class MyClassA {

  parameter1: number = 5;

  myFunc() {
    return parameter1 * 2;
  }

}

export class MyClassB {
   anotherParamenter = null;
}

If my cursor is located inside the myFunc scope and I call registerHoverProvider or whatever, I want to know that I'm inside the class MyClassA . 如果我的光标位于myFunc范围内,并且我调用registerHoverProvider或任何其他名称,我想知道自己在类MyClassA

Is it possible to retrieve the object or only the cursor position? 是否可以检索对象或仅检索光标位置?

The context of text in a code editor is highly dependent of what language the code is in. You need a parser for the language to determine the syntactic and semantic details of the written code. 代码编辑器中文本的上下文高度依赖于代码所使用的语言。您需要一种语言解析器来确定所编写代码的语法和语义细节。

Hence, the first thing you have to do therefor is to implement a parser or use a parsing service provided by other tools. 因此,您要做的第一件事是实现解析器或使用其他工具提供的解析服务。 Once this is in place you can query that to find the syntactic context (eg myFunc in MyClassA in module foo ) and then act on that to generate informations for your hover provider. 一旦到位,您可以查询以找到语法上下文(例如,模块fooMyClassA中的myFunc ),然后对其进行操作以为悬停提供程序生成信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM