From 1c425ad58c9c09ecb4378d53a33acdda20063ea2 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Mon, 6 Sep 2010 18:48:49 +0200 Subject: [PATCH] add code skeleton creation code in OverrideToStringMethod --- .../Src/OverrideToStringMethodRefactoring.cs | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/OverrideToStringMethodRefactoring.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/OverrideToStringMethodRefactoring.cs index ce44657c20..c8dc2718c2 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/OverrideToStringMethodRefactoring.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/OverrideToStringMethodRefactoring.cs @@ -2,8 +2,10 @@ // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; +using System.Linq; using ICSharpCode.AvalonEdit.Editing; using ICSharpCode.AvalonEdit.Snippets; +using ICSharpCode.NRefactory.Ast; using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.SharpDevelop.Dom.Refactoring; @@ -17,6 +19,14 @@ namespace SharpRefactoring { public void Insert(CompletionContext context, ICompletionItem item) { + if (item == null) + throw new ArgumentNullException("item"); + + if (!(item is OverrideCompletionItem)) + throw new ArgumentException("item is not an OverrideCompletionItem"); + + OverrideCompletionItem completionItem = item as OverrideCompletionItem; + ITextEditor textEditor = context.Editor; IEditorUIService uiService = textEditor.GetService(typeof(IEditorUIService)) as IEditorUIService; @@ -31,21 +41,35 @@ namespace SharpRefactoring CodeGenerator generator = parseInfo.CompilationUnit.Language.CodeGenerator; IClass current = parseInfo.CompilationUnit.GetInnermostClass(textEditor.Caret.Line, textEditor.Caret.Column); + ClassFinder finder = new ClassFinder(current, textEditor.Caret.Line, textEditor.Caret.Column); if (current == null) return; - ITextAnchor anchor = textEditor.Document.CreateAnchor(textEditor.Caret.Offset); - anchor.MovementType = AnchorMovementType.AfterInsertion; + ITextAnchor endAnchor = textEditor.Document.CreateAnchor(textEditor.Caret.Offset); + endAnchor.MovementType = AnchorMovementType.AfterInsertion; ITextAnchor startAnchor = textEditor.Document.CreateAnchor(textEditor.Caret.Offset); - anchor.MovementType = AnchorMovementType.BeforeInsertion; + startAnchor.MovementType = AnchorMovementType.BeforeInsertion; + + MethodDeclaration member = (MethodDeclaration)generator.GetOverridingMethod(completionItem.Member, finder); + + string indent = DocumentUtilitites.GetWhitespaceBefore(textEditor.Document, textEditor.Caret.Offset); + string codeForBaseCall = generator.GenerateCode(member.Body.Children.OfType().First(), ""); + string code = generator.GenerateCode(member, indent); + int marker = code.IndexOf(codeForBaseCall); + + textEditor.Document.Insert(startAnchor.Offset, code.Substring(0, marker).TrimStart()); + + ITextAnchor insertionPos = textEditor.Document.CreateAnchor(endAnchor.Offset); + insertionPos.MovementType = AnchorMovementType.BeforeInsertion; InsertionContext insertionContext = new InsertionContext(textEditor.GetService(typeof(TextArea)) as TextArea, startAnchor.Offset); - AbstractInlineRefactorDialog dialog = new OverrideToStringMethodDialog(insertionContext, textEditor, startAnchor, anchor, current.Fields); + AbstractInlineRefactorDialog dialog = new OverrideToStringMethodDialog(insertionContext, textEditor, startAnchor, insertionPos, current.Fields); + dialog.Element = uiService.CreateInlineUIElement(insertionPos, dialog); - dialog.Element = uiService.CreateInlineUIElement(anchor, dialog); + textEditor.Document.InsertNormalized(endAnchor.Offset, Environment.NewLine + code.Substring(marker + codeForBaseCall.Length)); insertionContext.RegisterActiveElement(new InlineRefactorSnippetElement(cxt => null, ""), dialog); insertionContext.RaiseInsertionCompleted(EventArgs.Empty);