Roslyn CodeFixProvider中的一致行结尾

本文关键字:结尾 CodeFixProvider Roslyn | 更新日期: 2023-09-27 18:26:30

我目前正在使用Visual Studio 2015中提供的Roslyn SDK开发CodeFixProvider。提供程序的目标是为throw语句添加缺少的<exception>文档。

我正在构建这样的语法:

var newNodes = new XmlNodeSyntax[]
{
    SyntaxFactory.XmlText(
        SyntaxFactory.TokenList(
            SyntaxFactory.XmlTextLiteral(
                SyntaxFactory.TriviaList(SyntaxFactory.DocumentationCommentExterior("///")),
                string.Empty,
                string.Empty,
                SyntaxFactory.TriviaList()
            ),
            SyntaxFactory.XmlTextNewLine(SyntaxFactory.TriviaList(), "'n", "'n", SyntaxFactory.TriviaList()),
            SyntaxFactory.XmlTextLiteral(
                SyntaxFactory.TriviaList(SyntaxFactory.DocumentationCommentExterior("///")),
                " ",
                " ",
                SyntaxFactory.TriviaList()
            )
        )
    ),
    SyntaxFactory.XmlElement(
        SyntaxFactory.XmlElementStartTag(SyntaxFactory.XmlName("exception"))
            .WithAttributes(SyntaxFactory.SingletonList<XmlAttributeSyntax>(
                SyntaxFactory.XmlCrefAttribute(
                    SyntaxFactory.XmlName("cref"),
                    SyntaxFactory.Token(SyntaxKind.DoubleQuoteToken),
                    SyntaxFactory.NameMemberCref(
                        SyntaxFactory.IdentifierName(thrownTypeSymbol.ToMinimalDisplayString(semanticModel, throwSyntax.GetLocation().SourceSpan.Start))
                    ),
                    SyntaxFactory.Token(SyntaxKind.DoubleQuoteToken)
                ).WithLeadingTrivia(
                    SyntaxFactory.TriviaList(
                        SyntaxFactory.Whitespace(" ")
                    )
                )
            )),
        SyntaxFactory.XmlElementEndTag(SyntaxFactory.XmlName("exception"))
    ).WithContent(
        SyntaxFactory.List<XmlNodeSyntax>(new[] {
            SyntaxFactory.XmlText()
                .WithTextTokens(
                    SyntaxFactory.TokenList(
                        SyntaxFactory.XmlTextNewLine(SyntaxFactory.TriviaList(), "'n", "'n", SyntaxFactory.TriviaList()),
                        SyntaxFactory.XmlTextLiteral(
                            SyntaxFactory.TriviaList(SyntaxFactory.DocumentationCommentExterior("///")),
                            " ",
                            " ",
                            SyntaxFactory.TriviaList()
                        ),
                        SyntaxFactory.XmlTextNewLine(SyntaxFactory.TriviaList(), "'n", "'n", SyntaxFactory.TriviaList()),
                        SyntaxFactory.XmlTextLiteral(
                            SyntaxFactory.TriviaList(SyntaxFactory.DocumentationCommentExterior("///")),
                            " ",
                            " ",
                            SyntaxFactory.TriviaList()
                        )
                    )
                )  
        })
    ),
    SyntaxFactory.XmlText(
        SyntaxFactory.TokenList(    
            SyntaxFactory.XmlTextNewLine(
                SyntaxFactory.TriviaList(),
                Environment.NewLine,
                Environment.NewLine,
                SyntaxFactory.TriviaList()
            )   
        )
    )
};

然后稍后将这些节点附加到现有的DocumentationCommentTriviaSyntax节点。

newTrivia = xmlTrivia.AddContent(newNodes);

问题
这确实添加了缺少的<exception>文档注释,但遗憾的是使用了错误的行结尾。我可以将硬编码的'n更改为Environment.NewLine,但这可能仍然与文件的行尾不匹配。

有没有一种方法可以使用应用修复的文档的行尾?

Roslyn CodeFixProvider中的一致行结尾

您可以检测哪个换行符像这个一样被使用

    private SyntaxTrivia _detectLineBreakTrivia(SyntaxNode root)
    {
       var result = root.DescendantTokens()
                        .SelectMany(token => token.TrailingTrivia)
                         .FirstOrDefault(trivia => trivia.IsKind(SyntaxKind.EndOfLineTrivia));
       if(Equals(result, default(SyntaxTrivia))
           resut = SyntaxFactory.CarriageReturnLineFeed;
       return result;
    }