Skip to content

Commit

Permalink
Merge pull request #412 from microsoft/fix369
Browse files Browse the repository at this point in the history
Generate code with consistent line endings
  • Loading branch information
AArnott authored Sep 18, 2021
2 parents b13bfcb + a743773 commit e0d1a27
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.Windows.CsWin32/FastSyntaxFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ internal static SeparatedSyntaxList<TNode> SeparatedList<TNode>()

internal static SyntaxToken Literal(char value) => SyntaxFactory.Literal(TriviaList(), SymbolDisplay.FormatLiteral(value, quote: true), value, TriviaList());

internal static SyntaxTriviaList ParseLeadingTrivia(string text) => SyntaxFactory.ParseLeadingTrivia(text);
internal static SyntaxTriviaList ParseLeadingTrivia(string text) => SyntaxFactory.ParseLeadingTrivia(text.Replace("\r\n", "\n"));

internal static IdentifierNameSyntax IdentifierName(string name) => SyntaxFactory.IdentifierName(Identifier(name));

Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public class Generator : IDisposable
");

private static readonly XmlTextSyntax DocCommentStart = XmlText(" ").WithLeadingTrivia(DocumentationCommentExterior("///"));
private static readonly XmlTextSyntax DocCommentEnd = XmlText(XmlTextNewLine("\r\n", continueXmlDocumentationComment: false));
private static readonly XmlTextSyntax DocCommentEnd = XmlText(XmlTextNewLine("\n", continueXmlDocumentationComment: false));

private static readonly SyntaxToken SemicolonWithLineFeed = TokenWithLineFeed(SyntaxKind.SemicolonToken);
private static readonly IdentifierNameSyntax ConstantsClassName = IdentifierName("Constants");
Expand Down
35 changes: 35 additions & 0 deletions test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Text;
using Microsoft.Windows.CsWin32;
using Microsoft.Windows.CsWin32.Tests;
using Xunit;
Expand Down Expand Up @@ -2170,6 +2171,38 @@ static string JoinAssemblyMetadata(string name)

private static IEnumerable<AttributeSyntax> FindAttribute(SyntaxList<AttributeListSyntax> attributeLists, string name) => attributeLists.SelectMany(al => al.Attributes).Where(a => a.Name.ToString() == name);

private static void AssertConsistentLineEndings(Compilation compilation)
{
foreach (SyntaxTree doc in compilation.SyntaxTrees)
{
AssertConsistentLineEndings(doc);
}
}

private static void AssertConsistentLineEndings(SyntaxTree syntaxTree)
{
SourceText sourceText = syntaxTree.GetText();
int firstLineBreakLength = default;
int lineCount = 1;
foreach (TextLine line in sourceText.Lines)
{
int thisLineBreakLength = line.EndIncludingLineBreak - line.End;
if (lineCount == 1)
{
firstLineBreakLength = thisLineBreakLength;
}
else
{
if (firstLineBreakLength != thisLineBreakLength && thisLineBreakLength > 0)
{
Assert.False(true, $"{syntaxTree.FilePath} Line {lineCount} had a {thisLineBreakLength}-byte line ending but line 1's line ending was {firstLineBreakLength} bytes long.");
}
}

lineCount++;
}
}

private CSharpCompilation AddGeneratedCode(CSharpCompilation compilation, Generator generator)
{
var compilationUnits = generator.GetCompilationUnits(CancellationToken.None);
Expand Down Expand Up @@ -2232,6 +2265,8 @@ private void AssertNoDiagnostics(CSharpCompilation compilation, bool logAllGener
Assert.Empty(emitDiagnostics);
Assert.True(emitSuccessful);
}

AssertConsistentLineEndings(compilation);
}

private void LogDiagnostics(ImmutableArray<Diagnostic> diagnostics)
Expand Down

0 comments on commit e0d1a27

Please sign in to comment.