Browse Source

ported ExtractMethodCommand to ITextEditor

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@4684 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Siegfried Pammer 17 years ago
parent
commit
c3fe371f8f
  1. 14
      src/AddIns/Misc/SharpRefactoring/SharpRefactoring.csproj
  2. 23
      src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs
  3. 27
      src/AddIns/Misc/SharpRefactoring/Src/ExtractMethodCommand.cs
  4. 6
      src/AddIns/Misc/SharpRefactoring/Src/Forms/ExtractMethodForm.cs
  5. 53
      src/AddIns/Misc/SharpRefactoring/Src/MethodExtractorBase.cs
  6. 5
      src/AddIns/Misc/SharpRefactoring/Src/Visitors/FindJumpInstructionsVisitor.cs
  7. 27
      src/AddIns/Misc/SharpRefactoring/Src/Visitors/FindMemberVisitor.cs

14
src/AddIns/Misc/SharpRefactoring/SharpRefactoring.csproj

@ -40,6 +40,12 @@ @@ -40,6 +40,12 @@
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
<ItemGroup>
<Reference Include="PresentationCore">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
<Reference Include="PresentationFramework">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
@ -47,6 +53,9 @@ @@ -47,6 +53,9 @@
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="SharpRefactoring.addin">
@ -76,11 +85,6 @@ @@ -76,11 +85,6 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Libraries\ICSharpCode.TextEditor\Project\ICSharpCode.TextEditor.csproj">
<Project>{2D18BE89-D210-49EB-A9DD-2246FBB3DF6D}</Project>
<Name>ICSharpCode.TextEditor</Name>
<Private>False</Private>
</ProjectReference>
<ProjectReference Include="..\..\..\Libraries\NRefactory\Project\NRefactory.csproj">
<Project>{3A9AE6AA-BC07-4A2F-972C-581E3AE2F195}</Project>
<Name>NRefactory</Name>

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

