Browse Source

improved ExtractMethod

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3760 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Siegfried Pammer 17 years ago
parent
commit
5f1fccd9ce
  1. 9
      src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs
  2. 2
      src/AddIns/Misc/SharpRefactoring/Src/ExtractMethodCommand.cs
  3. 8
      src/AddIns/Misc/SharpRefactoring/Src/Forms/ExtractMethodForm.Designer.cs
  4. 56
      src/AddIns/Misc/SharpRefactoring/Src/Forms/ExtractMethodForm.cs
  5. 30
      src/AddIns/Misc/SharpRefactoring/Src/MethodExtractorBase.cs

9
src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs

@ -27,7 +27,7 @@ namespace SharpRefactoring @@ -27,7 +27,7 @@ namespace SharpRefactoring
public class CSharpMethodExtractor : MethodExtractorBase
{
public CSharpMethodExtractor(ICSharpCode.TextEditor.TextEditorControl textEditor, ISelection selection)
: base(textEditor, selection, new CSharpOutputVisitor())
: base(textEditor, selection)
{
}
@ -176,9 +176,16 @@ namespace SharpRefactoring @@ -176,9 +176,16 @@ namespace SharpRefactoring
CreateReturnStatement(newMethod, possibleReturnValues);
newMethod.Name = "NewMethod";
this.extractedMethod = newMethod;
return true;
}
public override IOutputAstVisitor GetOutputVisitor()
{
return new CSharpOutputVisitor();
}
}
}

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

