如何获取特定 JetBrains.ReSharper.Psi.IDeclaredElement 的文件名和行号?

发布于 11-26 20:42 字数 772 浏览 2 评论 0原文

我想为 resharper 编写一个测试框架扩展。有关此内容的文档位于: http://confluence.jetbrains.net/display/ ReSharper/Test+Framework+Support

其中一个方面是指示特定代码段是否是测试的一部分。该代码段表示为 IDeclaredElement。

是否可以获取由特定 IDeclaredElement 表示的一段代码的文件名和行号?

根据以下回复:

@Evgeny,感谢您的回答,我想知道您是否能为我澄清一点。

假设用户在 Visual Studio 中打开此测试: https:// github.com/fschwiet/DreamNJasmine/blob/master/NJasmine.Tests/SampleTest.cs 假设用户右键单击第 48 行“player.Resume()”表达式。 IDeclaredElement 会具体告诉我他们想在第 48 行运行吗?或者它会给我一个对应于整个类的 IDeclaredElement 以及整个类的文件名/行号范围?

我应该自己玩这个,但我很高兴利用你已经知道的东西。

I want to write a test framework extension for resharper. The docs for this are here: http://confluence.jetbrains.net/display/ReSharper/Test+Framework+Support

One aspect of this is indicating if a particular piece of code is part of a test. The piece of code is represented as a IDeclaredElement.

Is it possible to get the filename and line number of a piece of code represented by a particular IDeclaredElement?

Following up to the response below:

@Evgeny, thanks for the answer, I wonder if you can clarify one point for me.

Suppose the user has this test open in visual studio: https://github.com/fschwiet/DreamNJasmine/blob/master/NJasmine.Tests/SampleTest.cs
Suppose the user right clicks on line 48, the "player.Resume()" expression.
Will the IDeclaredElement tell me specifically they want to run at line 48? Or is it going to give me a IDeclaredElement corresponding to the entire class, and a filename/line number range for the entire class?

I should play with this myself, but I appreciate tapping into what you already know.

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

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

发布评论

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

评论(2

(り薆情海2024-12-03 20:42:23

是的。

“IDeclaredElement”实体是代码符号(类、方法、变量等)。它可以从程序集元数据加载,可以在源代码中声明,也可以隐式来自源代码。

您可以使用

var declarations = declaredElement.GetDeclarations() 

来获取声明它的所有 AST 元素(例如,这可能会返回分部类的多个声明)

然后,对于任何 IDeclaration,您可以使用

var documentRange = declaration.GetDocumentRange()
if (documentRange.IsValid())
  Console.WriteLine ("File: {0} Line:{1}",
 DocumentManager.GetInstance(declaration.GetSolution()).GetProjectFile(documentRange.Document).Name,
documentRange.Document.GetCoordsByOffset(documentRange.TextRange.StartOffset).Line
);

顺便说一句,您正在开发哪个测试框架扩展?

Yes.

The "IDeclaredElement" entity is the code symbol (class, method, variable, etc.). It could be loaded from assembly metadata, it could be declared in source code, it could come from source code implicitly.

You can use

var declarations = declaredElement.GetDeclarations() 

to get all AST elements which declares it (this could return multiple declarations for partial class, for example)

Then, for any IDeclaration, you can use

var documentRange = declaration.GetDocumentRange()
if (documentRange.IsValid())
  Console.WriteLine ("File: {0} Line:{1}",
 DocumentManager.GetInstance(declaration.GetSolution()).GetProjectFile(documentRange.Document).Name,
documentRange.Document.GetCoordsByOffset(documentRange.TextRange.StartOffset).Line
);

By the way, which test framework extension are you developing?

韬韬不绝2024-12-03 20:42:23

IDeclaredElement 会具体告诉我他们想要运行在
第 48 行?

再说一遍:IDeclaredElement 在文件中没有位置。相反,它的声明有它们。
对于每个声明(IDeclaration 是一个常规 AST 节点),文档中都有一个覆盖它的范围。在前面的示例中,我使用了 TextRange.StartOffset,但您也可以使用 TextRange.EndOffset。

如果需要文件中更精确的位置,请遍历AST树,并检查文档中的坐标以获取具体的表达式/语句

Will the IDeclaredElement tell me specifically they want to run at
line 48?

Once more: IDeclaredElement has no positions in the file. Instead, it's declaration have them.
For every declaration (IDeclaration is a regular AST node) there is range in document which covers it. In my previous example, I used TextRange.StartOffset, though you can use TextRange.EndOffset.

If you need more prcise position in the file, please traverse AST tree, and check the coordinates in the document for specific expression/statement

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