Skip to content

Commit

Permalink
- Implemented "Egyptian Brackets" fixing
Browse files Browse the repository at this point in the history
- Fine tuning of "indent alignment"
  • Loading branch information
lbs-contributor committed Jan 27, 2019
1 parent d40541f commit d305db9
Show file tree
Hide file tree
Showing 22 changed files with 830 additions and 261 deletions.
66 changes: 66 additions & 0 deletions CM+.Tests/BracketsNormalizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis.CSharp;
using Xunit;

namespace CMPlus.Tests
{
public class BracketsNormalizer : TestBase
{
[Fact]
public void Fix_EgyptianBrackets()
{
var code =
@"public OptimiserAction Create(SlideArea location)
{
var parameters = new List<Parameter>{
_locationParameter.WithValue((ushort)(location == SlideArea.OutputDrawer ? 1 : 0))
}
}";
var processedCode = code.GetSyntaxRoot()
.FixBrackets()
.ToString()
.GetLines();

Assert.Equal(" var parameters = new List<Parameter>", processedCode[2]);
Assert.Equal(" {", processedCode[3]);
Assert.Equal(" }", processedCode[5]);
}

[Fact]
public void DoesNot_Change_SameLine_Pair()
{
var code =
@"class Test
{
int Index { get; }
}";
var processedCode = code.GetSyntaxRoot()
.FixBrackets()
.ToString()
.GetLines();

Assert.Equal(" int Index { get; }", processedCode[2]);
}

[Fact]
public void Ignore_StringInterpolations()
{
var code =
@"public OptimiserAction Create(SlideArea location)
{
var message = $@""Error: {
1.ToString().
Length}"";
}";
var processedCode = code.GetSyntaxRoot()
.FixBrackets()
.ToString()
.GetLines();

Assert.Equal(" var message = $@\"Error: {", processedCode[2]);
Assert.Equal(" 1.ToString().", processedCode[3]);
}
}
}
1 change: 1 addition & 0 deletions CM+.Tests/CM+.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
</Choose>
<ItemGroup>
<Compile Include="BlankLinesCleaner.cs" />
<Compile Include="BracketsNormalizer.cs" />
<Compile Include="Common\Global.cs" />
<None Include="Common\TestClassB.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
197 changes: 140 additions & 57 deletions CM+.Tests/Common/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,124 @@

