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


C# RefactoringContext.GetNode方法代码示例

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


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

示例1: GetActions

		public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			var variableDeclaration = context.GetNode<VariableDeclarationStatement>();
			if (variableDeclaration == null)
				yield break;
			var entryNode = FindCurrentScopeEntryNode(variableDeclaration);
			if (entryNode == null)
				yield break;
			var selectedInitializer = context.GetNode<VariableInitializer>();
			if (selectedInitializer != null) {
				if (!selectedInitializer.NameToken.Contains(context.Location))
					yield break;
				if (HasDependency(context, entryNode, selectedInitializer)) {
					yield return MoveDeclarationAction(context, entryNode, variableDeclaration, selectedInitializer);
				} else {
					yield return MoveInitializerAction(context, entryNode, variableDeclaration, selectedInitializer);
				}
			} else {
				if (!variableDeclaration.Type.Contains(context.Location) || variableDeclaration.Variables.Count <= 1)
					yield break;
				yield return new CodeAction(context.TranslateString("Move declaration to outer scope"), script => {
					script.Remove(variableDeclaration);
					script.InsertBefore(entryNode, variableDeclaration.Clone());
				}, variableDeclaration);
			}
		}
开发者ID:0xb1dd1e,项目名称:NRefactory,代码行数:26,代码来源:MoveToOuterScopeAction.cs

示例2: GetActions

        public IEnumerable<CodeAction> GetActions(RefactoringContext context)
        {
            if (!context.IsSomethingSelected) {
                yield break;
            }
            var pexpr = context.GetNode<PrimitiveExpression>();
            if (pexpr == null || !(pexpr.Value is string)) {
                yield break;
            }
            if (pexpr.LiteralValue.StartsWith("@", StringComparison.Ordinal)) {
                if (!(pexpr.StartLocation < new TextLocation(context.Location.Line, context.Location.Column - 1) && new TextLocation(context.Location.Line, context.Location.Column + 1) < pexpr.EndLocation)) {
                    yield break;
                }
            } else {
                if (!(pexpr.StartLocation < context.Location && context.Location < pexpr.EndLocation)) {
                    yield break;
                }
            }

            yield return new CodeAction (context.TranslateString("Introduce format item"), script => {
                var invocation = context.GetNode<InvocationExpression>();
                if (invocation != null && invocation.Target.IsMatch(PrototypeFormatReference)) {
                    AddFormatCallToInvocation(context, script, pexpr, invocation);
                    return;
                }

                var arg = CreateFormatArgument(context);
                var newInvocation = new InvocationExpression (PrototypeFormatReference.Clone()) {
                    Arguments = { CreateFormatString(context, pexpr, 0), arg }
                };

                script.Replace(pexpr, newInvocation);
                script.Select(arg);
            });
        }
开发者ID:artifexor,项目名称:NRefactory,代码行数:35,代码来源:IntroduceFormatItemAction.cs

示例3: GetActions

        public IEnumerable<CodeAction> GetActions(RefactoringContext context)
        {
            var createExpression = context.GetNode<ObjectCreateExpression>();
            if (createExpression != null)
                return GetActions(context, createExpression);

            var simpleType = context.GetNode<SimpleType>();
            if (simpleType != null && !(simpleType.Parent is EventDeclaration || simpleType.Parent is CustomEventDeclaration))
                return GetActions(context, simpleType);

            return Enumerable.Empty<CodeAction>();
        }
开发者ID:Xiaoqing,项目名称:NRefactory,代码行数:12,代码来源:CreateClassDeclarationAction.cs

