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


C# RefactoringContext.TranslateString方法代码示例

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


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

示例1: GetActions

		public IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			var expr = GetCreatePropertyOrFieldNode(context);
			if (expr == null)
				yield break;
			if (!(expr is MemberReferenceExpression))
				yield break;
			var propertyName = CreatePropertyAction.GetPropertyName(expr);
			if (propertyName == null)
				yield break;
			if (IsInvocationTarget(expr))
				yield break;
			var statement = expr.GetParent<Statement>();
			if (statement == null)
				yield break;
			if (!(context.Resolve(expr).IsError))
				yield break;
			var guessedType = CreateFieldAction.GuessType(context, expr);
			if (guessedType == null || guessedType.Kind != TypeKind.Enum)
				yield break;
			var state = context.GetResolverStateBefore(expr);
			if (state.CurrentMember == null || state.CurrentTypeDefinition == null)
				yield break;

			yield return new CodeAction(context.TranslateString("Create enum value"), script => {
				var decl = new EnumMemberDeclaration {
					Name = propertyName
				};
				script.InsertWithCursor(context.TranslateString("Create enum value"), guessedType.GetDefinition (), decl);
			});
			
		}
开发者ID:RainsSoft,项目名称:playscript-monodevelop,代码行数:32,代码来源:CreateEnumValue.cs

示例2: GetActions

		public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			var property = context.GetNode<PropertyDeclaration>();
			if (property == null || !property.NameToken.Contains(context.Location))
				yield break;

			var field = RemoveBackingStoreAction.GetBackingField(context);
			if (field == null)
				yield break;
			var resolvedType = ReflectionHelper.ParseReflectionName ("System.EventHandler").Resolve (context.Compilation);
			if (resolvedType == null)
				yield break;
			var type = (TypeDeclaration)property.Parent;

			yield return new CodeAction(context.TranslateString("Create changed event"), script => {
				var eventDeclaration = CreateChangedEventDeclaration (context, property);
				var methodDeclaration = CreateEventInvocatorAction.CreateEventInvocator (context, type, eventDeclaration, eventDeclaration.Variables.First (), resolvedType.GetDelegateInvokeMethod (), false);
				var stmt = new ExpressionStatement (new InvocationExpression (
					new IdentifierExpression (methodDeclaration.Name),
					new MemberReferenceExpression (context.CreateShortType("System", "EventArgs"), "Empty")
				));
				script.InsertWithCursor(
					context.TranslateString("Create event invocator"),
					Script.InsertPosition.After,
					new AstNode[] { eventDeclaration, methodDeclaration }
				).ContinueScript(delegate {
					script.InsertBefore (property.Setter.Body.RBraceToken, stmt);
					script.FormatText (stmt);
				});
			}, property.NameToken);
		}
开发者ID:qhta,项目名称:NRefactory,代码行数:31,代码来源:CreateChangedEventAction.cs

示例3: GetActions

		public IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			var pdecl = GetPropertyDeclaration(context);
			if (pdecl == null) { 
				yield break;
			}

			var type = pdecl.Parent as TypeDeclaration;
			if (type != null && type.ClassType == ClassType.Interface) {
				yield break;
			}
			yield return new CodeAction (pdecl.Setter.IsNull ? context.TranslateString("Add getter") : context.TranslateString("Add setter"), script => {
				var accessorStatement = BuildAccessorStatement(context, pdecl);
			
				Accessor accessor = new Accessor () {
					Body = new BlockStatement { accessorStatement }
				};
				accessor.Role = pdecl.Setter.IsNull ? PropertyDeclaration.SetterRole : PropertyDeclaration.GetterRole;

				if (pdecl.Setter.IsNull && !pdecl.Getter.IsNull) {
					script.InsertBefore(pdecl.RBraceToken, accessor);
				} else if (pdecl.Getter.IsNull && !pdecl.Setter.IsNull) {
					script.InsertBefore(pdecl.Setter, accessor);
				} else {
					script.InsertBefore(pdecl.Getter, accessor);
				}
				script.Select(accessorStatement);
				script.FormatText(pdecl);
			});
		}
开发者ID:KAW0,项目名称:Alter-Native,代码行数:30,代码来源:AddAnotherAccessorAction.cs

