当前位置: 首页>>代码示例>>C#>>正文


C# SyntaxNode.CSharpKind方法代码示例

本文整理汇总了C#中SyntaxNode.CSharpKind方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.CSharpKind方法的具体用法?C# SyntaxNode.CSharpKind怎么用?C# SyntaxNode.CSharpKind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SyntaxNode的用法示例。


在下文中一共展示了SyntaxNode.CSharpKind方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: PathSyntaxReference

 public PathSyntaxReference(SyntaxNode node)
 {
     this.tree = node.SyntaxTree;
     this.kind = node.CSharpKind();
     this.textSpan = node.Span;
     this.pathFromRoot = ComputePathFromRoot(node);
 }
开发者ID:EkardNT,项目名称:Roslyn,代码行数:7,代码来源:CSharpSyntaxTreeFactory.PathSyntaxReference.cs

示例2: GetUpdatedDocumentAsync

        internal override Task<Document> GetUpdatedDocumentAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, string diagnosticId, CancellationToken cancellationToken)
        {
            // if nothing can be fixed, return the unchanged node
            var newRoot = root;
            var kind = nodeToFix.CSharpKind();
            var syntaxFactoryService = document.GetLanguageService<SyntaxGenerator>();
            switch (kind)
            {
                case SyntaxKind.Argument:
                    // StringComparison.CurrentCulture => StringComparison.Ordinal
                    // StringComparison.CurrentCultureIgnoreCase => StringComparison.OrdinalIgnoreCase
                    var argument = (ArgumentSyntax)nodeToFix;
                    var memberAccess = argument.Expression as MemberAccessExpressionSyntax;
                    if (memberAccess != null)
                    {
                        // preserve the "IgnoreCase" suffix if present
                        bool isIgnoreCase = memberAccess.Name.GetText().ToString().EndsWith(CA1309DiagnosticAnalyzer.IgnoreCaseText);
                        var newOrdinalText = isIgnoreCase ? CA1309DiagnosticAnalyzer.OrdinalIgnoreCaseText : CA1309DiagnosticAnalyzer.OrdinalText;
                        var newIdentifier = syntaxFactoryService.IdentifierName(newOrdinalText);
                        var newMemberAccess = memberAccess.WithName((SimpleNameSyntax)newIdentifier).WithAdditionalAnnotations(Formatter.Annotation);
                        newRoot = root.ReplaceNode(memberAccess, newMemberAccess);
                    }

                    break;
                case SyntaxKind.IdentifierName:
                    // string.Equals(a, b) => string.Equals(a, b, StringComparison.Ordinal)
                    // string.Compare(a, b) => string.Compare(a, b, StringComparison.Ordinal)
                    var identifier = (IdentifierNameSyntax)nodeToFix;
                    var invokeParent = identifier.GetAncestor<InvocationExpressionSyntax>();
                    if (invokeParent != null)
                    {
                        var methodSymbol = model.GetSymbolInfo(identifier).Symbol as IMethodSymbol;
                        if (methodSymbol != null && CanAddStringComparison(methodSymbol))
                        {
                            // append a new StringComparison.Ordinal argument
                            var newArg = syntaxFactoryService.Argument(CreateOrdinalMemberAccess(syntaxFactoryService, model))
                                .WithAdditionalAnnotations(Formatter.Annotation);
                            var newInvoke = invokeParent.AddArgumentListArguments((ArgumentSyntax)newArg).WithAdditionalAnnotations(Formatter.Annotation);
                            newRoot = root.ReplaceNode(invokeParent, newInvoke);
                        }
                    }

                    break;
                case SyntaxKind.EqualsExpression:
                case SyntaxKind.NotEqualsExpression:
                    // "a == b" => "string.Equals(a, b, StringComparison.Ordinal)"
                    // "a != b" => "!string.Equals(a, b, StringComparison.Ordinal)"
                    var binaryExpression = (BinaryExpressionSyntax)nodeToFix;
                    var invocation = CreateEqualsExpression(syntaxFactoryService, model, binaryExpression.Left, binaryExpression.Right, kind == SyntaxKind.EqualsExpression).WithAdditionalAnnotations(Formatter.Annotation);
                    newRoot = root.ReplaceNode(nodeToFix, invocation);
                    break;
            }

            if (newRoot == root)
            {
                return Task.FromResult(document);
            }

            return Task.FromResult(document.WithSyntaxRoot(newRoot));
        }
开发者ID:modulexcite,项目名称:pattern-matching-csharp,代码行数:60,代码来源:CA1309CSharpCodeFixProvider.cs

示例3: PositionalSyntaxReference

                public PositionalSyntaxReference(SyntaxNode node)
                {
                    this.tree = node.SyntaxTree;
                    this.textSpan = node.Span;
                    this.kind = node.CSharpKind();

                    System.Diagnostics.Debug.Assert(textSpan.Length > 0);
                }