示例4: GetActions

		public IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			var pexpr = context.GetNode<PrimitiveExpression>();
			if (pexpr == null)
				yield break;
			var statement = context.GetNode<Statement>();
			if (statement == null) {
				yield break;
			}

			var resolveResult = context.Resolve(pexpr);

			yield return new CodeAction(context.TranslateString("Create local constant"), script => {
				string name = CreateMethodDeclarationAction.CreateBaseName(pexpr, resolveResult.Type);
				var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
				if (service != null)
					name = service.CheckName(context, name, AffectedEntity.LocalConstant);

				var initializer = new VariableInitializer(name, pexpr.Clone());
				var decl = new VariableDeclarationStatement() {
					Type = context.CreateShortType(resolveResult.Type),
					Modifiers = Modifiers.Const,
					Variables = { initializer }
				};

				script.InsertBefore(statement, decl);
				var variableUsage = new IdentifierExpression(name);
				script.Replace(pexpr, variableUsage);
				script.Link(initializer.NameToken, variableUsage);
			});

			yield return new CodeAction(context.TranslateString("Create constant field"), script => {
				string name = CreateMethodDeclarationAction.CreateBaseName(pexpr, resolveResult.Type);
				var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
				if (service != null)
					name = service.CheckName(context, name, AffectedEntity.ConstantField);

				var initializer = new VariableInitializer(name, pexpr.Clone());

				var decl = new FieldDeclaration() {
					ReturnType = context.CreateShortType(resolveResult.Type),
					Modifiers = Modifiers.Const,
					Variables = { initializer }
				};

				var variableUsage = new IdentifierExpression(name);
				script.Replace(pexpr, variableUsage);
//				script.Link(initializer.NameToken, variableUsage);
				script.InsertWithCursor(context.TranslateString("Create constant"), Script.InsertPosition.Before, decl);
			});
		}
开发者ID:Gobiner,项目名称:ILSpy,代码行数:51,代码来源:IntroduceConstantAction.cs

示例5: GetActions

		public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			var identifier = context.GetNode<IdentifierExpression>();
			if (identifier != null && !(identifier.Parent is InvocationExpression && ((InvocationExpression)identifier.Parent).Target == identifier))
				return GetActionsFromIdentifier(context, identifier);
			
			var memberReference = context.GetNode<MemberReferenceExpression>();
			if (memberReference != null && !(memberReference.Parent is InvocationExpression && ((InvocationExpression)memberReference.Parent).Target == memberReference))
				return GetActionsFromMemberReferenceExpression(context, memberReference);

			var invocation = context.GetNode<InvocationExpression>();
			if (invocation != null)
				return GetActionsFromInvocation(context, invocation);
			return Enumerable.Empty<CodeAction>();
		}
开发者ID:qhta,项目名称:NRefactory,代码行数:15,代码来源:CreateMethodDeclarationAction.cs

示例6: GetActions

		public IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			var initializer = context.GetNode<VariableInitializer>();
			if (initializer != null) {
				var action = HandleInitializer(context, initializer);
				if (action != null)
					yield return action;
			}
			var expressionStatement = context.GetNode<ExpressionStatement>();
			if (expressionStatement != null) {
				var action = HandleExpressionStatement(context, expressionStatement);
				if (action != null)
					yield return action;
			}
		}
开发者ID:adisik,项目名称:simple-assembly-explorer,代码行数:15,代码来源:ConvertToInitializerAction.cs

示例7: ActionFromUsingStatement

		CodeAction ActionFromUsingStatement(RefactoringContext context)
		{
			var initializer = context.GetNode<VariableInitializer>();
			if (initializer == null)
				return null;
			var initializerRR = context.Resolve(initializer) as LocalResolveResult;
			if (initializerRR == null)
				return null;
			var elementType = GetElementType(initializerRR, context);
			if (elementType == null)
				return null;
			var usingStatement = initializer.Parent.Parent as UsingStatement;
			if (usingStatement == null)
				return null;
			return new CodeAction(context.TranslateString("Iterate via foreach"), script => {
				var iterator = MakeForeach(new IdentifierExpression(initializer.Name), elementType, context);
				if (usingStatement.EmbeddedStatement is EmptyStatement) {
					var blockStatement = new BlockStatement();
					blockStatement.Statements.Add(iterator);
					script.Replace(usingStatement.EmbeddedStatement, blockStatement);
					script.FormatText(blockStatement);
				} else if (usingStatement.EmbeddedStatement is BlockStatement) {
					var anchorNode = usingStatement.EmbeddedStatement.FirstChild;
					script.InsertAfter(anchorNode, iterator);
					script.FormatText(usingStatement.EmbeddedStatement);
				}
			});
		}
