diff --git a/README.md b/README.md index ecd241ed..b78df4c4 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/Compiler/Argument/CompilerArguments.cs b/src/Compiler/Argument/CompilerArguments.cs index 1a8f0377..8bf994aa 100644 --- a/src/Compiler/Argument/CompilerArguments.cs +++ b/src/Compiler/Argument/CompilerArguments.cs @@ -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; } } diff --git a/src/Compiler/Argument/Pretty.cs b/src/Compiler/Argument/Pretty.cs new file mode 100644 index 00000000..7a160f83 --- /dev/null +++ b/src/Compiler/Argument/Pretty.cs @@ -0,0 +1,10 @@ +using System; + +namespace Compiler.Argument; + +[Flags] +public enum Pretty +{ + NONE, + PRETTY +} diff --git a/src/Compiler/Output/OutputGenerator.cs b/src/Compiler/Output/OutputGenerator.cs index d03a7591..e6037363 100644 --- a/src/Compiler/Output/OutputGenerator.cs +++ b/src/Compiler/Output/OutputGenerator.cs @@ -2,6 +2,7 @@ using Compiler.Model; using System.Linq; using System.Threading.Tasks; +using Compiler.Argument; using Compiler.Collector; namespace Compiler.Output @@ -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) @@ -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 @@ -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 + }; + } } } diff --git a/src/Compiler/SectorFileCompiler.cs b/src/Compiler/SectorFileCompiler.cs index ba0ffb2a..0f3ef3ef 100644 --- a/src/Compiler/SectorFileCompiler.cs +++ b/src/Compiler/SectorFileCompiler.cs @@ -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>(); diff --git a/src/CompilerCli/Argument/ArgumentParserFactory.cs b/src/CompilerCli/Argument/ArgumentParserFactory.cs index 7054beaf..0297f597 100644 --- a/src/CompilerCli/Argument/ArgumentParserFactory.cs +++ b/src/CompilerCli/Argument/ArgumentParserFactory.cs @@ -19,6 +19,7 @@ public static ArgumentParser Make() new CheckConfigCompilerArgument(), new LintCompilerArgument(), new ValidateCompilerArgument(), + new PrettyCompilerArgument() }, new SortedSet<AbstractCliArgument> { diff --git a/src/CompilerCli/Compiler/PrettyCompilerArgument.cs b/src/CompilerCli/Compiler/PrettyCompilerArgument.cs new file mode 100644 index 00000000..7e85221b --- /dev/null +++ b/src/CompilerCli/Compiler/PrettyCompilerArgument.cs @@ -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"; + } +} diff --git a/tests/CompilerCliTest/Argument/ArgumentParserFactoryTest.cs b/tests/CompilerCliTest/Argument/ArgumentParserFactoryTest.cs index ee8e21ae..28b286fa 100644 --- a/tests/CompilerCliTest/Argument/ArgumentParserFactoryTest.cs +++ b/tests/CompilerCliTest/Argument/ArgumentParserFactoryTest.cs @@ -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)); diff --git a/tests/CompilerCliTest/Compiler/PrettyCompilerArgumentTest.cs b/tests/CompilerCliTest/Compiler/PrettyCompilerArgumentTest.cs new file mode 100644 index 00000000..5cb132e2 --- /dev/null +++ b/tests/CompilerCliTest/Compiler/PrettyCompilerArgumentTest.cs @@ -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()); + } +}