开发者ID:modulexcite,项目名称:pattern-matching-csharp,代码行数:8,代码来源:CSharpSyntaxTreeFactoryService.PositionalSyntaxReference.cs

示例4: GetUpdatedDocumentAsync

        internal override Task<Document> GetUpdatedDocumentAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, string diagnosticId, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var charSetType = WellKnownTypes.CharSet(model.Compilation);
            var dllImportType = WellKnownTypes.DllImportAttribute(model.Compilation);
            var marshalAsType = WellKnownTypes.MarshalAsAttribute(model.Compilation);
            var unmanagedType = WellKnownTypes.UnmanagedType(model.Compilation);
            if (charSetType == null || dllImportType == null || marshalAsType == null || unmanagedType == null)
            {
                return Task.FromResult(document);
            }

            var syntaxFactoryService = document.Project.LanguageServices.GetService<SyntaxGenerator>();

            // return the unchanged root if no fix is available
            var newRoot = root;
            if (nodeToFix.CSharpKind() == SyntaxKind.Attribute)
            {
                // could be either a [DllImport] or [MarshalAs] attribute
                var attribute = (AttributeSyntax)nodeToFix;
                var attributeType = model.GetSymbolInfo(attribute).Symbol;
                var arguments = attribute.ArgumentList.Arguments;
                if (dllImportType.Equals(attributeType.ContainingType))
                {
                    // [DllImport] attribute, add or replace CharSet named parameter
                    var argumentValue = CreateCharSetArgument(syntaxFactoryService, charSetType).WithAdditionalAnnotations(Formatter.Annotation);
                    var namedParameter = arguments.FirstOrDefault(arg => arg.NameEquals != null && arg.NameEquals.Name.Identifier.Text == CharSetText);
                    if (namedParameter == null)
                    {
                        // add the parameter
                        namedParameter = SyntaxFactory.AttributeArgument(SyntaxFactory.NameEquals(CharSetText), null, (ExpressionSyntax)argumentValue)
                            .WithAdditionalAnnotations(Formatter.Annotation);
                        var newArguments = arguments.Add(namedParameter);
                        var newArgumentList = attribute.ArgumentList.WithArguments(newArguments);
                        newRoot = root.ReplaceNode(attribute.ArgumentList, newArgumentList);
                    }
                    else
                    {
                        // replace the parameter
                        var newNamedParameter = namedParameter.WithExpression((ExpressionSyntax)argumentValue);
                        newRoot = root.ReplaceNode(namedParameter, newNamedParameter);
                    }
                }
                else if (marshalAsType.Equals(attributeType.ContainingType) && arguments.Count == 1)
                {
                    // [MarshalAs] attribute, replace the only argument
                    var newExpression = CreateMarshalAsArgument(syntaxFactoryService, unmanagedType)
                        .WithLeadingTrivia(arguments[0].GetLeadingTrivia())
                        .WithTrailingTrivia(arguments[0].GetTrailingTrivia());
                    var newArgument = arguments[0].WithExpression((ExpressionSyntax)newExpression);
                    newRoot = root.ReplaceNode(arguments[0], newArgument);
                }
            }

            return Task.FromResult(document.WithSyntaxRoot(newRoot));
        }
开发者ID:modulexcite,项目名称:pattern-matching-csharp,代码行数:57,代码来源:CA2101CSharpCodeFixProvider.cs

示例5: GetLocalName

 internal static string GetLocalName(SyntaxNode node)
 {
     switch (node.CSharpKind())
     {
         case SyntaxKind.VariableDeclarator:
             return ((VariableDeclaratorSyntax)node).Identifier.ToString();
         default:
             throw new NotImplementedException();
     }
 }
开发者ID:modulexcite,项目名称:pattern-matching-csharp,代码行数:10,代码来源:EditAndContinueTestBase.cs

示例6: IsAttributeName

        public static bool IsAttributeName(SyntaxNode node)
        {
            var parent = node.Parent;
            if (parent == null || !IsName(node.CSharpKind()))
            {
                return false;
            }

            switch (parent.CSharpKind())
            {
                case SyntaxKind.QualifiedName:
                var qn = (QualifiedNameSyntax)parent;
                return qn.Right == node ? IsAttributeName(parent) : false;

                case SyntaxKind.AliasQualifiedName:
                var an = (AliasQualifiedNameSyntax)parent;
                return an.Name == node ? IsAttributeName(parent) : false;
            }

            var p = node.Parent as AttributeSyntax;
            return p != null && p.Name == node;
        }
开发者ID:modulexcite,项目名称:pattern-matching-csharp,代码行数:22,代码来源:SyntaxNodeFacts.cs

示例7: IsIndexerMemberCRef

 public bool IsIndexerMemberCRef(SyntaxNode node)
 {
     return node.CSharpKind() == SyntaxKind.IndexerMemberCref;
 }
开发者ID:EkardNT,项目名称:Roslyn,代码行数:4,代码来源:CSharpSyntaxFactsService.cs

