-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathAstLineCounter.cs
53 lines (43 loc) · 1.79 KB
/
AstLineCounter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Semmle.Util;
namespace Semmle.Extraction.CSharp.Populators
{
internal class AstLineCounter : CSharpSyntaxVisitor<LineCounts>
{
public override LineCounts DefaultVisit(SyntaxNode node)
{
var text = node.SyntaxTree.GetText().GetSubText(node.GetLocation().SourceSpan).ToString();
return LineCounter.ComputeLineCounts(text);
}
public override LineCounts VisitMethodDeclaration(MethodDeclarationSyntax method)
{
return Visit(method.Identifier, GetBody(method));
}
public static LineCounts Visit(SyntaxToken identifier, SyntaxNode body)
{
var start = identifier.GetLocation().SourceSpan.Start;
var end = body.GetLocation().SourceSpan.End - 1;
var textSpan = new Microsoft.CodeAnalysis.Text.TextSpan(start, end - start);
var text = body.SyntaxTree.GetText().GetSubText(textSpan) + "\r\n";
return LineCounter.ComputeLineCounts(text);
}
public override LineCounts VisitConstructorDeclaration(ConstructorDeclarationSyntax method)
{
return Visit(method.Identifier, GetBody(method));
}
public override LineCounts VisitDestructorDeclaration(DestructorDeclarationSyntax method)
{
return Visit(method.Identifier, GetBody(method));
}
public override LineCounts VisitOperatorDeclaration(OperatorDeclarationSyntax node)
{
return Visit(node.OperatorToken, GetBody(node));
}
private static SyntaxNode GetBody(BaseMethodDeclarationSyntax node)
{
return node.Body ?? (SyntaxNode)node.ExpressionBody!;
}
}
}