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


C# SyntaxNode.FirstAncestorOrSelf方法代码示例

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


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

示例1: GetParameterNamesFromCreationContext

 private static IList<string> GetParameterNamesFromCreationContext(SyntaxNode node)
 {
     var creationContext =
         node.FirstAncestorOrSelf<SimpleLambdaExpressionSyntax>() ??
         node.FirstAncestorOrSelf<ParenthesizedLambdaExpressionSyntax>() ??
         node.FirstAncestorOrSelf<AccessorDeclarationSyntax>() ??
         (SyntaxNode)node.FirstAncestorOrSelf<BaseMethodDeclarationSyntax>();
     return GetParameterNames(creationContext);
 }
开发者ID:JeanLLopes,项目名称:code-cracker,代码行数:9,代码来源:ArgumentExceptionAnalyzer.cs

示例2: GetTypeDeclaration

        private static SyntaxNode GetTypeDeclaration(SyntaxNode node)
        {
            var type = node.FirstAncestorOrSelf<ClassDeclarationSyntax>();

            if (type == null)
            {
                type = node.SyntaxTree.GetRoot()
                        .DescendantNodes().OfType<ClassDeclarationSyntax>()
                        .FirstOrDefault();
            }

            return type;
        }
开发者ID:tugberkugurlu,项目名称:omnisharp-roslyn,代码行数:13,代码来源:TestCommandController.cs

示例3: IsDisposedOrAssigned

 private static bool IsDisposedOrAssigned(SemanticModel semanticModel, SyntaxNode node, ILocalSymbol identitySymbol)
 {
     var method = node.FirstAncestorOrSelf<MethodDeclarationSyntax>();
     if (method == null) return false;
     if (IsReturned(method, semanticModel, identitySymbol)) return true;
     foreach (var statement in method.Body.DescendantNodes().OfType<StatementSyntax>())
     {
         if (statement.SpanStart > node.SpanStart
         && (IsCorrectDispose(statement as ExpressionStatementSyntax, semanticModel, identitySymbol)
         || IsAssignedToField(statement as ExpressionStatementSyntax, semanticModel, identitySymbol)))
             return true;
     }
     return false;
 }
开发者ID:nagyistoce,项目名称:code-cracker,代码行数:14,代码来源:DisposableVariableNotDisposedAnalyzer.cs

示例4: IsContainedInNativeMethodsClass

        internal static bool IsContainedInNativeMethodsClass(SyntaxNode syntax)
        {
            while (syntax != null)
            {
                ClassDeclarationSyntax classDeclarationSyntax = syntax.FirstAncestorOrSelf<ClassDeclarationSyntax>();
                if (IsNativeMethodsClass(classDeclarationSyntax))
                {
                    return true;
                }

                syntax = syntax?.Parent;
            }

            return false;
        }
开发者ID:nvincent,项目名称:StyleCopAnalyzers,代码行数:15,代码来源:NamedTypeHelpers.cs

示例5: CreateUnknownCodeElement

        public override EnvDTE.CodeElement CreateUnknownCodeElement(CodeModelState state, FileCodeModel fileCodeModel, SyntaxNode node)
        {
            switch (node.Kind())
            {
                case SyntaxKind.NamespaceDeclaration:
                    return (EnvDTE.CodeElement)CodeNamespace.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));

                case SyntaxKind.ClassDeclaration:
                    return (EnvDTE.CodeElement)CodeClass.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));
                case SyntaxKind.InterfaceDeclaration:
                    return (EnvDTE.CodeElement)CodeInterface.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));
                case SyntaxKind.StructDeclaration:
                    return (EnvDTE.CodeElement)CodeStruct.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));
                case SyntaxKind.EnumDeclaration:
                    return (EnvDTE.CodeElement)CodeEnum.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));
                case SyntaxKind.DelegateDeclaration:
                    return (EnvDTE.CodeElement)CodeDelegate.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));

                case SyntaxKind.MethodDeclaration:
                case SyntaxKind.ConstructorDeclaration:
                case SyntaxKind.DestructorDeclaration:
                case SyntaxKind.OperatorDeclaration:
                case SyntaxKind.ConversionOperatorDeclaration:
                    return (EnvDTE.CodeElement)CodeFunction.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));

                case SyntaxKind.PropertyDeclaration:
                case SyntaxKind.IndexerDeclaration:
                    return (EnvDTE.CodeElement)CodeProperty.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));

                case SyntaxKind.EventDeclaration:
                    return (EnvDTE.CodeElement)CodeEvent.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));

                case SyntaxKind.VariableDeclarator:
                    var eventFieldDeclaration = node.FirstAncestorOrSelf<EventFieldDeclarationSyntax>();
                    if (eventFieldDeclaration != null)
                    {
                        return (EnvDTE.CodeElement)CodeEvent.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));
                    }

                    goto case SyntaxKind.EnumMemberDeclaration;

                case SyntaxKind.EnumMemberDeclaration:
                    return (EnvDTE.CodeElement)CodeVariable.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));

                default:
                    throw Exceptions.ThrowEUnexpected();
            }
        }
