Skip to content

Commit c87a60c

Browse files
author
machibuse
committed
Extract syntax parsing helpers into SyntaxHelper and refactor ClassVisitor to use them
1 parent db6fe03 commit c87a60c

2 files changed

Lines changed: 45 additions & 24 deletions

File tree

Source/Porticle.Grpc.TypeMapper/ClassVisitor.cs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.CodeAnalysis;
33
using Microsoft.CodeAnalysis.CSharp;
44
using Microsoft.CodeAnalysis.CSharp.Syntax;
5+
using static Porticle.Grpc.TypeMapper.SyntaxHelper;
56

67
namespace Porticle.Grpc.TypeMapper;
78

@@ -43,28 +44,4 @@ public class ClassVisitor(TaskLoggingHelper log, bool wrapAllNonNullableStrings,
4344

4445
return node;
4546
}
46-
47-
private static ClassDeclarationSyntax ClassFromSource(string classCode)
48-
{
49-
var syntaxTree = CSharpSyntaxTree.ParseText(classCode);
50-
var root = syntaxTree.GetRoot();
51-
var nestedClass = root.DescendantNodes().OfType<ClassDeclarationSyntax>().Single();
52-
return nestedClass.WithLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
53-
}
54-
55-
private static InterfaceDeclarationSyntax InterfaceFromSource(string classCode)
56-
{
57-
var syntaxTree = CSharpSyntaxTree.ParseText(classCode);
58-
var root = syntaxTree.GetRoot();
59-
var nestedClass = root.DescendantNodes().OfType<InterfaceDeclarationSyntax>().Single();
60-
return nestedClass.WithLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
61-
}
62-
63-
private static MethodDeclarationSyntax MethodFromSource(string methodCode)
64-
{
65-
var syntaxTree = CSharpSyntaxTree.ParseText("class _ { " + methodCode + " }");
66-
var root = syntaxTree.GetRoot();
67-
var method = root.DescendantNodes().OfType<MethodDeclarationSyntax>().Single();
68-
return method.WithLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
69-
}
7047
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp;
3+
using Microsoft.CodeAnalysis.CSharp.Syntax;
4+
5+
namespace Porticle.Grpc.TypeMapper;
6+
7+
public static class SyntaxHelper
8+
{
9+
/// <summary>
10+
/// Parses C# source code and extracts the contained class declaration.
11+
/// </summary>
12+
/// <param name="classCode">The complete C# source code containing exactly one class declaration.</param>
13+
public static ClassDeclarationSyntax ClassFromSource(string classCode)
14+
{
15+
var syntaxTree = CSharpSyntaxTree.ParseText(classCode);
16+
var root = syntaxTree.GetRoot();
17+
var nestedClass = root.DescendantNodes().OfType<ClassDeclarationSyntax>().Single();
18+
return nestedClass.WithLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
19+
}
20+
21+
/// <summary>
22+
/// Parses C# source code and extracts the contained interface declaration.
23+
/// </summary>
24+
/// <param name="classCode">The complete C# source code containing exactly one interface declaration.</param>
25+
public static InterfaceDeclarationSyntax InterfaceFromSource(string classCode)
26+
{
27+
var syntaxTree = CSharpSyntaxTree.ParseText(classCode);
28+
var root = syntaxTree.GetRoot();
29+
var nestedClass = root.DescendantNodes().OfType<InterfaceDeclarationSyntax>().Single();
30+
return nestedClass.WithLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
31+
}
32+
33+
/// <summary>
34+
/// Parses the source code of a single method and extracts the contained method declaration.
35+
/// </summary>
36+
/// <param name="methodCode">The method source code (without enclosing class).</param>
37+
public static MethodDeclarationSyntax MethodFromSource(string methodCode)
38+
{
39+
var syntaxTree = CSharpSyntaxTree.ParseText("class _ { " + methodCode + " }");
40+
var root = syntaxTree.GetRoot();
41+
var method = root.DescendantNodes().OfType<MethodDeclarationSyntax>().Single();
42+
return method.WithLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
43+
}
44+
}

0 commit comments

Comments
 (0)