-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding formatting for spread element (#1179)
* Adding formatting for spread element closes #1167 * add spread walker
- Loading branch information
Showing
4 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
Src/CSharpier.Tests/FormattingTests/TestFiles/cs/SpreadElements.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
int[] someArray = [.. someOtherArray]; | ||
int[] someOtherArray = [.. value1, .. value2, .. value3]; | ||
|
||
int[] someOtherArray = | ||
[ | ||
.. value1________________________________, | ||
.. value2________________________________, | ||
.. value3________________________________ | ||
]; | ||
|
||
List<KeyValuePair<string, string>> list = | ||
[ | ||
.. attribute | ||
.Targets.Select(target => | ||
KeyValuePair.Create( | ||
target, | ||
context.EntityDefinitions.TryGetValue(target, out var type) ? type.ClassName : null | ||
) | ||
) | ||
.Where(kvp => kvp.Value != null) | ||
]; |
13 changes: 13 additions & 0 deletions
13
Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/SpreadElement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace CSharpier.SyntaxPrinter.SyntaxNodePrinters; | ||
|
||
internal static class SpreadElement | ||
{ | ||
public static Doc Print(SpreadElementSyntax node, FormattingContext context) | ||
{ | ||
return Doc.Group( | ||
Token.Print(node.OperatorToken, context), | ||
" ", | ||
Node.Print(node.Expression, context) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
namespace SyntaxFinder; | ||
|
||
using System.Collections.Concurrent; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
public class SpreadWalker : CSharpSyntaxWalker | ||
{ | ||
private static int total; | ||
private static int matching; | ||
private readonly string file; | ||
|
||
public SpreadWalker(string file) | ||
{ | ||
this.file = file; | ||
} | ||
|
||
public override void VisitSpreadElement(SpreadElementSyntax node) | ||
{ | ||
if (node.Kind() is SyntaxKind.SpreadElement) | ||
{ | ||
this.VisitNode(node); | ||
} | ||
|
||
base.VisitSpreadElement(node); | ||
} | ||
|
||
private void VisitNode(SpreadElementSyntax node) | ||
{ | ||
Console.WriteLine(node.Parent!.SyntaxTree.GetText().ToString(node.Parent!.FullSpan)); | ||
Interlocked.Increment(ref total); | ||
|
||
if (node.Expression.GetLeadingTrivia().Any(o => o.Kind() is SyntaxKind.WhitespaceTrivia)) | ||
{ | ||
Interlocked.Increment(ref matching); | ||
} | ||
} | ||
|
||
public static void WriteResult() | ||
{ | ||
ResultWriter.WriteMatching(total, matching); | ||
} | ||
} |