Skip to content

Commit 6cc858b

Browse files
committed
Move AstLineCounter to top level class
1 parent 4f693be commit 6cc858b

File tree

3 files changed

+49
-52
lines changed

3 files changed

+49
-52
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Method.AstLineCounter.cs

Lines changed: 0 additions & 51 deletions
This file was deleted.

csharp/extractor/Semmle.Extraction.CSharp/Entities/Method.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Semmle.Extraction.CSharp.Entities
1010
{
11-
public abstract partial class Method : CachedSymbol<IMethodSymbol>, IExpressionParentEntity, IStatementParentEntity
11+
public abstract class Method : CachedSymbol<IMethodSymbol>, IExpressionParentEntity, IStatementParentEntity
1212
{
1313
protected Method(Context cx, IMethodSymbol init)
1414
: base(cx, init) { }
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp;
3+
using Microsoft.CodeAnalysis.CSharp.Syntax;
4+
using Semmle.Util;
5+
6+
namespace Semmle.Extraction.CSharp.Populators
7+
{
8+
internal class AstLineCounter : CSharpSyntaxVisitor<LineCounts>
9+
{
10+
public override LineCounts DefaultVisit(SyntaxNode node)
11+
{
12+
var text = node.SyntaxTree.GetText().GetSubText(node.GetLocation().SourceSpan).ToString();
13+
return LineCounter.ComputeLineCounts(text);
14+
}
15+
16+
public override LineCounts VisitMethodDeclaration(MethodDeclarationSyntax method)
17+
{
18+
return Visit(method.Identifier, method.Body ?? (SyntaxNode)method.ExpressionBody);
19+
}
20+
21+
public static LineCounts Visit(SyntaxToken identifier, SyntaxNode body)
22+
{
23+
var start = identifier.GetLocation().SourceSpan.Start;
24+
var end = body.GetLocation().SourceSpan.End - 1;
25+
26+
var textSpan = new Microsoft.CodeAnalysis.Text.TextSpan(start, end - start);
27+
28+
var text = body.SyntaxTree.GetText().GetSubText(textSpan) + "\r\n";
29+
return LineCounter.ComputeLineCounts(text);
30+
}
31+
32+
public override LineCounts VisitConstructorDeclaration(ConstructorDeclarationSyntax method)
33+
{
34+
return Visit(method.Identifier, (SyntaxNode)method.Body ?? method.ExpressionBody);
35+
}
36+
37+
public override LineCounts VisitDestructorDeclaration(DestructorDeclarationSyntax method)
38+
{
39+
return Visit(method.Identifier, (SyntaxNode)method.Body ?? method.ExpressionBody);
40+
}
41+
42+
public override LineCounts VisitOperatorDeclaration(OperatorDeclarationSyntax node)
43+
{
44+
return Visit(node.OperatorToken, node.Body ?? (SyntaxNode)node.ExpressionBody);
45+
}
46+
}
47+
48+
}

0 commit comments

Comments
 (0)