Skip to content

Commit

Permalink
better walkers
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed May 12, 2024
1 parent f77ed5c commit 831e5cc
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 44 deletions.
5 changes: 5 additions & 0 deletions Src/SyntaxFinder/ResultWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ public static class ResultWriter
{
public static void WriteMatching(int total, int matching)
{
if (total == 0)
{
return;
}

if (matching > total)
{
Console.WriteLine("Matching was > than Total, so you did something wrong.");
Expand Down
12 changes: 3 additions & 9 deletions Src/SyntaxFinder/SyntaxFinderWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@ namespace SyntaxFinder;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

public abstract class SyntaxFinderWalker : CSharpSyntaxWalker
public abstract class SyntaxFinderWalker(string file) : CSharpSyntaxWalker
{
private bool wroteFile;
private readonly int maxCodeWrites = 250;
private static int codeWrites = 0;
private readonly string file;

protected SyntaxFinderWalker(string file)
{
this.file = file;
}
private static int codeWrites;

protected void WriteCode(SyntaxNode syntaxNode)
{
Expand All @@ -34,7 +28,7 @@ protected void WriteFilePath(bool onlyIfWritingCode = false)

if (!this.wroteFile)
{
Console.WriteLine(this.file);
Console.WriteLine(file);
this.wroteFile = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
namespace SyntaxFinder;
namespace SyntaxFinder.Walkers;

using System.Collections.Concurrent;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

public class ExampleWalker : CSharpSyntaxWalker
public class ExampleWalker(string file) : SyntaxFinderWalker(file)
{
public static readonly ConcurrentDictionary<string, List<string>> MembersInType = new();
public static int Total;
public static int Matching;
private readonly string file;
private bool wroteFile;
private readonly int maxCodeWrites = 250;
private int codeWrites = 0;

public ExampleWalker(string file)
{
this.file = file;
}

public override void VisitCompilationUnit(CompilationUnitSyntax node)
{
Expand Down Expand Up @@ -70,24 +61,6 @@ node.Parent is TypeDeclarationSyntax typeDeclarationSyntax
base.VisitMethodDeclaration(node);
}

private void WriteCode(SyntaxNode syntaxNode)
{
if (this.codeWrites < this.maxCodeWrites)
{
Interlocked.Increment(ref this.codeWrites);
Console.WriteLine(syntaxNode.SyntaxTree.GetText().ToString(syntaxNode.Span));
}
}

private void WriteFilePath()
{
if (!this.wroteFile)
{
Console.WriteLine(this.file);
this.wroteFile = true;
}
}

private static bool IsMultiline(SyntaxNode syntaxNode)
{
var lineSpan = syntaxNode.SyntaxTree.GetLineSpan(syntaxNode.Span);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace SyntaxFinder;
namespace SyntaxFinder.Walkers;

using System.Collections.Concurrent;
using Microsoft.CodeAnalysis.CSharp.Syntax;

public class ModifiersWalker : SyntaxFinderWalker
Expand Down
37 changes: 37 additions & 0 deletions Src/SyntaxFinder/Walkers/NestedConditionalExpressionsWalker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace SyntaxFinder.Walkers;

using System.Collections.Concurrent;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

public class NestedConditionalExpressionsWalker(string file) : SyntaxFinderWalker(file)
{
public static readonly ConcurrentDictionary<string, List<string>> MembersInType = new();
public static int Total;
public static int Matching;

public override void VisitConditionalExpression(ConditionalExpressionSyntax node)
{
if (node.WhenTrue is ConditionalExpressionSyntax)
{
this.WriteFilePath();
// this.WriteCode(node.Parent!);
Interlocked.Increment(ref Total);
}
}

public static void WriteResult()
{
foreach (var entry in MembersInType)
{
Console.WriteLine(entry.Key);
foreach (var member in entry.Value.OrderBy(o => o))
{
Console.WriteLine(" " + member);
}
}

ResultWriter.WriteMatching(Total, Matching);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace SyntaxFinder;
namespace SyntaxFinder.Walkers;

using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace SyntaxFinder;
namespace SyntaxFinder.Walkers;

using System.Collections.Concurrent;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

Expand Down

0 comments on commit 831e5cc

Please sign in to comment.