public override Task <TextEditContainer?> Handle(DocumentFormattingParams request, CancellationToken cancellationToken) { CompilationContext?context = this.compilationManager.GetCompilation(request.TextDocument.Uri); if (context == null) { // we have not yet compiled this document, which shouldn't really happen this.logger.LogError("Document formatting request arrived before file {Uri} could be compiled.", request.TextDocument.Uri); return(Task.FromResult <TextEditContainer?>(null)); } long indentSize = request.Options.TabSize; IndentKindOption indentKindOption = request.Options.InsertSpaces ? IndentKindOption.Space : IndentKindOption.Tab; bool insertFinalNewline = request.Options.ContainsKey("insertFinalNewline") && request.Options.InsertFinalNewline; ProgramSyntax programSyntax = context.ProgramSyntax; PrettyPrintOptions options = new PrettyPrintOptions(NewlineOption.Auto, indentKindOption, indentSize, insertFinalNewline); string?output = PrettyPrinter.PrintProgram(programSyntax, options); if (output == null) { return(Task.FromResult <TextEditContainer?>(null)); } return(Task.FromResult <TextEditContainer?>(new TextEditContainer(new TextEdit { Range = programSyntax.Span.ToRange(context.LineStarts), NewText = output }))); }
public override IOutputAstVisitor CreateOutputVisitor() { CSharpOutputVisitor v = new CSharpOutputVisitor(); PrettyPrintOptions pOpt = v.Options; BraceStyle braceStyle; if (this.Options.BracesOnSameLine) { braceStyle = BraceStyle.EndOfLine; } else { braceStyle = BraceStyle.NextLine; } pOpt.StatementBraceStyle = braceStyle; pOpt.EventAddBraceStyle = braceStyle; pOpt.EventRemoveBraceStyle = braceStyle; pOpt.PropertyBraceStyle = braceStyle; pOpt.PropertyGetBraceStyle = braceStyle; pOpt.PropertySetBraceStyle = braceStyle; pOpt.IndentationChar = this.Options.IndentString[0]; pOpt.IndentSize = this.Options.IndentString.Length; pOpt.TabSize = this.Options.IndentString.Length; return(v); }
public void PrintSyntax_IndentKindOptionTab_ShouldIndentUsingTabs() { var programSyntax = ParserHelper.Parse(@" var foo = { xxx: true yyy: { mmm: false nnn: [ 100 'something' { aaa: bbb } ] } }"); var options = new PrettyPrintOptions(NewlineOption.Auto, IndentKindOption.Tab, 5, false); var output = PrettyPrinter.PrintProgram(programSyntax, options); output.Should().Be( @"var foo = { xxx: true yyy: { mmm: false nnn: [ 100 'something' { aaa: bbb } ] } }"); }
public void PrettyPrintOptions_SetIndentSize_ShouldEnsureValueDoesNotExceedLimit(long indentSize) { var options = new PrettyPrintOptions(NewlineOption.Auto, IndentKindOption.Space, indentSize, false); options.IndentSize.Should().BeGreaterOrEqualTo(1); options.IndentSize.Should().BeLessOrEqualTo(1000); }
public void PrintProgram_ProgramWithoutDiagnostics_ShouldRoundTrip(DataSet dataSet) { var program = ParserHelper.Parse(dataSet.Bicep); var options = new PrettyPrintOptions(NewlineOption.Auto, IndentKindOption.Space, 2, true); var formattedOutput = PrettyPrinter.PrintProgram(program, options); formattedOutput.Should().NotBeNull(); // The program should still be parsed without any errors after formatting. var formattedProgram = ParserHelper.Parse(formattedOutput !); formattedProgram.GetParseDiagnostics().Should().BeEmpty(); var buffer = new StringBuilder(); var printVisitor = new PrintVisitor(buffer, x => // Remove newlines and whitespaces. (x is Token token && token.Type == TokenType.NewLine) || (x is SyntaxTrivia trivia && trivia.Type == SyntaxTriviaType.Whitespace)); printVisitor.Visit(program); string programText = buffer.ToString(); buffer.Clear(); printVisitor.Visit(program); string formattedProgramText = buffer.ToString(); formattedProgramText.Should().Be(programText); }
public void IfStatementSeparateLines() { PrettyPrintOptions options = new PrettyPrintOptions(); options.PlaceElseOnNewLine = true; options.StatementBraceStyle = BraceStyle.NextLine; TestStatement("if (a)\n" + "{\n" + " m1();\n" + "}\n" + "else\n" + "{\n" + " m2();\n" + "}", options); TestStatement("if (a)\n" + "{\n" + " m1();\n" + "}\n" + "else if (b)\n" + "{\n" + " m2();\n" + "}\n" + "else\n" + "{\n" + " m3();\n" + "}", options); }
public void PrintProgram_AnyProgram_ShouldRoundTrip(DataSet dataSet) { var program = ParserHelper.Parse(dataSet.Bicep); var diagnostics = program.GetParseDiagnostics(); var diagnosticMessages = diagnostics.Select(d => d.Message); var options = new PrettyPrintOptions(NewlineOption.Auto, IndentKindOption.Space, 2, true); var formattedOutput = PrettyPrinter.PrintProgram(program, options); var formattedProgram = ParserHelper.Parse(formattedOutput !); var newDiagnostics = formattedProgram.GetParseDiagnostics(); var newDiagnosticMessages = newDiagnostics.Select(d => d.Message); // Diagnostic messages should remain the same after formatting. newDiagnostics.Should().HaveSameCount(diagnostics); newDiagnosticMessages.Should().BeEquivalentTo(diagnosticMessages); var buffer = new StringBuilder(); var printVisitor = new PrintVisitor(buffer, x => // Remove newlines and whitespaces. (x is Token token && token.Type == TokenType.NewLine) || (x is SyntaxTrivia trivia && trivia.Type == SyntaxTriviaType.Whitespace)); printVisitor.Visit(program); string programText = buffer.ToString(); buffer.Clear(); printVisitor.Visit(program); string formattedProgramText = buffer.ToString(); formattedProgramText.Should().Be(programText); }
protected internal string GetCode(INode node) { CSharpOutputVisitor outputVisitor = new CSharpOutputVisitor(); PrettyPrintOptions options = (PrettyPrintOptions)outputVisitor.Options; options.SpacesAfterComma = false; node.AcceptVisitor(outputVisitor, null); return(outputVisitor.Text); }
private StringWriter _writer; // for HtmlEncode #endregion Fields #region Constructors public HtmlPrettyPrintWriter(PrettyPrintOptions options, string missingNodeClass, string debugClass, string garbageClass) : base(options) { _buffer = new StringBuilder(); _writer = new StringWriter(_buffer); _missingNodeClass = missingNodeClass; _debugClass = debugClass; _garbageClass = garbageClass; }
public void PrintProgram_NewLineOptionAutoWithNoNewlineInProgram_ShouldUseEnvironmentNewline() { var programSyntax = ParserHelper.Parse("var foo = bar"); var options = new PrettyPrintOptions(NewlineOption.Auto, IndentKindOption.Space, 2, true); var output = PrettyPrinter.PrintProgram(programSyntax, options); output.Should().Be($"var foo = bar{Environment.NewLine}"); }
public HtmlPrettyPrintWriter(PrettyPrintOptions options, string missingNodeClass, string debugClass, string garbageClass) : base(options) { _buffer = new StringBuilder(); _writer = new StringWriter(_buffer); _missingNodeClass = missingNodeClass; _debugClass = debugClass; _garbageClass = garbageClass; }
public HtmlPrettyPrintWriter(PrettyPrintOptions options, string missingNodeClass, string debugClass, string garbageClass) : base(options) { _buffer = new StringBuilder(); _writer = new StringWriter(_buffer); _missingNodeClass = new SpanClass(Language.Instance, missingNodeClass, missingNodeClass, null); _debugClass = new SpanClass(Language.Instance, debugClass, debugClass, null); _garbageClass = new SpanClass(Language.Instance, garbageClass, garbageClass, null); }
void TestProgram(string program, PrettyPrintOptions options) { IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(program)); parser.Parse(); Assert.AreEqual("", parser.Errors.ErrorOutput); CSharpOutputVisitor outputVisitor = new CSharpOutputVisitor(); outputVisitor.Options = options; outputVisitor.VisitCompilationUnit(parser.CompilationUnit, null); Assert.AreEqual("", outputVisitor.Errors.ErrorOutput); Assert.AreEqual(StripWhitespace(program), StripWhitespace(outputVisitor.Text)); }
static PrettyPrintOptions CreatePrettyPrintOptions() { PrettyPrintOptions options = new PrettyPrintOptions(); options.PropertyBraceStyle = BraceStyle.NextLine; options.PropertyGetBraceStyle = BraceStyle.NextLine; options.PropertySetBraceStyle = BraceStyle.NextLine; options.ClassBraceStyle = BraceStyle.NextLine; options.IndentationChar = ' '; options.IndentSize = 4; options.TabSize = 4; return(options); }
private void SetPrettyPrintOptions(IOutputAstVisitor outputVisitor) { PrettyPrintOptions options = outputVisitor.Options as PrettyPrintOptions; options.NamespaceBraceStyle = BraceStyle.EndOfLine; options.ClassBraceStyle = BraceStyle.EndOfLine; options.MethodBraceStyle = BraceStyle.EndOfLine; options.PropertyBraceStyle = BraceStyle.EndOfLine; options.PropertyGetBraceStyle = BraceStyle.EndOfLine; options.PropertySetBraceStyle = BraceStyle.EndOfLine; options.ConstructorBraceStyle = BraceStyle.EndOfLine; options.TabSize = 8; options.IndentSize = 4; options.IndentationChar = ' '; }
public void PrintProgram_ProgramWithoutDiagnostics_ShouldProduceExpectedOutput(DataSet dataSet) { var program = ParserHelper.Parse(dataSet.Bicep); var options = new PrettyPrintOptions(NewlineOption.Auto, IndentKindOption.Space, 2, true); var formattedOutput = PrettyPrinter.PrintProgram(program, options); formattedOutput.Should().NotBeNull(); var resultsFile = FileHelper.SaveResultFile(this.TestContext !, Path.Combine(dataSet.Name, DataSet.TestFileMainFormatted), formattedOutput !); formattedOutput.Should().EqualWithLineByLineDiffOutput( formattedOutput !, expectedLocation: OutputHelper.GetBaselineUpdatePath(dataSet, DataSet.TestFileMainFormatted), actualLocation: resultsFile); }
void TestStatement(string statement, PrettyPrintOptions options) { StringBuilder b = new StringBuilder(); b.AppendLine("void Method()"); b.AppendLine("{"); using (StringReader r = new StringReader(statement)) { string line; while ((line = r.ReadLine()) != null) { b.Append(" "); b.AppendLine(line); } } b.AppendLine("}"); TestTypeMember(b.ToString(), options); }
void TestTypeMember(string program, PrettyPrintOptions options) { StringBuilder b = new StringBuilder(); b.AppendLine("class A"); b.AppendLine("{"); using (StringReader r = new StringReader(program)) { string line; while ((line = r.ReadLine()) != null) { b.Append(" "); b.AppendLine(line); } } b.AppendLine("}"); TestProgram(b.ToString(), options); }
public void PrintProgram_DoesNotRequestInsertFinalNewline_ShouldTrimNewlineAtTheEnd() { var programSyntax = ParserHelper.Parse(string.Concat(new[] { "var foo = bar\n", "var bar = foo\n" })); var options = new PrettyPrintOptions(NewlineOption.LF, IndentKindOption.Space, 2, false); var output = PrettyPrinter.PrintProgram(programSyntax, options); output.Should().Be(string.Concat(new[] { "var foo = bar\n", "var bar = foo" })); }
public void PrintProgram_NewLineOptionAuto_ShouldInferNewlineKindFromTheFirstNewline() { var programSyntax = ParserHelper.Parse(string.Concat(new[] { "var foo = bar\r", "var bar = foo\n" })); var options = new PrettyPrintOptions(NewlineOption.Auto, IndentKindOption.Space, 2, true); var output = PrettyPrinter.PrintProgram(programSyntax, options); output.Should().Be(string.Concat(new[] { "var foo = bar\r", "var bar = foo\r" })); }
public void PrintProgram_NewLineOptionNotAuto_ShouldUseSpecifiedNewline(NewlineOption newlineOption, string originalNewline, string expectedNewline) { var programSyntax = ParserHelper.Parse(string.Join(originalNewline, new[] { "var foo = bar", "var bar = foo", "var baz = bar" })); var options = new PrettyPrintOptions(newlineOption, IndentKindOption.Space, 2, false); var output = PrettyPrinter.PrintProgram(programSyntax, options); output.Should().Be(string.Join(expectedNewline, new[] { "var foo = bar", "var bar = foo", "var baz = bar" })); }
public void PrintProgram_AnyProgram_ShouldRoundTrip(DataSet dataSet) { var program = ParserHelper.Parse(dataSet.Bicep); var diagnostics = program.GetParseDiagnostics(); var diagnosticMessages = diagnostics.Select(d => d.Message); var options = new PrettyPrintOptions(NewlineOption.Auto, IndentKindOption.Space, 2, true); var formattedOutput = PrettyPrinter.PrintProgram(program, options); var formattedProgram = ParserHelper.Parse(formattedOutput !); var newDiagnostics = formattedProgram.GetParseDiagnostics(); var newDiagnosticMessages = newDiagnostics.Select(d => d.Message); // Diagnostic messages should remain the same after formatting. newDiagnostics.Should().HaveSameCount(diagnostics); newDiagnosticMessages.Should().BeEquivalentTo(diagnosticMessages); // Normalize formatting var regex = new Regex("[\\r\\n\\s]+"); string programText = regex.Replace(program.ToTextPreserveFormatting(), ""); string formattedProgramText = regex.Replace(formattedProgram.ToTextPreserveFormatting(), ""); formattedProgramText.Should().Be(programText); }
public void Example_uses_consistent_formatting(ExampleData example) { // save all the files in the containing directory to disk so that we can test module resolution var parentStream = Path.GetDirectoryName(example.BicepStreamName) !.Replace('\\', '/'); var outputDirectory = FileHelper.SaveEmbeddedResourcesWithPathPrefix(TestContext, typeof(ExamplesTests).Assembly, example.OutputFolderName, parentStream); var bicepFileName = Path.Combine(outputDirectory, Path.GetFileName(example.BicepStreamName)); var originalContents = File.ReadAllText(bicepFileName); var program = ParserHelper.Parse(originalContents); var printOptions = new PrettyPrintOptions(NewlineOption.LF, IndentKindOption.Space, 2, true); var formattedContents = PrettyPrinter.PrintProgram(program, printOptions); formattedContents.Should().NotBeNull(); File.WriteAllText(bicepFileName + ".formatted", formattedContents); originalContents.Should().EqualWithLineByLineDiffOutput( TestContext, formattedContents !, expectedLocation: example.BicepStreamName, actualLocation: bicepFileName + ".formatted"); }
public void PrettyPrintOptions_SetIndentSizeSmallerThanOne_ShouldSetToOne(long indentSize) { var options = new PrettyPrintOptions(NewlineOption.Auto, IndentKindOption.Space, indentSize, false); options.IndentSize.Should().Be(1); }
public void PrettyPrintOptions_SetIndentSizeGreaterThanOneThousand_ShouldSetToOneThousand(long indentSize) { var options = new PrettyPrintOptions(NewlineOption.Auto, IndentKindOption.Space, indentSize, false); options.IndentSize.Should().Be(1000); }
public void PrettyPrintOptions_SetIndentSizeBetweenOneAndOneThousand_ShouldSetToSpecifiedValue(long indentSize) { var options = new PrettyPrintOptions(NewlineOption.Auto, IndentKindOption.Space, indentSize, false); options.IndentSize.Should().Be((int)indentSize); }