From abef6e6f74c4d6445d026fe930bdd6f05aa3e456 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Thu, 9 Sep 2010 10:49:50 +0200 Subject: [PATCH] Implement * refactorings do now work properly with partial classes --- .../Src/ContextActions/ImplementAbstractClass.cs | 3 +-- .../Src/ContextActions/ImplementInterface.cs | 6 +++--- .../ContextActions/ImplementInterfaceExplicit.cs | 6 +++--- .../Project/Src/RefactoringHelpers.cs | 15 ++++++++++++++- .../FindReferencesAndRenameHelper.cs | 3 +++ 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementAbstractClass.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementAbstractClass.cs index 5e1eb2eae6..ffe41d0045 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementAbstractClass.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementAbstractClass.cs @@ -20,8 +20,7 @@ namespace SharpRefactoring.ContextActions { public override IEnumerable GetAvailableActions(EditorContext editorContext) { - foreach (var targetClass in editorContext.GetClassDeclarationsOnCurrentLine().Where(c => c.ClassType == ClassType.Class)) { - + foreach (var targetClass in editorContext.GetClassDeclarationsOnCurrentLine().Where(c => c.ClassType == ClassType.Class).Select(c2 => c2.GetCurrentClassPart(editorContext.Editor.FileName))) { foreach (var implementAction in RefactoringService.GetImplementAbstractClassActions(targetClass)) { yield return implementAction; } diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementInterface.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementInterface.cs index e965a7d47a..d309b88574 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementInterface.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementInterface.cs @@ -26,9 +26,9 @@ namespace SharpRefactoring.ContextActions // Using CurrentLineAST is basically OK, but when the "class" keyword is on different line than class name, // parsing only one line never tells us that we are looking at TypeDeclaration // Alternative solution could be to try to resolve also IdentifierExpression to see if it is class declaration. - foreach (var targetClass in editorContext.GetClassDeclarationsOnCurrentLine(). - Where(c => c.ClassType == ClassType.Class || c.ClassType == ClassType.Interface)) { - + foreach (var targetClass in editorContext.GetClassDeclarationsOnCurrentLine() + .Where(c => c.ClassType == ClassType.Class || c.ClassType == ClassType.Interface) + .Select(c2 => c2.GetCurrentClassPart(editorContext.Editor.FileName))) { foreach (var implementAction in RefactoringService.GetImplementInterfaceActions(targetClass)) { yield return implementAction; } diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementInterfaceExplicit.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementInterfaceExplicit.cs index 9f326eda84..889072990a 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementInterfaceExplicit.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementInterfaceExplicit.cs @@ -23,9 +23,9 @@ namespace SharpRefactoring.ContextActions { public override IEnumerable GetAvailableActions(EditorContext editorContext) { - foreach (var targetClass in editorContext.GetClassDeclarationsOnCurrentLine(). - Where(c => c.ClassType == ClassType.Class || c.ClassType == ClassType.Interface)) { - + foreach (var targetClass in editorContext.GetClassDeclarationsOnCurrentLine() + .Where(c => c.ClassType == ClassType.Class || c.ClassType == ClassType.Interface) + .Select(c2 => c2.GetCurrentClassPart(editorContext.Editor.FileName))) { foreach (var implementAction in RefactoringService.GetImplementInterfaceActions(targetClass, true)) { yield return implementAction; } diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/RefactoringHelpers.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/RefactoringHelpers.cs index 842a523cb0..a8f7ebe6d6 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/RefactoringHelpers.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/RefactoringHelpers.cs @@ -5,11 +5,12 @@ using System; using System.IO; using ICSharpCode.Core; using ICSharpCode.SharpDevelop; +using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.SharpDevelop.Project; namespace SharpRefactoring { - public class RefactoringHelpers + public static class RefactoringHelpers { /// /// Renames file as well as files it is dependent upon. @@ -39,5 +40,17 @@ namespace SharpRefactoring } } } + + public static IClass GetCurrentClassPart(this IClass c, string fileName) + { + if (c is CompoundClass) { + foreach (IClass part in ((CompoundClass)c).Parts) { + if (fileName.Equals(part.CompilationUnit.FileName, StringComparison.OrdinalIgnoreCase)) + return part; + } + } + + return c; + } } } diff --git a/src/Main/Base/Project/Src/Services/RefactoringService/FindReferencesAndRenameHelper.cs b/src/Main/Base/Project/Src/Services/RefactoringService/FindReferencesAndRenameHelper.cs index 4cb9ec0b19..5cac72b0cb 100644 --- a/src/Main/Base/Project/Src/Services/RefactoringService/FindReferencesAndRenameHelper.cs +++ b/src/Main/Base/Project/Src/Services/RefactoringService/FindReferencesAndRenameHelper.cs @@ -509,6 +509,9 @@ namespace ICSharpCode.SharpDevelop.Refactoring public static IDocument GetDocument(IClass c) { + if (c is CompoundClass) + throw new ArgumentException("Cannot get document from compound class - must pass a specific class part"); + ITextEditorProvider tecp = FileService.OpenFile(c.CompilationUnit.FileName) as ITextEditorProvider; if (tecp == null) return null; return tecp.TextEditor.Document;