Browse Source

Refactoring: decoupled 'Implement interface' actions from menu building.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@6181 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Martin Koníček 15 years ago
parent
commit
7102b476e2
  1. 32
      src/AddIns/Misc/SharpRefactoring/Project/Src/ClassRefactoringSubmenuBuilder.cs
  2. 52
      src/Main/Base/Project/Src/Services/RefactoringService/RefactoringService.cs

32
src/AddIns/Misc/SharpRefactoring/Project/Src/ClassRefactoringSubmenuBuilder.cs

@ -137,41 +137,35 @@ namespace SharpRefactoring @@ -137,41 +137,35 @@ namespace SharpRefactoring
CodeGenerator codeGen = c.ProjectContent.Language.CodeGenerator;
if (codeGen == null) return;
List<ToolStripItem> subItems = new List<ToolStripItem>();
if (c.ProjectContent.Language.SupportsImplicitInterfaceImplementation) {
// 'Implement interface (implicit)' menu item with subitems
AddImplementInterfaceCommandItems(subItems, c, false);
if (subItems.Count > 0) {
list.Add(new ICSharpCode.Core.WinForms.Menu("${res:SharpDevelop.Refactoring.ImplementInterfaceImplicit}", subItems.ToArray()));
subItems = new List<ToolStripItem>();
}
}
AddImplementInterfaceCommandItems(subItems, c, true);
// 'Implement interface (explicit)' menu item with subitems
AddImplementInterfaceCommandItems(subItems, c, true);
if (subItems.Count > 0) {
if (c.ProjectContent.Language.SupportsImplicitInterfaceImplementation) {
list.Add(new ICSharpCode.Core.WinForms.Menu("${res:SharpDevelop.Refactoring.ImplementInterfaceExplicit}", subItems.ToArray()));
} else {
list.Add(new ICSharpCode.Core.WinForms.Menu("${res:SharpDevelop.Refactoring.ImplementInterface}", subItems.ToArray()));
}
string explicitMenuItemLabel = StringParser.Parse(c.ProjectContent.Language.SupportsImplicitInterfaceImplementation
? "${res:SharpDevelop.Refactoring.ImplementInterfaceExplicit}"
: "${res:SharpDevelop.Refactoring.ImplementInterface}");
list.Add(new ICSharpCode.Core.WinForms.Menu(explicitMenuItemLabel, subItems.ToArray()));
}
}
void AddImplementInterfaceCommandItems(List<ToolStripItem> subItems, IClass c, bool explicitImpl)
{
CodeGenerator codeGen = c.ProjectContent.Language.CodeGenerator;
IAmbience ambience = AmbienceService.GetCurrentAmbience();
ambience.ConversionFlags = ConversionFlags.ShowTypeParameterList;
foreach (IReturnType rt in c.BaseTypes) {
IClass interf = rt.GetUnderlyingClass();
if (interf != null && interf.ClassType == ClassType.Interface) {
IReturnType rtCopy = rt; // copy for access by anonymous method
EventHandler eh = delegate {
var d = FindReferencesAndRenameHelper.GetDocument(c);
if (d != null)
codeGen.ImplementInterface(rtCopy, new RefactoringDocumentAdapter(d), explicitImpl, c);
ParserService.ParseCurrentViewContent();
};
subItems.Add(new MenuCommand(ambience.Convert(interf), eh));
}
foreach (var implementInterfaceAction in RefactoringService.GetImplementInterfaceActions(c, explicitImpl)) {
var implementInterfaceA = implementInterfaceAction;
subItems.Add(new MenuCommand(
ambience.Convert(implementInterfaceAction.Interface),
delegate { implementInterfaceA.Execute(); }));
}
}

52
src/Main/Base/Project/Src/Services/RefactoringService/RefactoringService.cs

@ -6,14 +6,15 @@ @@ -6,14 +6,15 @@
// </file>
using System;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Dom.Refactoring;
using ICSharpCode.SharpDevelop.Editor;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Project;
@ -471,5 +472,52 @@ namespace ICSharpCode.SharpDevelop.Refactoring @@ -471,5 +472,52 @@ namespace ICSharpCode.SharpDevelop.Refactoring
}
}
#endregion
#region Interface / abstract class implementation
/// <summary>
/// Gets actions which can add implementation of interface to this class.
/// </summary>
public static IEnumerable<ImplementInterfaceAction> GetImplementInterfaceActions(IClass c, bool isExplicitImpl)
{
IAmbience ambience = c.ProjectContent.Language.GetAmbience();
ambience.ConversionFlags = ConversionFlags.ShowTypeParameterList;
foreach (IReturnType rt in c.BaseTypes) {
IClass interf = rt.GetUnderlyingClass();
if (interf != null && interf.ClassType == ClassType.Interface) {
IReturnType rtCopy = rt;
yield return new ImplementInterfaceAction(rtCopy, c, isExplicitImpl);
}
}
}
#endregion
}
/// <summary>
/// Action describing how to add implementation of an interface to a class.
/// </summary>
public class ImplementInterfaceAction
{
public IReturnType Interface { get; private set; }
public IClass TargetClass { get; private set; }
public bool IsExplicitImpl { get; private set; }
public void Execute()
{
var codeGen = TargetClass.ProjectContent.Language.CodeGenerator;
var d = FindReferencesAndRenameHelper.GetDocument(TargetClass);
if (d != null)
codeGen.ImplementInterface(this.Interface, new RefactoringDocumentAdapter(d), this.IsExplicitImpl, this.TargetClass);
ParserService.ParseCurrentViewContent();
}
public ImplementInterfaceAction(IReturnType interfaceType, IClass targetClass, bool isExplicitImpl)
{
if (interfaceType == null)
throw new ArgumentNullException("interfaceType");
if (targetClass == null)
throw new ArgumentNullException("targetClass");
this.Interface = interfaceType;
this.TargetClass = targetClass;
this.IsExplicitImpl = isExplicitImpl;
}
}
}

Loading…
Cancel
Save