34 changed files with 2256 additions and 51 deletions
@ -0,0 +1,92 @@
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// AddAnotherAccessor.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// Add another accessor to a property declaration that has only one.
|
||||
/// </summary>
|
||||
public class AddAnotherAccessor : IContextAction |
||||
{ |
||||
public bool IsValid (RefactoringContext context) |
||||
{ |
||||
var pdecl = GetPropertyDeclaration (context); |
||||
if (pdecl == null) |
||||
return false; |
||||
var type = pdecl.Parent as TypeDeclaration; |
||||
if (type != null && type.ClassType == ICSharpCode.NRefactory.TypeSystem.ClassType.Interface) |
||||
return false; |
||||
|
||||
return pdecl.Setter.IsNull || pdecl.Getter.IsNull; |
||||
} |
||||
|
||||
public void Run (RefactoringContext context) |
||||
{ |
||||
var pdecl = GetPropertyDeclaration (context); |
||||
var accessorStatement = BuildAccessorStatement (context, pdecl); |
||||
|
||||
Accessor accessor = new Accessor () { |
||||
Body = new BlockStatement { accessorStatement } |
||||
}; |
||||
|
||||
pdecl.AddChild (accessor, pdecl.Setter.IsNull ? PropertyDeclaration.SetterRole : PropertyDeclaration.GetterRole); |
||||
|
||||
var offset = context.GetOffset (pdecl.RBraceToken.StartLocation) - 1; |
||||
|
||||
using (var script = context.StartScript ()) { |
||||
script.Select (accessorStatement); |
||||
script.Insert (offset, accessor); |
||||
script.FormatText (ctx => GetPropertyDeclaration (context)); |
||||
} |
||||
} |
||||
|
||||
static Statement BuildAccessorStatement (RefactoringContext context, PropertyDeclaration pdecl) |
||||
{ |
||||
// TODO:
|
||||
// if (pdecl.Setter.IsNull && !pdecl.Getter.IsNull) {
|
||||
// var field = RemoveBackingStore.ScanGetter (context, pdecl);
|
||||
// if (field != null)
|
||||
// return new ExpressionStatement (new AssignmentExpression (new IdentifierExpression (field.Name), AssignmentOperatorType.Assign, new IdentifierExpression ("value")));
|
||||
// }
|
||||
//
|
||||
// if (!pdecl.Setter.IsNull && pdecl.Getter.IsNull) {
|
||||
// var field = RemoveBackingStore.ScanSetter (context, pdecl);
|
||||
// if (field != null)
|
||||
// return new ReturnStatement (new IdentifierExpression (field.Name));
|
||||
// }
|
||||
|
||||
return new ThrowStatement (new ObjectCreateExpression (context.CreateShortType ("System.NotImplementedException"))); |
||||
} |
||||
|
||||
static PropertyDeclaration GetPropertyDeclaration (RefactoringContext context) |
||||
{ |
||||
var node = context.GetNode (); |
||||
if (node == null) |
||||
return null; |
||||
return node.Parent as PropertyDeclaration; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,114 @@
@@ -0,0 +1,114 @@
|
||||
//
|
||||
// CheckIfParameterIsNull.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using ICSharpCode.NRefactory.PatternMatching; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// Creates a 'if (param == null) throw new System.ArgumentNullException ();' contruct for a parameter.
|
||||
/// </summary>
|
||||
public class CheckIfParameterIsNull : IContextAction |
||||
{ |
||||
//TODO: Create 'multiple' null checks when more than 1 parameter is selected.
|
||||
public bool IsValid (RefactoringContext context) |
||||
{ |
||||
var parameter = GetParameterDeclaration (context); |
||||
if (parameter == null) |
||||
return false; |
||||
|
||||
var bodyStatement = parameter.Parent.GetChildByRole (AstNode.Roles.Body); |
||||
|
||||
if (bodyStatement == null) |
||||
return false; |
||||
|
||||
if (parameter.Type is PrimitiveType) |
||||
return (((PrimitiveType)parameter.Type).Keyword == "object" || ((PrimitiveType)parameter.Type).Keyword == "string") && !HasNullCheck (parameter); |
||||
|
||||
// TODO: check for structs
|
||||
return !HasNullCheck (parameter); |
||||
} |
||||
|
||||
public void Run (RefactoringContext context) |
||||
{ |
||||
var parameter = GetParameterDeclaration (context); |
||||
|
||||
var bodyStatement = parameter.Parent.GetChildByRole (AstNode.Roles.Body); |
||||
|
||||
var statement = new IfElseStatement () { |
||||
Condition = new BinaryOperatorExpression (new IdentifierExpression (parameter.Name), BinaryOperatorType.Equality, new NullReferenceExpression ()), |
||||
TrueStatement = new ThrowStatement (new ObjectCreateExpression (context.CreateShortType ("System.ArgumentNullException"), new PrimitiveExpression (parameter.Name))) |
||||
}; |
||||
|
||||
var offset = context.GetOffset (bodyStatement.StartLocation) + 1; |
||||
|
||||
using (var script = context.StartScript ()) { |
||||
script.Insert (offset, statement); |
||||
} |
||||
} |
||||
|
||||
static ParameterDeclaration GetParameterDeclaration (RefactoringContext context) |
||||
{ |
||||
return context.GetNode<ICSharpCode.NRefactory.CSharp.ParameterDeclaration> (); |
||||
} |
||||
|
||||
static bool HasNullCheck (ParameterDeclaration parameter) |
||||
{ |
||||
var visitor = new CheckNullVisitor (parameter); |
||||
parameter.Parent.AcceptVisitor (visitor, null); |
||||
return visitor.ContainsNullCheck; |
||||
} |
||||
|
||||
class CheckNullVisitor : DepthFirstAstVisitor<object, object> |
||||
{ |
||||
ParameterDeclaration parameter; |
||||
|
||||
public bool ContainsNullCheck { |
||||
get; |
||||
set; |
||||
} |
||||
|
||||
public CheckNullVisitor (ParameterDeclaration parameter) |
||||
{ |
||||
this.parameter = parameter; |
||||
} |
||||
|
||||
public object VisitIfElseStatement (IfElseStatement ifElseStatement, object data) |
||||
{ |
||||
if (ifElseStatement.Condition is BinaryOperatorExpression) { |
||||
var binOp = ifElseStatement.Condition as BinaryOperatorExpression; |
||||
if ((binOp.Operator == BinaryOperatorType.Equality || binOp.Operator == BinaryOperatorType.InEquality) && |
||||
binOp.Left.IsMatch (new IdentifierExpression (parameter.Name)) && binOp.Right.IsMatch (new NullReferenceExpression ()) || |
||||
binOp.Right.IsMatch (new IdentifierExpression (parameter.Name)) && binOp.Left.IsMatch (new NullReferenceExpression ())) { |
||||
ContainsNullCheck = true; |
||||
} |
||||
} |
||||
|
||||
return base.VisitIfElseStatement (ifElseStatement, data); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// ConvertDecToHex.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// Convert a dec numer to hex. For example: 16 -> 0x10
|
||||
/// </summary>
|
||||
public class ConvertDecToHex : IContextAction |
||||
{ |
||||
public bool IsValid (RefactoringContext context) |
||||
{ |
||||
var pexpr = context.GetNode<PrimitiveExpression> (); |
||||
if (pexpr == null || pexpr.LiteralValue.ToUpper ().StartsWith ("0X")) |
||||
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); |
||||
} |
||||
|
||||
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))); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,108 @@
@@ -0,0 +1,108 @@
|
||||
//
|
||||
// ConvertForeachToFor.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// Converts a foreach loop to for.
|
||||
/// </summary>
|
||||
public class ConvertForeachToFor : IContextAction |
||||
{ |
||||
public bool IsValid (RefactoringContext context) |
||||
{ |
||||
return GetForeachStatement (context) != null; |
||||
} |
||||
|
||||
public void Run (RefactoringContext context) |
||||
{ // TODO: Missing resolver!
|
||||
|
||||
// var foreachStatement = GetForeachStatement (context);
|
||||
//
|
||||
// var resolver = context.Resolver;
|
||||
//
|
||||
// var result = resolver.Resolve (foreachStatement.InExpression.ToString (), new DomLocation (foreachStatement.InExpression.StartLocation.Line, foreachStatement.InExpression.StartLocation.Column));
|
||||
// string itemNumberProperty = "Count";
|
||||
//
|
||||
// if (result != null && result.ResolvedType != null && result.ResolvedType.ArrayDimensions > 0)
|
||||
// itemNumberProperty = "Length";
|
||||
//
|
||||
// ForStatement forStatement = new ForStatement () {
|
||||
// Initializers = {
|
||||
// new VariableDeclarationStatement (new PrimitiveType ("int"), "i", new PrimitiveExpression (0))
|
||||
// },
|
||||
// Condition = new BinaryOperatorExpression (new IdentifierExpression ("i"), BinaryOperatorType.LessThan, new MemberReferenceExpression (foreachStatement.InExpression.Clone (), itemNumberProperty)),
|
||||
// Iterators = {
|
||||
// new ExpressionStatement (new UnaryOperatorExpression (UnaryOperatorType.PostIncrement, new IdentifierExpression ("i")))
|
||||
// },
|
||||
// EmbeddedStatement = new BlockStatement {
|
||||
// new VariableDeclarationStatement (foreachStatement.VariableType.Clone (), foreachStatement.VariableName, new IndexerExpression (foreachStatement.InExpression.Clone (), new IdentifierExpression ("i")))
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// var editor = context.Document.Editor;
|
||||
// var offset = editor.LocationToOffset (foreachStatement.StartLocation.Line, foreachStatement.StartLocation.Column);
|
||||
// var endOffset = editor.LocationToOffset (foreachStatement.EndLocation.Line, foreachStatement.EndLocation.Column);
|
||||
// var offsets = new List<int> ();
|
||||
// string lineIndent = editor.GetLineIndent (foreachStatement.Parent.StartLocation.Line);
|
||||
// string text = context.OutputNode (forStatement, context.Document.CalcIndentLevel (lineIndent) + 1, delegate(int nodeOffset, AstNode astNode) {
|
||||
// if (astNode is VariableInitializer && ((VariableInitializer)astNode).Name == "i")
|
||||
// offsets.Add (nodeOffset);
|
||||
// if (astNode is IdentifierExpression && ((IdentifierExpression)astNode).Identifier == "i") {
|
||||
// offsets.Add (nodeOffset);
|
||||
// }
|
||||
// });
|
||||
// string foreachBlockText;
|
||||
//
|
||||
// if (foreachStatement.EmbeddedStatement is BlockStatement) {
|
||||
// foreachBlockText = editor.GetTextBetween (foreachStatement.EmbeddedStatement.StartLocation.Line, foreachStatement.EmbeddedStatement.StartLocation.Column + 1,
|
||||
// foreachStatement.EmbeddedStatement.EndLocation.Line, foreachStatement.EmbeddedStatement.EndLocation.Column - 1);
|
||||
// } else {
|
||||
// foreachBlockText = editor.GetTextBetween (foreachStatement. EmbeddedStatement.StartLocation.Line, foreachStatement.EmbeddedStatement.StartLocation.Column,
|
||||
// foreachStatement.EmbeddedStatement.EndLocation.Line, foreachStatement.EmbeddedStatement.EndLocation.Column);
|
||||
// }
|
||||
// string singeleIndent = GetSingleIndent (editor);
|
||||
// string indent = lineIndent + singeleIndent;
|
||||
// foreachBlockText = indent + foreachBlockText.TrimEnd () + editor.EolMarker;
|
||||
// int i = text.LastIndexOf ('}');
|
||||
// while (i > 1 && text[i - 1] == ' ' || text[i - 1] == '\t')
|
||||
// i--;
|
||||
//
|
||||
// text = text.Insert (i, foreachBlockText).TrimEnd ();
|
||||
// string trimmedText = text.TrimStart ();
|
||||
// editor.Replace (offset, endOffset - offset + 1, trimmedText);
|
||||
// context.StartTextLinkMode (offset, "i".Length, offsets.Select (o => o - (text.Length - trimmedText.Length)));
|
||||
} |
||||
|
||||
static ForeachStatement GetForeachStatement (RefactoringContext context) |
||||
{ |
||||
var astNode = context.GetNode (); |
||||
if (astNode == null) |
||||
return null; |
||||
return (astNode as ForeachStatement) ?? astNode.Parent as ForeachStatement; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// ConvertHexToDec.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// Convert a hex numer to dec. For example: 0x10 -> 16
|
||||
/// </summary>
|
||||
public class ConvertHexToDec: IContextAction |
||||
{ |
||||
public bool IsValid (RefactoringContext context) |
||||
{ |
||||
var pexpr = context.GetNode<PrimitiveExpression> (); |
||||
if (pexpr == null || !pexpr.LiteralValue.ToUpper ().StartsWith ("0X")) |
||||
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); |
||||
} |
||||
|
||||
public void Run (RefactoringContext context) |
||||
{ |
||||
var pexpr = context.GetNode<PrimitiveExpression> (); |
||||
|
||||
using (var script = context.StartScript ()) { |
||||
script.Replace (pexpr, new PrimitiveExpression (pexpr.Value)); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,78 @@
@@ -0,0 +1,78 @@
|
||||
//
|
||||
// CreateBackingStore.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public class CreateBackingStore : IContextAction |
||||
{ |
||||
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; |
||||
} |
||||
|
||||
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); |
||||
|
||||
int offset = context.GetOffset (property.StartLocation.Line, 1); |
||||
script.Insert (offset, backingStore); |
||||
|
||||
script.Link (initializer, id1, id2); |
||||
} |
||||
} |
||||
|
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,59 @@
@@ -0,0 +1,59 @@
|
||||
//
|
||||
// FlipOperatorArguments.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public class FlipOperatorArguments : IContextAction |
||||
{ |
||||
public bool IsValid (RefactoringContext context) |
||||
{ |
||||
return GetBinaryOperatorExpression (context) != null; |
||||
} |
||||
|
||||
public void Run (RefactoringContext context) |
||||
{ |
||||
var binop = GetBinaryOperatorExpression (context); |
||||
|
||||
using (var script = context.StartScript ()) { |
||||
script.Replace (binop.Left, binop.Right); |
||||
script.Replace (binop.Right, binop.Left); |
||||
} |
||||
} |
||||
|
||||
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; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,92 @@
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// GenerateSwitchLabels.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public class GenerateSwitchLabels : IContextAction |
||||
{ |
||||
// TODO: Resolver!
|
||||
|
||||
public bool IsValid (RefactoringContext context) |
||||
{ |
||||
return false; |
||||
// var switchStatement = GetSwitchStatement (context);
|
||||
// if (switchStatement == null)
|
||||
// return false;
|
||||
// var resolver = context.Resolver;
|
||||
// var result = resolver.Resolve (switchStatement.Expression.ToString (), new DomLocation (switchStatement.StartLocation.Line, switchStatement.StartLocation.Column));
|
||||
// if (result == null || result.ResolvedType == null)
|
||||
// return false;
|
||||
// var type = context.Document.Dom.GetType (result.ResolvedType);
|
||||
//
|
||||
// return type != null && type.ClassType == ClassType.Enum;
|
||||
} |
||||
|
||||
public void Run (RefactoringContext context) |
||||
{ |
||||
// var switchStatement = GetSwitchStatement (context);
|
||||
// var resolver = context.Resolver;
|
||||
//
|
||||
// var result = resolver.Resolve (switchStatement.Expression.ToString (), new DomLocation (switchStatement.StartLocation.Line, switchStatement.StartLocation.Column));
|
||||
// var type = context.Document.Dom.GetType (result.ResolvedType);
|
||||
//
|
||||
// var target = new TypeReferenceExpression (ShortenTypeName (context.Document, result.ResolvedType));
|
||||
// foreach (var field in type.Fields) {
|
||||
// if (!(field.IsLiteral || field.IsConst))
|
||||
// continue;
|
||||
// switchStatement.SwitchSections.Add (new SwitchSection () {
|
||||
// CaseLabels = {
|
||||
// new CaseLabel (new MemberReferenceExpression ( target.Clone (), field.Name))
|
||||
// },
|
||||
// Statements = {
|
||||
// new BreakStatement ()
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// switchStatement.SwitchSections.Add (new SwitchSection () {
|
||||
// CaseLabels = {
|
||||
// new CaseLabel ()
|
||||
// },
|
||||
// Statements = {
|
||||
// new ThrowStatement (new ObjectCreateExpression (ShortenTypeName (context.Document, "System.ArgumentOutOfRangeException")))
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// context.Do (switchStatement.Replace (context.Document, switchStatement));
|
||||
} |
||||
|
||||
static SwitchStatement GetSwitchStatement (RefactoringContext context) |
||||
{ |
||||
var switchStatment = context.GetNode<SwitchStatement> (); |
||||
if (switchStatment != null && switchStatment.SwitchSections.Count == 0) |
||||
return switchStatment; |
||||
return null; |
||||
} |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,89 @@
@@ -0,0 +1,89 @@
|
||||
//
|
||||
// InsertAnonymousMethodSignature.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public class InsertAnonymousMethodSignature : IContextAction |
||||
{ |
||||
//TODO: Resolve
|
||||
public bool IsValid (RefactoringContext context) |
||||
{ |
||||
return false; |
||||
// IType type;
|
||||
// return GetAnonymousMethodExpression (context, out type) != null;
|
||||
} |
||||
|
||||
public void Run (RefactoringContext context) |
||||
{ |
||||
// IType type;
|
||||
// var anonymousMethodExpression = GetAnonymousMethodExpression (context, out type);
|
||||
//
|
||||
// var delegateMethod = type.Methods.First ();
|
||||
//
|
||||
// var sb = new StringBuilder ("(");
|
||||
// for (int k = 0; k < delegateMethod.Parameters.Count; k++) {
|
||||
// if (k > 0)
|
||||
// sb.Append (", ");
|
||||
// var parameterType = context.Document.Dom.GetType (delegateMethod.Parameters [k].ReturnType);
|
||||
// IReturnType returnType = parameterType != null ? new DomReturnType (parameterType) : delegateMethod.Parameters [k].ReturnType;
|
||||
// sb.Append (context.OutputNode (ShortenTypeName (context.Document, returnType), 0));
|
||||
// sb.Append (" ");
|
||||
// sb.Append (delegateMethod.Parameters [k].Name);
|
||||
// }
|
||||
// sb.Append (")");
|
||||
//
|
||||
// context.DoInsert (context.Document.Editor.LocationToOffset (anonymousMethodExpression.DelegateToken.EndLocation.Line, anonymousMethodExpression.DelegateToken.EndLocation.Column), sb.ToString ());
|
||||
} |
||||
|
||||
// AnonymousMethodExpression GetAnonymousMethodExpression (RefactoringContext context, out IType delegateType)
|
||||
// {
|
||||
// delegateType = null;
|
||||
//
|
||||
// var anonymousMethodExpression = context.GetNode<AnonymousMethodExpression> ();
|
||||
// if (anonymousMethodExpression == null || !anonymousMethodExpression.DelegateToken.Contains (context.Location.Line, context.Location.Column) || anonymousMethodExpression.HasParameterList)
|
||||
// return null;
|
||||
// MonoDevelop.Projects.Dom.ResolveResult resolveResult = null;
|
||||
// var parent = anonymousMethodExpression.Parent;
|
||||
// if (parent is AssignmentExpression) {
|
||||
// resolveResult = context.Resolver.Resolve (((AssignmentExpression)parent).Left.ToString (), context.Location);
|
||||
// } else if (parent is VariableDeclarationStatement) {
|
||||
// resolveResult = context.Resolver.Resolve (((VariableDeclarationStatement)parent).Type.ToString (), context.Location);
|
||||
// } else if (parent is InvocationExpression) {
|
||||
// // TODO: handle invocations
|
||||
// }
|
||||
//
|
||||
// if (resolveResult == null || resolveResult.ResolvedType == null)
|
||||
// return null;
|
||||
// delegateType = context.Document.Dom.GetType (resolveResult.ResolvedType);
|
||||
// if (delegateType == null || delegateType.ClassType != ClassType.Delegate)
|
||||
// return null;
|
||||
//
|
||||
// return anonymousMethodExpression;
|
||||
// }
|
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,100 @@
@@ -0,0 +1,100 @@
|
||||
//
|
||||
// IntroduceFormatItem.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Novell, Inc (http://www.novell.com)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using System; |
||||
using ICSharpCode.NRefactory.PatternMatching; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// Introduce format item. Works on strings that contain selections.
|
||||
/// "this is <some> string" => string.Format ("this is {0} string", <some>)
|
||||
/// </summary>
|
||||
public class IntroduceFormatItem : IContextAction |
||||
{ |
||||
readonly static MemberReferenceExpression PrototypeFormatReference = new MemberReferenceExpression (new TypeReferenceExpression (new PrimitiveType ("string")), "Format"); |
||||
|
||||
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 AstLocation (context.Location.Line, context.Location.Column - 1) && |
||||
new AstLocation (context.Location.Line, context.Location.Column + 1) < pexpr.EndLocation; |
||||
return pexpr.StartLocation < context.Location && context.Location < pexpr.EndLocation; |
||||
} |
||||
|
||||
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); |
||||
} |
||||
} |
||||
|
||||
void AddFormatCallToInvocation (RefactoringContext context, PrimitiveExpression pExpr, InvocationExpression invocation) |
||||
{ |
||||
var newInvocation = (InvocationExpression)invocation.Clone (); |
||||
|
||||
newInvocation.Arguments.First ().ReplaceWith (CreateFormatString (context, pExpr, newInvocation.Arguments.Count () - 1)); |
||||
newInvocation.Arguments.Add (CreateFormatArgument (context)); |
||||
|
||||
using (var script = context.StartScript ()) { |
||||
script.Replace (invocation, newInvocation); |
||||
} |
||||
} |
||||
|
||||
static PrimitiveExpression CreateFormatArgument (RefactoringContext context) |
||||
{ |
||||
return new PrimitiveExpression (context.SelectedText); |
||||
} |
||||
|
||||
static PrimitiveExpression CreateFormatString (RefactoringContext context, PrimitiveExpression pExpr, int argumentNumber) |
||||
{ |
||||
var start = context.GetOffset (pExpr.StartLocation); |
||||
var end = context.GetOffset (pExpr.EndLocation); |
||||
|
||||
return new PrimitiveExpression ("", context.GetText (start, context.SelectionStart - start) + "{" + argumentNumber + "}" + context.GetText (context.SelectionEnd, end - context.SelectionEnd)); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// InvertIf.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public class InvertIf : IContextAction |
||||
{ |
||||
public bool IsValid (RefactoringContext context) |
||||
{ |
||||
var ifStatement = GetIfElseStatement (context); |
||||
return ifStatement != null && !ifStatement.TrueStatement.IsNull && !ifStatement.FalseStatement.IsNull; |
||||
} |
||||
|
||||
// TODO: Invert if without else
|
||||
// ex. if (cond) DoSomething () == if (!cond) return; DoSomething ()
|
||||
// beware of loop contexts return should be continue then.
|
||||
public void Run (RefactoringContext context) |
||||
{ |
||||
var ifStatement = GetIfElseStatement (context); |
||||
|
||||
using (var script = context.StartScript ()) { |
||||
script.Replace (ifStatement.Condition, CSharpUtil.InvertCondition (ifStatement.Condition)); |
||||
script.Replace (ifStatement.TrueStatement, ifStatement.FalseStatement); |
||||
script.Replace (ifStatement.FalseStatement, ifStatement.TrueStatement); |
||||
script.FormatText (ctx => GetIfElseStatement (ctx)); |
||||
} |
||||
} |
||||
|
||||
static IfElseStatement GetIfElseStatement (RefactoringContext context) |
||||
{ |
||||
var result = context.GetNode<IfElseStatement> (); |
||||
if (result != null && result.IfToken.Contains (context.Location)) |
||||
return result; |
||||
return null; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// RemoveBraces.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
using System.Linq; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public class RemoveBraces : IContextAction |
||||
{ |
||||
public bool IsValid (RefactoringContext context) |
||||
{ |
||||
return GetBlockStatement (context) != null; |
||||
} |
||||
|
||||
public void Run (RefactoringContext context) |
||||
{ |
||||
var block = GetBlockStatement (context); |
||||
|
||||
using (var script = context.StartScript ()) { |
||||
script.Remove (block.LBraceToken); |
||||
script.Remove (block.RBraceToken); |
||||
script.FormatText (ctx => ctx.Unit.GetNodeAt (block.Parent.StartLocation)); |
||||
} |
||||
} |
||||
|
||||
static BlockStatement GetBlockStatement (RefactoringContext context) |
||||
{ |
||||
var block = context.GetNode<BlockStatement> (); |
||||
if (block == null || block.LBraceToken.IsNull || block.RBraceToken.IsNull) |
||||
return null; |
||||
if (block.Parent.Role == TypeDeclaration.MemberRole) |
||||
return null; |
||||
if (block.Statements.Count () != 1) |
||||
return null; |
||||
return block; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,53 @@
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// ReplaceEmptyString.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public class ReplaceEmptyString : IContextAction |
||||
{ |
||||
public bool IsValid (RefactoringContext context) |
||||
{ |
||||
return GetEmptyString (context) != null; |
||||
} |
||||
|
||||
public void Run (RefactoringContext context) |
||||
{ |
||||
var expr = GetEmptyString (context); |
||||
using (var script = context.StartScript ()) { |
||||
script.Replace (expr, new MemberReferenceExpression (new TypeReferenceExpression (new PrimitiveType ("string")), "Empty")); |
||||
} |
||||
} |
||||
|
||||
static PrimitiveExpression GetEmptyString (RefactoringContext context) |
||||
{ |
||||
var node = context.GetNode<PrimitiveExpression> (); |
||||
if (node == null || !(node.Value is string) || node.Value.ToString () != "") |
||||
return null; |
||||
return node; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,74 @@
@@ -0,0 +1,74 @@
|
||||
//
|
||||
// SplitDeclarationAndAssignment.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
using System.Linq; |
||||
using ICSharpCode.NRefactory.PatternMatching; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public class SplitDeclarationAndAssignment : IContextAction |
||||
{ |
||||
public bool IsValid (RefactoringContext context) |
||||
{ |
||||
AstType type; |
||||
return GetVariableDeclarationStatement (context, out type) != null; |
||||
} |
||||
|
||||
public void Run (RefactoringContext context) |
||||
{ |
||||
AstType type; |
||||
var varDecl = GetVariableDeclarationStatement (context, out type); |
||||
|
||||
var assign = new AssignmentExpression (new IdentifierExpression (varDecl.Variables.First ().Name), AssignmentOperatorType.Assign, varDecl.Variables.First ().Initializer.Clone ()); |
||||
|
||||
var newVarDecl = (VariableDeclarationStatement)varDecl.Clone (); |
||||
|
||||
if (newVarDecl.Type.IsMatch (new SimpleType ("var"))) |
||||
newVarDecl.Type = type; |
||||
|
||||
newVarDecl.Variables.First ().Initializer = Expression.Null; |
||||
|
||||
using (var script = context.StartScript ()) { |
||||
script.Replace (varDecl, varDecl.Parent is ForStatement ? (AstNode)assign : new ExpressionStatement (assign)); |
||||
script.Insert (context.GetOffset (varDecl.StartLocation.Line, 1), newVarDecl); |
||||
} |
||||
} |
||||
|
||||
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 = context.ResolveType (result.Variables.First ().Initializer); |
||||
if (resolvedType == null) |
||||
return null; |
||||
return result; |
||||
} |
||||
resolvedType = null; |
||||
return null; |
||||
} |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,56 @@
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// SplitString.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public class SplitString: IContextAction |
||||
{ |
||||
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 AstLocation (context.Location.Line, context.Location.Column - 2) && |
||||
new AstLocation (context.Location.Line, context.Location.Column + 2) < pexpr.EndLocation; |
||||
return pexpr.StartLocation < new AstLocation (context.Location.Line, context.Location.Column - 1) && |
||||
new AstLocation (context.Location.Line, context.Location.Column + 1) < pexpr.EndLocation; |
||||
} |
||||
|
||||
public void Run (RefactoringContext context) |
||||
{ |
||||
var pexpr = context.GetNode<PrimitiveExpression> (); |
||||
int offset = context.GetOffset (context.Location); |
||||
using (var script = context.StartScript ()) { |
||||
script.Insert (offset, pexpr.LiteralValue.StartsWith ("@") ? "\" + @\"" : "\" + \""); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,69 @@
@@ -0,0 +1,69 @@
|
||||
//
|
||||
// UseExplicitType.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public class UseExplicitType: IContextAction |
||||
{ |
||||
public bool IsValid (RefactoringContext context) |
||||
{ |
||||
return GetVariableDeclarationStatement (context) != null; |
||||
} |
||||
|
||||
// TODO: Resolving
|
||||
public void Run (RefactoringContext context) |
||||
{ |
||||
// var varDecl = GetVariableDeclarationStatement (context);
|
||||
// var resolver = context.Resolver;
|
||||
// var resolveResult = resolver.Resolve (varDecl.Variables.First ().Initializer.ToString (), context.Location);
|
||||
//
|
||||
// int offset = context.Document.Editor.LocationToOffset (varDecl.Type.StartLocation.Line, varDecl.Type.StartLocation.Column);
|
||||
// int endOffset = context.Document.Editor.LocationToOffset (varDecl.Type.EndLocation.Line, varDecl.Type.EndLocation.Column);
|
||||
// string text = context.OutputNode (ShortenTypeName (context.Document, resolveResult.ResolvedType), 0).Trim ();
|
||||
// context.DoReplace (offset, endOffset - offset, text);
|
||||
// context.CommitChanges ();
|
||||
// context.Document.Editor.Caret.Offset = offset + text.Length;
|
||||
} |
||||
|
||||
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"))) {
|
||||
// var resolver = context.Resolver;
|
||||
// var resolveResult = resolver.Resolve (result.Variables.First ().Initializer.ToString (), context.Location);
|
||||
// if (resolveResult == null || resolveResult.ResolvedType == null || string.IsNullOrEmpty (resolveResult.ResolvedType.FullName))
|
||||
// return null;
|
||||
// return result;
|
||||
//
|
||||
// }
|
||||
return null; |
||||
} |
||||
|
||||
|
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,56 @@
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// UseVarKeyword.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
using System.Linq; |
||||
using ICSharpCode.NRefactory.PatternMatching; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public class UseVarKeyword: IContextAction |
||||
{ |
||||
public bool IsValid (RefactoringContext context) |
||||
{ |
||||
return GetVariableDeclarationStatement (context) != null; |
||||
} |
||||
|
||||
public void Run (RefactoringContext context) |
||||
{ |
||||
var varDecl = GetVariableDeclarationStatement (context); |
||||
using (var script = context.StartScript ()) { |
||||
script.Replace (varDecl.Type, new SimpleType ("var")); |
||||
} |
||||
} |
||||
|
||||
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; |
||||
} |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// CreateLinkAction.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public abstract class CreateLinkAction : Action |
||||
{ |
||||
public IEnumerable<AstNode> Linked { |
||||
get; |
||||
private set; |
||||
} |
||||
|
||||
public CreateLinkAction (IEnumerable<AstNode> linked) |
||||
{ |
||||
this.Linked = linked; |
||||
} |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,39 @@
@@ -0,0 +1,39 @@
|
||||
//
|
||||
// FormatTextAction.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public abstract class FormatTextAction : Action |
||||
{ |
||||
public Func<RefactoringContext, AstNode> Callback { get; private set; } |
||||
|
||||
public FormatTextAction (Func<RefactoringContext, AstNode> callback) |
||||
{ |
||||
this.Callback = callback; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,75 @@
@@ -0,0 +1,75 @@
|
||||
//
|
||||
// IChangeFactory.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// A factory that creates the changes.
|
||||
/// </summary>
|
||||
public interface IActionFactory |
||||
{ |
||||
TextReplaceAction CreateTextReplaceAction (int offset, int removedChars, string insertedText); |
||||
NodeOutputAction CreateNodeOutputAction (int offset, int removedChars, NodeOutput output); |
||||
NodeSelectionAction CreateNodeSelectionAction (AstNode node); |
||||
FormatTextAction CreateFormatTextAction (Func<RefactoringContext, AstNode> callback); |
||||
CreateLinkAction CreateLinkAction (IEnumerable<AstNode> linkedNodes); |
||||
} |
||||
|
||||
public abstract class AbstractActionFactory : IActionFactory |
||||
{ |
||||
#region IActionFactory implementation
|
||||
public virtual TextReplaceAction CreateTextReplaceAction (int offset, int removedChars, string insertedText) |
||||
{ |
||||
throw new NotImplementedException (); |
||||
} |
||||
|
||||
public virtual NodeOutputAction CreateNodeOutputAction (int offset, int removedChars, NodeOutput output) |
||||
{ |
||||
throw new NotImplementedException (); |
||||
} |
||||
|
||||
public virtual NodeSelectionAction CreateNodeSelectionAction (AstNode node) |
||||
{ |
||||
throw new NotImplementedException (); |
||||
} |
||||
|
||||
public virtual FormatTextAction CreateFormatTextAction (Func<RefactoringContext, AstNode> callback) |
||||
{ |
||||
throw new NotImplementedException (); |
||||
} |
||||
|
||||
public virtual CreateLinkAction CreateLinkAction (IEnumerable<AstNode> linkedNodes) |
||||
{ |
||||
throw new NotImplementedException (); |
||||
} |
||||
#endregion
|
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// ContextAction.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public interface IContextAction |
||||
{ |
||||
bool IsValid (RefactoringContext context); |
||||
void Run (RefactoringContext context); |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,120 @@
@@ -0,0 +1,120 @@
|
||||
//
|
||||
// NodeOutputChange.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// This is the node that should be outputted at a position.
|
||||
/// </summary>
|
||||
public class NodeOutput |
||||
{ |
||||
public readonly Dictionary<AstNode, Segment> NodeSegments = new Dictionary<AstNode, Segment> (); |
||||
|
||||
public string Text { |
||||
get; |
||||
set; |
||||
} |
||||
|
||||
public class Segment : ISegment |
||||
{ |
||||
public int Offset { |
||||
get; |
||||
set; |
||||
} |
||||
|
||||
public int Length { |
||||
get { |
||||
return EndOffset - Offset; |
||||
} |
||||
} |
||||
|
||||
public int EndOffset { |
||||
get; |
||||
set; |
||||
} |
||||
|
||||
public Segment (int offset) |
||||
{ |
||||
this.Offset = offset; |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
/// <summary>
|
||||
/// Outputs an Ast node at a specific offset.
|
||||
/// </summary>
|
||||
public abstract class NodeOutputAction : TextReplaceAction |
||||
{ |
||||
public NodeOutput NodeOutput { |
||||
get; |
||||
private set; |
||||
} |
||||
|
||||
public override string InsertedText { |
||||
get { |
||||
return NodeOutput.Text; |
||||
} |
||||
set { |
||||
throw new NotSupportedException ("Changing text with this propery is not supported on NodeOutputChange."); |
||||
} |
||||
} |
||||
|
||||
public NodeOutputAction (int offset, int removedChars, NodeOutput output) : base (offset, removedChars) |
||||
{ |
||||
if (output == null) |
||||
throw new ArgumentNullException ("output"); |
||||
this.NodeOutput = output; |
||||
} |
||||
} |
||||
|
||||
|
||||
/// <summary>
|
||||
/// An (Offset,Length)-pair.
|
||||
/// </summary>
|
||||
public interface ISegment |
||||
{ |
||||
/// <summary>
|
||||
/// Gets the start offset of the segment.
|
||||
/// </summary>
|
||||
int Offset { get; } |
||||
|
||||
/// <summary>
|
||||
/// Gets the length of the segment.
|
||||
/// </summary>
|
||||
/// <remarks>Must not be negative.</remarks>
|
||||
int Length { get; } |
||||
|
||||
/// <summary>
|
||||
/// Gets the end offset of the segment.
|
||||
/// </summary>
|
||||
/// <remarks>EndOffset = Offset + Length;</remarks>
|
||||
int EndOffset { get; } |
||||
} |
||||
} |
||||
@ -0,0 +1,40 @@
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// SelectionAction.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public abstract class NodeSelectionAction : Action |
||||
{ |
||||
public AstNode AstNode { get; private set; } |
||||
|
||||
public NodeSelectionAction (AstNode astNode) |
||||
{ |
||||
this.AstNode = astNode; |
||||
} |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,109 @@
@@ -0,0 +1,109 @@
|
||||
//
|
||||
// RefactoringContext.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
using System.Linq; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public abstract class RefactoringContext : AbstractActionFactory |
||||
{ |
||||
public CompilationUnit Unit { |
||||
get; |
||||
protected set; |
||||
} |
||||
|
||||
public AstLocation Location { |
||||
get; |
||||
protected set; |
||||
} |
||||
|
||||
public abstract bool HasCSharp3Support { |
||||
get; |
||||
} |
||||
|
||||
public abstract CSharpFormattingOptions FormattingOptions { |
||||
get; |
||||
} |
||||
|
||||
public abstract AstType CreateShortType (AstType fullType); |
||||
|
||||
public abstract AstType CreateShortType (string fullTypeName); |
||||
|
||||
public AstNode GetNode () |
||||
{ |
||||
return Unit.GetNodeAt (Location); |
||||
} |
||||
|
||||
public T GetNode<T> () where T : AstNode |
||||
{ |
||||
return Unit.GetNodeAt<T> (Location); |
||||
} |
||||
|
||||
public abstract Script StartScript (); |
||||
|
||||
#region Text stuff
|
||||
public abstract string EolMarker { get; } |
||||
public abstract bool IsSomethingSelected { get; } |
||||
public abstract string SelectedText { get; } |
||||
public abstract int SelectionStart { get; } |
||||
public abstract int SelectionEnd { get; } |
||||
public abstract int SelectionLength { get; } |
||||
public abstract int GetOffset (AstLocation location); |
||||
public int GetOffset (int line, int col) |
||||
{ |
||||
return GetOffset (new AstLocation (line, col)); |
||||
} |
||||
public abstract AstLocation GetLocation (int offset); |
||||
public abstract string GetText (int offset, int length); |
||||
#endregion
|
||||
|
||||
#region Resolving
|
||||
public abstract AstType ResolveType (AstNode node); |
||||
#endregion
|
||||
|
||||
public string GetNameProposal (string name, bool camelCase = true) |
||||
{ |
||||
string baseName = (camelCase ? char.ToLower (name [0]) : char.ToUpper (name [0])) + name.Substring (1); |
||||
|
||||
var type = GetNode<TypeDeclaration> (); |
||||
if (type == null) |
||||
return baseName; |
||||
|
||||
int number = -1; |
||||
string proposedName; |
||||
do { |
||||
proposedName = AppendNumberToName (baseName, number++); |
||||
} while (type.Members.Select (m => m.GetChildByRole (AstNode.Roles.Identifier)).Any (n => n.Name == proposedName)); |
||||
return proposedName; |
||||
} |
||||
|
||||
static string AppendNumberToName (string baseName, int number) |
||||
{ |
||||
return baseName + (number > 0 ? (number + 1).ToString () : ""); |
||||
} |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,156 @@
@@ -0,0 +1,156 @@
|
||||
//
|
||||
// Script.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public abstract class Script : IDisposable |
||||
{ |
||||
public RefactoringContext Context { |
||||
get; |
||||
private set; |
||||
} |
||||
|
||||
protected readonly List<Action> changes = new List<Action> (); |
||||
|
||||
public IEnumerable<Action> Actions { |
||||
get { |
||||
return changes; |
||||
} |
||||
} |
||||
|
||||
public Script (RefactoringContext context) |
||||
{ |
||||
if (context == null) |
||||
throw new ArgumentNullException ("context"); |
||||
this.Context = context; |
||||
} |
||||
|
||||
public void Queue (Action change) |
||||
{ |
||||
changes.Add (change); |
||||
} |
||||
|
||||
public void Insert (int offset, string text) |
||||
{ |
||||
Queue (Context.CreateTextReplaceAction (offset, 0, text)); |
||||
} |
||||
|
||||
public void Insert (int offset, AstNode node) |
||||
{ |
||||
var output = OutputNode (GetIndentLevelAt (offset), node); |
||||
Queue (Context.CreateNodeOutputAction (offset, 0, output)); |
||||
} |
||||
|
||||
public void Link (params AstNode[] nodes) |
||||
{ |
||||
Queue (Context.CreateLinkAction (nodes)); |
||||
} |
||||
|
||||
public void Link (IEnumerable<AstNode> nodes) |
||||
{ |
||||
Queue (Context.CreateLinkAction (nodes)); |
||||
} |
||||
|
||||
public void Remove (AstNode node) |
||||
{ |
||||
var startOffset = Context.GetOffset (node.StartLocation); |
||||
var endOffset = Context.GetOffset (node.EndLocation); |
||||
Remove (startOffset, endOffset - startOffset); |
||||
} |
||||
|
||||
public void Remove (int offset, int length) |
||||
{ |
||||
Queue (Context.CreateTextReplaceAction (offset, length, null)); |
||||
} |
||||
|
||||
public void Replace (int offset, int length, string text) |
||||
{ |
||||
Queue (Context.CreateTextReplaceAction (offset, length, text)); |
||||
} |
||||
|
||||
public void Replace (AstNode node, AstNode replaceWith) |
||||
{ |
||||
var startOffset = Context.GetOffset (node.StartLocation); |
||||
var endOffset = Context.GetOffset (node.EndLocation); |
||||
int level = 0; |
||||
// if (!(replaceWith is Expression))
|
||||
// level = GetIndentLevelAt (startOffset);
|
||||
Queue (Context.CreateNodeOutputAction (startOffset, endOffset - startOffset, OutputNode (level, replaceWith))); |
||||
} |
||||
|
||||
public void FormatText (Func<RefactoringContext, AstNode> callback) |
||||
{ |
||||
Queue (Context.CreateFormatTextAction (callback)); |
||||
} |
||||
|
||||
public void Select (AstNode node) |
||||
{ |
||||
Queue (Context.CreateNodeSelectionAction (node)); |
||||
} |
||||
|
||||
int GetIndentLevelAt (int offset) |
||||
{ |
||||
var node = Context.Unit.GetNodeAt (Context.GetLocation (offset)); |
||||
int level = 0; |
||||
while (node != null) { |
||||
if (node is BlockStatement || node is TypeDeclaration || node is NamespaceDeclaration) |
||||
level++; |
||||
node = node.Parent; |
||||
} |
||||
return level; |
||||
} |
||||
|
||||
NodeOutput OutputNode (int indentLevel, AstNode node) |
||||
{ |
||||
NodeOutput result = new NodeOutput (); |
||||
var formatter = new StringBuilderOutputFormatter (); |
||||
formatter.Indentation = indentLevel; |
||||
formatter.EolMarker = Context.EolMarker; |
||||
if (node is Statement) |
||||
formatter.NewLine (); |
||||
var visitor = new OutputVisitor (formatter, Context.FormattingOptions); |
||||
visitor.OutputStarted += (sender, e) => { |
||||
result.NodeSegments [e.AstNode] = new NodeOutput.Segment (formatter.Length); |
||||
}; |
||||
visitor.OutputFinished += (sender, e) => { |
||||
result.NodeSegments [e.AstNode].EndOffset = formatter.Length; |
||||
}; |
||||
node.AcceptVisitor (visitor, null); |
||||
result.Text = formatter.ToString ().TrimEnd (); |
||||
if (node is FieldDeclaration) |
||||
result.Text += Context.EolMarker; |
||||
|
||||
return result; |
||||
} |
||||
|
||||
#region IDisposable implementation
|
||||
public abstract void Dispose (); |
||||
#endregion
|
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,158 @@
@@ -0,0 +1,158 @@
|
||||
//
|
||||
// StringBuilderOutputFormatter.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using System; |
||||
using System.Text; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public class StringBuilderOutputFormatter : IOutputFormatter |
||||
{ |
||||
readonly StringBuilder sb = new StringBuilder (); |
||||
int indentation; |
||||
bool needsIndent = true; |
||||
|
||||
public int Length { |
||||
get { |
||||
WriteIndentation (); |
||||
return sb.Length; |
||||
} |
||||
} |
||||
|
||||
public int Indentation { |
||||
get { |
||||
return this.indentation; |
||||
} |
||||
set { |
||||
indentation = value; |
||||
} |
||||
} |
||||
|
||||
public string EolMarker { |
||||
get; |
||||
set; |
||||
} |
||||
|
||||
public override string ToString () |
||||
{ |
||||
return sb.ToString (); |
||||
} |
||||
|
||||
public void WriteIdentifier (string ident) |
||||
{ |
||||
WriteIndentation (); |
||||
sb.Append (ident); |
||||
} |
||||
|
||||
public void WriteKeyword (string keyword) |
||||
{ |
||||
WriteIndentation (); |
||||
sb.Append (keyword); |
||||
} |
||||
|
||||
public void WriteToken (string token) |
||||
{ |
||||
WriteIndentation (); |
||||
sb.Append (token); |
||||
} |
||||
|
||||
public void Space () |
||||
{ |
||||
WriteIndentation (); |
||||
sb.Append (' '); |
||||
} |
||||
|
||||
public void OpenBrace (BraceStyle style) |
||||
{ |
||||
WriteIndentation (); |
||||
sb.Append (' '); |
||||
sb.Append ('{'); |
||||
Indent (); |
||||
NewLine (); |
||||
} |
||||
|
||||
public void CloseBrace (BraceStyle style) |
||||
{ |
||||
Unindent (); |
||||
WriteIndentation (); |
||||
sb.Append ('}'); |
||||
} |
||||
|
||||
void WriteIndentation () |
||||
{ |
||||
if (needsIndent) { |
||||
needsIndent = false; |
||||
for (int i = 0; i < indentation; i++) { |
||||
sb.Append ('\t'); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public void NewLine () |
||||
{ |
||||
sb.Append (EolMarker); |
||||
needsIndent = true; |
||||
} |
||||
|
||||
public void Indent () |
||||
{ |
||||
indentation++; |
||||
} |
||||
|
||||
public void Unindent () |
||||
{ |
||||
indentation--; |
||||
} |
||||
|
||||
public void WriteComment (CommentType commentType, string content) |
||||
{ |
||||
WriteIndentation (); |
||||
switch (commentType) { |
||||
case CommentType.SingleLine: |
||||
sb.Append ("//"); |
||||
sb.AppendLine (content); |
||||
break; |
||||
case CommentType.MultiLine: |
||||
sb.Append ("/*"); |
||||
sb.Append (content); |
||||
sb.Append ("*/"); |
||||
break; |
||||
case CommentType.Documentation: |
||||
sb.Append ("///"); |
||||
sb.AppendLine (content); |
||||
break; |
||||
} |
||||
} |
||||
|
||||
public virtual void StartNode (AstNode node) |
||||
{ |
||||
} |
||||
|
||||
public virtual void EndNode (AstNode node) |
||||
{ |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,134 @@
@@ -0,0 +1,134 @@
|
||||
//
|
||||
// TextReplaceChange.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// This is the base action for changes in a text document.
|
||||
/// </summary>
|
||||
public abstract class TextReplaceAction : Action |
||||
{ |
||||
/// <summary>
|
||||
/// Gets or sets the offset.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The offset of the replace.
|
||||
/// </value>
|
||||
public int Offset { |
||||
get; |
||||
set; |
||||
} |
||||
|
||||
int removedChars; |
||||
/// <summary>
|
||||
/// Gets or sets the numer of chars to removed.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The numer of chars to remove.
|
||||
/// </value>
|
||||
/// <exception cref='ArgumentOutOfRangeException'>
|
||||
/// Is thrown when an argument passed to a method is invalid because it is outside the allowable range of values as
|
||||
/// specified by the method.
|
||||
/// </exception>
|
||||
public int RemovedChars { |
||||
get { |
||||
return removedChars; |
||||
} |
||||
set { |
||||
if (value < 0) |
||||
throw new ArgumentOutOfRangeException ("RemovedChars", "needs to be >= 0"); |
||||
removedChars = value; |
||||
} |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the inserted text.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The text to insert.
|
||||
/// </value>
|
||||
public virtual string InsertedText { |
||||
get; |
||||
set; |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ICSharpCode.NRefactory.CSharp.Refactoring.TextReplaceAction"/> class.
|
||||
/// </summary>
|
||||
/// <param name='offset'>
|
||||
/// The offset of the replace.
|
||||
/// </param>
|
||||
/// <param name='removedChars'>
|
||||
/// The numer of chars to remove.
|
||||
/// </param>
|
||||
/// <exception cref='ArgumentOutOfRangeException'>
|
||||
/// Is thrown when an argument passed to a method is invalid because it is outside the allowable range of values as
|
||||
/// specified by the method.
|
||||
/// </exception>
|
||||
protected TextReplaceAction (int offset, int removedChars) |
||||
{ |
||||
if (removedChars < 0) |
||||
throw new ArgumentOutOfRangeException ("removedChars", "removedChars needs to be >= 0"); |
||||
if (offset < 0) |
||||
throw new ArgumentOutOfRangeException ("offset", "offset needs to be >= 0"); |
||||
this.removedChars = removedChars; |
||||
this.Offset = offset; |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ICSharpCode.NRefactory.CSharp.Refactoring.TextReplaceAction"/> class.
|
||||
/// </summary>
|
||||
/// <param name='offset'>
|
||||
/// The offset of the replace.
|
||||
/// </param>
|
||||
/// <param name='removedChars'>
|
||||
/// The numer of chars to remove.
|
||||
/// </param>
|
||||
/// <param name='insertedText'>
|
||||
/// The text to insert.
|
||||
/// </param>
|
||||
/// <exception cref='ArgumentOutOfRangeException'>
|
||||
/// Is thrown when an argument passed to a method is invalid because it is outside the allowable range of values as
|
||||
/// specified by the method.
|
||||
public TextReplaceAction (int offset, int removedChars, string insertedText) : this (offset, removedChars) |
||||
{ |
||||
this.InsertedText = insertedText; |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="System.String"/> that represents the current <see cref="ICSharpCode.NRefactory.CSharp.Refactoring.TextReplaceAction"/>.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A <see cref="System.String"/> that represents the current <see cref="ICSharpCode.NRefactory.CSharp.Refactoring.TextReplaceAction"/>.
|
||||
/// </returns>
|
||||
public override string ToString () |
||||
{ |
||||
return string.Format ("[TextReplaceAction: Offset={0}, RemovedChars={1}, InsertedText={2}]", Offset, RemovedChars, InsertedText); |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue