本文整理汇总了C#中AstNode.GetParent方法的典型用法代码示例。如果您正苦于以下问题:C# AstNode.GetParent方法的具体用法?C# AstNode.GetParent怎么用?C# AstNode.GetParent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AstNode
的用法示例。
在下文中一共展示了AstNode.GetParent方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HidesMember
protected static bool HidesMember(BaseRefactoringContext ctx, AstNode node, string variableName)
{
var typeDecl = node.GetParent<TypeDeclaration> ();
if (typeDecl == null)
return false;
var typeResolveResult = ctx.Resolve (typeDecl) as TypeResolveResult;
if (typeResolveResult == null)
return false;
var entityDecl = node.GetParent<EntityDeclaration> ();
var isStatic = (entityDecl.Modifiers & Modifiers.Static) == Modifiers.Static;
return typeResolveResult.Type.GetMembers (m => m.Name == variableName && m.IsStatic == isStatic).Any ();
}
示例2: HidesMember
protected static bool HidesMember(BaseRefactoringContext ctx, AstNode node, string variableName)
{
var typeDecl = node.GetParent<TypeDeclaration>();
if (typeDecl == null)
return false;
var entityDecl = node.GetParent<EntityDeclaration>();
var memberResolveResult = ctx.Resolve(entityDecl) as MemberResolveResult;
if (memberResolveResult == null)
return false;
var typeResolveResult = ctx.Resolve(typeDecl) as TypeResolveResult;
if (typeResolveResult == null)
return false;
var sourceMember = memberResolveResult.Member;
return typeResolveResult.Type.GetMembers(m => m.Name == variableName).Any(m2 => IsAccessible(sourceMember, m2));
}
示例3: GetAction
CodeAction GetAction (RefactoringContext context, AstNode node, MethodDeclaration method)
{
return new CodeAction (context.TranslateString ("Extract anonymous method"),
script =>
{
var identifier = new IdentifierExpression ("Method");
script.Replace (node, identifier);
script.InsertBefore (node.GetParent<EntityDeclaration> (), method);
script.Link (method.NameToken, identifier);
});
}
示例4: GetNodeKind
protected override NodeKind GetNodeKind (AstNode node)
{
var assignment = node.GetParent<AssignmentExpression> ();
if (assignment != null && assignment.Left == node) {
if (assignment.Operator == AssignmentOperatorType.Assign)
return NodeKind.Modification;
return NodeKind.ReferenceAndModification;
}
var unaryExpr = node.GetParent<UnaryOperatorExpression> ();
if (unaryExpr != null && unaryExpr.Expression == node &&
(unaryExpr.Operator == UnaryOperatorType.Increment ||
unaryExpr.Operator == UnaryOperatorType.PostIncrement ||
unaryExpr.Operator == UnaryOperatorType.Decrement ||
unaryExpr.Operator == UnaryOperatorType.PostDecrement)) {
return NodeKind.ReferenceAndModification;
}
if (node.Parent is ForeachStatement)
return NodeKind.Modification;
return NodeKind.Reference;
}
示例5: CreateTypeAndNamespaceCompletionData
IEnumerable<ICompletionData> CreateTypeAndNamespaceCompletionData(TextLocation location, ResolveResult resolveResult, AstNode resolvedNode, CSharpResolver state)
{
if (resolveResult == null || resolveResult.IsError) {
return null;
}
var exprParent = resolvedNode.GetParent<Expression>();
var unit = exprParent != null ? exprParent.GetParent<SyntaxTree>() : null;
var astResolver = unit != null ? CompletionContextProvider.GetResolver(state, unit) : null;
IType hintType = exprParent != null && astResolver != null ?
TypeGuessing.GetValidTypes(astResolver, exprParent).FirstOrDefault() :
null;
var result = new CompletionDataWrapper(this);
var lookup = new MemberLookup(
ctx.CurrentTypeDefinition,
Compilation.MainAssembly
);
if (resolveResult is NamespaceResolveResult) {
var nr = (NamespaceResolveResult)resolveResult;
if (!(resolvedNode.Parent is UsingDeclaration || resolvedNode.Parent != null && resolvedNode.Parent.Parent is UsingDeclaration)) {
foreach (var cl in nr.Namespace.Types) {
if (hintType != null && hintType.Kind != TypeKind.Array && cl.Kind == TypeKind.Interface) {
continue;
}
if (!lookup.IsAccessible(cl, false))
continue;
result.AddType(cl, false, IsAttributeContext(resolvedNode));
}
}
foreach (var ns in nr.Namespace.ChildNamespaces) {
result.AddNamespace(lookup, ns);
}
} else if (resolveResult is TypeResolveResult) {
var type = resolveResult.Type;
foreach (var nested in type.GetNestedTypes ()) {
if (hintType != null && hintType.Kind != TypeKind.Array && nested.Kind == TypeKind.Interface) {
continue;
}
var def = nested.GetDefinition();
if (def != null && !lookup.IsAccessible(def, false))
continue;
result.AddType(nested, false);
}
}
return result.Result;
}
示例6: CreateTypeAndNamespaceCompletionData
IEnumerable<ICompletionData> CreateTypeAndNamespaceCompletionData(TextLocation location, ResolveResult resolveResult, AstNode resolvedNode, CSharpResolver state)
{
if (resolveResult == null || resolveResult.IsError) {
return null;
}
var exprParent = resolvedNode.GetParent<Expression>();
var unit = exprParent != null ? exprParent.GetParent<CompilationUnit>() : null;
var astResolver = unit != null ? new CSharpAstResolver(
state,
unit,
CSharpParsedFile
) : null;
IType hintType = exprParent != null && astResolver != null ?
CreateFieldAction.GetValidTypes(astResolver, exprParent) .FirstOrDefault() :
null;
var result = new CompletionDataWrapper(this);
if (resolveResult is NamespaceResolveResult) {
var nr = (NamespaceResolveResult)resolveResult;
if (!(resolvedNode.Parent is UsingDeclaration || resolvedNode.Parent != null && resolvedNode.Parent.Parent is UsingDeclaration)) {
foreach (var cl in nr.Namespace.Types) {
string name = cl.Name;
if (hintType != null && hintType.Kind != TypeKind.Array && cl.Kind == TypeKind.Interface) {
continue;
}
if (IsAttributeContext(resolvedNode) && name.EndsWith("Attribute") && name.Length > "Attribute".Length) {
name = name.Substring(0, name.Length - "Attribute".Length);
}
result.AddType(cl, name);
}
}
foreach (var ns in nr.Namespace.ChildNamespaces) {
result.AddNamespace(ns.Name);
}
} else if (resolveResult is TypeResolveResult) {
var type = resolveResult.Type;
foreach (var nested in type.GetNestedTypes ()) {
if (hintType != null && hintType.Kind != TypeKind.Array && nested.Kind == TypeKind.Interface) {
continue;
}
result.AddType(nested, nested.Name);
}
}
return result.Result;
}
示例7: GetParentFinallyBlock
public AstNode GetParentFinallyBlock(AstNode node, bool stopOnLoops)
{
var insideTryFinally = false;
var target = node.GetParent(n =>
{
if (n is LambdaExpression || n is AnonymousMethodExpression || n is MethodDeclaration)
{
return true;
}
if (stopOnLoops && (n is WhileStatement || n is ForeachStatement || n is ForStatement || n is DoWhileStatement))
{
return true;
}
if (n is TryCatchStatement && !((TryCatchStatement)n).FinallyBlock.IsNull)
{
insideTryFinally = true;
return true;
}
return false;
});
return insideTryFinally ? ((TryCatchStatement)target).FinallyBlock : null;
}
示例8: IsDirectAsyncBlockChild
protected bool IsDirectAsyncBlockChild(AstNode node)
{
var block = node.GetParent<BlockStatement>();
if (block != null && (block.Parent is MethodDeclaration || block.Parent is AnonymousMethodExpression || block.Parent is LambdaExpression))
{
return true;
}
return false;
}
示例9: ResolveNode
public ResolveResult ResolveNode(AstNode node, ILog log)
{
var syntaxTree = node.GetParent<SyntaxTree>();
this.InitResolver(syntaxTree);
var result = this.resolver.Resolve(node);
if (result is MethodGroupResolveResult && node.Parent != null)
{
var methodGroupResolveResult = (MethodGroupResolveResult)result;
var parentResolveResult = this.ResolveNode(node.Parent, log);
var parentInvocation = parentResolveResult as InvocationResolveResult;
IParameterizedMember method = methodGroupResolveResult.Methods.LastOrDefault();
bool isInvocation = node.Parent is InvocationExpression && (((InvocationExpression)(node.Parent)).Target == node);
if (node is Expression)
{
var conversion = this.Resolver.GetConversion((Expression)node);
if (conversion != null && conversion.IsMethodGroupConversion)
{
return new MemberResolveResult(new TypeResolveResult(conversion.Method.DeclaringType), conversion.Method);
}
}
if (isInvocation && parentInvocation != null)
{
var or = methodGroupResolveResult.PerformOverloadResolution(this.compilation, parentInvocation.GetArgumentsForCall().ToArray());
if (or.FoundApplicableCandidate)
{
method = or.BestCandidate;
return new MemberResolveResult(new TypeResolveResult(method.DeclaringType), method);
}
}
if (parentInvocation != null && method == null)
{
var typeDef = methodGroupResolveResult.TargetType as DefaultResolvedTypeDefinition;
if (typeDef != null)
{
var methods = typeDef.Methods.Where(m => m.Name == methodGroupResolveResult.MethodName);
method = methods.FirstOrDefault();
}
}
if (method == null)
{
var extMethods = methodGroupResolveResult.GetEligibleExtensionMethods(false);
if (!extMethods.Any())
{
extMethods = methodGroupResolveResult.GetExtensionMethods();
}
if (!extMethods.Any() || !extMethods.First().Any())
{
throw new EmitterException(node, "Cannot find method defintion");
}
method = extMethods.First().First();
}
if (parentInvocation == null || method.FullName != parentInvocation.Member.FullName)
{
MemberResolveResult memberResolveResult = new MemberResolveResult(new TypeResolveResult(method.DeclaringType), method);
return memberResolveResult;
}
return parentResolveResult;
}
if ((result == null || result.IsError) && log != null)
{
if (result is CSharpInvocationResolveResult && ((CSharpInvocationResolveResult)result).OverloadResolutionErrors != OverloadResolutionErrors.None)
{
return result;
}
log.LogWarning(string.Format("Node resolving has failed {0}: {1}", node.StartLocation, node.ToString()));
}
return result;
}
示例10: Node
public Node (AstNode astNode, NodeKind kind)
{
AstNode = astNode;
Kind = kind;
ContainingStatement = astNode.GetParent<Statement> ();
}
示例11: IsInsideTryBlock
static bool IsInsideTryBlock(AstNode node)
{
var tryCatchStatement = node.GetParent<TryCatchStatement> ();
if (tryCatchStatement == null)
return false;
return tryCatchStatement.TryBlock.Contains (node.StartLocation.Line,node.StartLocation.Column);
}
示例12: ResolveExpression
public static string ResolveExpression (TextEditorData editor, ResolveResult result, AstNode node, out int startOffset)
{
//Console.WriteLine ("result is a {0}", result.GetType ().Name);
startOffset = -1;
if (result is NamespaceResolveResult ||
result is ConversionResolveResult ||
result is ConstantResolveResult ||
result is ForEachResolveResult ||
result is TypeIsResolveResult ||
result is TypeOfResolveResult ||
result is ErrorResolveResult)
return null;
if (result.IsCompileTimeConstant)
return null;
startOffset = editor.LocationToOffset (node.StartLocation.Line, node.StartLocation.Column);
if (result is InvocationResolveResult) {
var ir = (InvocationResolveResult) result;
if (ir.Member.Name == ".ctor") {
// if the user is hovering over something like "new Abc (...)", we want to show them type information for Abc
return ir.Member.DeclaringType.FullName;
}
// do not support general method invocation for tooltips because it could cause side-effects
return null;
} else if (result is LocalResolveResult) {
if (node is ParameterDeclaration) {
// user is hovering over a method parameter, but we don't want to include the parameter type
var param = (ParameterDeclaration) node;
return GetIdentifierName (editor, param.NameToken, out startOffset);
}
if (node is VariableInitializer) {
// user is hovering over something like "int fubar = 5;", but we don't want the expression to include the " = 5"
var variable = (VariableInitializer) node;
return GetIdentifierName (editor, variable.NameToken, out startOffset);
}
} else if (result is MemberResolveResult) {
var mr = (MemberResolveResult) result;
if (node is PropertyDeclaration) {
var prop = (PropertyDeclaration) node;
var name = GetIdentifierName (editor, prop.NameToken, out startOffset);
// if the property is static, then we want to return "Full.TypeName.Property"
if (prop.Modifiers.HasFlag (Modifiers.Static))
return mr.Member.DeclaringType.FullName + "." + name;
// otherwise we want to return "this.Property" so that it won't conflict with anything else in the local scope
return "this." + name;
}
if (node is FieldDeclaration) {
var field = (FieldDeclaration) node;
var name = GetIdentifierName (editor, field.NameToken, out startOffset);
// if the field is static, then we want to return "Full.TypeName.Field"
if (field.Modifiers.HasFlag (Modifiers.Static))
return mr.Member.DeclaringType.FullName + "." + name;
// otherwise we want to return "this.Field" so that it won't conflict with anything else in the local scope
return "this." + name;
}
if (node is VariableInitializer) {
// user is hovering over a field declaration that includes initialization
var variable = (VariableInitializer) node;
var name = GetIdentifierName (editor, variable.NameToken, out startOffset);
// walk up the AST to find the FieldDeclaration so that we can determine if it is static or not
var field = variable.GetParent<FieldDeclaration> ();
// if the field is static, then we want to return "Full.TypeName.Field"
if (field.Modifiers.HasFlag (Modifiers.Static))
return mr.Member.DeclaringType.FullName + "." + name;
// otherwise we want to return "this.Field" so that it won't conflict with anything else in the local scope
return "this." + name;
}
if (node is NamedExpression) {
// user is hovering over 'Property' in an expression like: var fubar = new Fubar () { Property = baz };
var variable = node.GetParent<VariableInitializer> ();
if (variable != null) {
var variableName = GetIdentifierName (editor, variable.NameToken, out startOffset);
var name = GetIdentifierName (editor, ((NamedExpression) node).NameToken, out startOffset);
return variableName + "." + name;
}
}
} else if (result is TypeResolveResult) {
return ((TypeResolveResult) result).Type.FullName;
}
return editor.GetTextBetween (node.StartLocation, node.EndLocation);
//.........这里部分代码省略.........
示例13: Visit
public bool Visit(AstNode node)
{
int type = node.GetType();
if (type == Token.SCRIPT)
{
return true;
}
if (node.GetParent() == null)
{
throw new InvalidOperationException("No parent for node: " + node + "\n" + node.ToSource(0));
}
return true;
}