示例8: IsElementAccessExpression

 public bool IsElementAccessExpression(SyntaxNode node)
 {
     return node.CSharpKind() == SyntaxKind.ElementAccessExpression;
 }
开发者ID:EkardNT,项目名称:Roslyn,代码行数:4,代码来源:CSharpSyntaxFactsService.cs

示例9: DefaultVisit

            public override void DefaultVisit(SyntaxNode node)
            {
                SyntaxKind nodeKind = node.CSharpKind();
                bool diagnose = ((SyntaxTree)node.SyntaxTree).ReportDocumentationCommentDiagnostics();

                if (nodeKind == SyntaxKind.XmlCrefAttribute)
                {
                    XmlCrefAttributeSyntax crefAttr = (XmlCrefAttributeSyntax)node;
                    CrefSyntax cref = crefAttr.Cref;

                    BinderFactory factory = compilation.GetBinderFactory(cref.SyntaxTree);
                    Binder binder = factory.GetBinder(cref);

                    // Do this for the diagnostics, even if it won't be written.
                    DiagnosticBag crefDiagnostics = DiagnosticBag.GetInstance();
                    string docCommentId = GetDocumentationCommentId(cref, binder, crefDiagnostics);
                    if (diagnose)
                    {
                        diagnostics.AddRange(crefDiagnostics);
                    }
                    crefDiagnostics.Free();

                    if ((object)writer != null)
                    {
                        Visit(crefAttr.Name);
                        VisitToken(crefAttr.EqualsToken);

                        // Not going to visit normally, because we want to skip trivia within
                        // the attribute value.
                        crefAttr.StartQuoteToken.WriteTo(writer, leading: true, trailing: false);

                        // We're not going to visit the cref because we want to bind it
                        // and write a doc comment ID in its place.
                        writer.Write(docCommentId);

                        // Not going to visit normally, because we want to skip trivia within
                        // the attribute value.
                        crefAttr.EndQuoteToken.WriteTo(writer, leading: false, trailing: true);
                    }

                    // Don't descend - we've already written out everything necessary.
                    return;
                }
                else if (nodeKind == SyntaxKind.XmlNameAttribute && diagnose)
                {
                    XmlNameAttributeSyntax nameAttr = (XmlNameAttributeSyntax)node;

                    BinderFactory factory = compilation.GetBinderFactory(nameAttr.SyntaxTree);
                    Binder binder = factory.GetBinder(nameAttr, nameAttr.Identifier.SpanStart);

                    // Do this for diagnostics, even if we aren't writing.
                    DocumentationCommentCompiler.BindName(nameAttr, binder, memberSymbol, ref documentedParameters, ref documentedTypeParameters, diagnostics);

                    // Do descend - we still need to write out the tokens of the attribute.
                }

                // NOTE: if we're recording any include element nodes (i.e. if includeElementsNodes is non-null),
                // then we want to record all of them, because we won't be able to distinguish in the XML DOM.
                if ((object)includeElementNodes != null)
                {
                    XmlNameSyntax nameSyntax = null;
                    if (nodeKind == SyntaxKind.XmlEmptyElement)
                    {
                        nameSyntax = ((XmlEmptyElementSyntax)node).Name;
                    }
                    else if (nodeKind == SyntaxKind.XmlElementStartTag)
                    {
                        nameSyntax = ((XmlElementStartTagSyntax)node).Name;
                    }

                    if ((object)nameSyntax != null && (object)nameSyntax.Prefix == null &&
                        DocumentationCommentXmlNames.ElementEquals(nameSyntax.LocalName.ValueText, DocumentationCommentXmlNames.IncludeElementName))
                    {
                        includeElementNodes.Add((CSharpSyntaxNode)node);
                    }
                }

                base.DefaultVisit(node);
            }
开发者ID:EkardNT,项目名称:Roslyn,代码行数:79,代码来源:DocumentationCommentCompiler.DocumentationCommentWalker.cs

示例10: TryGetSpeculativeSemanticModel

        public bool TryGetSpeculativeSemanticModel(SemanticModel oldSemanticModel, SyntaxNode oldNode, SyntaxNode newNode, out SemanticModel speculativeModel)
        {
            Contract.Requires(oldNode.CSharpKind() == newNode.CSharpKind());

            var model = oldSemanticModel;

            // currently we only support method. field support will be added later.
            var oldMethod = oldNode as BaseMethodDeclarationSyntax;
            var newMethod = newNode as BaseMethodDeclarationSyntax;
            if (oldMethod == null || newMethod == null || oldMethod.Body == null)
            {
                speculativeModel = null;
                return false;
            }

            SemanticModel csharpModel;
            bool success = model.TryGetSpeculativeSemanticModelForMethodBody(oldMethod.Body.OpenBraceToken.Span.End, newMethod, out csharpModel);
            speculativeModel = csharpModel;
            return success;
        }
开发者ID:nagyist,项目名称:roslyn,代码行数:20,代码来源:CSharpSemanticFactsService.cs


注:本文中的SyntaxNode.CSharpKind方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。