Browse Source

it is now possible to use C# ExtractMethod in:

- properties (getters and setters, implicit value parameter is added automatically)
- operators
- constructors

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@5720 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Siegfried Pammer 15 years ago
parent
commit
a720c302c6
  1. 32
      src/AddIns/Misc/SharpRefactoring/Project/Src/CSharpMethodExtractor.cs
  2. 22
      src/AddIns/Misc/SharpRefactoring/Project/Src/MethodExtractorBase.cs

32
src/AddIns/Misc/SharpRefactoring/Project/Src/CSharpMethodExtractor.cs

@ -202,11 +202,24 @@ namespace SharpRefactoring @@ -202,11 +202,24 @@ namespace SharpRefactoring
if (parentNode is MethodDeclaration) {
yield return new Variable((parentNode as MethodDeclaration).Body, pde);
} else if (parentNode is ConstructorDeclaration) {
yield return new Variable((parentNode as ConstructorDeclaration).Body, pde);
} else {
throw new NotSupportedException("not supported!");
}
}
}
if (parentNode is PropertyDeclaration && IsInSetter(parentNode as PropertyDeclaration)) {
PropertyDeclaration pd = parentNode as PropertyDeclaration;
yield return new Variable(
new LocalLookupVariable(
"value", pd.TypeReference,
pd.SetRegion.StartLocation, pd.SetRegion.EndLocation,
false, false, null, null, false
)
);
}
}
public override IOutputAstVisitor GetOutputVisitor()
@ -218,5 +231,24 @@ namespace SharpRefactoring @@ -218,5 +231,24 @@ namespace SharpRefactoring
{
return new NRefactoryResolver(Dom.LanguageProperties.CSharp);
}
bool IsInSetter(PropertyDeclaration property)
{
if (!property.HasSetRegion)
return false;
int startOffset = textEditor.Document.PositionToOffset(property.SetRegion.StartLocation.Line,
property.SetRegion.StartLocation.Column);
int endOffset = textEditor.Document.PositionToOffset(property.SetRegion.EndLocation.Line,
property.SetRegion.EndLocation.Column);
int selectionEnd = textEditor.SelectionStart + textEditor.SelectionLength;
return textEditor.SelectionStart >= startOffset &&
textEditor.SelectionStart <= endOffset &&
selectionEnd >= startOffset &&
selectionEnd <= endOffset;
}
}
}

22
src/AddIns/Misc/SharpRefactoring/Project/Src/MethodExtractorBase.cs

@ -64,20 +64,14 @@ namespace SharpRefactoring @@ -64,20 +64,14 @@ namespace SharpRefactoring
InvocationExpression expr = new InvocationExpression(new IdentifierExpression(method.Name), CreateArgumentExpressions(method.Parameters));
if (method.TypeReference.Type != "System.Void") {
if (parent is MethodDeclaration) {
if (method.TypeReference == (parent as MethodDeclaration).TypeReference)
caller = new ReturnStatement(expr);
else {
returnVariable.Initializer = expr;
caller = new LocalVariableDeclaration(returnVariable);
}
}
TypeReference parentType = GetParentReturnType(parent);
if (method.TypeReference == parentType)
caller = new ReturnStatement(expr);
else {
returnVariable.Initializer = expr;
caller = new LocalVariableDeclaration(returnVariable);
}
}
else {
} else {
caller = new ExpressionStatement(expr);
}
return caller;
@ -272,6 +266,14 @@ namespace SharpRefactoring @@ -272,6 +266,14 @@ namespace SharpRefactoring
public abstract bool Extract();
public abstract Dom.IResolver GetResolver();
static TypeReference GetParentReturnType(ParametrizedNode parent)
{
if (parent is MemberNode)
return (parent as MemberNode).TypeReference;
return null;
}
}
public class Variable {

Loading…
Cancel
Save