@ -36,7 +36,7 @@ namespace SharpRefactoring @@ -36,7 +36,7 @@ namespace SharpRefactoring
MethodExtractorBase extractor = GetCurrentExtractor(textEditor);
if (extractor != null) {
if (extractor.Extract()) {
ExtractMethodForm form = new ExtractMethodForm("NewMethod", extractor.CreatePreview());
ExtractMethodForm form = new ExtractMethodForm(extractor.ExtractedMethod, new Func<IOutputAstVisitor>(extractor.GetOutputVisitor));
if (form.ShowDialog() == DialogResult.OK) {
extractor.ExtractedMethod.Name = form.Text;

8
src/AddIns/Misc/SharpRefactoring/Src/Forms/ExtractMethodForm.Designer.cs generated

@ -77,7 +77,7 @@ namespace SharpRefactoring.Forms @@ -77,7 +77,7 @@ namespace SharpRefactoring.Forms
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(467, 20);
this.txtName.TabIndex = 2;
this.txtName.TextChanged += new System.EventHandler(this.txtName_TextChanged);
this.txtName.TextChanged += new System.EventHandler(this.txtNameTextChanged);
//
// txtPreview
//
@ -102,7 +102,7 @@ namespace SharpRefactoring.Forms @@ -102,7 +102,7 @@ namespace SharpRefactoring.Forms
this.btnCancel.TabIndex = 4;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
this.btnCancel.Click += new System.EventHandler(this.btnCancelClick);
//
// btnOK
//
@ -113,7 +113,7 @@ namespace SharpRefactoring.Forms @@ -113,7 +113,7 @@ namespace SharpRefactoring.Forms
this.btnOK.TabIndex = 5;
this.btnOK.Text = "OK";
this.btnOK.UseVisualStyleBackColor = true;
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
this.btnOK.Click += new System.EventHandler(this.btnOKClick);
//
// ExtractMethodForm
//
@ -136,9 +136,9 @@ namespace SharpRefactoring.Forms @@ -136,9 +136,9 @@ namespace SharpRefactoring.Forms
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Extract Method";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.ExtractMethodFormFormClosing);
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.Label label1;

56
src/AddIns/Misc/SharpRefactoring/Src/Forms/ExtractMethodForm.cs

@ -7,13 +7,14 @@ @@ -7,13 +7,14 @@
* Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
*/
using ICSharpCode.SharpDevelop.Refactoring;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using ICSharpCode.Core;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.PrettyPrinter;
namespace SharpRefactoring.Forms
{
@ -22,46 +23,59 @@ namespace SharpRefactoring.Forms @@ -22,46 +23,59 @@ namespace SharpRefactoring.Forms
/// </summary>
public partial class ExtractMethodForm : Form
{
public ExtractMethodForm(string name, string preview)
Func<IOutputAstVisitor> generator;
MethodDeclaration declaration;
BlockStatement body;
bool cancelUnload = false;
public ExtractMethodForm(MethodDeclaration declaration, Func<IOutputAstVisitor> generator)
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
this.txtName.Text = name;
this.txtPreview.Text = preview;
this.declaration = declaration;
this.generator = generator;
IOutputAstVisitor visitor = this.generator.Invoke();
body = declaration.Body;
declaration.Body = new BlockStatement();
declaration.AcceptVisitor(visitor, null);
txtName_TextChanged(null, EventArgs.Empty);
this.txtName.Text = this.declaration.Name;
this.txtPreview.Text = visitor.Text;
this.txtName.SelectAll();
}
private void btnOK_Click(object sender, EventArgs e)
void btnOKClick(object sender, EventArgs e)
{
if (FindReferencesAndRenameHelper.CheckName(this.txtName.Text, string.Empty)) {
this.Text = this.txtName.Text;
this.DialogResult = DialogResult.OK;
this.declaration.Body = body;
cancelUnload = false;
} else
cancelUnload = true;
}
private void btnCancel_Click(object sender, EventArgs e)
void btnCancelClick(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
private void txtName_TextChanged(object sender, EventArgs e)
void txtNameTextChanged(object sender, EventArgs e)
{
string text = this.txtPreview.Text;
if (string.IsNullOrEmpty(text))
return;
string afterName = text.Substring(text.IndexOf('('));
List<string> list = text.Split(' ').ToList();
list.RemoveAt(list.Count - 1);
declaration.Name = this.txtName.Text;
IOutputAstVisitor visitor = this.generator.Invoke();
declaration.AcceptVisitor(visitor, null);
this.txtPreview.Text = visitor.Text;
}
this.txtPreview.Text = string.Join(" ", list.ToArray()) + " " + this.txtName.Text + afterName;
void ExtractMethodFormFormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = cancelUnload;
}
}
}

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

@ -27,7 +27,7 @@ namespace SharpRefactoring @@ -27,7 +27,7 @@ namespace SharpRefactoring
/// <summary>
/// Description of MethodExtractorBase.
/// </summary>
public class MethodExtractorBase
public abstract class MethodExtractorBase
{
protected ICSharpCode.TextEditor.TextEditorControl textEditor;
protected ISelection currentSelection;
@ -36,7 +36,6 @@ namespace SharpRefactoring @@ -36,7 +36,6 @@ namespace SharpRefactoring
protected ParametrizedNode parentNode;
protected Statement caller;
protected List<LocalVariableDeclaration> beforeCallDeclarations;
protected IOutputAstVisitor output;
protected VariableDeclaration returnedVariable;
protected List<ISpecial> specialsList;
@ -51,12 +50,11 @@ namespace SharpRefactoring @@ -51,12 +50,11 @@ namespace SharpRefactoring
get { return extractedMethod; }
}
public MethodExtractorBase(ICSharpCode.TextEditor.TextEditorControl textEditor, ISelection selection, IOutputAstVisitor output)
public MethodExtractorBase(ICSharpCode.TextEditor.TextEditorControl textEditor, ISelection selection)
{
this.currentDocument = textEditor.Document;
this.textEditor = textEditor;
this.currentSelection = selection;
this.output = output;
}
protected static Statement CreateCaller(ParametrizedNode parent, MethodDeclaration method, VariableDeclaration returnVariable)
@ -106,18 +104,6 @@ namespace SharpRefactoring @@ -106,18 +104,6 @@ namespace SharpRefactoring
}
}
public string CreatePreview()
{
BlockStatement body = this.extractedMethod.Body;
this.extractedMethod.Body = new BlockStatement();
this.extractedMethod.AcceptVisitor(output, null);
this.extractedMethod.Body = body;
return output.Text;
}
public void InsertCall()
{
string call = GenerateCode(CreateCaller(this.parentNode, this.extractedMethod, this.returnedVariable), false);
@ -132,7 +118,9 @@ namespace SharpRefactoring @@ -132,7 +118,9 @@ namespace SharpRefactoring
public void InsertAfterCurrentMethod()
{
using (SpecialNodesInserter.Install(this.specialsList, this.output)) {
IOutputAstVisitor outputVisitor = this.GetOutputVisitor();
using (SpecialNodesInserter.Install(this.specialsList, outputVisitor)) {
string code = "\r\n\r\n" + GenerateCode(this.extractedMethod, true);
code = code.TrimEnd('\r', '\n', ' ', '\t');
@ -300,12 +288,8 @@ namespace SharpRefactoring @@ -300,12 +288,8 @@ namespace SharpRefactoring
return hav.HasAssignment;
}
public abstract IOutputAstVisitor GetOutputVisitor();
public virtual bool Extract()
{
throw new InvalidOperationException("Cannot use plain MethodExtractor, please use a language specific implementation!");
public abstract bool Extract();
}
}
}

Loading…
Cancel
Save