@ -5,11 +5,11 @@ @@ -5,11 +5,11 @@
// <version>$Revision: 3287 $</version>
// </file>
using ICSharpCode.SharpDevelop.Editor;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ICSharpCode.Core;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.Ast;
@ -18,7 +18,6 @@ using ICSharpCode.NRefactory.Visitors; @@ -18,7 +18,6 @@ using ICSharpCode.NRefactory.Visitors;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom.NRefactoryResolver;
using ICSharpCode.SharpDevelop.Project;
using ICSharpCode.TextEditor.Document;
using SharpRefactoring.Visitors;
using Dom = ICSharpCode.SharpDevelop.Dom;
@ -28,8 +27,8 @@ namespace SharpRefactoring @@ -28,8 +27,8 @@ namespace SharpRefactoring
{
static readonly StringComparer CSharpNameComparer = StringComparer.Ordinal;
public CSharpMethodExtractor(ICSharpCode.TextEditor.TextEditorControl textEditor, ISelection selection)
: base(textEditor, selection)
public CSharpMethodExtractor(ITextEditor textEditor)
: base(textEditor)
{
}
@ -47,7 +46,7 @@ namespace SharpRefactoring @@ -47,7 +46,7 @@ namespace SharpRefactoring
public override bool Extract()
{
using (var parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader("class Tmp { void Test() {\n " + this.currentSelection.SelectedText + "\n}}"))) {
using (var parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader("class Tmp { void Test() {\n " + this.textEditor.SelectedText + "\n}}"))) {
parser.Parse();
if (parser.Errors.Count > 0) {
@ -68,17 +67,17 @@ namespace SharpRefactoring @@ -68,17 +67,17 @@ namespace SharpRefactoring
List<VariableDeclaration> otherReturnValues = new List<VariableDeclaration>();
// Initialise new method
newMethod.Body = GetBlock(this.currentSelection.SelectedText);
newMethod.Body = GetBlock(this.textEditor.SelectedText);
newMethod.Body.StartLocation = new Location(0,0);
this.parentNode = GetParentMember(this.currentSelection.StartPosition.Line, this.currentSelection.StartPosition.Column, this.currentSelection.EndPosition.Line, this.currentSelection.EndPosition.Column);
this.parentNode = GetParentMember(start, end);
if (parentNode == null) {
MessageService.ShowError("${res:AddIns.SharpRefactoring.ExtractMethod.InvalidSelection}");
return false;
}
if (!CheckForJumpInstructions(newMethod, this.currentSelection))
if (!CheckForJumpInstructions(newMethod))
return false;
newMethod.Modifier = parentNode.Modifier;
@ -101,12 +100,12 @@ namespace SharpRefactoring @@ -101,12 +100,12 @@ namespace SharpRefactoring
bool isInitialized = (variable.Initializer != null) ? !variable.Initializer.IsNull : false;
bool hasAssignment = HasAssignment(newMethod, variable);
if (IsInSel(variable.StartPos, this.currentSelection) && hasOccurrencesAfter) {
if (IsInCurrentSelection(variable.StartPos) && hasOccurrencesAfter) {
possibleReturnValues.Add(new VariableDeclaration(variable.Name, variable.Initializer, variable.Type));
otherReturnValues.Add(new VariableDeclaration(variable.Name, variable.Initializer, variable.Type));
}
if (!(IsInSel(variable.StartPos, this.currentSelection) || IsInSel(variable.EndPos, this.currentSelection))) {
if (!(IsInCurrentSelection(variable.StartPos) || IsInCurrentSelection(variable.EndPos))) {
ParameterDeclarationExpression newParam = null;
if ((hasOccurrencesAfter && isInitialized) || variable.WasRefParam)
@ -169,7 +168,7 @@ namespace SharpRefactoring @@ -169,7 +168,7 @@ namespace SharpRefactoring
Dom.ExpressionResult res = new Dom.ExpressionResult(variable.Name,
Dom.DomRegion.FromLocation(variable.StartPos, variable.EndPos),
Dom.ExpressionContext.Default, null);
Dom.ResolveResult result = this.GetResolver().Resolve(res, info, this.textEditor.Document.TextContent);
Dom.ResolveResult result = this.GetResolver().Resolve(res, info, this.textEditor.Document.Text);
if (variable.Type.Type == "var")
variable.Type = Dom.Refactoring.CodeGenerator.ConvertType(result.ResolvedType, new Dom.ClassFinder(result.CallingMember));

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

@ -5,18 +5,14 @@ @@ -5,18 +5,14 @@
// <version>$Revision$</version>
// </file>
using ICSharpCode.SharpDevelop.Dom.Refactoring;
using System;
using System.Windows.Forms;
using ICSharpCode.Core;
using ICSharpCode.NRefactory.PrettyPrinter;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Dom.Refactoring;
using ICSharpCode.SharpDevelop.Editor;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Project;
using ICSharpCode.SharpDevelop.Refactoring;
using ICSharpCode.TextEditor;
using SharpRefactoring.Forms;
namespace SharpRefactoring
@ -26,7 +22,7 @@ namespace SharpRefactoring @@ -26,7 +22,7 @@ namespace SharpRefactoring
protected override void Run(ITextEditor textEditor, RefactoringProvider provider)
{
if (textEditor.SelectionLength > 0) {
/*
MethodExtractorBase extractor = GetCurrentExtractor(textEditor);
if (extractor != null) {
if (extractor.Extract()) {
@ -34,31 +30,28 @@ namespace SharpRefactoring @@ -34,31 +30,28 @@ namespace SharpRefactoring
if (form.ShowDialog() == DialogResult.OK) {
extractor.ExtractedMethod.Name = form.Text;
try {
textEditor.Document.UndoStack.StartUndoGroup();
using (textEditor.Document.OpenUndoGroup()) {
extractor.InsertAfterCurrentMethod();
extractor.InsertCall();
textEditor.Document.FormattingStrategy.IndentLines(textEditor.ActiveTextAreaControl.TextArea, 0, textEditor.Document.TotalNumberOfLines - 1);
} finally {
textEditor.Document.UndoStack.EndUndoGroup();
textEditor.Language.FormattingStrategy.IndentLines(textEditor, 0, textEditor.Document.TotalNumberOfLines - 1);
}
textEditor.ActiveTextAreaControl.SelectionManager.ClearSelection();
textEditor.Select(textEditor.SelectionStart, 0);
}
}
}*/
}
}
}
/*
MethodExtractorBase GetCurrentExtractor(TextEditorControl editor)
MethodExtractorBase GetCurrentExtractor(ITextEditor editor)
{
switch (ProjectBindingService.GetCodonPerCodeFileName(editor.FileName).Language) {
case "C#":
return new CSharpMethodExtractor(editor, editor.ActiveTextAreaControl.SelectionManager.SelectionCollection[0]);
return new CSharpMethodExtractor(editor);
default:
MessageService.ShowError(string.Format(StringParser.Parse("${res:AddIns.SharpRefactoring.ExtractMethodNotSupported}"), ProjectBindingService.GetCodonPerCodeFileName(editor.FileName).Language));
return null;
}
}*/
}
}
}

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

@ -4,12 +4,10 @@ @@ -4,12 +4,10 @@
// <owner name="Siegfried Pammer" email="sie_pam@gmx.at"/>
// <version>$Revision$</version>
// </file>
using ICSharpCode.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using ICSharpCode.NRefactory;
using ICSharpCode.Core;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.PrettyPrinter;
using ICSharpCode.SharpDevelop.Refactoring;

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

@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
// <owner name="Siegfried Pammer" email="sie_pam@gmx.at"/>
// <version>$Revision: 3287 $</version>
// </file>
using System;
using System.Collections.Generic;
using System.IO;
@ -15,8 +16,7 @@ using ICSharpCode.NRefactory.Ast; @@ -15,8 +16,7 @@ using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.PrettyPrinter;
using ICSharpCode.NRefactory.Visitors;
using ICSharpCode.SharpDevelop;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document;
using ICSharpCode.SharpDevelop.Editor;
using SharpRefactoring.Visitors;
using Dom = ICSharpCode.SharpDevelop.Dom;
@ -27,8 +27,7 @@ namespace SharpRefactoring @@ -27,8 +27,7 @@ namespace SharpRefactoring
/// </summary>
public abstract class MethodExtractorBase
{
protected ICSharpCode.TextEditor.TextEditorControl textEditor;
protected ISelection currentSelection;
protected ITextEditor textEditor;
protected IDocument currentDocument;
protected MethodDeclaration extractedMethod;
protected ParametrizedNode parentNode;
@ -50,14 +49,13 @@ namespace SharpRefactoring @@ -50,14 +49,13 @@ namespace SharpRefactoring
get { return extractedMethod; }
}
public MethodExtractorBase(ICSharpCode.TextEditor.TextEditorControl textEditor, ISelection selection)
public MethodExtractorBase(ITextEditor textEditor)
{
this.currentDocument = textEditor.Document;
this.textEditor = textEditor;
this.currentSelection = selection;
this.start = new Location(this.currentSelection.StartPosition.Column + 1, this.currentSelection.StartPosition.Line + 1);
this.end = new Location(this.currentSelection.EndPosition.Column + 1, this.currentSelection.EndPosition.Line + 1);
this.start = this.currentDocument.OffsetToPosition(this.textEditor.SelectionStart);
this.end = this.currentDocument.OffsetToPosition(this.textEditor.SelectionStart + this.textEditor.SelectionLength);
}
protected static Statement CreateCaller(ParametrizedNode parent, MethodDeclaration method, VariableDeclaration returnVariable)
@ -116,7 +114,7 @@ namespace SharpRefactoring @@ -116,7 +114,7 @@ namespace SharpRefactoring
builder.AppendLine(GenerateCode(v, false));
}
this.currentDocument.Replace(this.currentSelection.Offset, this.currentSelection.Length, builder.ToString() + "\r\n" + call);
this.currentDocument.Replace(this.textEditor.SelectionStart, this.textEditor.SelectionLength, builder.ToString() + "\r\n" + call);
}
public void InsertAfterCurrentMethod()
@ -128,30 +126,32 @@ namespace SharpRefactoring @@ -128,30 +126,32 @@ namespace SharpRefactoring
code = code.TrimEnd('\r', '\n', ' ', '\t');
Dom.IMember p = GetParentMember(this.textEditor, this.currentSelection.StartPosition.Line + 1, this.currentSelection.StartPosition.Column + 1);
TextLocation loc = new ICSharpCode.TextEditor.TextLocation(
p.BodyRegion.EndColumn - 1, p.BodyRegion.EndLine - 1);
Dom.IMember p = GetParentMember(this.textEditor, start.Line, start.Column);
int offset = textEditor.Document.PositionToOffset(loc);
int offset = textEditor.Document.PositionToOffset(p.BodyRegion.EndLine, p.BodyRegion.EndColumn);
textEditor.Document.Insert(offset, code);
}
}
protected static bool CheckForJumpInstructions(MethodDeclaration method, ISelection selection)
protected static bool CheckForJumpInstructions(MethodDeclaration method)
{
FindJumpInstructionsVisitor fjiv = new FindJumpInstructionsVisitor(method, selection);
FindJumpInstructionsVisitor fjiv = new FindJumpInstructionsVisitor(method);
method.AcceptVisitor(fjiv, null);
return fjiv.IsOk;
}
protected static bool IsInSel(Location location, ISelection sel)
protected bool IsInCurrentSelection(Location location)
{
bool result = (sel.ContainsPosition(new ICSharpCode.TextEditor.TextLocation(location.Column - 1, location.Line - 1)));
return result;
return IsInCurrentSelection(textEditor.Document.PositionToOffset(location.Line, location.Column));
}
protected bool IsInCurrentSelection(int offset)
{
return (offset >= textEditor.SelectionStart &&
offset < (textEditor.SelectionStart + textEditor.SelectionLength));
}
protected static BlockStatement GetBlock(string data)
@ -198,17 +198,14 @@ namespace SharpRefactoring @@ -198,17 +198,14 @@ namespace SharpRefactoring
return expressions;
}
protected virtual string GenerateCode(INode unit, bool installSpecials)
{
throw new InvalidOperationException("Cannot use plain MethodExtractor, please use a language specific implementation!");
}
protected abstract string GenerateCode(INode unit, bool installSpecials);
protected Dom.IMember GetParentMember(ICSharpCode.TextEditor.TextEditorControl textEditor, TextLocation location)
protected Dom.IMember GetParentMember(ITextEditor textEditor, Location location)
{
return GetParentMember(textEditor, location.Line, location.Column);
}
protected Dom.IMember GetParentMember(ICSharpCode.TextEditor.TextEditorControl textEditor, int line, int column)
protected Dom.IMember GetParentMember(ITextEditor textEditor, int line, int column)
{
Dom.ParseInformation parseInfo = ParserService.GetParseInformation(textEditor.FileName);
if (parseInfo != null) {
@ -230,9 +227,9 @@ namespace SharpRefactoring @@ -230,9 +227,9 @@ namespace SharpRefactoring
return null;
}
protected ParametrizedNode GetParentMember(int startLine, int startColumn, int endLine, int endColumn)
protected ParametrizedNode GetParentMember(Location start, Location end)
{
using (IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(this.currentDocument.TextContent))) {
using (IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(this.currentDocument.Text))) {
parser.Parse();
if (parser.Errors.Count > 0) {
@ -240,7 +237,7 @@ namespace SharpRefactoring @@ -240,7 +237,7 @@ namespace SharpRefactoring
return null;
}
FindMemberVisitor fmv = new FindMemberVisitor(startColumn, startLine, endColumn, endLine);
FindMemberVisitor fmv = new FindMemberVisitor(start, end);
parser.CompilationUnit.AcceptVisitor(fmv, null);

5
src/AddIns/Misc/SharpRefactoring/Src/Visitors/FindJumpInstructionsVisitor.cs

@ -12,7 +12,6 @@ using ICSharpCode.NRefactory; @@ -12,7 +12,6 @@ using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.Visitors;
using Dom = ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.TextEditor.Document;
namespace SharpRefactoring.Visitors
{
@ -22,7 +21,6 @@ namespace SharpRefactoring.Visitors @@ -22,7 +21,6 @@ namespace SharpRefactoring.Visitors
public class FindJumpInstructionsVisitor : AbstractAstVisitor
{
MethodDeclaration method;
ISelection selection;
List<LabelStatement> labels;
List<CaseLabel> cases;
bool isOk = true;
@ -31,10 +29,9 @@ namespace SharpRefactoring.Visitors @@ -31,10 +29,9 @@ namespace SharpRefactoring.Visitors
get { return isOk; }
}
public FindJumpInstructionsVisitor(MethodDeclaration method, ISelection selection)
public FindJumpInstructionsVisitor(MethodDeclaration method)
{
this.method = method;
this.selection = selection;
this.labels = new List<LabelStatement>();
this.cases = new List<CaseLabel>();
}

27
src/AddIns/Misc/SharpRefactoring/Src/Visitors/FindMemberVisitor.cs

@ -14,26 +14,23 @@ namespace SharpRefactoring.Visitors @@ -14,26 +14,23 @@ namespace SharpRefactoring.Visitors
{
public class FindMemberVisitor : AbstractAstVisitor
{
int startColumn, startLine;
int endColumn, endLine;
Location start, end;
ParametrizedNode member = null;
public ParametrizedNode Member {
get { return member; }
}
public FindMemberVisitor(int startColumn, int startLine, int endColumn, int endLine)
public FindMemberVisitor(Location start, Location end)
{
this.startColumn = startColumn;
this.startLine = startLine;
this.endColumn = endColumn;
this.endLine = endLine;
this.start = start;
this.end = end;
}
public override object VisitMethodDeclaration(MethodDeclaration methodDeclaration, object data)
{
if ((methodDeclaration.Body.StartLocation < new Location(startColumn + 1, startLine + 1)) &&
(methodDeclaration.Body.EndLocation > new Location(endColumn + 1, endLine + 1))) {
if ((methodDeclaration.Body.StartLocation < start) &&
(methodDeclaration.Body.EndLocation > end)) {
this.member = methodDeclaration;
}
@ -42,8 +39,8 @@ namespace SharpRefactoring.Visitors @@ -42,8 +39,8 @@ namespace SharpRefactoring.Visitors
public override object VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration, object data)
{
if ((propertyDeclaration.BodyStart < new Location(startColumn + 1, startLine + 1)) &&
(propertyDeclaration.BodyEnd > new Location(endColumn + 1, endLine + 1))) {
if ((propertyDeclaration.BodyStart < start) &&
(propertyDeclaration.BodyEnd > end)) {
this.member = propertyDeclaration;
}
return base.VisitPropertyDeclaration(propertyDeclaration, data);
@ -51,8 +48,8 @@ namespace SharpRefactoring.Visitors @@ -51,8 +48,8 @@ namespace SharpRefactoring.Visitors
public override object VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration, object data)
{
if ((constructorDeclaration.Body.StartLocation < new Location(startColumn + 1, startLine + 1)) &&
(constructorDeclaration.Body.EndLocation > new Location(endColumn + 1, endLine + 1))) {
if ((constructorDeclaration.Body.StartLocation < start) &&
(constructorDeclaration.Body.EndLocation > end)) {
this.member = constructorDeclaration;
}
@ -61,8 +58,8 @@ namespace SharpRefactoring.Visitors @@ -61,8 +58,8 @@ namespace SharpRefactoring.Visitors
public override object VisitOperatorDeclaration(OperatorDeclaration operatorDeclaration, object data)
{
if ((operatorDeclaration.Body.StartLocation < new Location(startColumn + 1, startLine + 1)) &&
(operatorDeclaration.Body.EndLocation > new Location(endColumn + 1, endLine + 1))) {
if ((operatorDeclaration.Body.StartLocation < start) &&
(operatorDeclaration.Body.EndLocation > end)) {
this.member = operatorDeclaration;
}

Loading…
Cancel
Save