开发者ID:SoumikMukherjeeDOTNET,项目名称:roslyn,代码行数:48,代码来源:CSharpCodeModelService.cs

示例6: CreateInternalCodeElement

        /// <summary>
        /// Do not use this method directly! Instead, go through <see cref="FileCodeModel.GetOrCreateCodeElement{T}(SyntaxNode)"/>
        /// </summary>
        public override EnvDTE.CodeElement CreateInternalCodeElement(
            CodeModelState state,
            FileCodeModel fileCodeModel,
            SyntaxNode node)
        {
            // Attributes, attribute arguments, parameters, imports directives, and
            // accessor functions do not have their own node keys. Rather, they are
            // based off their parents node keys and other data.
            switch (node.Kind())
            {
                case SyntaxKind.Attribute:
                    return (EnvDTE.CodeElement)CreateInternalCodeAttribute(state, fileCodeModel, node);

                case SyntaxKind.AttributeArgument:
                    return (EnvDTE.CodeElement)CreateInternalCodeAttributeArgument(state, fileCodeModel, (AttributeArgumentSyntax)node);

                case SyntaxKind.Parameter:
                    return (EnvDTE.CodeElement)CreateInternalCodeParameter(state, fileCodeModel, (ParameterSyntax)node);

                case SyntaxKind.UsingDirective:
                    return CreateInternalCodeImport(state, fileCodeModel, (UsingDirectiveSyntax)node);
            }

            if (IsAccessorNode(node))
            {
                return (EnvDTE.CodeElement)CreateInternalCodeAccessorFunction(state, fileCodeModel, node);
            }

            var nodeKey = GetNodeKey(node);

            switch (node.Kind())
            {
                case SyntaxKind.ClassDeclaration:
                    return (EnvDTE.CodeElement)CodeClass.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.InterfaceDeclaration:
                    return (EnvDTE.CodeElement)CodeInterface.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.StructDeclaration:
                    return (EnvDTE.CodeElement)CodeStruct.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.EnumDeclaration:
                    return (EnvDTE.CodeElement)CodeEnum.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.EnumMemberDeclaration:
                    return (EnvDTE.CodeElement)CodeVariable.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.DelegateDeclaration:
                    return (EnvDTE.CodeElement)CodeDelegate.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.MethodDeclaration:
                case SyntaxKind.ConstructorDeclaration:
                case SyntaxKind.DestructorDeclaration:
                case SyntaxKind.OperatorDeclaration:
                case SyntaxKind.ConversionOperatorDeclaration:
                    return (EnvDTE.CodeElement)CodeFunction.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.NamespaceDeclaration:
                    return (EnvDTE.CodeElement)CodeNamespace.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.PropertyDeclaration:
                case SyntaxKind.IndexerDeclaration:
                    return (EnvDTE.CodeElement)CodeProperty.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.EventDeclaration:
                    return (EnvDTE.CodeElement)CodeEvent.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.VariableDeclarator:
                    var baseFieldDeclaration = node.FirstAncestorOrSelf<BaseFieldDeclarationSyntax>();
                    if (baseFieldDeclaration != null)
                    {
                        if (baseFieldDeclaration.Kind() == SyntaxKind.FieldDeclaration)
                        {
                            return (EnvDTE.CodeElement)CodeVariable.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                        }
                        else if (baseFieldDeclaration.Kind() == SyntaxKind.EventFieldDeclaration)
                        {
                            return (EnvDTE.CodeElement)CodeEvent.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                        }
                    }

                    throw Exceptions.ThrowEUnexpected();
                default:
                    throw new InvalidOperationException();
            }
        }
