本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxNode.GetTrailingTrivia方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.GetTrailingTrivia方法的具体用法?C# SyntaxNode.GetTrailingTrivia怎么用?C# SyntaxNode.GetTrailingTrivia使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.GetTrailingTrivia方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReplaceDateTimeAsync
private async Task<Document> ReplaceDateTimeAsync(Document document,
SyntaxNode node,
CancellationToken cancellationToken)
{
// Get the root syntax node for the current document
var root = await document.GetSyntaxRootAsync();
// Convert the syntax node into the specialized kind
var convertedNode = (IdentifierNameSyntax)node;
// Create a new syntax node
var newNode = convertedNode?.WithIdentifier(SyntaxFactory.
ParseToken("DateTimeOffset")).
WithLeadingTrivia(node.GetLeadingTrivia()).
WithTrailingTrivia(node.GetTrailingTrivia());
// Create a new root syntax node for the current document
// replacing the syntax node that has diagnostic with
// a new syntax node
var newRoot = root.ReplaceNode(node, newNode);
// Generate a new document
var newDocument = document.WithSyntaxRoot(newRoot);
return newDocument;
}
示例2: GetBestNewLineTrivia
/// <summary>
/// Look at the context of the node to determine the best possible new line trivia. It will prefer
/// existing new lines over creating a new one to help ensure the same new lines are preserved
/// throughout the file.
/// </summary>
internal static SyntaxTrivia GetBestNewLineTrivia(SyntaxNode node, SyntaxTrivia? defaultNewLineTrivia = null)
{
SyntaxTrivia trivia;
if (TryGetExistingNewLine(node.GetLeadingTrivia(), out trivia) ||
TryGetExistingNewLine(node.GetTrailingTrivia(), out trivia))
{
return trivia;
}
return defaultNewLineTrivia ?? SyntaxFactory.CarriageReturnLineFeed;
}
示例3: ReplaceAddExpressionByStringBuilderAppendExpression
private static SyntaxNode ReplaceAddExpressionByStringBuilderAppendExpression(AssignmentExpressionSyntax assignmentExpression, SyntaxNode expressionStatement, SyntaxNode expressionStatementParent, string builderName)
{
var appendExpressionOnLoop = assignmentExpression.IsKind(SyntaxKind.SimpleAssignmentExpression)
? SyntaxFactory.ParseStatement($"{builderName}.Append({((BinaryExpressionSyntax)assignmentExpression.Right).Right.ToString()});\r\n")
: SyntaxFactory.ParseStatement($"{builderName}.Append({assignmentExpression.Right.ToString()});\r\n");
appendExpressionOnLoop = appendExpressionOnLoop
.WithLeadingTrivia(expressionStatement.GetLeadingTrivia())
.WithTrailingTrivia(expressionStatement.GetTrailingTrivia());
var newExpressionStatementParent = expressionStatementParent.ReplaceNode(expressionStatement, appendExpressionOnLoop);
return newExpressionStatementParent;
}
示例4: UseVarAsync
private Task<Solution> UseVarAsync(Document document, SyntaxNode root, SyntaxNode statement)
{
var varIdentifier = SyntaxFactory.IdentifierName("var")
.WithLeadingTrivia(statement.GetLeadingTrivia())
.WithTrailingTrivia(statement.GetTrailingTrivia());
var newRoot = root.ReplaceNode(statement, varIdentifier);
var newDocument = document.WithSyntaxRoot(newRoot);
return Task.FromResult(newDocument.Project.Solution);
}
示例5: GetFix
private async Task<Document> GetFix(Document document, SyntaxNode expression, CancellationToken cancellationToken)
{
// Rewrite the expression to include a .ConfigureAwait() after it. We reattach trailing trivia to the end.
// This is especially important for VB, as the end-of-line may be in the trivia
DocumentEditor editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
SyntaxGenerator generator = editor.Generator;
SyntaxNode memberAccess = generator.MemberAccessExpression(expression.WithoutTrailingTrivia(), "ConfigureAwait");
SyntaxNode falseLiteral = generator.FalseLiteralExpression();
SyntaxNode invocation = generator.InvocationExpression(memberAccess, falseLiteral);
invocation = invocation.WithLeadingTrivia(expression.GetLeadingTrivia()).WithTrailingTrivia(expression.GetTrailingTrivia());
editor.ReplaceNode(expression, invocation);
return editor.GetChangedDocument();
}
示例6: ConvertToArrayEmpty
private async Task<Document> ConvertToArrayEmpty(Document document, SyntaxNode nodeToFix, CancellationToken cancellationToken)
{
DocumentEditor editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
SyntaxGenerator generator = editor.Generator;
SyntaxNode variableDecl = generator.GetDeclaration(nodeToFix, DeclarationKind.Variable);
SyntaxNode typeNode = generator.GetType(variableDecl);
var type = semanticModel.GetTypeInfo(typeNode, cancellationToken).Type as IArrayTypeSymbol;
INamedTypeSymbol arrayTypeSymbol = semanticModel.Compilation.GetTypeByMetadataName(AvoidZeroLengthArrayAllocationsAnalyzer.ArrayTypeName);
SyntaxNode arrayEmptyName = generator.QualifiedName(generator.TypeExpression(arrayTypeSymbol),
generator.GenericName(AvoidZeroLengthArrayAllocationsAnalyzer.ArrayEmptyMethodName, type.ElementType));
SyntaxNode arrayEmptyInvocation = generator.InvocationExpression(arrayEmptyName);
arrayEmptyInvocation = arrayEmptyInvocation.WithLeadingTrivia(nodeToFix.GetLeadingTrivia()).WithTrailingTrivia(nodeToFix.GetTrailingTrivia());
editor.ReplaceNode(nodeToFix, arrayEmptyInvocation);
return editor.GetChangedDocument();
}
示例7: CreateEqualsExpression
internal SyntaxNode CreateEqualsExpression(SyntaxGenerator syntaxFactoryService, SemanticModel model, SyntaxNode operand1, SyntaxNode operand2, bool isEquals)
{
var stringType = model.Compilation.GetSpecialType(SpecialType.System_String);
var memberAccess = syntaxFactoryService.MemberAccessExpression(
syntaxFactoryService.TypeExpression(stringType),
syntaxFactoryService.IdentifierName(UseOrdinalStringComparisonAnalyzer.EqualsMethodName));
var ordinal = CreateOrdinalMemberAccess(syntaxFactoryService, model);
var invocation = syntaxFactoryService.InvocationExpression(
memberAccess,
operand1,
operand2.WithoutTrailingTrivia(),
ordinal)
.WithAdditionalAnnotations(Formatter.Annotation);
if (!isEquals)
{
invocation = syntaxFactoryService.LogicalNotExpression(invocation);
}
invocation = invocation.WithTrailingTrivia(operand2.GetTrailingTrivia());
return invocation;
}
示例8: WriteTrailingTrivia
public static void WriteTrailingTrivia(OutputWriter writer, SyntaxNode node)
{
SyntaxTriviaList triviaList = node.GetTrailingTrivia();
foreach (var trivia in triviaList)
{
if (trivia.CSharpKind() == SyntaxKind.SingleLineCommentTrivia ||
trivia.CSharpKind() == SyntaxKind.MultiLineCommentTrivia ||
trivia.CSharpKind() == SyntaxKind.SingleLineDocumentationCommentTrivia ||
trivia.CSharpKind() == SyntaxKind.MultiLineDocumentationCommentTrivia ||
trivia.CSharpKind() == SyntaxKind.WhitespaceTrivia)
{
if (trivia.CSharpKind() == SyntaxKind.WhitespaceTrivia)
{
if (trivia.ToFullString().EndsWith("\n"))
{
writer.WriteLine();
writer.WriteIndent();
}
}
else
writer.WriteLine(trivia.ToFullString());
}
}
}
示例9: HasLineEnding
internal static bool HasLineEnding(SyntaxNode node)
{
return node.HasTrailingTrivia &&
node.GetTrailingTrivia().Last().IsKind(SyntaxKind.EndOfLineTrivia);
}
示例10: HasWatchComment
bool HasWatchComment(SyntaxNode node)
{
// expect that only valid node types are passed here
return
node
.GetTrailingTrivia()
.Any(t => t.Kind() == SyntaxKind.SingleLineCommentTrivia && t.ToString().StartsWith("//="));
}
示例11: Visit
public override SyntaxNode Visit(SyntaxNode node)
{
PatternSearchResult result;
if (!_results.TryGetValue(node, out result))
return base.Visit(node);
var pattern = _patterns[result.Search];
var rewrittenCaptures = result.Captures.Select(VisitCapture)
.ToDictionary(c => c.Variable);
var expander = new PatternExpander(pattern, rewrittenCaptures);
var syntaxNode = expander.Visit(pattern.Node);
return syntaxNode.WithLeadingTrivia(node.GetLeadingTrivia())
.WithTrailingTrivia(node.GetTrailingTrivia());
}