本文整理汇总了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);
}
示例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;
}
示例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;
}
示例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;
}
示例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();
}
}
示例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();
}
}
示例7: GetFieldFromVariableNode
protected override SyntaxNode GetFieldFromVariableNode(SyntaxNode node)
{
return node.Kind() == SyntaxKind.VariableDeclarator
? node.FirstAncestorOrSelf<BaseFieldDeclarationSyntax>()
: node;
}
示例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();
}
示例9: GetContainingQueryExpression
internal override SyntaxNode GetContainingQueryExpression(SyntaxNode node)
{
return node.FirstAncestorOrSelf<QueryExpressionSyntax>();
}
示例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;
}
示例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;
}
示例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;
}
示例13: GetContainingExpression
private static ExpressionSyntax GetContainingExpression(SyntaxNode node)
{
return node.FirstAncestorOrSelf<ExpressionSyntax>();
}
示例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;
}
}
示例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);
}