开发者ID:SoumikMukherjeeDOTNET,项目名称:roslyn,代码行数:79,代码来源:CSharpCodeModelService.cs

示例7: GetFieldFromVariableNode

 protected override SyntaxNode GetFieldFromVariableNode(SyntaxNode node)
 {
     return node.Kind() == SyntaxKind.VariableDeclarator
         ? node.FirstAncestorOrSelf<BaseFieldDeclarationSyntax>()
         : node;
 }
开发者ID:SoumikMukherjeeDOTNET,项目名称:roslyn,代码行数:6,代码来源:CSharpCodeModelService.cs

示例8: GetDefaultAccessibility

        private EnvDTE.vsCMAccess GetDefaultAccessibility(SyntaxNode node)
        {
            if (node is EnumMemberDeclarationSyntax)
            {
                return EnvDTE.vsCMAccess.vsCMAccessPublic;
            }

            if (node is BaseFieldDeclarationSyntax ||
                node is BaseMethodDeclarationSyntax ||
                node is BasePropertyDeclarationSyntax)
            {
                // Members of interfaces and enums are public, while all other
                // members are private.
                if (node.HasAncestor<InterfaceDeclarationSyntax>() ||
                    node.HasAncestor<EnumDeclarationSyntax>())
                {
                    return EnvDTE.vsCMAccess.vsCMAccessPublic;
                }
                else
                {
                    return EnvDTE.vsCMAccess.vsCMAccessPrivate;
                }
            }

            if (node is BaseTypeDeclarationSyntax ||
                node is DelegateDeclarationSyntax)
            {
                // Types declared within types are private by default,
                // otherwise internal.
                if (node.HasAncestor<BaseTypeDeclarationSyntax>())
                {
                    return EnvDTE.vsCMAccess.vsCMAccessPrivate;
                }
                else
                {
                    return EnvDTE.vsCMAccess.vsCMAccessProject;
                }
            }

            if (node is AccessorDeclarationSyntax)
            {
                return GetAccess(node.FirstAncestorOrSelf<BasePropertyDeclarationSyntax>());
            }

            throw Exceptions.ThrowEFail();
        }
开发者ID:SoumikMukherjeeDOTNET,项目名称:roslyn,代码行数:46,代码来源:CSharpCodeModelService.cs

示例9: GetContainingQueryExpression

 internal override SyntaxNode GetContainingQueryExpression(SyntaxNode node)
 {
     return node.FirstAncestorOrSelf<QueryExpressionSyntax>();
 }
开发者ID:GeertVL,项目名称:roslyn,代码行数:4,代码来源:CSharpEditAndContinueAnalyzer.cs

示例10: GetStatementParent

        private static SyntaxNode GetStatementParent(SyntaxNode node)
        {
            StatementSyntax statementSyntax = node.FirstAncestorOrSelf<StatementSyntax>();
            if (statementSyntax == null)
            {
                return null;
            }

            if (statementSyntax.IsKind(SyntaxKind.IfStatement) && statementSyntax.Parent.IsKind(SyntaxKind.ElseClause))
            {
                return statementSyntax.Parent;
            }

            return statementSyntax;
        }
开发者ID:JaRau,项目名称:StyleCopAnalyzers,代码行数:15,代码来源:SA1501CodeFixProvider.cs

示例11: DifferentStatementsWithSameParent

        private static bool DifferentStatementsWithSameParent(SyntaxNode first, SyntaxNode second)
        {
            var firstStatement = first.FirstAncestorOrSelf<StatementSyntax>();
            var secondStatement = second.FirstAncestorOrSelf<StatementSyntax>();

            return firstStatement != secondStatement && firstStatement.Parent == secondStatement.Parent;
        }
开发者ID:jango2015,项目名称:sonarlint-vs,代码行数:7,代码来源:DeadStores.cs

