Skip to content

Commit

Permalink
Object creation still uses SpaceBrace (#467)
Browse files Browse the repository at this point in the history
* Make sure new object does not space brace

closes #461

* Adding a way to try to find usages of different syntax

* Go back to the start

* Adding another test case

Co-authored-by: Lasath Fernando <[email protected]>
  • Loading branch information
belav and shocklateboy92 authored Oct 26, 2021
1 parent d3a1a0b commit 570197c
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CSharpier.sln
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpier.Tests.Generators"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpier.Generators", "Src\CSharpier.Generators\CSharpier.Generators.csproj", "{D846F494-57FC-4315-A2A8-357343175D2E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SyntaxFinder", "Src\SyntaxFinder\SyntaxFinder.csproj", "{0B075DD3-1FEE-483E-8665-43C226188287}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -98,5 +100,9 @@ Global
{D846F494-57FC-4315-A2A8-357343175D2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D846F494-57FC-4315-A2A8-357343175D2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D846F494-57FC-4315-A2A8-357343175D2E}.Release|Any CPU.Build.0 = Release|Any CPU
{0B075DD3-1FEE-483E-8665-43C226188287}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0B075DD3-1FEE-483E-8665-43C226188287}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B075DD3-1FEE-483E-8665-43C226188287}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B075DD3-1FEE-483E-8665-43C226188287}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
2 changes: 2 additions & 0 deletions Scripts/CreateTestingPR.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,5 @@ if ($firstRun) {
$newPr = & hub pull-request -b belav:$preBranch -m "Testing $branch"
Write-Output $newPr
}

Pop-Location
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public class ClassName
private SomeObject SomeLongerName = new SomeObject(
"lkjasdflkjasdfkljaskldjf",
"klasldkfaksdfasdfkjasdklf"
) {
)
{
Property1 = 1,
Property2 = 2
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ public class ClassName

public ClassName WithInitializer = new ClassName { Property = true };

public ClassName WithMediumInitializer = new ClassName
{
Property_____________________________ = true
};

public ClassName WithBigInitializer = new ClassName
{
Property1 = true,
Expand All @@ -28,7 +33,8 @@ public class ClassName
public ClassName WithComplexConstructor = new ClassName(
"parameter1",
new ClassName("parameter1", "parameter2") { Property1 = true }
) {
)
{
Property1 = true
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ public static class ObjectCreationExpression
{
public static Doc Print(ObjectCreationExpressionSyntax node)
{
var groupId = Guid.NewGuid().ToString();

return Doc.Group(
Token.PrintWithSuffix(node.NewKeyword, " "),
Node.Print(node.Type),
node.ArgumentList != null
? Doc.GroupWithId(
groupId,
? Doc.Group(
ArgumentListLike.Print(
node.ArgumentList.OpenParenToken,
node.ArgumentList.Arguments,
Expand All @@ -24,10 +21,7 @@ public static Doc Print(ObjectCreationExpressionSyntax node)
)
: Doc.Null,
node.Initializer != null
? Doc.Concat(
node.ArgumentList != null ? Doc.IfBreak(" ", Doc.Line, groupId) : Doc.Line,
InitializerExpression.Print(node.Initializer)
)
? Doc.Concat(Doc.Line, InitializerExpression.Print(node.Initializer))
: Doc.Null
);
}
Expand Down
169 changes: 169 additions & 0 deletions Src/SyntaxFinder/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
using System;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace SyntaxFinder
{
// Customize this in some way to find variations of syntax
// csharpier-repos isn't a huge result set, but it can give you
// some idea what syntax people prefer
// make sure csharpier-repos is on main before running
class Program
{
static void Main(string[] args)
{
foreach (
var file in Directory.EnumerateFiles(
"C:\\projects\\csharpier-repos",
"*.cs",
SearchOption.AllDirectories
)
)
{
if (Ignored.Is(file))
{
continue;
}

var contents = File.ReadAllText(file);
var tree = CSharpSyntaxTree.ParseText(contents);

var customWalker = new CustomWalker();
customWalker.Visit(tree.GetRoot());
}

WriteResult("Matching", CustomWalker.Matching.ToString("n0"));
WriteResult("Total", CustomWalker.Total.ToString("n0"));
WriteResult(
"Percent",
(Convert.ToDecimal(CustomWalker.Matching) / CustomWalker.Total * 100).ToString("n")
+ "%"
);
}

private static void WriteResult(string label, string value)
{
Console.WriteLine((label + ":").PadRight(20) + value.PadLeft(10));
}
}

public class CustomWalker : CSharpSyntaxWalker
{
public static int Total;
public static int Matching;

public override void VisitDoStatement(DoStatementSyntax node)
{
var fileLinePositionSpan = node.SyntaxTree.GetLineSpan(node.Span);
var sourceText = node.SyntaxTree.GetTextAsync().Result;
for (
var line = fileLinePositionSpan.EndLinePosition.Line;
line >= fileLinePositionSpan.StartLinePosition.Line;
line--
)
{
var actualLine = sourceText.Lines[line].ToString();
if (actualLine.Contains("} while"))
{
Total++;
Matching++;
break;
}
else if (actualLine.Contains("while"))
{
Total++;
break;
}
}

base.VisitDoStatement(node);
}
}

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",
};
}
}
13 changes: 13 additions & 0 deletions Src/SyntaxFinder/SyntaxFinder.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

</Project>

0 comments on commit 570197c

Please sign in to comment.