private static Doc Print(BlockSyntax node, string?groupId) { Doc statementSeparator = node.Parent is AccessorDeclarationSyntax && node.Statements.Count <= 1 ? Doc.Line : Doc.HardLine; var docs = new List <Doc> { groupId != null ? Doc.IfBreak(" ", Doc.Line, groupId) : node.Parent is ParenthesizedLambdaExpressionSyntax ? Doc.Null : Doc.Line, Token.Print(node.OpenBraceToken) }; if (node.Statements.Count > 0) { var innerDoc = Doc.Indent( statementSeparator, Doc.Join(statementSeparator, node.Statements.Select(Node.Print)) ); DocUtilities.RemoveInitialDoubleHardLine(innerDoc); docs.Add(Doc.Concat(innerDoc, statementSeparator)); } docs.Add( node.Statements.Count == 0 ? " " : Doc.Null, Token.Print(node.CloseBraceToken) ); return(Doc.Group(docs)); }
public void RemoveInitialDoubleHardLine_Should_Only_Remove_Initial_HardLines() { var doc = Doc.Concat("1", Doc.HardLine, Doc.HardLine); DocUtilities.RemoveInitialDoubleHardLine(doc); doc.Should().BeEquivalentTo(Doc.Concat("1", Doc.HardLine, Doc.HardLine)); }
public void RemoveInitialDoubleHardLine_Should_Handle_Empty_List() { var doc = new List <Doc>(); DocUtilities.RemoveInitialDoubleHardLine(doc); doc.Should().BeEmpty(); }
public void RemoveInitialDoubleHardLine_Should_Remove_Simple_Double_HardLine() { var doc = new List <Doc> { Doc.HardLine, Doc.HardLine }; DocUtilities.RemoveInitialDoubleHardLine(doc); doc.Should().HaveCount(1); }
public void RemoveInitialDoubleHardLine_Should_Remove_Null() { var doc = new List <Doc> { Doc.Null }; DocUtilities.RemoveInitialDoubleHardLine(doc); doc.Should().BeEmpty(); }
public void RemoveInitialDoubleHardLine_Work_With_Doc_Null_Before_String() { var doc = Doc.Concat(Doc.HardLine, Doc.Null, "1", Doc.HardLine, "2"); DocUtilities.RemoveInitialDoubleHardLine(doc); DocSerializer.Serialize(doc) .Should() .Be(DocSerializer.Serialize(Doc.Concat(Doc.HardLine, "1", Doc.HardLine, "2"))); }
public void RemoveInitialDoubleHardLine_Should_Remove_Deep_Concated_HardLine() { var concat = Doc.Concat(Doc.HardLine, Doc.HardLine); var doc = new List <Doc> { Doc.Concat(concat) }; DocUtilities.RemoveInitialDoubleHardLine(doc); concat.Contents.Should().ContainSingle(); }
public void RemoveInitialDoubleHardLine_Should_Not_Remove_Deep_Concated_HardLine() { var concat = Doc.Concat(Doc.HardLine); var doc = new List <Doc> { Doc.Concat(concat) }; DocUtilities.RemoveInitialDoubleHardLine(doc); concat.Contents.Should().BeEquivalentTo(Doc.HardLine); }
public void RemoveInitialDoubleHardLine_Should_Remove_Grouped_Double_HardLine() { var contents = new List <Doc> { Doc.HardLine, Doc.HardLine }; var doc = Doc.Group(contents); DocUtilities.RemoveInitialDoubleHardLine(doc); contents.Should().ContainSingle(); }
public void RemoveInitialDoubleHardLine_Should_Not_Remove_Deep_Indented_HardLine() { var indent = Doc.Indent(Doc.HardLine); var doc = new List <Doc> { Doc.Indent(indent) }; DocUtilities.RemoveInitialDoubleHardLine(doc); indent.Contents.Should().BeEquivalentTo(Doc.HardLine); }
public void RemoveInitialDoubleHardLine_Should_Remove_Indented_HardLine() { var indent = Doc.Indent(Doc.HardLine); var doc = new List <Doc> { Doc.HardLine, indent }; DocUtilities.RemoveInitialDoubleHardLine(doc); indent.Contents.Should().Be(Doc.Null); }
public void RemoveInitialDoubleHardLine_Should_Remove_Concated_HardLine() { var concat = Doc.Concat(Doc.HardLine, Doc.HardLine); var doc = new List <Doc> { concat }; DocUtilities.RemoveInitialDoubleHardLine(doc); concat.Should().BeEquivalentTo(Doc.Concat(Doc.HardLine, Doc.Null)); }
public void RemoveInitialDoubleHardLine_Should_Remove_Simple_Double_HardLine() { var doc = new List <Doc> { Doc.HardLine, Doc.HardLine }; DocUtilities.RemoveInitialDoubleHardLine(doc); doc.Should().BeEquivalentTo(new List <Doc> { Doc.HardLine, Doc.Null }); }
public void RemoveInitialDoubleHardLine_Should_Remove_Grouped_Double_HardLine() { var contents = new List <Doc> { Doc.HardLine, Doc.HardLine }; var doc = Doc.Group(contents); DocUtilities.RemoveInitialDoubleHardLine(doc); contents.Should().BeEquivalentTo(new List <Doc> { Doc.HardLine, Doc.Null }); }
public void RemoveInitialDoubleHardLine_Should_Remove_Single_HardLine() { var concat = Doc.Concat(Doc.HardLine, Doc.HardLine, Doc.HardLine); var doc = new List <Doc> { Doc.Concat(concat) }; DocUtilities.RemoveInitialDoubleHardLine(doc); concat.Contents.Should() .BeEquivalentTo(new List <Doc> { Doc.HardLine, Doc.Null, Doc.HardLine }); }
public static Doc Print(BaseTypeDeclarationSyntax node) { ParameterListSyntax? parameterList = null; TypeParameterListSyntax?typeParameterList = null; var constraintClauses = Enumerable.Empty <TypeParameterConstraintClauseSyntax>(); var hasMembers = false; SyntaxToken?keyword = null; Doc members = Doc.Null; SyntaxToken?semicolonToken = null; string? groupId = null; if (node is TypeDeclarationSyntax typeDeclarationSyntax) { typeParameterList = typeDeclarationSyntax.TypeParameterList; constraintClauses = typeDeclarationSyntax.ConstraintClauses; hasMembers = typeDeclarationSyntax.Members.Count > 0; if (typeDeclarationSyntax.Members.Count > 0) { members = Doc.Indent( Doc.HardLine, Doc.Join(Doc.HardLine, typeDeclarationSyntax.Members.Select(Node.Print)) ); } if (node is ClassDeclarationSyntax classDeclarationSyntax) { keyword = classDeclarationSyntax.Keyword; } else if (node is StructDeclarationSyntax structDeclarationSyntax) { keyword = structDeclarationSyntax.Keyword; } else if (node is InterfaceDeclarationSyntax interfaceDeclarationSyntax) { keyword = interfaceDeclarationSyntax.Keyword; } else if (node is RecordDeclarationSyntax recordDeclarationSyntax) { keyword = recordDeclarationSyntax.Keyword; groupId = Guid.NewGuid().ToString(); parameterList = recordDeclarationSyntax.ParameterList; } semicolonToken = typeDeclarationSyntax.SemicolonToken; } else if (node is EnumDeclarationSyntax enumDeclarationSyntax) { members = Doc.Indent( Doc.HardLine, SeparatedSyntaxList.Print( enumDeclarationSyntax.Members, EnumMemberDeclaration.Print, Doc.HardLine ) ); hasMembers = enumDeclarationSyntax.Members.Count > 0; keyword = enumDeclarationSyntax.EnumKeyword; semicolonToken = enumDeclarationSyntax.SemicolonToken; } var docs = new List <Doc> { ExtraNewLines.Print(node), AttributeLists.Print(node, node.AttributeLists), Modifiers.Print(node.Modifiers) }; if (keyword != null) { docs.Add(Token.PrintWithSuffix(keyword.Value, " ")); } docs.Add(Token.Print(node.Identifier)); if (typeParameterList != null) { docs.Add(TypeParameterList.Print(typeParameterList)); } if (parameterList != null) { docs.Add(ParameterList.Print(parameterList, groupId)); } if (node.BaseList != null) { docs.Add(BaseList.Print(node.BaseList)); } docs.Add(ConstraintClauses.Print(constraintClauses)); if (hasMembers) { DocUtilities.RemoveInitialDoubleHardLine(members); var useSpaceBraceWithParameterList = groupId != null && parameterList != null; docs.Add( useSpaceBraceWithParameterList ? Doc.IfBreak(" ", Doc.Line, groupId) : Doc.HardLine, Token.Print(node.OpenBraceToken), members, Doc.HardLine, Token.Print(node.CloseBraceToken) ); } else if (node.OpenBraceToken.Kind() != SyntaxKind.None) { Doc separator = node.CloseBraceToken.LeadingTrivia.Any() ? Doc.Line : " "; docs.Add( separator, Token.Print(node.OpenBraceToken), separator, Token.Print(node.CloseBraceToken) ); } if (semicolonToken.HasValue) { docs.Add(Token.Print(semicolonToken.Value)); } return(Doc.Concat(docs)); }
public static Doc Print(NamespaceDeclarationSyntax node) { var docs = new List <Doc> { ExtraNewLines.Print(node), AttributeLists.Print(node, node.AttributeLists), Modifiers.Print(node.Modifiers), Token.Print(node.NamespaceKeyword), " ", Node.Print(node.Name) }; var innerDocs = new List <Doc>(); var hasMembers = node.Members.Count > 0; var hasUsing = node.Usings.Count > 0; var hasExterns = node.Externs.Count > 0; if (hasMembers || hasUsing || hasExterns) { innerDocs.Add(Doc.HardLine); if (hasExterns) { innerDocs.Add( Doc.Join(Doc.HardLine, node.Externs.Select(ExternAliasDirective.Print)), Doc.HardLine ); } if (hasUsing) { innerDocs.Add( Doc.Join(Doc.HardLine, node.Usings.Select(UsingDirective.Print)), Doc.HardLine ); } if (hasMembers) { innerDocs.Add( Doc.Join(Doc.HardLine, node.Members.Select(Node.Print)), Doc.HardLine ); } innerDocs.RemoveAt(innerDocs.Count - 1); } else { innerDocs.Add(" "); } DocUtilities.RemoveInitialDoubleHardLine(innerDocs); docs.Add( Doc.Group( Doc.Line, Token.Print(node.OpenBraceToken), Doc.Indent(innerDocs), hasMembers || hasUsing || hasExterns ? Doc.HardLine : Doc.Null, Token.Print(node.CloseBraceToken), Token.Print(node.SemicolonToken) ) ); return(Doc.Concat(docs)); }