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); }); }
CodeAction ActionFromVariableInitializer(RefactoringContext context) { var initializer = context.GetNode <VariableInitializer>(); if (initializer == null || initializer.Parent.Parent is ForStatement || !initializer.NameToken.Contains(context.Location)) { return(null); } var initializerRR = context.Resolve(initializer) as LocalResolveResult; if (initializerRR == null) { return(null); } var elementType = GetElementType(initializerRR, context); if (elementType == null) { return(null); } return(new CodeAction(context.TranslateString("Iterate via foreach"), script => { var iterator = MakeForeach(new IdentifierExpression(initializer.Name), elementType, context); script.InsertAfter(context.GetNode <Statement>(), iterator); }, initializer)); }
public override System.Collections.Generic.IEnumerable <CodeAction> GetActions(RefactoringContext context) { var expression = context.GetNode(i => i is BinaryOperatorExpression || i is UnaryOperatorExpression); if (expression == null) { yield break; } var node = context.GetNode(); if (node == null || !(node is PrimitiveExpression) && node.StartLocation != context.Location) { yield break; } var rr = context.Resolve(expression); if (rr.ConstantValue == null) { yield break; } yield return(new CodeAction( context.TranslateString("Compute constant value"), script => script.Replace(expression, new PrimitiveExpression(rr.ConstantValue)), node )); }
public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { var simpleType = context.GetNode <SimpleType>(); if (simpleType != null && !(simpleType.Parent is EventDeclaration || simpleType.Parent is CustomEventDeclaration)) { return(GetActions(context, simpleType)); } var createExpression = context.GetNode <ObjectCreateExpression>(); if (createExpression != null) { return(GetActions(context, createExpression)); } var identifier = context.GetNode <IdentifierExpression>(); if (identifier != null && (identifier.Parent is MemberReferenceExpression)) { return(GetActions(context, identifier)); } return(Enumerable.Empty <CodeAction>()); }
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); } }
public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { Expression node = context.GetNode <IdentifierExpression>(); if (node == null) { var mr = context.GetNode <MemberReferenceExpression>(); if (mr == null || !mr.MemberNameToken.IsInside(context.Location)) { yield break; } node = mr; } if (node == null) { yield break; } var rr = context.Resolve(node) as MethodGroupResolveResult; if (rr == null || rr.IsError) { yield break; } var type = TypeGuessing.GetValidTypes(context.Resolver, node).FirstOrDefault(t => t.Kind == TypeKind.Delegate); if (type == null) { yield break; } var invocationMethod = type.GetDelegateInvokeMethod(); if (invocationMethod == null) { yield break; } yield return(new CodeAction( context.TranslateString("Convert to anonymous method"), script => { var expr = new InvocationExpression(node.Clone(), invocationMethod.Parameters.Select(p => new IdentifierExpression(context.GetNameProposal(p.Name)))); var stmt = invocationMethod.ReturnType.IsKnownType(KnownTypeCode.Void) ? (Statement)expr : new ReturnStatement(expr); var anonymousMethod = new AnonymousMethodExpression { Body = new BlockStatement { stmt } }; foreach (var p in invocationMethod.Parameters) { var decl = new ParameterDeclaration(context.CreateShortType(p.Type), context.GetNameProposal(p.Name)); anonymousMethod.Parameters.Add(decl); } script.Replace(node, anonymousMethod); }, node )); }
public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { // lambda var lambda = context.GetNode <LambdaExpression> (); if (lambda != null && lambda.ArrowToken.Contains(context.Location)) { if (ContainsLocalReferences(context, lambda, lambda.Body)) { yield break; } bool noReturn = false; BlockStatement body; if (lambda.Body is BlockStatement) { body = (BlockStatement)lambda.Body.Clone(); } else { if (!(lambda.Body is Expression)) { yield break; } body = new BlockStatement(); var type = LambdaHelper.GetLambdaReturnType(context, lambda); if (type == null || type.ReflectionName == "System.Void") { noReturn = true; body.Add((Expression)lambda.Body.Clone()); } else { body.Add(new ReturnStatement((Expression)lambda.Body.Clone())); } } var method = GetMethod(context, (LambdaResolveResult)context.Resolve(lambda), body, noReturn); yield return(GetAction(context, lambda, method)); } // anonymous method var anonymousMethod = context.GetNode <AnonymousMethodExpression> (); if (anonymousMethod != null && anonymousMethod.DelegateToken.Contains(context.Location)) { if (ContainsLocalReferences(context, anonymousMethod, anonymousMethod.Body)) { yield break; } var method = GetMethod(context, (LambdaResolveResult)context.Resolve(anonymousMethod), (BlockStatement)anonymousMethod.Body.Clone()); yield return(GetAction(context, anonymousMethod, method)); } }
public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { Expression node = context.GetNode <IdentifierExpression>(); if (node == null) { var mr = context.GetNode <MemberReferenceExpression>(); if (mr == null || !mr.MemberNameToken.IsInside(context.Location)) { yield break; } node = mr; } if (node == null) { yield break; } var rr = context.Resolve(node) as MethodGroupResolveResult; if (rr == null || rr.IsError) { yield break; } var type = TypeGuessing.GetValidTypes(context.Resolver, node).FirstOrDefault(t => t.Kind == TypeKind.Delegate); if (type == null) { yield break; } var invocationMethod = type.GetDelegateInvokeMethod(); if (invocationMethod == null) { yield break; } yield return(new CodeAction( context.TranslateString("Convert to lambda expression"), script => { var invocation = new InvocationExpression(node.Clone(), invocationMethod.Parameters.Select(p => new IdentifierExpression(context.GetNameProposal(p.Name)))); var lambda = new LambdaExpression { Body = invocation }; lambda.Parameters.AddRange( invocation.Arguments .Cast <IdentifierExpression>() .Select(p => new ParameterDeclaration { Name = p.Identifier }) ); script.Replace(node, lambda); }, node )); }
public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { var identifier = context.GetNode <IdentifierExpression>(); if (identifier == null) { yield break; } if (CreateFieldAction.IsInvocationTarget(identifier)) { yield break; } var statement = context.GetNode <Statement>(); if (statement == null) { yield break; } if (!(context.Resolve(identifier).IsError)) { yield break; } var guessedType = TypeGuessing.GuessAstType(context, identifier); if (guessedType == null) { yield break; } yield return(new CodeAction(context.TranslateString("Create local variable"), script => { var initializer = new VariableInitializer(identifier.Identifier); var decl = new VariableDeclarationStatement() { Type = guessedType, Variables = { initializer } }; if (identifier.Parent is AssignmentExpression && ((AssignmentExpression)identifier.Parent).Left == identifier) { initializer.Initializer = ((AssignmentExpression)identifier.Parent).Right.Clone(); if (!context.UseExplicitTypes) { decl.Type = new SimpleType("var"); } script.Replace(statement, decl); } else { script.InsertBefore(statement, decl); } }, identifier) { Severity = ICSharpCode.NRefactory.Refactoring.Severity.Error }); }
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>(); }
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); }); }
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>(); }
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; } }
static PrimitiveExpression GetEmptyString(RefactoringContext context) { var node = context.GetNode<PrimitiveExpression> (); if (node == null || !(node.Value is string) || node.Value.ToString () != "") return null; return node; }
static VariableDeclarationStatement GetVariableDeclarationStatement (RefactoringContext context) { var result = context.GetNode<VariableDeclarationStatement> (); if (result != null && result.Variables.Count == 1 && !result.Variables.First ().Initializer.IsNull && result.Type.Contains (context.Location.Line, context.Location.Column) && !result.Type.IsMatch (new SimpleType ("var"))) return result; return null; }
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 }; }
public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { if (context.IsSomethingSelected) { yield break; } var node = context.GetNode <VariableDeclarationStatement>(); if (node == null || node.Variables.Count != 1) { yield break; } var initializer = node.Variables.First(); if (!initializer.NameToken.Contains(context.Location) || initializer.Initializer.IsNull) { yield break; } var resolveResult = context.Resolve(initializer) as LocalResolveResult; if (resolveResult == null || resolveResult.IsError) { yield break; } var unit = context.RootNode as SyntaxTree; if (unit == null) { yield break; } yield return(new CodeAction(context.TranslateString("Inline local variable"), script => { refFinder.FindLocalReferences(resolveResult.Variable, context.UnresolvedFile, unit, context.Compilation, (n, r) => script.Replace(n, AddParensIfRequired(n, initializer.Initializer.Clone())), default(CancellationToken)); script.Remove(node); }, initializer)); }
CodeAction HandleExpressionStatement(RefactoringContext context, ExpressionStatement expressionStatement) { var expression = expressionStatement.Expression as AssignmentExpression; if (expression == null || expression.Operator != AssignmentOperatorType.Assign) { return(null); } if (!(expression.Right is ObjectCreateExpression)) { return(null); } var expressionResolveResult = context.Resolve(expression.Left); if (!(expressionResolveResult is LocalResolveResult) && !(expressionResolveResult is MemberResolveResult)) { return(null); } IList <AstNode> statements = GetNodes(context.GetNode <Statement>()); var converter = new StatementsToInitializerConverter(context); var newExpression = converter.ConvertToInitializer(expression, ref statements); if (newExpression == null || statements.Count == 0) { return(null); } return(MakeAction(context, expression, newExpression, statements)); }
public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { var ifStatement = context.GetNode <IfElseStatement>(); if (ifStatement == null) { yield break; } var bOp = ifStatement.GetNodeAt <BinaryOperatorExpression>(context.Location); if (bOp == null || !bOp.OperatorToken.Contains(context.Location)) { yield break; } if (bOp.Ancestors.OfType <BinaryOperatorExpression>().Any(b => b.Operator != bOp.Operator)) { yield break; } if (bOp.Operator == BinaryOperatorType.ConditionalAnd) { yield return(CreateAndSplit(context, ifStatement, bOp)); } else if (bOp.Operator == BinaryOperatorType.ConditionalOr) { yield return(CreateOrSplit(context, ifStatement, bOp)); } }
public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { var method = context.GetNode <MethodDeclaration>(); if (method == null || !method.NameToken.Contains(context.Location)) { yield break; } if (method.HasModifier(Modifiers.Static)) { yield break; } var param = method.Parameters.FirstOrDefault(); if (param == null || param.ParameterModifier != ParameterModifier.This) { yield break; } yield return(new CodeAction( context.TranslateString("Convert method to static"), script => script.ChangeModifier(method, method.Modifiers | Modifiers.Static), method) { Severity = ICSharpCode.NRefactory.Refactoring.Severity.Error }); }
public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { AstNode node = context.GetNode(); if (node is Identifier) { node = node.Parent; } if (node is SimpleType || node is IdentifierExpression) { return(GetActionsForType(context, node) .Concat(GetActionsForAddNamespaceUsing(context, node))); } else if (node is MemberReferenceExpression && node.Parent is InvocationExpression) { return(GetActionsForExtensionMethodInvocation(context, (InvocationExpression)node.Parent)); } else if (node is MemberReferenceExpression) { return(GetActionsForAddNamespaceUsing(context, node)); } else { return(EmptyList <CodeAction> .Instance); } }
public override 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 - 2) && new TextLocation(context.Location.Line, context.Location.Column + 2) < pexpr.EndLocation)) { yield break; } } else { 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; } } yield return(new CodeAction(context.TranslateString("Split string literal"), script => { int offset = context.GetOffset(context.Location); script.InsertText(offset, pexpr.LiteralValue.StartsWith("@", StringComparison.Ordinal) ? "\" + @\"" : "\" + \""); }, pexpr)); }
static PreProcessorDirective GetDirective(RefactoringContext context) { var directive = context.GetNode<PreProcessorDirective> (); if (directive == null || directive.Type != PreProcessorDirectiveType.Region) return null; return directive; }
public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { var catchClause = context.GetNode <CatchClause>(); if (catchClause == null) { yield break; } if (!catchClause.Type.IsNull) { yield break; } yield return(new CodeAction(context.TranslateString("Add type specifier"), script => { var newType = context.CreateShortType("System", "Exception"); var namingHelper = new NamingHelper(context); var newIdentifier = Identifier.Create(namingHelper.GenerateVariableName(newType, "e")); script.Replace(catchClause, new CatchClause { Type = newType, VariableNameToken = newIdentifier, Body = catchClause.Body.Clone() as BlockStatement }); script.Select(newType); }, catchClause)); }
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); }
public override 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 - 2) && new TextLocation(context.Location.Line, context.Location.Column + 2) < pexpr.EndLocation)) { yield break; } } else { 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; } } yield return new CodeAction(context.TranslateString("Split string literal"), script => { int offset = context.GetOffset (context.Location); script.InsertText (offset, pexpr.LiteralValue.StartsWith("@", StringComparison.Ordinal) ? "\" + @\"" : "\" + \""); }, pexpr); }
static AnonymousMethodExpression GetAnonymousMethodExpression (RefactoringContext context, out ITypeDefinition delegateType) { delegateType = null; var anonymousMethodExpression = context.GetNode<AnonymousMethodExpression> (); if (anonymousMethodExpression == null || !anonymousMethodExpression.DelegateToken.Contains (context.Location.Line, context.Location.Column) || anonymousMethodExpression.HasParameterList) return null; AstType resolvedType = null; var parent = anonymousMethodExpression.Parent; if (parent is AssignmentExpression) { resolvedType = context.ResolveType (((AssignmentExpression)parent).Left); } else if (parent is VariableDeclarationStatement) { resolvedType = context.ResolveType (((VariableDeclarationStatement)parent).Type); } else if (parent is InvocationExpression) { // TODO: handle invocations } if (resolvedType == null) return null; delegateType = context.GetDefinition (resolvedType); if (delegateType == null || delegateType.ClassType != ClassType.Delegate) return null; return anonymousMethodExpression; }
public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { string keyword; Statement embeddedStatement; // BlockStatement block; var curNode = context.GetNode(); if (!RemoveBracesAction.IsSpecialNode(curNode, out keyword, out embeddedStatement)) { yield break; } if (embeddedStatement is BlockStatement) { yield break; } yield return(new CodeAction( string.Format(context.TranslateString("Add braces to '{0}'"), keyword), script => { script.Replace(embeddedStatement, new BlockStatement { embeddedStatement.Clone() }); }, curNode )); }
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)); }); }
static IfElseStatement GetIfElseStatement (RefactoringContext context) { var result = context.GetNode<IfElseStatement> (); if (result != null && result.IfToken.Contains (context.Location)) return result; return null; }
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 ); }
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); }
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); }
public override IEnumerable<CodeAction> GetActions(RefactoringContext context) { if (context.IsSomethingSelected) { yield break; } var node = context.GetNode<VariableDeclarationStatement>(); if (node == null || node.Variables.Count != 1) { yield break; } var initializer = node.Variables.First(); if (!initializer.NameToken.Contains(context.Location) || initializer.Initializer.IsNull) { yield break; } var resolveResult = context.Resolve(initializer) as LocalResolveResult; if (resolveResult == null || resolveResult.IsError) { yield break; } var unit = context.RootNode as SyntaxTree; if (unit == null) { yield break; } yield return new CodeAction(context.TranslateString("Inline local variable"), script => { refFinder.FindLocalReferences(resolveResult.Variable, context.UnresolvedFile, unit, context.Compilation, (n, r) => script.Replace(n, AddParensIfRequired (n, initializer.Initializer.Clone())), default(CancellationToken)); script.Remove(node); }, initializer); }
static IfElseStatement GetIfElseStatement(RefactoringContext context) { var result = context.GetNode<IfElseStatement>(); if (result == null) return null; if (!(result.IfToken.Contains(context.Location) && !result.TrueStatement.IsNull && result.FalseStatement.IsNull)) return null; if (!(result.Parent is BlockStatement)) return null; var condition = (result.Parent.Parent is WhileStatement) || (result.Parent.Parent is ForeachStatement) || (result.Parent.Parent is ForStatement); if (!condition) return null; var nextSibling = result.GetNextSibling(n => n is Statement); if (nextSibling == null) return result; nextSibling = nextSibling.GetNextSibling (n => n is Statement); if (nextSibling != null) return null; return result; }
static PropertyDeclaration GetPropertyDeclaration (RefactoringContext context) { var node = context.GetNode (); if (node == null) return null; return node.Parent as PropertyDeclaration; }
public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { var invocation = context.GetNode <InvocationExpression>(); if (invocation == null) { yield break; } var memberReference = invocation.Target as MemberReferenceExpression; if (memberReference == null) { yield break; } var invocationRR = context.Resolve(invocation) as AlInvocationResolveResult; if (invocationRR == null) { yield break; } if (invocationRR.IsExtensionMethodInvocation) { yield return(new CodeAction(context.TranslateString("Convert to static method call"), script => { script.Replace(invocation, ToStaticMethodInvocation(invocation, memberReference, invocationRR)); }, invocation)); } }
static VariableDeclarationStatement GetVariableDeclarationStatement (RefactoringContext context) { var result = context.GetNode<VariableDeclarationStatement> (); if (result != null && result.Variables.Count == 1 && !result.Variables.First ().Initializer.IsNull && result.Type.Contains (context.Location) && !result.Type.IsVar ()) return result; return null; }
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; }
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); }
static AnonymousMethodExpression GetAnonymousMethodExpression (RefactoringContext context, out IType delegateType) { delegateType = null; var anonymousMethodExpression = context.GetNode<AnonymousMethodExpression> (); if (anonymousMethodExpression == null || !anonymousMethodExpression.DelegateToken.Contains (context.Location) || anonymousMethodExpression.HasParameterList) return null; IType resolvedType = null; var parent = anonymousMethodExpression.Parent; if (parent is AssignmentExpression) { resolvedType = context.Resolve (((AssignmentExpression)parent).Left).Type; } else if (parent is VariableInitializer) { resolvedType = context.Resolve (((VariableDeclarationStatement)parent.Parent).Type).Type; } else if (parent is InvocationExpression) { // TODO: handle invocations } if (resolvedType == null) return null; delegateType = resolvedType; if (delegateType.Kind != TypeKind.Delegate) return null; return anonymousMethodExpression; }
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))) ); }); }
static SwitchStatement GetSwitchStatement (RefactoringContext context) { var switchStatment = context.GetNode<SwitchStatement> (); if (switchStatment != null && switchStatment.SwitchSections.Count == 0) return switchStatment; return null; }
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); } }); }
static PrimitiveExpression GetEmptyString (RefactoringContext context) { var node = context.GetNode<PrimitiveExpression> (); if (node == null || !(node.Value is string) || node.Value.ToString () != "") return null; return node; }
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); }
public override IEnumerable<CodeAction> GetActions(RefactoringContext context) { var property = context.GetNode<PropertyDeclaration>(); if (property == null || !property.NameToken.Contains(context.Location)) yield break; var field = GetBackingField(context, property); if (!IsValidField(field, property.GetParent<TypeDeclaration>())) { yield break; } // create new auto property var newProperty = (PropertyDeclaration)property.Clone(); newProperty.Getter.Body = BlockStatement.Null; newProperty.Setter.Body = BlockStatement.Null; yield return new CodeAction(context.TranslateString("Convert to auto property"), script => { script.Rename((IEntity)field, newProperty.Name); var oldField = context.RootNode.GetNodeAt<FieldDeclaration>(field.Region.Begin); if (oldField.Variables.Count == 1) { script.Remove(oldField); } else { var newField = (FieldDeclaration)oldField.Clone(); foreach (var init in newField.Variables) { if (init.Name == field.Name) { init.Remove(); break; } } script.Replace(oldField, newField); } script.Replace (property, newProperty); }, property.NameToken); }
public void Run(RefactoringContext context) { var pexpr = context.GetNode <PrimitiveExpression> (); using (var script = context.StartScript()) { script.Replace(pexpr, new PrimitiveExpression(pexpr.Value, string.Format("0x{0:x}", pexpr.Value))); } }
public override 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); }, pexpr)); }
public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { var property = context.GetNode <PropertyDeclaration>(); if (property == null || !property.NameToken.Contains(context.Location)) { yield break; } if (!(!property.Getter.IsNull && !property.Setter.IsNull && // automatic properties always need getter & setter property.Getter.Body.IsNull && property.Setter.Body.IsNull)) { yield break; } yield return(new CodeAction(context.TranslateString("Create backing store"), script => { string backingStoreName = context.GetNameProposal(property.Name); // create field var backingStore = new FieldDeclaration(); if (property.Modifiers.HasFlag(Modifiers.Static)) { backingStore.Modifiers |= Modifiers.Static; } backingStore.ReturnType = property.ReturnType.Clone(); var initializer = new VariableInitializer(backingStoreName); backingStore.Variables.Add(initializer); // create new property & implement the get/set bodies var newProperty = (PropertyDeclaration)property.Clone(); Expression id1; if (backingStoreName == "value") { id1 = new ThisReferenceExpression().Member("value"); } else { id1 = new IdentifierExpression(backingStoreName); } Expression id2 = id1.Clone(); newProperty.Getter.Body = new BlockStatement() { new ReturnStatement(id1) }; newProperty.Setter.Body = new BlockStatement() { new AssignmentExpression(id2, AssignmentOperatorType.Assign, new IdentifierExpression("value")) }; script.Replace(property, newProperty); script.InsertBefore(property, backingStore); script.Link(initializer, id1, id2); }, property.NameToken)); }
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); }
static PropertyDeclaration GetPropertyDeclaration(RefactoringContext context) { var node = context.GetNode(); if (node == null) { return(null); } return(node.Parent as PropertyDeclaration); }
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)); } }
static VariableDeclarationStatement GetVariableDeclarationStatement(RefactoringContext context) { var result = context.GetNode <VariableDeclarationStatement> (); if (result != null && result.Variables.Count == 1 && !result.Variables.First().Initializer.IsNull&& result.Type.Contains(context.Location) && !result.Type.IsVar()) { return(result); } return(null); }
static PreProcessorDirective GetDirective(RefactoringContext context) { var directive = context.GetNode <PreProcessorDirective> (); if (directive == null || directive.Type != PreProcessorDirectiveType.Region && directive.Type != PreProcessorDirectiveType.Endregion) { return(null); } return(directive); }
static IfElseStatement GetIfElseStatement(RefactoringContext context) { var result = context.GetNode <IfElseStatement> (); if (result != null && result.IfToken.Contains(context.Location)) { return(result); } return(null); }
public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { var simpleType = context.GetNode <SimpleType>(); if (simpleType != null && (simpleType.Parent is EventDeclaration || simpleType.Parent is CustomEventDeclaration)) { return(GetActions(context, simpleType)); } return(Enumerable.Empty <CodeAction>()); }
static ForeachStatement GetForeachStatement(RefactoringContext context) { var foreachStatement = context.GetNode <ForeachStatement>(); if (foreachStatement == null || !foreachStatement.ForeachToken.Contains(context.Location)) { return(null); } return(foreachStatement); }
public bool IsValid(RefactoringContext context) { var pexpr = context.GetNode <PrimitiveExpression> (); if (pexpr == null || pexpr.LiteralValue.StartsWith("0X", System.StringComparison.OrdinalIgnoreCase)) { return(false); } return((pexpr.Value is int) || (pexpr.Value is long) || (pexpr.Value is short) || (pexpr.Value is sbyte) || (pexpr.Value is uint) || (pexpr.Value is ulong) || (pexpr.Value is ushort) || (pexpr.Value is byte)); }