Browse Source

Updated ExtractMethod refactoring

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@3331 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Siegfried Pammer 17 years ago
parent
commit
dcfd505a9e
  1. 2
      src/AddIns/Misc/SharpRefactoring/SharpRefactoring.addin
  2. 1
      src/AddIns/Misc/SharpRefactoring/SharpRefactoring.csproj
  3. 28
      src/AddIns/Misc/SharpRefactoring/Src/ExtractMethodCommand.cs
  4. 2
      src/AddIns/Misc/SharpRefactoring/Src/MethodExtractorBase.cs
  5. 74
      src/AddIns/Misc/SharpRefactoring/Src/Visitors/FindLocalVariablesVisitor.cs
  6. 9
      src/AddIns/Misc/SharpRefactoring/Src/Visitors/HasAssignmentsVisitor.cs

2
src/AddIns/Misc/SharpRefactoring/SharpRefactoring.addin

@ -16,7 +16,7 @@ @@ -16,7 +16,7 @@
<Path name = "/SharpDevelop/Workbench/MainMenu/Refactor">
<MenuItem id = "ExtractMethod"
label = "Extract method"
label = "${res:AddIns.SharpRefactoring.ExtractMethod}"
class = "SharpRefactoring.ExtractMethodCommand"/>
</Path>
</AddIn>

1
src/AddIns/Misc/SharpRefactoring/SharpRefactoring.csproj

@ -68,7 +68,6 @@ @@ -68,7 +68,6 @@
<Compile Include="Src\ParameterCheckRefactoringMenuBuilder.cs" />
<Compile Include="Src\Transformers\ReplaceUnnecessaryVariableDeclarationsTransformer.cs" />
<Compile Include="Src\Visitors\FindJumpInstructionsVisitor.cs" />
<Compile Include="Src\Visitors\FindLocalVariablesVisitor.cs" />
<Compile Include="Src\Visitors\FindMemberVisitor.cs" />
<Compile Include="Src\Visitors\FindReferenceVisitor.cs" />
<Compile Include="Src\Visitors\HasAssignmentsVisitor.cs" />

28
src/AddIns/Misc/SharpRefactoring/Src/ExtractMethodCommand.cs

@ -34,18 +34,19 @@ namespace SharpRefactoring @@ -34,18 +34,19 @@ namespace SharpRefactoring
if (textEditor.ActiveTextAreaControl.SelectionManager.HasSomethingSelected)
{
MethodExtractorBase extractor = GetCurrentExtractor(textEditor);
if (extractor.Extract()) {
ExtractMethodForm form = new ExtractMethodForm("NewMethod", extractor.CreatePreview());
if (form.ShowDialog() == DialogResult.OK) {
extractor.ExtractedMethod.Name = form.Text;
textEditor.Document.UndoStack.StartUndoGroup();
extractor.InsertAfterCurrentMethod();
extractor.InsertCall();
textEditor.Document.FormattingStrategy.IndentLines(textEditor.ActiveTextAreaControl.TextArea, 0, textEditor.Document.TotalNumberOfLines - 1);
textEditor.Document.UndoStack.EndUndoGroup();
textEditor.ActiveTextAreaControl.SelectionManager.ClearSelection();
if (extractor != null) {
if (extractor.Extract()) {
ExtractMethodForm form = new ExtractMethodForm("NewMethod", extractor.CreatePreview());
if (form.ShowDialog() == DialogResult.OK) {
extractor.ExtractedMethod.Name = form.Text;
textEditor.Document.UndoStack.StartUndoGroup();
extractor.InsertAfterCurrentMethod();
extractor.InsertCall();
textEditor.Document.FormattingStrategy.IndentLines(textEditor.ActiveTextAreaControl.TextArea, 0, textEditor.Document.TotalNumberOfLines - 1);
textEditor.Document.UndoStack.EndUndoGroup();
textEditor.ActiveTextAreaControl.SelectionManager.ClearSelection();
}
}
}
}
@ -57,7 +58,8 @@ namespace SharpRefactoring @@ -57,7 +58,8 @@ namespace SharpRefactoring
case "C#":
return new CSharpMethodExtractor(editor, editor.ActiveTextAreaControl.SelectionManager.SelectionCollection[0]);
default:
throw new NotSupportedException("Extracting methods in the current language is not supported!");
MessageService.ShowError(string.Format(StringParser.Parse("${res:AddIns.SharpRefactoring.ExtractMethodNotSupported}"), ProjectService.CurrentProject.Language));
return null;
}
}
}

