Skip to content

Commit 0983917

Browse files
authored
Adding formatting for spread element (#1179)
* Adding formatting for spread element closes #1167 * add spread walker
1 parent 1dd1674 commit 0983917

4 files changed

Lines changed: 81 additions & 2 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
int[] someArray = [.. someOtherArray];
2+
int[] someOtherArray = [.. value1, .. value2, .. value3];
3+
4+
int[] someOtherArray =
5+
[
6+
.. value1________________________________,
7+
.. value2________________________________,
8+
.. value3________________________________
9+
];
10+
11+
List<KeyValuePair<string, string>> list =
12+
[
13+
.. attribute
14+
.Targets.Select(target =>
15+
KeyValuePair.Create(
16+
target,
17+
context.EntityDefinitions.TryGetValue(target, out var type) ? type.ClassName : null
18+
)
19+
)
20+
.Where(kvp => kvp.Value != null)
21+
];
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace CSharpier.SyntaxPrinter.SyntaxNodePrinters;
2+
3+
internal static class SpreadElement
4+
{
5+
public static Doc Print(SpreadElementSyntax node, FormattingContext context)
6+
{
7+
return Doc.Group(
8+
Token.Print(node.OperatorToken, context),
9+
" ",
10+
Node.Print(node.Expression, context)
11+
);
12+
}
13+
}

Src/SyntaxFinder/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.CodeAnalysis.CSharp;
1+
using System.Reflection;
2+
using Microsoft.CodeAnalysis.CSharp;
23
using SyntaxFinder;
34

45
var files = Directory.EnumerateFiles(
@@ -47,4 +48,4 @@
4748
}
4849
);
4950

50-
// ObjectInitializerWalker.WriteResult();
51+
typeToRun.GetMethod("WriteResult")?.Invoke(null, null);

Src/SyntaxFinder/SpreadWalker.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace SyntaxFinder;
2+
3+
using System.Collections.Concurrent;
4+
using Microsoft.CodeAnalysis;
5+
using Microsoft.CodeAnalysis.CSharp;
6+
using Microsoft.CodeAnalysis.CSharp.Syntax;
7+
8+
public class SpreadWalker : CSharpSyntaxWalker
9+
{
10+
private static int total;
11+
private static int matching;
12+
private readonly string file;
13+
14+
public SpreadWalker(string file)
15+
{
16+
this.file = file;
17+
}
18+
19+
public override void VisitSpreadElement(SpreadElementSyntax node)
20+
{
21+
if (node.Kind() is SyntaxKind.SpreadElement)
22+
{
23+
this.VisitNode(node);
24+
}
25+
26+
base.VisitSpreadElement(node);
27+
}
28+
29+
private void VisitNode(SpreadElementSyntax node)
30+
{
31+
Console.WriteLine(node.Parent!.SyntaxTree.GetText().ToString(node.Parent!.FullSpan));
32+
Interlocked.Increment(ref total);
33+
34+
if (node.Expression.GetLeadingTrivia().Any(o => o.Kind() is SyntaxKind.WhitespaceTrivia))
35+
{
36+
Interlocked.Increment(ref matching);
37+
}
38+
}
39+
40+
public static void WriteResult()
41+
{
42+
ResultWriter.WriteMatching(total, matching);
43+
}
44+
}

0 commit comments

Comments
 (0)