public void Run(RefactoringContext context) { var property = context.GetNode <PropertyDeclaration> (); string backingStoreName = context.GetNameProposal(property.Name); // create field var backingStore = new FieldDeclaration(); 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(); var id1 = new IdentifierExpression(backingStoreName); var id2 = new IdentifierExpression(backingStoreName); newProperty.Getter.Body = new BlockStatement() { new ReturnStatement(id1) }; newProperty.Setter.Body = new BlockStatement() { new ExpressionStatement(new AssignmentExpression(id2, AssignmentOperatorType.Assign, new IdentifierExpression("value"))) }; using (var script = context.StartScript()) { script.Replace(property, newProperty); script.InsertBefore(property, backingStore); script.Link(initializer, id1, id2); } }
// void ReplaceBackingFieldReferences (MDRefactoringContext context, IField backingStore, PropertyDeclaration property) // { // using (var monitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor (true, true)) { // foreach (var memberRef in MonoDevelop.Ide.FindInFiles.ReferenceFinder.FindReferences (backingStore, monitor)) { // if (property.Contains (memberRef.Line, memberRef.Column)) // continue; // if (backingStore.Location.Line == memberRef.Line && backingStore.Location.Column == memberRef.Column) // continue; // context.Do (new TextReplaceChange () { // FileName = memberRef.FileName, // Offset = memberRef.Position, // RemovedChars = memberRef.Name.Length, // InsertedText = property.Name // }); // } // } // } // static IField GetBackingField(RefactoringContext context) { var propertyDeclaration = context.GetNode <PropertyDeclaration> (); // automatic properties always need getter & setter if (propertyDeclaration == null || propertyDeclaration.Getter.IsNull || propertyDeclaration.Setter.IsNull || propertyDeclaration.Getter.Body.IsNull || propertyDeclaration.Setter.Body.IsNull) { return(null); } if (!context.HasCSharp3Support || propertyDeclaration.HasModifier(ICSharpCode.NRefactory.Cpp.Modifiers.Abstract) || ((TypeDeclaration)propertyDeclaration.Parent).ClassType == ClassType.Interface) { return(null); } var getterField = ScanGetter(context, propertyDeclaration); if (getterField == null) { return(null); } var setterField = ScanSetter(context, propertyDeclaration); if (setterField == null) { return(null); } if (getterField.Region != setterField.Region) { return(null); } return(getterField); }
public void Run(RefactoringContext context) { var pexpr = context.GetNode <PrimitiveExpression> (); int offset = context.GetOffset(context.Location); using (var script = context.StartScript()) { script.InsertText(offset, pexpr.LiteralValue.StartsWith("@") ? "\" + @\"" : "\" + \""); } }
public bool IsValid(RefactoringContext context) { var propertyDeclaration = context.GetNode <PropertyDeclaration> (); return(propertyDeclaration != null && !propertyDeclaration.Getter.IsNull && !propertyDeclaration.Setter.IsNull && // automatic properties always need getter & setter propertyDeclaration.Getter.Body.IsNull && propertyDeclaration.Setter.Body.IsNull); }
static ForeachStatement GetForeachStatement(RefactoringContext context) { var result = context.GetNode <ForeachStatement> (); if (result != null && result.VariableType.Contains(context.Location) && !result.VariableType.IsMatch(varType)) { return(result); } return(null); }
static IfElseStatement GetIfElseStatement(RefactoringContext context) { var result = context.GetNode <IfElseStatement> (); if (result != null && result.IfToken.Contains(context.Location)) { return(result); } return(null); }
static SwitchStatement GetSwitchStatement(RefactoringContext context) { var switchStatment = context.GetNode <SwitchStatement> (); if (switchStatment != null && switchStatment.SwitchSections.Count == 0) { return(switchStatment); } return(null); }
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) && !result.Type.IsMatch(varType)) { return(result); } return(null); }
static EventDeclaration GetEventDeclaration(RefactoringContext context, out VariableInitializer initializer) { var result = context.GetNode <EventDeclaration> (); if (result == null) { initializer = null; return(null); } initializer = result.Variables.First(v => v.NameToken.Contains(context.Location)); return(initializer != null ? result : null); }
public void Run(RefactoringContext context) { var pexpr = context.GetNode <PrimitiveExpression> (); var invocation = context.GetNode <InvocationExpression> (); if (invocation != null && invocation.Target.IsMatch(PrototypeFormatReference)) { AddFormatCallToInvocation(context, pexpr, invocation); return; } var arg = CreateFormatArgument(context); var newInvocation = new InvocationExpression(PrototypeFormatReference.Clone()) { Arguments = { CreateFormatString(context, pexpr, 0), arg } }; using (var script = context.StartScript()) { script.Replace(pexpr, newInvocation); script.Select(arg); } }
static VariableDeclarationStatement GetVariableDeclarationStatement(RefactoringContext context, out AstType resolvedType) { var result = context.GetNode <VariableDeclarationStatement> (); if (result != null && result.Variables.Count == 1 && !result.Variables.First().Initializer.IsNull&& result.Variables.First().NameToken.Contains(context.Location.Line, context.Location.Column)) { resolvedType = result.Type.Clone(); // resolvedType = context.Resolve (result.Variables.First ().Initializer).Type.ConvertToAstType (); // if (resolvedType == null) // return null; return(result); } resolvedType = null; return(null); }
public static BinaryOperatorExpression GetBinaryOperatorExpression(RefactoringContext context) { var node = context.GetNode <BinaryOperatorExpression> (); if (node == null || !node.OperatorToken.Contains(context.Location)) { return(null); } var result = node as BinaryOperatorExpression; if (result == null || (result.Operator != BinaryOperatorType.Equality && result.Operator != BinaryOperatorType.InEquality)) { return(null); } return(result); }
static ForeachStatement GetForeachStatement(RefactoringContext context) { var astNode = context.GetNode(); if (astNode == null) { return(null); } var result = (astNode as ForeachStatement) ?? astNode.Parent as ForeachStatement; if (result == null || context.Resolve(result.InExpression) == null) { return(null); } return(result); }
public bool IsValid(RefactoringContext context) { if (GetUnresolvedArguments(context).Count > 0) { return(true); } var identifier = CreateField.GetIdentifier(context); if (identifier == null) { return(false); } if (context.GetNode <Statement> () == null) { return(false); } return(context.Resolve(identifier) == null && GuessType(context, identifier) != null); }
static BlockStatement GetBlockStatement(RefactoringContext context) { var block = context.GetNode <BlockStatement> (); if (block == null || block.LBraceToken.IsNull || block.RBraceToken.IsNull) { return(null); } if (!(block.Parent is Statement)) { return(null); } if (block.Statements.Count() != 1) { return(null); } return(block); }
public void Run(RefactoringContext context) { var property = context.GetNode <PropertyDeclaration> (); var field = GetBackingField(context); context.ReplaceReferences(field, property); // create new auto property var newProperty = (PropertyDeclaration)property.Clone(); newProperty.Getter.Body = BlockStatement.Null; newProperty.Setter.Body = BlockStatement.Null; using (var script = context.StartScript()) { script.Remove(context.Unit.GetNodeAt <FieldDeclaration> (field.Region.BeginLine, field.Region.BeginColumn)); script.Replace(property, newProperty); } }
public bool IsValid(RefactoringContext context) { if (!context.IsSomethingSelected) { return(false); } var pexpr = context.GetNode <PrimitiveExpression> (); if (pexpr == null || !(pexpr.Value is string)) { return(false); } if (pexpr.LiteralValue.StartsWith("@")) { return(pexpr.StartLocation < new TextLocation(context.Location.Line, context.Location.Column - 1) && new TextLocation(context.Location.Line, context.Location.Column + 1) < pexpr.EndLocation); } return(pexpr.StartLocation < context.Location && context.Location < pexpr.EndLocation); }
public void Run(RefactoringContext context) { var stmt = context.GetNode <Statement> (); var unresolvedArguments = GetUnresolvedArguments(context); if (unresolvedArguments.Count > 0) { using (var script = context.StartScript()) { foreach (var id in unresolvedArguments) { script.InsertBefore(stmt, GenerateLocalVariableDeclaration(context, id)); } } return; } using (var script = context.StartScript()) { script.InsertBefore(stmt, GenerateLocalVariableDeclaration(context, CreateField.GetIdentifier(context))); } }
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); }
VariableInitializer GetVariableInitializer(RefactoringContext context) { return(context.GetNode <VariableInitializer> ()); }
IdentifierExpression GetIdentifier(RefactoringContext context) { return(context.GetNode <IdentifierExpression> ()); }
InvocationExpression GetInvocation(RefactoringContext context) { return(context.GetNode <InvocationExpression> ()); }
static ParameterDeclaration GetParameterDeclaration(RefactoringContext context) { return(context.GetNode <ICSharpCode.NRefactory.Cpp.ParameterDeclaration> ()); }