开发者ID:Gobiner,项目名称:ILSpy,代码行数:28,代码来源:IterateViaForeachAction.cs

示例8: GetIfElseStatement

		static IfElseStatement GetIfElseStatement (RefactoringContext context)
		{
			var result = context.GetNode<IfElseStatement> ();
			if (result != null && result.IfToken.Contains (context.Location))
				return result;
			return null;
		}
开发者ID:0xb1dd1e,项目名称:NRefactory,代码行数:7,代码来源:InvertIfAction.cs

示例9: GetActions

        public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
        {
            var node = context.GetNode<InvocationExpression>();
            if (node == null)
                yield break;
            if ((node.Target is IdentifierExpression) && !node.Target.IsInside(context.Location))
                yield break;
            if ((node.Target is MemberReferenceExpression) && !((MemberReferenceExpression)node.Target).MemberNameToken.IsInside(context.Location))
                yield break;
            var rr = context.Resolve(node) as CSharpInvocationResolveResult;
            if (rr == null || rr.IsError || rr.Member.Name != "Equals" || !rr.Member.DeclaringType.IsKnownType(KnownTypeCode.Object))
                yield break;
            Expression expr = node;
            bool useEquality = true;
            var uOp = node.Parent as UnaryOperatorExpression;
            if (uOp != null && uOp.Operator == UnaryOperatorType.Not) {
                expr = uOp;
                useEquality = false;
            }

            yield return new CodeAction(
                useEquality ? context.TranslateString("Use '=='") : context.TranslateString("Use '!='"),
                script => {
                    script.Replace(
                        expr,
                        new BinaryOperatorExpression(
                            node.Arguments.First().Clone(),
                            useEquality ? BinaryOperatorType.Equality :  BinaryOperatorType.InEquality,
                            node.Arguments.Last().Clone()
                        )
                    );
                },
                node.Target
            );
        }
开发者ID:porcus,项目名称:NRefactory,代码行数:35,代码来源:ConvertEqualsToEqualityOperatorAction.cs

示例10: GetActions

		public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			var entity = context.GetNode<ConstructorDeclaration>();
			if (entity == null)
				yield break;
			var type = entity.Parent as TypeDeclaration;

			if (type == null || entity.Name == type.Name)
				yield break;

			var typeDeclaration = entity.GetParent<TypeDeclaration>();

			yield return new CodeAction(context.TranslateString("This is a constructor"), script => script.Replace(entity.NameToken, Identifier.Create(typeDeclaration.Name, TextLocation.Empty)), entity) {
				Severity = ICSharpCode.NRefactory.Refactoring.Severity.Error
			};

			yield return new CodeAction(context.TranslateString("This is a void method"), script => {
				var generatedMethod = new MethodDeclaration();
				generatedMethod.Modifiers = entity.Modifiers;
				generatedMethod.ReturnType = new PrimitiveType("void");
				generatedMethod.Name = entity.Name;
				generatedMethod.Parameters.AddRange(entity.Parameters.Select(parameter => (ParameterDeclaration)parameter.Clone()));
				generatedMethod.Body = (BlockStatement)entity.Body.Clone();
				generatedMethod.Attributes.AddRange(entity.Attributes.Select(attribute => (AttributeSection)attribute.Clone()));

				script.Replace(entity, generatedMethod);
			}, entity) {
				Severity = ICSharpCode.NRefactory.Refactoring.Severity.Error
			};
		}
开发者ID:0xb1dd1e,项目名称:NRefactory,代码行数:30,代码来源:CS1520MethodMustHaveAReturnTypeAction.cs

