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

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

@ -36,7 +36,7 @@ namespace SharpRefactoring
MethodExtractorBase extractor = GetCurrentExtractor(textEditor); MethodExtractorBase extractor = GetCurrentExtractor(textEditor);
if (extractor != null) { if (extractor != null) {
if (extractor.Extract()) { 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) { if (form.ShowDialog() == DialogResult.OK) {
extractor.ExtractedMethod.Name = form.Text; extractor.ExtractedMethod.Name = form.Text;

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

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

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

@ -7,13 +7,14 @@
* Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern. * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
*/ */
using ICSharpCode.SharpDevelop.Refactoring;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.Linq; using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
using ICSharpCode.NRefactory;
using ICSharpCode.Core; using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.PrettyPrinter;
namespace SharpRefactoring.Forms namespace SharpRefactoring.Forms
{ {
@ -22,46 +23,59 @@ namespace SharpRefactoring.Forms
/// </summary> /// </summary>
public partial class ExtractMethodForm : Form 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. // The InitializeComponent() call is required for Windows Forms designer support.
// //
InitializeComponent(); InitializeComponent();
this.txtName.Text = name;
this.txtPreview.Text = preview;
txtName_TextChanged(null, EventArgs.Empty); this.declaration = declaration;
this.generator = generator;
this.txtName.SelectAll(); 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) void btnOKClick(object sender, EventArgs e)
{ {
this.Text = this.txtName.Text; if (FindReferencesAndRenameHelper.CheckName(this.txtName.Text, string.Empty)) {
this.DialogResult = DialogResult.OK; this.Text = this.txtName.Text;
} this.DialogResult = DialogResult.OK;
this.declaration.Body = body;
private void btnCancel_Click(object sender, EventArgs e) cancelUnload = false;
{ } else
this.DialogResult = DialogResult.Cancel; cancelUnload = true;
} }
private void txtName_TextChanged(object sender, EventArgs e)
{
string text = this.txtPreview.Text;
if (string.IsNullOrEmpty(text))
return;
string afterName = text.Substring(text.IndexOf('(')); void btnCancelClick(object sender, EventArgs e)
{
List<string> list = text.Split(' ').ToList(); this.DialogResult = DialogResult.Cancel;
}
list.RemoveAt(list.Count - 1);
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
/// <summary> /// <summary>
/// Description of MethodExtractorBase. /// Description of MethodExtractorBase.
/// </summary> /// </summary>
public class MethodExtractorBase public abstract class MethodExtractorBase
{ {
protected ICSharpCode.TextEditor.TextEditorControl textEditor; protected ICSharpCode.TextEditor.TextEditorControl textEditor;
protected ISelection currentSelection; protected ISelection currentSelection;
@ -36,7 +36,6 @@ namespace SharpRefactoring
protected ParametrizedNode parentNode; protected ParametrizedNode parentNode;
protected Statement caller; protected Statement caller;
protected List<LocalVariableDeclaration> beforeCallDeclarations; protected List<LocalVariableDeclaration> beforeCallDeclarations;
protected IOutputAstVisitor output;
protected VariableDeclaration returnedVariable; protected VariableDeclaration returnedVariable;
protected List<ISpecial> specialsList; protected List<ISpecial> specialsList;
@ -51,12 +50,11 @@ namespace SharpRefactoring
get { return extractedMethod; } 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.currentDocument = textEditor.Document;
this.textEditor = textEditor; this.textEditor = textEditor;
this.currentSelection = selection; this.currentSelection = selection;
this.output = output;
} }
protected static Statement CreateCaller(ParametrizedNode parent, MethodDeclaration method, VariableDeclaration returnVariable) protected static Statement CreateCaller(ParametrizedNode parent, MethodDeclaration method, VariableDeclaration returnVariable)
@ -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() public void InsertCall()
{ {
string call = GenerateCode(CreateCaller(this.parentNode, this.extractedMethod, this.returnedVariable), false); string call = GenerateCode(CreateCaller(this.parentNode, this.extractedMethod, this.returnedVariable), false);
@ -132,7 +118,9 @@ namespace SharpRefactoring
public void InsertAfterCurrentMethod() 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); string code = "\r\n\r\n" + GenerateCode(this.extractedMethod, true);
code = code.TrimEnd('\r', '\n', ' ', '\t'); code = code.TrimEnd('\r', '\n', ' ', '\t');
@ -299,13 +287,9 @@ namespace SharpRefactoring
return hav.HasAssignment; return hav.HasAssignment;
} }
public virtual bool Extract() public abstract IOutputAstVisitor GetOutputVisitor();
{
throw new InvalidOperationException("Cannot use plain MethodExtractor, please use a language specific implementation!"); public abstract bool Extract();
}
} }
} }

Loading…
Cancel
Save