本文整理汇总了C#中SyntaxAnnotation类的典型用法代码示例。如果您正苦于以下问题:C# SyntaxAnnotation类的具体用法?C# SyntaxAnnotation怎么用?C# SyntaxAnnotation使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SyntaxAnnotation类属于命名空间,在下文中一共展示了SyntaxAnnotation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateAsync
public static async Task<InsertionPoint> CreateAsync(SemanticDocument document, SyntaxNode node, CancellationToken cancellationToken)
{
var root = document.Root;
var annotation = new SyntaxAnnotation();
var newRoot = root.AddAnnotations(SpecializedCollections.SingletonEnumerable(Tuple.Create(node, annotation)));
return new InsertionPoint(await document.WithSyntaxRootAsync(newRoot, cancellationToken).ConfigureAwait(false), annotation);
}
示例2: AnnotationResolver
private SyntaxToken AnnotationResolver(
SyntaxNode node,
TriviaLocation location,
SyntaxAnnotation annotation,
SyntaxNode callsite,
MethodDeclarationSyntax method)
{
var token = node.GetAnnotatedNodesAndTokens(annotation).FirstOrDefault().AsToken();
if (token.RawKind != 0)
{
return token;
}
switch (location)
{
case TriviaLocation.BeforeBeginningOfSpan:
return callsite.GetFirstToken(includeZeroWidth: true).GetPreviousToken(includeZeroWidth: true);
case TriviaLocation.AfterEndOfSpan:
return callsite.GetLastToken(includeZeroWidth: true).GetNextToken(includeZeroWidth: true);
case TriviaLocation.AfterBeginningOfSpan:
return method.Body.OpenBraceToken.GetNextToken(includeZeroWidth: true);
case TriviaLocation.BeforeEndOfSpan:
return method.Body.CloseBraceToken.GetPreviousToken(includeZeroWidth: true);
}
return Contract.FailWithReturn<SyntaxToken>("can't happen");
}
示例3: GenerateTypeSyntax
public static TypeSyntax GenerateTypeSyntax(this ITypeSymbol typeSymbol, SyntaxAnnotation simplifierAnnotation = null)
{
var typeSyntax = (TypeSyntax)generateTypeSyntaxMethod.Invoke(null, new object[] { typeSymbol });
if (simplifierAnnotation != null)
return typeSyntax.WithAdditionalAnnotations(simplifierAnnotation);
return typeSyntax;
}
示例4: CanRemoveTypeFromParameter
private static bool CanRemoveTypeFromParameter(
SyntaxNode node,
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
// We reduce any the parameters that are contained inside ParameterList
if (node != null && node.IsParentKind(SyntaxKind.ParameterList) && node.Parent.IsParentKind(SyntaxKind.ParenthesizedLambdaExpression))
{
var parameterSyntax = (ParameterSyntax)node;
if (parameterSyntax.Type != null)
{
var annotation = new SyntaxAnnotation();
var newParameterSyntax = parameterSyntax.WithType(null).WithAdditionalAnnotations(annotation);
var oldLambda = node.FirstAncestorOrSelf<ParenthesizedLambdaExpressionSyntax>();
var newLambda = oldLambda.ReplaceNode(parameterSyntax, newParameterSyntax);
var speculationAnalyzer = new SpeculationAnalyzer(oldLambda, newLambda, semanticModel, cancellationToken);
newParameterSyntax = (ParameterSyntax)speculationAnalyzer.ReplacedExpression.GetAnnotatedNodesAndTokens(annotation).First();
var oldSymbol = semanticModel.GetDeclaredSymbol(parameterSyntax, cancellationToken);
var newSymbol = speculationAnalyzer.SpeculativeSemanticModel.GetDeclaredSymbol(newParameterSyntax, cancellationToken);
if (oldSymbol != null &&
newSymbol != null &&
oldSymbol.Type == newSymbol.Type)
{
return !speculationAnalyzer.ReplacementChangesSemantics();
}
}
}
return false;
}
示例5: SimplifyASTForMethod
/// <summary>
/// This is jus a test to check the ability to preprocess the AST
/// </summary>
/// <param name="methodNode"></param>
/// <param name="semanticModel"></param>
/// <returns></returns>
public static IMethodSymbol SimplifyASTForMethod(ref SyntaxNode methodNode, ref SemanticModel semanticModel)
{
var oldMethod = semanticModel.GetDeclaredSymbol(methodNode) as IMethodSymbol;
var newMethodNode = MethodSimpifier.Test(methodNode);
var annotation = new SyntaxAnnotation("Hi");
newMethodNode = newMethodNode.WithAdditionalAnnotations(annotation);
var root = methodNode.SyntaxTree.GetRoot();
var newRoot = root.ReplaceNode(methodNode, newMethodNode);
var oldCompilation = semanticModel.Compilation;
var newCompilation = oldCompilation.ReplaceSyntaxTree(root.SyntaxTree, newRoot.SyntaxTree);
var newSemanticModel = newCompilation.GetSemanticModel(newRoot.SyntaxTree);
var recoveredMethodNode = newRoot.GetAnnotatedNodes(annotation).Single();
var method = newSemanticModel.GetDeclaredSymbol(recoveredMethodNode) as IMethodSymbol;
methodNode = recoveredMethodNode;
semanticModel = newSemanticModel;
return method;
}
示例6: TryGetSimplifiedTypeNameInCaseContext
protected override bool TryGetSimplifiedTypeNameInCaseContext(Document document, string fullyQualifiedTypeName, string firstEnumMemberName, int startPosition, int endPosition, CancellationToken cancellationToken, out string simplifiedTypeName)
{
simplifiedTypeName = string.Empty;
var typeAnnotation = new SyntaxAnnotation();
var str = "case " + fullyQualifiedTypeName + "." + firstEnumMemberName + ":" + Environment.NewLine + " break;";
var textChange = new TextChange(new TextSpan(startPosition, endPosition - startPosition), str);
var typeSpanToAnnotate = new TextSpan(startPosition + "case ".Length, fullyQualifiedTypeName.Length);
var textWithCaseAdded = document.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken).WithChanges(textChange);
var documentWithCaseAdded = document.WithText(textWithCaseAdded);
var syntaxRoot = documentWithCaseAdded.GetSyntaxRootSynchronously(cancellationToken);
var nodeToReplace = syntaxRoot.DescendantNodes().FirstOrDefault(n => n.Span == typeSpanToAnnotate);
if (nodeToReplace == null)
{
return false;
}
var updatedRoot = syntaxRoot.ReplaceNode(nodeToReplace, nodeToReplace.WithAdditionalAnnotations(typeAnnotation, Simplifier.Annotation));
var documentWithAnnotations = documentWithCaseAdded.WithSyntaxRoot(updatedRoot);
var simplifiedDocument = Simplifier.ReduceAsync(documentWithAnnotations, cancellationToken: cancellationToken).Result;
simplifiedTypeName = simplifiedDocument.GetSyntaxRootSynchronously(cancellationToken).GetAnnotatedNodesAndTokens(typeAnnotation).Single().ToString();
return true;
}
示例7: ComputeRefactoringsAsync
public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
var document = context.Document;
if (document.Project.Solution.Workspace.Kind == WorkspaceKind.MiscellaneousFiles)
return;
var span = context.Span;
if (!span.IsEmpty)
return;
var cancellationToken = context.CancellationToken;
if (cancellationToken.IsCancellationRequested)
return;
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
if (model.IsFromGeneratedCode(cancellationToken))
return;
var root = await model.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);
SyntaxToken token = root.FindToken(span.Start);
if (!token.IsKind(SyntaxKind.IdentifierToken))
return;
var property = token.Parent as PropertyDeclarationSyntax;
if (property == null || !property.Identifier.Span.Contains(span))
return;
if (IsEmptyComputedProperty(property))
{
context.RegisterRefactoring(
CodeActionFactory.Create(
token.Span,
DiagnosticSeverity.Info,
GettextCatalog.GetString("Convert to auto-property"),
t2 =>
{
var newRoot = root.ReplaceNode(property, CreateNewProperty(property).WithAdditionalAnnotations(Formatter.Annotation).WithLeadingTrivia(property.GetLeadingTrivia()));
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}
)
);
return;
}
var field = GetBackingField(model, property);
if (!IsValidField(field, property.Parent as TypeDeclarationSyntax))
return;
//variable declarator->declaration->field declaration
var backingFieldNode = root.FindNode(field.Locations.First().SourceSpan).Ancestors().OfType<FieldDeclarationSyntax>().First();
var propertyAnnotation = new SyntaxAnnotation();
var fieldAnnotation = new SyntaxAnnotation();
//annotate our property node and our field node
root = root.ReplaceNode((SyntaxNode)property, property.WithAdditionalAnnotations(propertyAnnotation));
root = root.ReplaceNode((SyntaxNode)root.FindNode(backingFieldNode.Span), backingFieldNode.WithAdditionalAnnotations(fieldAnnotation));
context.RegisterRefactoring(
CodeActionFactory.Create(token.Span, DiagnosticSeverity.Info, GettextCatalog.GetString("Convert to auto-property"),
PerformAction(document, model, root, field.Name, CreateNewProperty(property), propertyAnnotation, fieldAnnotation))
);
}
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:58,代码来源:ReplacePropertyWithBackingFieldWithAutoPropertyCodeRefactoringProvider.cs
示例8: WithTwoChildren
internal WithTwoChildren(DiagnosticInfo[] diagnostics, SyntaxAnnotation[] annotations, GreenNode child0, GreenNode child1)
: base(diagnostics, annotations)
{
this.SlotCount = 2;
this.AdjustFlagsAndWidth(child0);
_child0 = child0;
this.AdjustFlagsAndWidth(child1);
_child1 = child1;
}
示例9: RemoveInitializer
public static SyntaxNode RemoveInitializer(SyntaxNode root, ConstructorDeclarationSyntax constructor)
{
var annotation = new SyntaxAnnotation();
var ctor = constructor;
var newRoot = root;
newRoot = newRoot.ReplaceNode(ctor, ctor.WithAdditionalAnnotations(annotation));
ctor = (ConstructorDeclarationSyntax)newRoot.GetAnnotatedNodes(annotation).First();
var initializer = ctor.Initializer;
if (RedundantInheritanceListCodeFixProvider.HasLineEnding(constructor.ParameterList))
{
newRoot = newRoot.RemoveNode(initializer, SyntaxRemoveOptions.KeepNoTrivia);
ctor = (ConstructorDeclarationSyntax)newRoot.GetAnnotatedNodes(annotation).First();
if (ctor.Body != null &&
ctor.Body.HasLeadingTrivia)
{
var lastTrivia = ctor.Body.GetLeadingTrivia().Last();
newRoot = lastTrivia.IsKind(SyntaxKind.EndOfLineTrivia)
? newRoot.ReplaceNode(
ctor.Body,
ctor.Body.WithoutLeadingTrivia())
: newRoot.ReplaceNode(
ctor.Body,
ctor.Body.WithLeadingTrivia(lastTrivia));
}
}
else
{
var trailingTrivia = SyntaxFactory.TriviaList();
if (initializer.HasTrailingTrivia)
{
trailingTrivia = initializer.GetTrailingTrivia();
}
newRoot = newRoot.RemoveNode(initializer, SyntaxRemoveOptions.KeepNoTrivia);
ctor = (ConstructorDeclarationSyntax)newRoot.GetAnnotatedNodes(annotation).First();
if (ctor.Body != null &&
ctor.Body.HasLeadingTrivia)
{
var lastTrivia = ctor.Body.GetLeadingTrivia().Last();
newRoot = newRoot.ReplaceNode(
ctor.Body,
ctor.Body.WithLeadingTrivia(trailingTrivia.Add(lastTrivia)));
}
else
{
if (initializer.HasTrailingTrivia)
{
newRoot = newRoot.ReplaceNode(ctor, ctor.WithTrailingTrivia(trailingTrivia));
}
}
}
ctor = (ConstructorDeclarationSyntax)newRoot.GetAnnotatedNodes(annotation).First();
return newRoot.ReplaceNode(ctor, ctor.WithoutAnnotations(annotation));
}
示例10: GetSolutionWithUpdatedOriginalType
internal abstract Solution GetSolutionWithUpdatedOriginalType(
Solution solutionWithFormattedInterfaceDocument,
INamedTypeSymbol extractedInterfaceSymbol,
IEnumerable<ISymbol> includedMembers,
Dictionary<ISymbol, SyntaxAnnotation> symbolToDeclarationAnnotationMap,
List<DocumentId> documentIds,
SyntaxAnnotation typeNodeAnnotation,
DocumentId documentIdWithTypeNode,
CancellationToken cancellationToken);
示例11: InsertionPoint
private InsertionPoint(SemanticDocument document, SyntaxAnnotation annotation)
{
//Contract.ThrowIfNull(document);
//Contract.ThrowIfNull(annotation);
this.SemanticDocument = document;
_annotation = annotation;
_context = CreateLazyContextNode();
}
示例12: SyntaxIdentifierWithTrailingTrivia
internal SyntaxIdentifierWithTrailingTrivia(string text, GreenNode trailing, DiagnosticInfo[] diagnostics, SyntaxAnnotation[] annotations)
: base(text, diagnostics, annotations)
{
if (trailing != null)
{
this.AdjustFlagsAndWidth(trailing);
_trailing = trailing;
}
}
示例13: CreateExtractMethodResult
private ExtractMethodResult CreateExtractMethodResult(
OperationStatus status, SemanticDocument semanticDocument,
SyntaxAnnotation invocationAnnotation, SyntaxAnnotation methodAnnotation)
{
var newRoot = semanticDocument.Root;
var annotatedTokens = newRoot.GetAnnotatedNodesAndTokens(invocationAnnotation);
var methodDefinition = newRoot.GetAnnotatedNodesAndTokens(methodAnnotation).FirstOrDefault().AsNode();
return new SimpleExtractMethodResult(status, semanticDocument.Document, GetMethodNameAtInvocation(annotatedTokens), methodDefinition);
}
示例14: CodeCleaners_Annotation
public void CodeCleaners_Annotation()
{
var document = CreateDocument("class C { }", LanguageNames.CSharp);
var annotation = new SyntaxAnnotation();
document = document.WithSyntaxRoot(document.GetSyntaxRootAsync().Result.WithAdditionalAnnotations(annotation));
var cleanDocument = CodeCleaner.CleanupAsync(document, annotation).Result;
Assert.Equal(document, cleanDocument);
}
示例15: VisitPropertyDeclaration
public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax property)
{
if (span.IntersectsWith(property.Span) && CodeGeneration.IsExpandableProperty(property, document))
{
var annotation = new SyntaxAnnotation();
annotations.Add(annotation);
return property.WithAdditionalAnnotations(annotation);
}
return base.VisitPropertyDeclaration(property);
}