示例4: 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.Class || resolveResult.Type.GetDefinition() == null || !resolveResult.Type.GetDefinition().IsAbstract)
				yield break;

			var toImplement = CollectMembersToImplement(state.CurrentTypeDefinition, resolveResult.Type);
			if (toImplement.Count == 0)
				yield break;

			yield return new CodeAction(
				context.TranslateString("Implement abstract members"), 
				script => script.InsertWithCursor(
					context.TranslateString("Implement abstract members"), 
					state.CurrentTypeDefinition, (s, c) => ImplementInterfaceAction.GenerateImplementation(c, toImplement.Select(m => Tuple.Create(m, false)), true)
				.Select(entity => {
					var decl = entity as EntityDeclaration;
					if (decl != null)
						decl.Modifiers |= Modifiers.Override;
					return entity;
				}).ToList()), type);
		}
开发者ID:0xb1dd1e,项目名称:NRefactory,代码行数:33,代码来源:ImplementAbstractMembersAction.cs

示例5: GetActions

		public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			var pdecl = context.GetNode<PropertyDeclaration> ();
			if (pdecl == null || !pdecl.Getter.IsNull && !pdecl.Setter.IsNull || !pdecl.NameToken.Contains(context.Location)) { 
				yield break;
			}

			var type = pdecl.Parent as TypeDeclaration;
			if (type != null && type.ClassType == ClassType.Interface) {
				yield break;
			}
			yield return new CodeAction (pdecl.Setter.IsNull ? context.TranslateString("Add setter") : context.TranslateString("Add getter"), script => {
				Statement accessorStatement = null;
			
				var accessor = new Accessor ();
				if (!pdecl.Getter.IsNull && !pdecl.Getter.Body.IsNull || !pdecl.Setter.IsNull && !pdecl.Setter.Body.IsNull) {
					accessorStatement = BuildAccessorStatement(context, pdecl);
					accessor.Body = new BlockStatement { accessorStatement };
				}

				accessor.Role = pdecl.Setter.IsNull ? PropertyDeclaration.SetterRole : PropertyDeclaration.GetterRole;

				if (pdecl.Setter.IsNull && !pdecl.Getter.IsNull) {
					script.InsertAfter(pdecl.Getter, accessor);
				} else if (pdecl.Getter.IsNull && !pdecl.Setter.IsNull) {
					script.InsertBefore(pdecl.Setter, accessor);
				} else {
					script.InsertBefore(pdecl.Getter, accessor);
				}
				script.FormatText(pdecl);
				if (accessorStatement != null)
					script.Select(accessorStatement);
			}, pdecl.NameToken);
		}
开发者ID:0xb1dd1e,项目名称:NRefactory,代码行数:34,代码来源:AddAnotherAccessorAction.cs

示例6: 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

示例7: GetActions

		public IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			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;

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

			yield return new CodeAction(context.TranslateString("Implement interface explicit"), script => {
				script.InsertWithCursor(
					context.TranslateString("Implement Interface"),
					state.CurrentTypeDefinition,
					ImplementInterfaceAction.GenerateImplementation (context, toImplement.Select (t => Tuple.Create (t.Item1, true)))
				);
			});
		}
开发者ID:Gobiner,项目名称:ILSpy,代码行数:29,代码来源:ImplementInterfaceExplicitAction.cs

示例8: 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

示例9: 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

示例10: GetActions

		public IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			var initializer = GetVariableInitializer(context);
			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) {
				yield break;
			}
			
			yield return new CodeAction(context.TranslateString("Create getter"), script => {
				script.InsertWithCursor(
					context.TranslateString("Create getter"),
					Script.InsertPosition.After,
					GeneratePropertyDeclaration(context, field, initializer));
			});
		}
开发者ID:adisik,项目名称:simple-assembly-explorer,代码行数:27,代码来源:GenerateGetterAction.cs

示例11: 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

