本文整理汇总了C#中LiteralExpressionSyntax.GetLeadingTrivia方法的典型用法代码示例。如果您正苦于以下问题:C# LiteralExpressionSyntax.GetLeadingTrivia方法的具体用法?C# LiteralExpressionSyntax.GetLeadingTrivia怎么用?C# LiteralExpressionSyntax.GetLeadingTrivia使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LiteralExpressionSyntax
的用法示例。
在下文中一共展示了LiteralExpressionSyntax.GetLeadingTrivia方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FormatXmlAsync
private async Task<Document> FormatXmlAsync(Document document, LiteralExpressionSyntax sleSyntax, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken);
var tree = root.SyntaxTree;
//Get the character position of the LiteralExpressionSyntax
FileLinePositionSpan position = tree.GetLineSpan(sleSyntax.Span);
int cSpace = position.StartLinePosition.Character;
//Figure out the preceeding trivia since we can't get the column position from GetLineSpan
var parentTrivia = sleSyntax.GetLeadingTrivia().ToFullString();
var xml = sleSyntax.GetFirstToken().ValueText;
var newXmlText = FormatXml(xml);
//Process each line of the formatted XML and prepend the parent trivia & spaces
string[] xmlLines = newXmlText.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
for (int i = 1; i < xmlLines.Length; i++)
{
xmlLines[i] = parentTrivia + new String(' ', (cSpace + 2)) + xmlLines[i];
}
newXmlText = String.Join("\r\n", xmlLines);
var newXmlValue = "@\"" + newXmlText + "\"";
var newNode = SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression,
SyntaxFactory.Token(SyntaxTriviaList.Empty, SyntaxKind.StringLiteralToken, newXmlValue, newXmlText, SyntaxTriviaList.Empty));
var newRoot = root.ReplaceNode(sleSyntax, newNode);
return document.WithSyntaxRoot(newRoot);
}
示例2: VisitLiteralExpression
public override SyntaxNode VisitLiteralExpression(LiteralExpressionSyntax node)
{
if (node.CSharpKind() != SyntaxKind.StringLiteralExpression) return node;
var pos = node.GetLocation().GetMappedLineSpan();
var result = OnRewrite(pos.StartLinePosition.Line + 1, pos.StartLinePosition.Character + 1, Source.Lines[pos.StartLinePosition.Line].ToString(), node.Token.ValueText);
if (result == null) return node;
var names = result.Split('.');
ExpressionSyntax newNode = SyntaxFactory.IdentifierName(names.First());
foreach (var name in names.Skip(1))
{
newNode = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, newNode, SyntaxFactory.IdentifierName(name));
}
return newNode.WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
}