namespace CMPlus.Tests
{
public class TestBase
class Program
{
static TestBase()
static void Main()
{
Runtime.Settings = new Settings { AlignIndents = true, RemoveXmlDocGaps = true, SortUsings = true };
TestAlignment4();
// TestAlignment();
// TestAlignment_();
// TestFluent();
// TestGaps();
}
}

public static class Global
static void AlignFile(string before, string after)
{
var formattedText = File.ReadAllText(before)
.GetSyntaxRoot()
.AlignIndents()
.ToString();

File.WriteAllText(after, formattedText);
}

static void TestAlignment6()
{
FormatCode(@"
{
public IReadOnlyList<ScenarioInfo> FindScenarios(ScenarioDetail detailLevel)
{
public static SyntaxNode GetSyntaxRoot(this string code)
=> CSharpSyntaxTree.ParseText(code).GetRoot();
var allScenarios = _scenarios.Values
.Where(a => detailLevel >= a.DetailLevel)
.OrderBy(a => a.DetailLevel)
.ToArray();
public static string[] GetLines(this string text)
=> text.Replace("\r\n", "\n").Split('\n');
if (allScenarios.Select(a => a.Name).Distinct().Count() != allScenarios.Length)
{
throw new InvalidOperationException(
$@""Some scenarios have duplicate names: {
string.Join("", "",
allScenarios.GroupBy(a => a.Name)
.Where(a => a.Count() > 1)
.Select(a => a.Key))
}"");
}
return allScenarios;
}
}");
}

class Program
{
static void Main()
static void TestAlignment5()
{
// TestAlignment();
TestAlignment_();
// TestFluent();
// TestGaps();
FormatCode(@"
public OptimiserAction Create(SlideArea location)
{
var message = $@""Error: {
1.ToString().
Length}"";
var arr = new int[]{
1,2,3
};
var parameters = new List<Parameter>{
_locationParameter.WithValue((ushort)(location == SlideArea.OutputDrawer ? 1 : 0))
}
var parameters = new List<Parameter>{
_locationParameter.WithValue((ushort)(location == SlideArea.OutputDrawer ? 1 : 0))
}
};");
}

static void TestFluent()
static void TestAlignment()
{
var code = File.ReadAllText(@"..\..\Common\TestClassA.cs");
Console.WriteLine(AlignFluent(code));
AlignCode(@"
public void ScheduleScanIfNeeded()
{
_diagnosticLogger.Log(
LogEventLevel.Verbose,
""CameraScheduleController AwaitingScanResults = {0}"",
_store.Camera.AwaitingScanResults);
}");
}

class ReagentLaneScan
static void TestAlignment4()
{
public int LaneNumber;
public int TimeLoaded;
public int Reagents;
AlignCode(@"
static void Main()
{
var project =
new Project(""MyProduct"",
new Dir(@""%ProgramFiles%\My Company\My Product"",
new File(""Program.cs"")),
new ManagedAction(CustomActions.MyAction));
project.UI = WUI.WixUI_ProgressOnly;
project.BuildMsi();
}");
}

static void TestAlignment()
static void TestAlignment2()
{
var reagentLaneScan = new ReagentLaneScan
AlignCode(@"
{
public static Plan Create(
{
if (plannedResources.Any())
{
LaneNumber = 1,
TimeLoaded = 1,
Reagents = 1
};
if (incompleteActions.Any())
throw new ArgumentException(
$@""The following actions were not planned correctly: {
string.Join(""\n"", incompleteActions.Select(a => a.ToString()))
}"");
}
}
}");
}

static void TestAlignment1()
{
AlignCode(@"
var reagentLaneScan = new ReagentLaneScan
{
Expand All @@ -80,26 +149,12 @@ static void TestAlignment()
};");
}

static void TestAlignment2()
static void TestAlignment3()
{
// Console.WriteLine(
AlignCode(@"
[assembly: InternalsVisibleTo(""InstrumentControl.Proxy.Tests""),
InternalsVisibleTo(""DynamicProxyGenAssembly2""),
InternalsVisibleTo(""TATSimulator"")]
var map = new Dictionary<int, int>
{
{1, 2},
{3, 4}
}
var reagentsIdentified = new ReagentsIdentified
{
Console.WriteLine(
1,
2
);
Lanes = racks.Select(
rack =>
{
Expand All @@ -120,24 +175,16 @@ static void TestAlignment2()
}).ToList()
}
"//)
); ;
// Console.WriteLine(AlignCode(@"
// enum Test
// {
// one,
// two
// }

// ")); return;
// Console.WriteLine(AlignCode(File.ReadAllText(@"..\..\Common\TestClassB.cs"))); return;
"
);
}

static void TestAlignment_()
{
printOut = false;
var utf8WithBom = new System.Text.UTF8Encoding(true);

var files = Directory.GetFiles(@"D:\dev\TMS\taipan-app-master\DomainControl", "*.cs", SearchOption.AllDirectories);
var files = Directory.GetFiles("DomainControl", "*.cs", SearchOption.AllDirectories);
var count = 0;
foreach (var file in files)
{
Expand All @@ -154,19 +201,32 @@ static void TestAlignment_()
}
}

static bool printOut = true;

static string AlignCode(string code)
{
var changes = new DecoratedView(code);
var root = code.GetSyntaxRoot();

root = root.AlignIndents(changes.OnLineChanged);

// Console.WriteLine(changes);
if (printOut)
Console.WriteLine(changes);

var formattedText = root.ToFullString();
return formattedText;
}

static string FormatCode(string code)
{
var root = code.GetSyntaxRoot()
.FixBrackets();

var formattedText = root.ToString();
Console.WriteLine(formattedText);
return formattedText;
}

static string AlignFluent(string code)
{
var root = code.GetSyntaxRoot();
Expand Down Expand Up @@ -261,5 +321,28 @@ static void TestGaps()
for (int i = 0; i < root.GetText().Lines.Count; i++)
Console.WriteLine($"{i}: {root.GetText().Lines[i]}");
}

static void TestFluent()
{
var code = File.ReadAllText(@"..\..\Common\TestClassA.cs");
Console.WriteLine(AlignFluent(code));
}
}

public class TestBase
{
static TestBase()
{
Runtime.Settings = new Settings { AlignIndents = true, RemoveXmlDocGaps = true, SortUsings = true };
}
}

public static class Global
{
public static SyntaxNode GetSyntaxRoot(this string code)
=> CSharpSyntaxTree.ParseText(code).GetRoot();

public static string[] GetLines(this string text)
=> text.Replace("\r\n", "\n").Split('\n');
}
}
Loading

0 comments on commit d305db9

Please sign in to comment.