示例12: GetActions

        public IEnumerable<CodeAction> GetActions(RefactoringContext context)
        {
            var createExpression = context.GetNode<Expression>() as ObjectCreateExpression;
            if (createExpression == null)
                yield break;

            var resolveResult = context.Resolve(createExpression) as CSharpInvocationResolveResult;
            if (resolveResult == null || !resolveResult.IsError || resolveResult.Member.DeclaringTypeDefinition == null || resolveResult.Member.DeclaringTypeDefinition.IsSealed || resolveResult.Member.DeclaringTypeDefinition.Region.IsEmpty)
                yield break;

            yield return new CodeAction(context.TranslateString("Create constructor"), script => {
                var decl = new ConstructorDeclaration() {
                    Name = resolveResult.Member.DeclaringTypeDefinition.Name,
                    Modifiers = Modifiers.Public,
                    Body = new BlockStatement() {
                        new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
                    }
                };
                decl.Parameters.AddRange(CreateMethodDeclarationAction.GenerateParameters(context, createExpression.Arguments));

                script.InsertWithCursor(
                    context.TranslateString("Create constructor"),
                    resolveResult.Member.DeclaringTypeDefinition,
                    decl
                );
            }, createExpression);
        }
开发者ID:segaman,项目名称:NRefactory,代码行数:27,代码来源:CreateConstructorDeclarationAction.cs

示例13: CreateFromExpression

		CodeAction CreateFromExpression(RefactoringContext context, Expression expression)
		{
			var resolveResult = context.Resolve(expression);
			if (resolveResult.IsError)
				return null;
			
			return new CodeAction(context.TranslateString("Extract method"), script => {
				string methodName = "NewMethod";
				var method = new MethodDeclaration() {
					ReturnType = context.CreateShortType(resolveResult.Type),
					Name = methodName,
					Body = new BlockStatement() {
						new ReturnStatement(expression.Clone())
					}
				};
				if (!StaticVisitor.UsesNotStaticMember(context, expression))
					method.Modifiers |= Modifiers.Static;
				var task = script.InsertWithCursor(context.TranslateString("Extract method"), Script.InsertPosition.Before, method);

				Action<Task> replaceStatements = delegate {
					var target = new IdentifierExpression(methodName);
					script.Replace(expression, new InvocationExpression(target));
					script.Link(target, method.NameToken);
				};

				if (task.IsCompleted) {
					replaceStatements (null);
				} else {
					task.ContinueWith (replaceStatements, TaskScheduler.FromCurrentSynchronizationContext ());
				}
			});
		}
开发者ID:adisik,项目名称:simple-assembly-explorer,代码行数:32,代码来源:ExtractMethodAction.cs

示例14: GetActions

		public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			var invocationExpression = context.GetNode<InvocationExpression>();
			if (invocationExpression == null)
				yield break;

			var resolveResult = context.Resolve(invocationExpression) as CSharpInvocationResolveResult;
			if (resolveResult == null) {
				yield break;
			}
			
			var method = (IMethod)resolveResult.Member;
			bool[] parameterIsSpecified = new bool[method.Parameters.Count];
			var argumentToParameterMap = resolveResult.GetArgumentToParameterMap();
			if (argumentToParameterMap != null) {
				foreach (int paramIndex in argumentToParameterMap)
					parameterIsSpecified[paramIndex] = true;
			} else {
				for (int i = 0; i < Math.Min(resolveResult.Arguments.Count, parameterIsSpecified.Length); i++) {
					parameterIsSpecified[i] = true;
				}
			}
			var missingParameters = new List<IParameter>();
			for (int i = 0; i < method.Parameters.Count; i++) {
				if (!parameterIsSpecified[i] && method.Parameters[i].IsOptional)
					missingParameters.Add(method.Parameters[i]);
			}
			
			foreach (var parameterToAdd in missingParameters) {
				//Add specific parameter
				yield return new CodeAction(string.Format(context.TranslateString("Add optional parameter \"{0}\""),
				                                         parameterToAdd.Name),
				                           script => {

					var newInvocation = (InvocationExpression)invocationExpression.Clone();
					AddArgument(newInvocation, parameterToAdd, parameterToAdd == missingParameters.First());
					script.Replace(invocationExpression, newInvocation);

				}, invocationExpression);
			}

			if (missingParameters.Count > 1) {
				//Add all parameters at once
				yield return new CodeAction(context.TranslateString("Add all optional parameters"),
				                            script => {

					var newInvocation = (InvocationExpression)invocationExpression.Clone();

					foreach (var parameterToAdd in missingParameters) {
						//We'll add the remaining parameters, in the order they were declared in the function
						AddArgument(newInvocation, parameterToAdd, true);
					}
					script.Replace(invocationExpression, newInvocation);

				}, invocationExpression);
			}
		}
开发者ID:0xb1dd1e,项目名称:NRefactory,代码行数:57,代码来源:AddOptionalParameterToInvocationAction.cs

示例15: 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


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