本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxNode.ReplaceToken方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.ReplaceToken方法的具体用法?C# SyntaxNode.ReplaceToken怎么用?C# SyntaxNode.ReplaceToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.ReplaceToken方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetTransformedDocumentAsync
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, Diagnostic diagnostic)
{
var token = root.FindToken(diagnostic.Location.SourceSpan.Start, findInsideTrivia: true);
var updatedDocument = document.WithSyntaxRoot(root.ReplaceToken(token, token.WithLeadingTrivia(token.LeadingTrivia.Add(SyntaxFactory.Space))));
return Task.FromResult(updatedDocument);
}
示例2: GetTransformedDocumentAsync
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, SyntaxToken token)
{
SyntaxToken corrected = token.WithTrailingTrivia(token.TrailingTrivia.Insert(0, SyntaxFactory.Space));
Document updatedDocument = document.WithSyntaxRoot(root.ReplaceToken(token, corrected));
return Task.FromResult(updatedDocument);
}
示例3: AppendAsyncSuffixToMethodNameAsync
private Task<Document> AppendAsyncSuffixToMethodNameAsync(Document document, SyntaxNode root, SyntaxToken methodNameToken
, CancellationToken cancellationToken)
{
string methodName = methodNameToken.Text;
string newMethodName = (methodName.EndsWith("async") ? methodName.Substring(0, methodName.Length - 5) : methodName)
+ "Async";
SyntaxNode newRoot = root.ReplaceToken(methodNameToken, SyntaxFactory.Identifier(newMethodName));
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}
开发者ID:RickyLin,项目名称:CodingConventionsCop,代码行数:9,代码来源:NameOfAwaitableMethodEndWithAsyncCodeFixProvider.cs
示例4: PerformAction
static Document PerformAction(Document document, SyntaxNode root, SyntaxToken token)
{
var node = token.Parent as LiteralExpressionSyntax;
var newRoot = root.ReplaceToken(
token,
SyntaxFactory.ParseToken(string.Format("&H{0:x}", token.Value))
.WithLeadingTrivia(node.GetLeadingTrivia())
.WithTrailingTrivia(node.GetTrailingTrivia())
);
return document.WithSyntaxRoot(newRoot);
}
示例5: GetTransformedDocumentAsync
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, SyntaxToken keywordToken)
{
SyntaxToken hashToken = keywordToken.GetPreviousToken(includeDirectives: true);
if (!hashToken.IsKind(SyntaxKind.HashToken))
{
return Task.FromResult(document);
}
SyntaxToken corrected = hashToken.WithoutTrailingWhitespace().WithoutFormatting();
Document updatedDocument = document.WithSyntaxRoot(root.ReplaceToken(hashToken, corrected));
return Task.FromResult(updatedDocument);
}
示例6: RegisterCodeFixesAsync
protected sealed override async Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context)
{
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var token = root.FindToken(diagnosticSpan.Start);
context.RegisterCodeFix(
CodeAction.Create(
Title,
c =>
{
var newRoot = root.ReplaceToken(token, SyntaxFactory.Token(SyntaxKind.None));
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
}),
context.Diagnostics);
}
示例7: RenameSymbolAsync
public static async Task<Solution> RenameSymbolAsync(Document document, SyntaxNode root, SyntaxToken declarationToken, string newName, CancellationToken cancellationToken)
{
var annotatedRoot = root.ReplaceToken(declarationToken, declarationToken.WithAdditionalAnnotations(RenameAnnotation.Create()));
var annotatedSolution = document.Project.Solution.WithDocumentSyntaxRoot(document.Id, annotatedRoot);
var annotatedDocument = annotatedSolution.GetDocument(document.Id);
annotatedRoot = await annotatedDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var annotatedToken = annotatedRoot.FindToken(declarationToken.SpanStart);
var semanticModel = await annotatedDocument.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var symbol = semanticModel.GetDeclaredSymbol(annotatedToken.Parent, cancellationToken);
var newSolution = await Renamer.RenameSymbolAsync(annotatedSolution, symbol, newName, null, cancellationToken).ConfigureAwait(false);
// TODO: return annotatedSolution instead of newSolution if newSolution contains any new errors (for any project)
return newSolution;
}
示例8: GetTransformedDocumentAsync
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, Diagnostic diagnostic)
{
var token = root.FindToken(diagnostic.Location.SourceSpan.Start, findInsideTrivia: true);
SyntaxToken updatedToken;
switch (token.Kind())
{
case SyntaxKind.XmlTextLiteralToken:
updatedToken = XmlSyntaxFactory.TextLiteral(" " + token.Text.TrimStart(' ')).WithTriviaFrom(token);
break;
default:
updatedToken = token.WithLeadingTrivia(token.LeadingTrivia.Add(SyntaxFactory.Space));
break;
}
Document updatedDocument = document.WithSyntaxRoot(root.ReplaceToken(token, updatedToken));
return Task.FromResult(updatedDocument);
}
示例9: InsertAttributeListIntoContainer
protected override SyntaxNode InsertAttributeListIntoContainer(int index, SyntaxNode list, SyntaxNode container)
{
// If the attribute list is being inserted at the first index and the container is not the compilation unit, copy leading trivia
// to the list that is being inserted.
if (index == 0 && !(container is CompilationUnitSyntax))
{
var firstToken = container.GetFirstToken();
if (firstToken.HasLeadingTrivia)
{
var trivia = firstToken.LeadingTrivia;
container = container.ReplaceToken(firstToken, firstToken.WithLeadingTrivia(SyntaxTriviaList.Empty));
list = list.WithLeadingTrivia(trivia);
}
}
if (container is CompilationUnitSyntax)
{
var compilationUnit = (CompilationUnitSyntax)container;
var newAttributeLists = compilationUnit.AttributeLists.Insert(index, (AttributeListSyntax)list);
return compilationUnit.WithAttributeLists(newAttributeLists);
}
else if (container is EnumDeclarationSyntax)
{
var enumDeclaration = (EnumDeclarationSyntax)container;
var newAttributeLists = enumDeclaration.AttributeLists.Insert(index, (AttributeListSyntax)list);
return enumDeclaration.WithAttributeLists(newAttributeLists);
}
else if (container is ClassDeclarationSyntax)
{
var classDeclaration = (ClassDeclarationSyntax)container;
var newAttributeLists = classDeclaration.AttributeLists.Insert(index, (AttributeListSyntax)list);
return classDeclaration.WithAttributeLists(newAttributeLists);
}
else if (container is StructDeclarationSyntax)
{
var structDeclaration = (StructDeclarationSyntax)container;
var newAttributeLists = structDeclaration.AttributeLists.Insert(index, (AttributeListSyntax)list);
return structDeclaration.WithAttributeLists(newAttributeLists);
}
else if (container is InterfaceDeclarationSyntax)
{
var interfaceDeclaration = (InterfaceDeclarationSyntax)container;
var newAttributeLists = interfaceDeclaration.AttributeLists.Insert(index, (AttributeListSyntax)list);
return interfaceDeclaration.WithAttributeLists(newAttributeLists);
}
else if (container is MethodDeclarationSyntax)
{
var method = (MethodDeclarationSyntax)container;
var newAttributeLists = method.AttributeLists.Insert(index, (AttributeListSyntax)list);
return method.WithAttributeLists(newAttributeLists);
}
else if (container is OperatorDeclarationSyntax)
{
var operationDeclaration = (OperatorDeclarationSyntax)container;
var newAttributeLists = operationDeclaration.AttributeLists.Insert(index, (AttributeListSyntax)list);
return operationDeclaration.WithAttributeLists(newAttributeLists);
}
else if (container is ConversionOperatorDeclarationSyntax)
{
var conversion = (ConversionOperatorDeclarationSyntax)container;
var newAttributeLists = conversion.AttributeLists.Insert(index, (AttributeListSyntax)list);
return conversion.WithAttributeLists(newAttributeLists);
}
else if (container is ConstructorDeclarationSyntax)
{
var constructor = (ConstructorDeclarationSyntax)container;
var newAttributeLists = constructor.AttributeLists.Insert(index, (AttributeListSyntax)list);
return constructor.WithAttributeLists(newAttributeLists);
}
else if (container is DestructorDeclarationSyntax)
{
var destructor = (DestructorDeclarationSyntax)container;
var newAttributeLists = destructor.AttributeLists.Insert(index, (AttributeListSyntax)list);
return destructor.WithAttributeLists(newAttributeLists);
}
else if (container is PropertyDeclarationSyntax)
{
var property = (PropertyDeclarationSyntax)container;
var newAttributeLists = property.AttributeLists.Insert(index, (AttributeListSyntax)list);
return property.WithAttributeLists(newAttributeLists);
}
else if (container is EventDeclarationSyntax)
{
var eventDeclaration = (EventDeclarationSyntax)container;
var newAttributeLists = eventDeclaration.AttributeLists.Insert(index, (AttributeListSyntax)list);
return eventDeclaration.WithAttributeLists(newAttributeLists);
}
else if (container is IndexerDeclarationSyntax)
{
var indexer = (IndexerDeclarationSyntax)container;
var newAttributeLists = indexer.AttributeLists.Insert(index, (AttributeListSyntax)list);
return indexer.WithAttributeLists(newAttributeLists);
}
else if (container is FieldDeclarationSyntax)
{
var field = (FieldDeclarationSyntax)container;
var newAttributeLists = field.AttributeLists.Insert(index, (AttributeListSyntax)list);
return field.WithAttributeLists(newAttributeLists);
}
//.........这里部分代码省略.........
示例10: RemoveAnnotationIfExists
private static SyntaxNode RemoveAnnotationIfExists(SyntaxNode root, SyntaxAnnotation annotation)
{
var element = root.GetAnnotatedNodesAndTokens(annotation).FirstOrDefault();
if (element == default(SyntaxNodeOrToken))
{
return root;
}
if (element.IsNode)
{
var node = element.AsNode();
return root.ReplaceNode(
node,
node.WithoutAnnotations(annotation));
}
var token = element.AsToken();
return root.ReplaceToken(
token,
token.WithoutAnnotations(annotation));
}
示例11: MoveMember
private static SyntaxNode MoveMember(SyntaxNode syntaxRoot, MemberDeclarationSyntax member, MemberDeclarationSyntax targetMember, IndentationOptions indentationOptions)
{
var firstToken = syntaxRoot.GetFirstToken();
var fileHeader = GetFileHeader(firstToken.LeadingTrivia);
syntaxRoot = syntaxRoot.TrackNodes(member, targetMember, firstToken.Parent);
var memberToMove = syntaxRoot.GetCurrentNode(member);
var targetMemberTracked = syntaxRoot.GetCurrentNode(targetMember);
if (!memberToMove.HasLeadingTrivia)
{
var targetIndentationLevel = IndentationHelper.GetIndentationSteps(indentationOptions, targetMember);
var indentationString = IndentationHelper.GenerateIndentationString(indentationOptions, targetIndentationLevel);
memberToMove = memberToMove.WithLeadingTrivia(SyntaxFactory.Whitespace(indentationString));
}
if (!HasLeadingBlankLines(targetMember)
&& HasLeadingBlankLines(member))
{
memberToMove = memberToMove.WithTrailingTrivia(memberToMove.GetTrailingTrivia().Add(SyntaxFactory.CarriageReturnLineFeed));
memberToMove = memberToMove.WithLeadingTrivia(GetLeadingTriviaWithoutLeadingBlankLines(memberToMove));
}
syntaxRoot = syntaxRoot.InsertNodesBefore(targetMemberTracked, new[] { memberToMove });
var fieldToMoveTracked = syntaxRoot.GetCurrentNodes(member).Last();
syntaxRoot = syntaxRoot.RemoveNode(fieldToMoveTracked, SyntaxRemoveOptions.KeepNoTrivia);
if (fileHeader.Any())
{
var oldFirstToken = syntaxRoot.GetCurrentNode(firstToken.Parent).ChildTokens().First();
syntaxRoot = syntaxRoot.ReplaceToken(oldFirstToken, oldFirstToken.WithLeadingTrivia(StripFileHeader(oldFirstToken.LeadingTrivia)));
var newFirstToken = syntaxRoot.GetFirstToken();
syntaxRoot = syntaxRoot.ReplaceToken(newFirstToken, newFirstToken.WithLeadingTrivia(fileHeader.AddRange(newFirstToken.LeadingTrivia)));
}
return syntaxRoot;
}
示例12: ReAddFileHeader
private static SyntaxNode ReAddFileHeader(SyntaxNode syntaxRoot, ImmutableArray<SyntaxTrivia> fileHeader)
{
if (fileHeader.IsEmpty)
{
// Only re-add the file header if it was stripped.
return syntaxRoot;
}
var firstToken = syntaxRoot.GetFirstToken(includeZeroWidth: true);
var newLeadingTrivia = firstToken.LeadingTrivia.InsertRange(0, fileHeader);
return syntaxRoot.ReplaceToken(firstToken, firstToken.WithLeadingTrivia(newLeadingTrivia));
}
示例13: ReAddFileHeader
private static SyntaxNode ReAddFileHeader(SyntaxNode syntaxRoot, SyntaxNode newSyntaxRoot)
{
var oldFirstToken = syntaxRoot.GetFirstToken();
if (!oldFirstToken.HasLeadingTrivia)
{
return newSyntaxRoot;
}
var fileHeader = UsingsHelper.GetFileHeader(oldFirstToken.LeadingTrivia.ToList());
if (!fileHeader.Any())
{
return newSyntaxRoot;
}
var newFirstToken = newSyntaxRoot.GetFirstToken();
var newLeadingTrivia = newFirstToken.LeadingTrivia.InsertRange(0, fileHeader);
return newSyntaxRoot.ReplaceToken(newFirstToken, newFirstToken.WithLeadingTrivia(newLeadingTrivia));
}
示例14: GetTransformedDocumentAsync
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, SyntaxToken token)
{
bool isAddingSpace = true;
switch (token.Kind())
{
case SyntaxKind.NewKeyword:
{
SyntaxToken nextToken = token.GetNextToken();
if (nextToken.IsKind(SyntaxKind.OpenBracketToken) || nextToken.IsKind(SyntaxKind.OpenParenToken))
{
isAddingSpace = false;
}
}
break;
case SyntaxKind.ReturnKeyword:
case SyntaxKind.ThrowKeyword:
{
SyntaxToken nextToken = token.GetNextToken();
if (nextToken.IsKind(SyntaxKind.SemicolonToken))
{
isAddingSpace = false;
}
}
break;
case SyntaxKind.CheckedKeyword:
case SyntaxKind.UncheckedKeyword:
isAddingSpace = token.GetNextToken().IsKind(SyntaxKind.OpenBraceToken);
break;
case SyntaxKind.DefaultKeyword:
case SyntaxKind.NameOfKeyword:
case SyntaxKind.SizeOfKeyword:
case SyntaxKind.TypeOfKeyword:
isAddingSpace = false;
break;
case SyntaxKind.IdentifierToken:
if (token.Text == "nameof")
{
// SA1000 would only have been reported for a nameof expression. No need to verify.
goto case SyntaxKind.NameOfKeyword;
}
return Task.FromResult(document);
default:
break;
}
if (isAddingSpace == token.HasTrailingTrivia)
{
return Task.FromResult(document);
}
if (isAddingSpace)
{
SyntaxTrivia whitespace = SyntaxFactory.Space;
SyntaxToken corrected = token.WithTrailingTrivia(token.TrailingTrivia.Insert(0, whitespace));
Document updatedDocument = document.WithSyntaxRoot(root.ReplaceToken(token, corrected));
return Task.FromResult(updatedDocument);
}
else
{
SyntaxToken corrected = token.WithoutTrailingWhitespace().WithoutFormatting();
Document updatedDocument = document.WithSyntaxRoot(root.ReplaceToken(token, corrected));
return Task.FromResult(updatedDocument);
}
}