[英]Consistent line endings in a Roslyn CodeFixProvider
I'm currently working on a CodeFixProvider
using the Roslyn SDK provided in Visual Studio 2015. The goal of the provider is to add missing <exception>
documentation for throw statements. 我目前正在使用Visual Studio 2015中提供的Roslyn SDK开发
CodeFixProvider
。提供程序的目标是为throw语句添加缺少的<exception>
文档。
I'm building the syntax like this: 我正在建立这样的语法:
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()
)
)
)
};
And then later appending the nodes to an existing DocumentationCommentTriviaSyntax
node. 然后将节点附加到现有的
DocumentationCommentTriviaSyntax
节点。
newTrivia = xmlTrivia.AddContent(newNodes);
The problem 问题
This does add the missing <exception> documentation comments, but sadly using the wrong line endings. 这确实会添加缺少的<exception>文档注释,但可悲的是使用了错误的行尾。 I could change the hardcoded
\\n
to Environment.NewLine
but that still might not match the line endings of the file. 我可以将硬编码的
\\n
更改为Environment.NewLine
但这仍可能与文件的行尾不匹配。
Is there a way to use the line endings of the document the fix is applied in? 有没有一种方法可以使用要应用修订的文档的行尾?
You could detect which linebreak is used like this 您可以像这样检测使用哪个换行符
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.