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. 206
      src/AddIns/Misc/SharpRefactoring/Src/Forms/ExtractMethodForm.Designer.cs
  4. 82
      src/AddIns/Misc/SharpRefactoring/Src/Forms/ExtractMethodForm.cs
  5. 32
      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;

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

@ -36,109 +36,109 @@ namespace SharpRefactoring.Forms @@ -36,109 +36,109 @@ namespace SharpRefactoring.Forms
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.txtPreview = new System.Windows.Forms.TextBox();
this.btnCancel = new System.Windows.Forms.Button();
this.btnOK = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(167, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Enter a name for the new method:";
//
// label2
//
this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Preview:";
//
// txtName
//
this.txtName.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtName.Location = new System.Drawing.Point(12, 25);
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);
//
// txtPreview
//
this.txtPreview.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtPreview.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.txtPreview.Location = new System.Drawing.Point(12, 64);
this.txtPreview.Multiline = true;
this.txtPreview.Name = "txtPreview";
this.txtPreview.ReadOnly = true;
this.txtPreview.Size = new System.Drawing.Size(467, 151);
this.txtPreview.TabIndex = 3;
//
// btnCancel
//
this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(404, 221);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 23);
this.btnCancel.TabIndex = 4;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// btnOK
//
this.btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnOK.Location = new System.Drawing.Point(323, 221);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(75, 23);
this.btnOK.TabIndex = 5;
this.btnOK.Text = "OK";
this.btnOK.UseVisualStyleBackColor = true;
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// ExtractMethodForm
//
this.AcceptButton = this.btnOK;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(491, 252);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.txtPreview);
this.Controls.Add(this.txtName);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "ExtractMethodForm";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Extract Method";
this.ResumeLayout(false);
this.PerformLayout();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.txtPreview = new System.Windows.Forms.TextBox();
this.btnCancel = new System.Windows.Forms.Button();
this.btnOK = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(167, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Enter a name for the new method:";
//
// label2
//
this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Preview:";
//
// txtName
//
this.txtName.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtName.Location = new System.Drawing.Point(12, 25);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(467, 20);
this.txtName.TabIndex = 2;
this.txtName.TextChanged += new System.EventHandler(this.txtNameTextChanged);
//
// txtPreview
//
this.txtPreview.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtPreview.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.txtPreview.Location = new System.Drawing.Point(12, 64);
this.txtPreview.Multiline = true;
this.txtPreview.Name = "txtPreview";
this.txtPreview.ReadOnly = true;
this.txtPreview.Size = new System.Drawing.Size(467, 151);
this.txtPreview.TabIndex = 3;
//
// btnCancel
//
this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(404, 221);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 23);
this.btnCancel.TabIndex = 4;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.btnCancelClick);
//
// btnOK
//
this.btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnOK.Location = new System.Drawing.Point(323, 221);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(75, 23);
this.btnOK.TabIndex = 5;
this.btnOK.Text = "OK";
this.btnOK.UseVisualStyleBackColor = true;
this.btnOK.Click += new System.EventHandler(this.btnOKClick);
//
// ExtractMethodForm
//
this.AcceptButton = this.btnOK;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(491, 252);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.txtPreview);
this.Controls.Add(this.txtName);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "ExtractMethodForm";
this.ShowIcon = false;
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;

82
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;
txtName_TextChanged(null, EventArgs.Empty);
this.txtName.SelectAll();
this.declaration = declaration;
this.generator = generator;
IOutputAstVisitor visitor = this.generator.Invoke();
body = declaration.Body;
declaration.Body = new BlockStatement();
declaration.AcceptVisitor(visitor, null);
this.txtName.Text = this.declaration.Name;
this.txtPreview.Text = visitor.Text;
this.txtName.SelectAll();
}
private void btnOK_Click(object sender, EventArgs e)
{
this.Text = this.txtName.Text;
this.DialogResult = DialogResult.OK;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
private void txtName_TextChanged(object sender, EventArgs e)
{
string text = this.txtPreview.Text;
if (string.IsNullOrEmpty(text))
return;
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;
}
string afterName = text.Substring(text.IndexOf('('));
List<string> list = text.Split(' ').ToList();
list.RemoveAt(list.Count - 1);
void btnCancelClick(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
this.txtPreview.Text = string.Join(" ", list.ToArray()) + " " + this.txtName.Text + afterName;
}
void txtNameTextChanged(object sender, EventArgs e)
{
declaration.Name = this.txtName.Text;
IOutputAstVisitor visitor = this.generator.Invoke();
declaration.AcceptVisitor(visitor, null);
this.txtPreview.Text = visitor.Text;
}
void ExtractMethodFormFormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = cancelUnload;
}
}
}

32
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');
@ -299,13 +287,9 @@ namespace SharpRefactoring @@ -299,13 +287,9 @@ namespace SharpRefactoring
return hav.HasAssignment;
}
public virtual bool Extract()
{
throw new InvalidOperationException("Cannot use plain MethodExtractor, please use a language specific implementation!");
}
public abstract IOutputAstVisitor GetOutputVisitor();
public abstract bool Extract();
}
}

Loading…
Cancel
Save