vs包:如何以编程方式识别代码语句的开头 [英] vs package: how to programatically identify the beginning of a code statement

查看:143
本文介绍了vs包:如何以编程方式识别代码语句的开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个免费的Visual Studio软件包(C#),该软件包包含自动插入评论的功能.

I'm developing a free visual studio package (C#) which includes a feature that automatically inserts comments.

如果有以下代码:

1 for ( x = 0; x < value; x++ )
2 {
3 if ( value1 == true &&
4 value2 == false ||
5 value3 != true)
6 {
7 return;
8 }
9 }


推荐答案

我最近不得不进行一些代码解析.有几种选择,但是如果可以的话,最新的就是最好的选择.

I recently had to do some code parsing.  There are a few options, but the most recent is your best bet if that's available to you.

简短的答案是使用罗斯林" (或NRefactory).

The short answer is "Use Roslyn" (or NRefactory).

这是一种简短的方法(未经检查,因此可能会有问题):

Here's a brief (unchecked, so there may be problems) way to do that:

    public IEnumerable<IfStatementSyntax> ParseForIfs(string text, IEnumerable<string> preprocessorSymbols)
    {
            var options = new CSharpParseOptions(documentationMode: DocumentationMode.None, preprocessorSymbols: preprocessorSymbols);
            SyntaxTree tree = CSharpSyntaxTree.ParseText(text, options);
            SyntaxNode root = tree.GetRoot();
            return root.DescendantNodesAndSelf().OfType<IfStatementSyntax>();
    } 

对于For和Return(将IfStatementSyntax分别更改为相应的语法)也可以做到这一点.如果您想将这三个作为一个IEnumerable:

The same can be done with For and Return (change IfStatementSyntax to the appropriate syntax for each).  If you want to get those three as one IEnumerable:

    public IEnumerable<SyntaxNode> ParseStatements(string text, IEnumerable<string> preprocessorSymbols)
    {
            var options = new CSharpParseOptions(documentationMode: DocumentationMode.None, preprocessorSymbols: preprocessorSymbols);
            SyntaxTree tree = CSharpSyntaxTree.ParseText(text, options);
            SyntaxNode root = tree.GetRoot();
            return root.DescendantNodesAndSelf()
                       .Where(x => x.IsKind(SyntaxKind.IfStatement) ||
                                   x.IsKind(SyntaxKind.ReturnStatement) ||
                                   x.IsKind(SyntaxKind.ForStatement) ||
                                   x.IsKind(SyntaxKind.ForEachStatement));
    } 

然后,如果需要详细信息,则将结果中的元素强制转换为适当的 Kind 语法,但是由于您仅对语句开始感兴趣,因此需要使用"SpanStart".属性从SyntaxNode关闭.

Then cast the elements in the result to the appropriate KindSyntax if you want details, but since you're only interested in the statement start, you want the "SpanStart" property off of SyntaxNode.

如果Roslyn并非代码解析的选择(即出于某种原因无法使用适当的框架),则NRefactory 5是一个合理的选择.它并没有那么快,但是可以工作.一个好的起点是 此代码项目文章.

If Roslyn isn't an option for code parsing (i.e. can't use the appropriate framework for whatever reason), NRefactory 5 is a reasonable alternative.  It's not nearly as quick but it works. A good place to start is this code project article.

"preprocessorSymbols"位是可选的,如果解析器可以忽略所有预处理器#if/#endif语句,则可以完全省略该位.如果您是作为Visual Studio扩展的一部分来执行此操作的, 这是我编写的扩展方法,在给定ITextView对象的情况下,该方法将为您提供.

The "preprocessorSymbols" bit is optional, you can omit it entirely if you're OK with the parser ignoring any preprocessor #if / #endif statements.  If you're doing this as part of a Visual Studio extension, here's an extension method I wrote that will get them for you given an ITextView object.


这篇关于vs包:如何以编程方式识别代码语句的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