- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Allowing new lines in initializers
closes #1110
Showing
12 changed files
with
361 additions
and
245 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
...ts/FormattingTests/TestFiles/cs/AnonymousObjectCreationExpressions_NewLines.expected.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,9 @@ | ||
var someObject = new | ||
{ | ||
NoLineAboveHere = 1, | ||
|
||
ThisLineIsOkay = 2, | ||
|
||
// comment | ||
AndThisLine = 3, | ||
}; |
10 changes: 10 additions & 0 deletions
10
...rpier.Tests/FormattingTests/TestFiles/cs/AnonymousObjectCreationExpressions_NewLines.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,10 @@ | ||
var someObject = new | ||
{ | ||
|
||
NoLineAboveHere = 1, | ||
|
||
ThisLineIsOkay = 2, | ||
|
||
// comment | ||
AndThisLine = 3, | ||
}; |
9 changes: 9 additions & 0 deletions
9
...Sharpier.Tests/FormattingTests/TestFiles/cs/InitializerExpressions_NewLines.expected.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,9 @@ | ||
var someObject = new SomeObject | ||
{ | ||
NoLineAboveHere = 1, | ||
|
||
ThisLineIsOkay = 2, | ||
|
||
// comment | ||
AndThisLine = 3, | ||
}; |
10 changes: 10 additions & 0 deletions
10
Src/CSharpier.Tests/FormattingTests/TestFiles/cs/InitializerExpressions_NewLines.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,10 @@ | ||
var someObject = new SomeObject | ||
{ | ||
|
||
NoLineAboveHere = 1, | ||
|
||
ThisLineIsOkay = 2, | ||
|
||
// comment | ||
AndThisLine = 3, | ||
}; |
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
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,110 @@ | ||
namespace SyntaxFinder; | ||
|
||
using System.Collections.Concurrent; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
public class CustomWalker : CSharpSyntaxWalker | ||
{ | ||
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 CustomWalker(string file) | ||
{ | ||
this.file = file; | ||
} | ||
|
||
public override void VisitCompilationUnit(CompilationUnitSyntax node) | ||
{ | ||
this.VisitType(node, node.Members); | ||
base.VisitCompilationUnit(node); | ||
} | ||
|
||
private void VisitType(CSharpSyntaxNode node, SyntaxList<MemberDeclarationSyntax> members) | ||
{ | ||
foreach (var member in members) | ||
{ | ||
MembersInType.AddOrUpdate( | ||
node.GetType().Name, | ||
new List<string>(), | ||
(key, list) => | ||
{ | ||
if (!list.Contains(member.GetType().Name)) | ||
{ | ||
list.Add(member.GetType().Name); | ||
} | ||
|
||
return list; | ||
} | ||
); | ||
} | ||
} | ||
|
||
public override void VisitMethodDeclaration(MethodDeclarationSyntax node) | ||
{ | ||
if ( | ||
( | ||
node.Parent is TypeDeclarationSyntax typeDeclarationSyntax | ||
&& node == typeDeclarationSyntax.Members.First() | ||
) || node.GetLeadingTrivia().Any(o => o.IsComment() || node.AttributeLists.Any()) | ||
) | ||
{ | ||
base.VisitMethodDeclaration(node); | ||
return; | ||
} | ||
|
||
Interlocked.Increment(ref Total); | ||
this.WriteCode(node.Parent!); | ||
|
||
if (node.GetLeadingTrivia().Any(o => o.Kind() is SyntaxKind.EndOfLineTrivia)) | ||
{ | ||
Interlocked.Increment(ref Matching); | ||
} | ||
|
||
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); | ||
return lineSpan.StartLinePosition.Line != lineSpan.EndLinePosition.Line; | ||
} | ||
|
||
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); | ||
} | ||
} |
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,85 @@ | ||
namespace SyntaxFinder; | ||
|
||
public static class Ignored | ||
{ | ||
public static bool Is(string file) | ||
{ | ||
return ignored.Any(file.Replace("\\", "/").Contains); | ||
} | ||
|
||
private static string[] ignored = new[] | ||
{ | ||
"roslyn/src/Compilers/Test/Core/Assert/ConditionalFactAttribute.cs", | ||
"roslyn/src/Compilers/Test/Core/Compilation/RuntimeUtilities.cs", | ||
"runtime/src/libraries/System.Net.Primitives/tests/FunctionalTests/SocketAddressTest.cs", | ||
"runtime/src/libraries/System.Security.Cryptography.Pkcs/tests/SignedCms/SignedDocuments.cs", | ||
"aspnetcore/src/ProjectTemplates/Web.ProjectTemplates/content/StarterWeb-CSharp/Controllers/HomeController.cs", | ||
"aspnetcore/src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-CSharp/Controllers/WeatherForecastController.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/EmptyExplicitExpression.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/EmptyImplicitExpression.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/EmptyImplicitExpressionInCode.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/EmptyImplicitExpressionInCode.Tabs.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/ExplicitExpressionAtEOF.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/HelpersMissingCloseParen.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/HelpersMissingOpenBrace.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/HiddenSpansInCode.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/HelpersMissingOpenParen.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/ImplicitExpressionAtEOF.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/Inherits.Designtime.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/Inherits.Runtime.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/OpenedIf.DesignTime.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/OpenedIf.DesignTime.Tabs.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/ParserError.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/RazorComments.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/RazorComments.DesignTime.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/Sections.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/UnfinishedExpressionInCode.cs", | ||
"AspNetWebStack/test/System.Web.Razor.Test/TestFiles/CodeGenerator/CS/Output/UnfinishedExpressionInCode.Tabs.cs", | ||
"runtime/src/libraries/System.Text.Encoding.CodePages/src/System/Text/EncodingTable.Data.cs", | ||
"runtime/src/libraries/System.Text.Json/tests/TrimmingTests/Collections/ICollection.cs", | ||
"runtime/src/tests/JIT/jit64/opt/cse/HugeArray.cs", | ||
"runtime/src/tests/JIT/jit64/opt/rngchk/RngchkStress2.cs", | ||
"runtime/src/tests/JIT/jit64/opt/cse/HugeArray1.cs", | ||
"runtime/src/tests/JIT/Methodical/largeframes/skip2/skippage2.cs", | ||
"runtime/src/tests/JIT/Methodical/largeframes/skip6/skippage6.cs", | ||
"runtime/src/tests/JIT/Performance/CodeQuality/Bytemark/neural-dat.cs", | ||
"runtime/src/tests/JIT/Regression/JitBlue/GitHub_10215/GitHub_10215.cs", | ||
"aspnetcore/src/ProjectTemplates/Web.ProjectTemplates/content/ComponentsWebAssembly-CSharp/Server/Controllers/WeatherForecastController.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Await_DesignTime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Await_Runtime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_Runtime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/EmptyAttributeTagHelpers_Runtime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/EmptyAttributeTagHelpers_DesignTime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/EmptyExplicitExpression_DesignTime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/EmptyImplicitExpressionInCode_DesignTime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/EmptyImplicitExpression_DesignTime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ExplicitExpressionAtEOF_DesignTime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/HiddenSpansInCode_DesignTime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/HiddenSpansInCode_Runtime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ImplicitExpressionAtEOF_DesignTime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives_DesignTime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Markup_InCodeBlocks_Runtime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Markup_InCodeBlocks_DesignTime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/OpenedIf_Runtime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ParserError_DesignTime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/OpenedIf_DesignTime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ParserError_Runtime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections_DesignTime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections_Runtime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/SingleLineControlFlowStatements_DesignTime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/SingleLineControlFlowStatements_Runtime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/TransitionsInTagHelperAttributes_DesignTime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/UnfinishedExpressionInCode_Runtime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/UnfinishedExpressionInCode_DesignTime.codegen.cs", | ||
"aspnetcore/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/TransitionsInTagHelperAttributes_Runtime.codegen.cs", | ||
"runtime/src/tests/JIT/jit64/opt/cse/hugeSimpleExpr1.cs", | ||
"runtime/src/tests/JIT/jit64/opt/cse/HugeField1.cs", | ||
"runtime/src/tests/JIT/jit64/opt/cse/HugeField2.cs", | ||
"roslyn/src/Compilers/Test/Resources/Core/SymbolsTests/Metadata/public-and-private.cs", | ||
"runtime/src/tests/JIT/jit64/opt/cse/hugeexpr1.cs", | ||
"runtime/src/tests/Loader/classloader/generics/Instantiation/Nesting/NestedGenericClasses.cs", | ||
"runtime/src/tests/Loader/classloader/generics/Instantiation/Nesting/NestedGenericTypesMix.cs", | ||
"runtime/src/tests/Loader/classloader/generics/Instantiation/Nesting/NestedGenericStructs.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,58 @@ | ||
namespace SyntaxFinder; | ||
|
||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
public class ObjectInitializerWalker : CSharpSyntaxWalker | ||
{ | ||
private static int total; | ||
private static int matching; | ||
private static double totalExpressions; | ||
private readonly string file; | ||
private static HashSet<string> matchingFiles = new(); | ||
|
||
public ObjectInitializerWalker(string file) | ||
{ | ||
this.file = file; | ||
} | ||
|
||
public override void VisitInitializerExpression(InitializerExpressionSyntax node) | ||
{ | ||
if (node.Kind() is SyntaxKind.ObjectInitializerExpression) | ||
{ | ||
this.VisitNode(node); | ||
} | ||
|
||
base.VisitInitializerExpression(node); | ||
} | ||
|
||
private void VisitNode(InitializerExpressionSyntax node) | ||
{ | ||
total++; | ||
|
||
totalExpressions += node.Expressions.Count; | ||
|
||
if ( | ||
node.Expressions.Any( | ||
o => o.GetLeadingTrivia().Any(o => o.Kind() is SyntaxKind.EndOfLineTrivia) | ||
) | ||
) | ||
{ | ||
matching++; | ||
matchingFiles.Add( | ||
node.SyntaxTree.GetLineSpan(node.Span).StartLinePosition.Line + " - " + this.file | ||
); | ||
} | ||
} | ||
|
||
public static void WriteResult() | ||
{ | ||
foreach (var file in matchingFiles) | ||
{ | ||
Console.WriteLine(file); | ||
} | ||
ResultWriter.WriteResult("Avg Expressions", (totalExpressions / total).ToString()); | ||
|
||
ResultWriter.WriteMatching(total, matching); | ||
} | ||
} |
Oops, something went wrong.