From c3fe371f8f8c949ad6695399e214506f752303b8 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sat, 15 Aug 2009 13:02:14 +0000 Subject: [PATCH] ported ExtractMethodCommand to ITextEditor git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@4684 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../SharpRefactoring/SharpRefactoring.csproj | 14 +++-- .../Src/CSharpMethodExtractor.cs | 23 ++++---- .../Src/ExtractMethodCommand.cs | 27 ++++------ .../Src/Forms/ExtractMethodForm.cs | 6 +-- .../Src/MethodExtractorBase.cs | 53 +++++++++---------- .../Visitors/FindJumpInstructionsVisitor.cs | 5 +- .../Src/Visitors/FindMemberVisitor.cs | 27 +++++----- 7 files changed, 70 insertions(+), 85 deletions(-) diff --git a/src/AddIns/Misc/SharpRefactoring/SharpRefactoring.csproj b/src/AddIns/Misc/SharpRefactoring/SharpRefactoring.csproj index de84e2978c..37aa4c8222 100644 --- a/src/AddIns/Misc/SharpRefactoring/SharpRefactoring.csproj +++ b/src/AddIns/Misc/SharpRefactoring/SharpRefactoring.csproj @@ -40,6 +40,12 @@ + + 3.0 + + + 3.0 + 3.5 @@ -47,6 +53,9 @@ + + 3.0 + @@ -76,11 +85,6 @@ - - {2D18BE89-D210-49EB-A9DD-2246FBB3DF6D} - ICSharpCode.TextEditor - False - {3A9AE6AA-BC07-4A2F-972C-581E3AE2F195} NRefactory diff --git a/src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs b/src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs index 5cc4fede65..471898ebc0 100644 --- a/src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs +++ b/src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs @@ -5,11 +5,11 @@ // $Revision: 3287 $ // +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; 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 { 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 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 List otherReturnValues = new List(); // 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 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 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)); diff --git a/src/AddIns/Misc/SharpRefactoring/Src/ExtractMethodCommand.cs b/src/AddIns/Misc/SharpRefactoring/Src/ExtractMethodCommand.cs index 7a2eba1802..4e0fcfe5f0 100644 --- a/src/AddIns/Misc/SharpRefactoring/Src/ExtractMethodCommand.cs +++ b/src/AddIns/Misc/SharpRefactoring/Src/ExtractMethodCommand.cs @@ -5,18 +5,14 @@ // $Revision$ // -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 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 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; } - }*/ + } } } diff --git a/src/AddIns/Misc/SharpRefactoring/Src/Forms/ExtractMethodForm.cs b/src/AddIns/Misc/SharpRefactoring/Src/Forms/ExtractMethodForm.cs index 2ac5fd05ed..34a3998f1d 100644 --- a/src/AddIns/Misc/SharpRefactoring/Src/Forms/ExtractMethodForm.cs +++ b/src/AddIns/Misc/SharpRefactoring/Src/Forms/ExtractMethodForm.cs @@ -4,12 +4,10 @@ // // $Revision$ // -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; diff --git a/src/AddIns/Misc/SharpRefactoring/Src/MethodExtractorBase.cs b/src/AddIns/Misc/SharpRefactoring/Src/MethodExtractorBase.cs index 6a1f117683..275d37a7cc 100644 --- a/src/AddIns/Misc/SharpRefactoring/Src/MethodExtractorBase.cs +++ b/src/AddIns/Misc/SharpRefactoring/Src/MethodExtractorBase.cs @@ -4,6 +4,7 @@ // // $Revision: 3287 $ // + using System; using System.Collections.Generic; using System.IO; @@ -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 /// 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 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 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 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 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 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 return null; } - FindMemberVisitor fmv = new FindMemberVisitor(startColumn, startLine, endColumn, endLine); + FindMemberVisitor fmv = new FindMemberVisitor(start, end); parser.CompilationUnit.AcceptVisitor(fmv, null); diff --git a/src/AddIns/Misc/SharpRefactoring/Src/Visitors/FindJumpInstructionsVisitor.cs b/src/AddIns/Misc/SharpRefactoring/Src/Visitors/FindJumpInstructionsVisitor.cs index c223c5ea11..9f84e3bf80 100644 --- a/src/AddIns/Misc/SharpRefactoring/Src/Visitors/FindJumpInstructionsVisitor.cs +++ b/src/AddIns/Misc/SharpRefactoring/Src/Visitors/FindJumpInstructionsVisitor.cs @@ -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 public class FindJumpInstructionsVisitor : AbstractAstVisitor { MethodDeclaration method; - ISelection selection; List labels; List cases; bool isOk = true; @@ -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(); this.cases = new List(); } diff --git a/src/AddIns/Misc/SharpRefactoring/Src/Visitors/FindMemberVisitor.cs b/src/AddIns/Misc/SharpRefactoring/Src/Visitors/FindMemberVisitor.cs index 2aed6274e7..976c934cff 100644 --- a/src/AddIns/Misc/SharpRefactoring/Src/Visitors/FindMemberVisitor.cs +++ b/src/AddIns/Misc/SharpRefactoring/Src/Visitors/FindMemberVisitor.cs @@ -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 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 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 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; }