示例12: DetermineReferenceKind

        private ReferenceKind DetermineReferenceKind(SyntaxToken token, SyntaxNode node, ISymbol referencedSymbol)
        {
            var kind = ReferenceKind.Reference;

            var baseList =
                (SyntaxNode)node.FirstAncestorOrSelf<Microsoft.CodeAnalysis.CSharp.Syntax.BaseListSyntax>() ??
                (SyntaxNode)node.FirstAncestorOrSelf<Microsoft.CodeAnalysis.VisualBasic.Syntax.InheritsStatementSyntax>() ??
                node.FirstAncestorOrSelf<Microsoft.CodeAnalysis.VisualBasic.Syntax.ImplementsStatementSyntax>();
            if (baseList != null)
            {
                var typeDeclaration = baseList.Parent;
                if (typeDeclaration != null)
                {
                    var derivedType = SemanticModel.GetDeclaredSymbol(typeDeclaration) as INamedTypeSymbol;
                    if (derivedType != null)
                    {
                        INamedTypeSymbol baseSymbol = referencedSymbol as INamedTypeSymbol;
                        if (baseSymbol != null)
                        {
                            if (baseSymbol.TypeKind == TypeKind.Class && baseSymbol.Equals(derivedType.BaseType))
                            {
                                kind = ReferenceKind.DerivedType;
                            }
                            else if (baseSymbol.TypeKind == TypeKind.Interface)
                            {
                                if (derivedType.TypeKind == TypeKind.Interface && derivedType.Interfaces.Contains(baseSymbol))
                                {
                                    kind = ReferenceKind.InterfaceInheritance;
                                }
                                else if (derivedType.Interfaces.Contains(baseSymbol))
                                {
                                    kind = ReferenceKind.InterfaceImplementation;
                                }
                            }
                        }
                    }
                }
            }

            if ((referencedSymbol.Kind == SymbolKind.Field ||
                referencedSymbol.Kind == SymbolKind.Property) &&
                IsWrittenTo(node))
            {
                kind = ReferenceKind.Write;
            }

            return kind;
        }
开发者ID:rgmills,项目名称:SourceBrowser,代码行数:48,代码来源:DocumentGenerator.References.cs

示例13: GetContainingExpression

 private static ExpressionSyntax GetContainingExpression(SyntaxNode node)
 {
     return node.FirstAncestorOrSelf<ExpressionSyntax>();
 }
开发者ID:hexuefengx,项目名称:StyleCopAnalyzers,代码行数:4,代码来源:SA1501StatementMustNotBeOnASingleLine.cs

示例14: IsNameableNode

        private static bool IsNameableNode(SyntaxNode node)
        {
            switch (node.Kind())
            {
                case SyntaxKind.ClassDeclaration:
                case SyntaxKind.ConstructorDeclaration:
                case SyntaxKind.ConversionOperatorDeclaration:
                case SyntaxKind.DelegateDeclaration:
                case SyntaxKind.DestructorDeclaration:
                case SyntaxKind.EnumDeclaration:
                case SyntaxKind.EnumMemberDeclaration:
                case SyntaxKind.EventDeclaration:
                case SyntaxKind.IndexerDeclaration:
                case SyntaxKind.InterfaceDeclaration:
                case SyntaxKind.MethodDeclaration:
                case SyntaxKind.NamespaceDeclaration:
                case SyntaxKind.OperatorDeclaration:
                case SyntaxKind.PropertyDeclaration:
                case SyntaxKind.StructDeclaration:
                    return true;

                case SyntaxKind.VariableDeclarator:
                    // Could be a regular field or an event field.
                    return node.FirstAncestorOrSelf<BaseFieldDeclarationSyntax>() != null;

                default:
                    return false;
            }
        }
开发者ID:SoumikMukherjeeDOTNET,项目名称:roslyn,代码行数:29,代码来源:CSharpCodeModelService.cs

示例15: ReportMemberUpdateRudeEdits

        internal override void ReportMemberUpdateRudeEdits(List<RudeEditDiagnostic> diagnostics, SyntaxNode newMember, TextSpan? span)
        {
            var classifier = new EditClassifier(this, diagnostics, null, newMember, EditKind.Update, span: span);

            classifier.ClassifyMemberBodyRudeUpdate(
                newMember as MethodDeclarationSyntax,
                newMember.FirstAncestorOrSelf<TypeDeclarationSyntax>(),
                isTriviaUpdate: true);

            classifier.ClassifyDeclarationBodyRudeUpdates(newMember);
        }
开发者ID:GeertVL,项目名称:roslyn,代码行数:11,代码来源:CSharpEditAndContinueAnalyzer.cs


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