Skip to content

Commit

Permalink
feat: pretty printing
Browse files Browse the repository at this point in the history
Adds the ability to insert an extra newline between large elements such as SECTOR
when printing, which provides improved readability.

Fixes #163
  • Loading branch information
AndyTWF committed Feb 19, 2023
1 parent 0badb2e commit 3314864
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ times, then the compiler will attempt to merge the configs together.
`--skip-validation` - If set, the compiler will skip the post-parse validation phase of compilation.
If running in full compilation mode, will still produce output.

`--pretty` - If set, "large" elements such as `SECTOR` will have an extra newline inserted between them, in order to provide improved readability.

`--strip-comments` - If set, any comments in the input will be removed. If an empty line is leftover, it will be discarded.

`--build-version` - Takes a single argument. Specifies the version of the build to replace the `{VERSION}` token in the input.
Expand Down
3 changes: 3 additions & 0 deletions src/Compiler/Argument/CompilerArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,8 @@ public override int GetHashCode()

// What mode should we run in.
public RunMode Mode { get; set; } = RunMode.COMPILE;

// Should we pretty print the output
public Pretty Pretty { get; set; } = Pretty.NONE;
}
}
10 changes: 10 additions & 0 deletions src/Compiler/Argument/Pretty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace Compiler.Argument;

[Flags]
public enum Pretty
{
NONE,
PRETTY
}
28 changes: 27 additions & 1 deletion src/Compiler/Output/OutputGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Compiler.Model;
using System.Linq;
using System.Threading.Tasks;
using Compiler.Argument;
using Compiler.Collector;

namespace Compiler.Output
Expand All @@ -11,15 +12,18 @@ public class OutputGenerator
private readonly SectorElementCollection sectorElements;
private readonly OutputGroupRepository outputGroups;
private readonly CompilableElementCollectorFactory collectorFactory;
private readonly CompilerArguments arguments;

public OutputGenerator(
SectorElementCollection sectorElements,
OutputGroupRepository outputGroups,
CompilableElementCollectorFactory collectorFactory
CompilableElementCollectorFactory collectorFactory,
CompilerArguments arguments
) {
this.sectorElements = sectorElements;
this.outputGroups = outputGroups;
this.collectorFactory = collectorFactory;
this.arguments = arguments;
}

public Task GenerateOutput(AbstractOutputFile outputFile)
Expand Down Expand Up @@ -63,6 +67,12 @@ public void CreateOutput(AbstractOutputFile outputFile)

element.Compile(sectorElements, outputStream);
}

// Add extra new line between elements if pretty printing is on
if (arguments.Pretty == Pretty.PRETTY && ElementCanBePrettyPrinted(provider))
{
outputStream.WriteLine();
}
}

// Write a newline at the end of a new section
Expand All @@ -73,5 +83,21 @@ public void CreateOutput(AbstractOutputFile outputFile)
// Flush the file to make sure it's written
outputStream.Flush();
}

private static bool ElementCanBePrettyPrinted(ICompilableElementProvider provider)
{
return provider switch
{
Geo => true,
Sector => true,
Sectorline => true,
CircleSectorline => true,
GroundNetwork => true,
SidStarRoute => true,
RadarHole => true,
Region => true,
_ => false
};
}
}
}
3 changes: 2 additions & 1 deletion src/Compiler/SectorFileCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public int Compile()
OutputGenerator generator = new OutputGenerator(
sectorElements,
outputGroups,
new CompilableElementCollectorFactory(sectorElements, outputGroups)
new CompilableElementCollectorFactory(sectorElements, outputGroups),
arguments
);

var outputTasks = new List<Task>();
Expand Down
1 change: 1 addition & 0 deletions src/CompilerCli/Argument/ArgumentParserFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static ArgumentParser Make()
new CheckConfigCompilerArgument(),
new LintCompilerArgument(),
new ValidateCompilerArgument(),
new PrettyCompilerArgument()
},
new SortedSet<AbstractCliArgument>
{
Expand Down
23 changes: 23 additions & 0 deletions src/CompilerCli/Compiler/PrettyCompilerArgument.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using Compiler.Argument;

namespace CompilerCli.Compiler;

public class PrettyCompilerArgument : AbstractCompilerArgument
{
public override void Parse(List<string> values, CompilerArguments compilerSettings)
{
if (values.Count != 0)
{
throw new ArgumentException("Pretty argument does not take any options");
}

compilerSettings.Pretty = Pretty.PRETTY;
}

public override string GetSpecifier()
{
return "--pretty";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class ArgumentParserFactoryTest
[InlineData(typeof(CheckConfigCompilerArgument))]
[InlineData(typeof(LintCompilerArgument))]
[InlineData(typeof(ValidateCompilerArgument))]
[InlineData(typeof(PrettyCompilerArgument))]
public void TestItAddsCompilerArguments(Type type)
{
Assert.True(ArgumentParserFactory.Make().HasCompilerArgument(type));
Expand Down
40 changes: 40 additions & 0 deletions tests/CompilerCliTest/Compiler/PrettyCompilerArgumentTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using Compiler.Argument;
using CompilerCli.Compiler;
using Xunit;

namespace CompilerCliTest.Compiler;

public class PrettyCompilerArgumentTest
{
private CompilerArguments arguments;
private PrettyCompilerArgument prettyCompilerArgument;

public PrettyCompilerArgumentTest()
{
arguments = new CompilerArguments();
prettyCompilerArgument = new PrettyCompilerArgument();
}

[Fact]
public void TestItSetsPrettyAsMode()
{
prettyCompilerArgument.Parse(new List<string>(), arguments);
Assert.Equal(Pretty.PRETTY, arguments.Pretty);
}

[Fact]
public void TestItThrowsExceptionOnValues()
{
Assert.Throws<ArgumentException>(
() => prettyCompilerArgument.Parse(new List<string>(new[] { "a"}), arguments)
);
}

[Fact]
public void TestItReturnsASpecifier()
{
Assert.Equal("--pretty", prettyCompilerArgument.GetSpecifier());
}
}

0 comments on commit 3314864

Please sign in to comment.