2
src/AddIns/Misc/SharpRefactoring/Src/MethodExtractorBase.cs

@ -284,7 +284,7 @@ namespace SharpRefactoring @@ -284,7 +284,7 @@ namespace SharpRefactoring
protected static bool HasAssignment(MethodDeclaration method, LocalLookupVariable variable)
{
HasAssignmentsVisitor hav = new HasAssignmentsVisitor(variable.Name, variable.TypeRef);
HasAssignmentsVisitor hav = new HasAssignmentsVisitor(variable.Name, variable.TypeRef, variable.StartPos, variable.EndPos);
method.AcceptVisitor(hav, null);

74
src/AddIns/Misc/SharpRefactoring/Src/Visitors/FindLocalVariablesVisitor.cs

@ -1,74 +0,0 @@ @@ -1,74 +0,0 @@
/*
* Created by SharpDevelop.
* User: HP
* Date: 28.04.2008
* Time: 22:18
*/
using System;
using System.Collections.Generic;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.Visitors;
namespace SharpRefactoring.Visitors
{
public class FindLocalVariablesVisitor : AbstractAstVisitor
{
List<VariableDeclaration> variables;
TypeReference currentType;
Location start, end;
public List<VariableDeclaration> Variables {
get { return variables; }
}
public FindLocalVariablesVisitor()
{
this.variables = new List<VariableDeclaration>();
}
public override object VisitLocalVariableDeclaration(LocalVariableDeclaration localVariableDeclaration, object data)
{
this.currentType = localVariableDeclaration.TypeReference;
this.start = localVariableDeclaration.StartLocation;
this.end = localVariableDeclaration.EndLocation;
return base.VisitLocalVariableDeclaration(localVariableDeclaration, data);
}
public override object VisitVariableDeclaration(VariableDeclaration variableDeclaration, object data)
{
variableDeclaration.TypeReference = currentType;
variableDeclaration.StartLocation = start;
variableDeclaration.EndLocation = end;
System.Diagnostics.Debug.Print(variableDeclaration.TypeReference + " " + variableDeclaration.Name);
this.variables.Add(variableDeclaration);
return base.VisitVariableDeclaration(variableDeclaration, data);
}
public override object VisitForeachStatement(ForeachStatement foreachStatement, object data)
{
VariableDeclaration v = new VariableDeclaration(foreachStatement.VariableName, Expression.Null, foreachStatement.TypeReference);
v.StartLocation = foreachStatement.StartLocation;
v.EndLocation = foreachStatement.StartLocation;
return base.VisitForeachStatement(foreachStatement, data);
}
public override object VisitCatchClause(CatchClause catchClause, object data)
{
VariableDeclaration v =
new VariableDeclaration(catchClause.VariableName,
new ObjectCreateExpression(catchClause.TypeReference,
new List<Expression>()),
catchClause.TypeReference);
v.StartLocation = catchClause.StartLocation;
v.EndLocation = catchClause.EndLocation;
this.variables.Add(v);
return base.VisitCatchClause(catchClause, data);
}
}
}

9
src/AddIns/Misc/SharpRefactoring/Src/Visitors/HasAssignmentsVisitor.cs

@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
* Time: 22:18
*/
using ICSharpCode.NRefactory;
using System;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.Visitors;
@ -16,22 +17,26 @@ namespace SharpRefactoring.Visitors @@ -16,22 +17,26 @@ namespace SharpRefactoring.Visitors
string name;
TypeReference type;
bool hasAssignment = false;
Location startRange, endRange;
public bool HasAssignment {
get { return hasAssignment; }
}
public HasAssignmentsVisitor(string name, TypeReference type)
public HasAssignmentsVisitor(string name, TypeReference type, Location startRange, Location endRange)
{
this.name = name;
this.type = type;
this.startRange = startRange;
this.endRange = endRange;
}
public override object VisitAssignmentExpression(AssignmentExpression assignmentExpression, object data)
{
if (!hasAssignment) {
if (assignmentExpression.Left is IdentifierExpression) {
hasAssignment = (((IdentifierExpression)assignmentExpression.Left).Identifier == name);
hasAssignment = (((IdentifierExpression)assignmentExpression.Left).Identifier == name) &&
(assignmentExpression.StartLocation >= startRange && assignmentExpression.EndLocation <= endRange);
}
}
return base.VisitAssignmentExpression(assignmentExpression, data);

Loading…
Cancel
Save