示例11: GetActions

		public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			var service = (CodeGenerationService)context.GetService(typeof(CodeGenerationService)); 
			if (service == null)
				yield break;

			var type = context.GetNode<AstType>();
			if (type == null || type.Role != Roles.BaseType)
				yield break;
			var state = context.GetResolverStateBefore(type);
			if (state.CurrentTypeDefinition == null)
				yield break;

			var resolveResult = context.Resolve(type);
			if (resolveResult.Type.Kind != TypeKind.Interface)
				yield break;

			bool interfaceMissing;
			var toImplement = ImplementInterfaceAction.CollectMembersToImplement(
				state.CurrentTypeDefinition,
				resolveResult.Type,
				false,
				out interfaceMissing
			);
			if (toImplement.Count == 0)
				yield break;

			yield return new CodeAction(context.TranslateString("Implement interface explicit"), script =>
				script.InsertWithCursor(
					context.TranslateString("Implement Interface"),
					state.CurrentTypeDefinition,
					(s, c) => ImplementInterfaceAction.GenerateImplementation (c, toImplement.Select (t => Tuple.Create (t.Item1, true)), interfaceMissing).ToList()
				)
			, type);
		}
开发者ID:0xb1dd1e,项目名称:NRefactory,代码行数:35,代码来源:ImplementInterfaceExplicitAction.cs

示例12: GetForeachStatement

		static ForeachStatement GetForeachStatement (RefactoringContext context)
		{
			var result = context.GetNode<ForeachStatement> ();
			if (result != null && result.VariableType.Contains (context.Location) && !result.VariableType.IsVar ())
				return result;
			return null;
		}
开发者ID:qhta,项目名称:NRefactory,代码行数:7,代码来源:UseVarKeywordAction.cs

示例13: GetSwitchStatement

		static SwitchStatement GetSwitchStatement (RefactoringContext context)
		{
			var switchStatment = context.GetNode<SwitchStatement> ();
			if (switchStatment != null && switchStatment.SwitchSections.Count == 0)
				return switchStatment;
			return null;
		}
开发者ID:hduregger,项目名称:monodevelop,代码行数:7,代码来源:GenerateSwitchLabels.cs

示例14: GetDirective

 static PreProcessorDirective GetDirective(RefactoringContext context)
 {
     var directive = context.GetNode<PreProcessorDirective> ();
     if (directive == null || directive.Type != PreProcessorDirectiveType.Region)
         return null;
     return directive;
 }
开发者ID:porcus,项目名称:NRefactory,代码行数:7,代码来源:RemoveRegionAction.cs

示例15: GetActions

		public IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			var initializer = context.GetNode<VariableInitializer>();
			if (initializer == null || !initializer.NameToken.Contains(context.Location.Line, context.Location.Column)) {
				yield break;
			}
			var type = initializer.Parent.Parent as TypeDeclaration;
			if (type == null) {
				yield break;
			}
			foreach (var member in type.Members) {
				if (member is PropertyDeclaration && ContainsGetter((PropertyDeclaration)member, initializer)) {
					yield break;
				}
			}
			var field = initializer.Parent as FieldDeclaration;
			if (field == null || field.HasModifier(Modifiers.Readonly) || field.HasModifier(Modifiers.Const)) {
				yield break;
			}
			var resolveResult = context.Resolve(initializer) as MemberResolveResult;
			if (resolveResult == null)
				yield break;
			yield return new CodeAction(context.TranslateString("Create property"), script => {
				var fieldName = context.GetNameProposal(initializer.Name, true);
				if (initializer.Name == context.GetNameProposal(initializer.Name, false)) {
					script.Rename(resolveResult.Member, fieldName);
				}
				script.InsertWithCursor(
					context.TranslateString("Create property"),
					Script.InsertPosition.After, GeneratePropertyDeclaration(context, field, fieldName));
			});
		}
开发者ID:Gobiner,项目名称:ILSpy,代码行数:32,代码来源:GeneratePropertyAction.cs


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