diff --git a/src/Microsoft.Windows.CsWin32/FastSyntaxFactory.cs b/src/Microsoft.Windows.CsWin32/FastSyntaxFactory.cs index 008bb060..f7c098bb 100644 --- a/src/Microsoft.Windows.CsWin32/FastSyntaxFactory.cs +++ b/src/Microsoft.Windows.CsWin32/FastSyntaxFactory.cs @@ -431,7 +431,7 @@ internal static SeparatedSyntaxList SeparatedList() 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)); diff --git a/src/Microsoft.Windows.CsWin32/Generator.cs b/src/Microsoft.Windows.CsWin32/Generator.cs index 742eda84..bc1c8ea6 100644 --- a/src/Microsoft.Windows.CsWin32/Generator.cs +++ b/src/Microsoft.Windows.CsWin32/Generator.cs @@ -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"); diff --git a/test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs b/test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs index b0143e73..e447372e 100644 --- a/test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs +++ b/test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs @@ -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; @@ -2170,6 +2171,38 @@ static string JoinAssemblyMetadata(string name) private static IEnumerable FindAttribute(SyntaxList 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); @@ -2232,6 +2265,8 @@ private void AssertNoDiagnostics(CSharpCompilation compilation, bool logAllGener Assert.Empty(emitDiagnostics); Assert.True(emitSuccessful); } + + AssertConsistentLineEndings(compilation); } private void LogDiagnostics(ImmutableArray diagnostics)