forked from rubberduck-vba/Rubberduck
/
CommandBarsModule.cs
233 lines (203 loc) · 9.03 KB
/
CommandBarsModule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Input;
using Microsoft.Office.Core;
using Microsoft.Vbe.Interop;
using Ninject;
using Ninject.Modules;
using Rubberduck.Navigation;
using Rubberduck.UI.Command;
using Rubberduck.UI.Command.MenuItems;
using Rubberduck.UI.Command.MenuItems.ParentMenus;
namespace Rubberduck.Root
{
public class CommandBarsModule : NinjectModule
{
private const int MENU_BAR = 1;
private const int CODE_WINDOW = 9;
private const int PROJECT_WINDOW = 14;
private const int MS_FORMS = 17;
private const int MS_FORMS_CONTROL = 18;
private readonly IKernel _kernel;
public CommandBarsModule(IKernel kernel)
{
_kernel = kernel;
}
public override void Load()
{
BindCommandsToMenuItems();
ConfigureRubberduckMenu();
ConfigureCodePaneContextMenu();
ConfigureFormDesignerContextMenu();
ConfigureFormDesignerControlContextMenu();
ConfigureProjectExplorerContextMenu();
}
private void ConfigureRubberduckMenu()
{
const int windowMenuId = 30009;
var parent = _kernel.Get<VBE>().CommandBars[MENU_BAR].Controls;
var beforeIndex = FindRubberduckMenuInsertionIndex(parent, windowMenuId);
var items = GetRubberduckMenuItems();
BindParentMenuItem<RubberduckParentMenu>(parent, beforeIndex, items);
}
private void ConfigureCodePaneContextMenu()
{
const int listMembersMenuId = 2529;
var parent = _kernel.Get<VBE>().CommandBars[CODE_WINDOW].Controls;
var beforeIndex = parent.Cast<CommandBarControl>().First(control => control.Id == listMembersMenuId).Index;
var items = GetCodePaneContextMenuItems();
BindParentMenuItem<RubberduckParentMenu>(parent, beforeIndex, items);
}
private void ConfigureFormDesignerContextMenu()
{
const int viewCodeMenuId = 2558;
var parent = _kernel.Get<VBE>().CommandBars[MS_FORMS].Controls;
var beforeIndex = parent.Cast<CommandBarControl>().First(control => control.Id == viewCodeMenuId).Index;
var items = GetFormDesignerContextMenuItems();
BindParentMenuItem<FormDesignerContextParentMenu>(parent, beforeIndex, items);
}
private void ConfigureFormDesignerControlContextMenu()
{
const int viewCodeMenuId = 2558;
var parent = _kernel.Get<VBE>().CommandBars[MS_FORMS_CONTROL].Controls;
var beforeIndex = parent.Cast<CommandBarControl>().First(control => control.Id == viewCodeMenuId).Index;
var items = GetFormDesignerContextMenuItems();
BindParentMenuItem<FormDesignerControlContextParentMenu>(parent, beforeIndex, items);
}
private void ConfigureProjectExplorerContextMenu()
{
const int projectPropertiesMenuId = 2578;
var parent = _kernel.Get<VBE>().CommandBars[PROJECT_WINDOW].Controls;
var beforeIndex = parent.Cast<CommandBarControl>().First(control => control.Id == projectPropertiesMenuId).Index;
var items = GetProjectWindowContextMenuItems();
BindParentMenuItem<ProjectWindowContextParentMenu>(parent, beforeIndex, items);
}
private void BindParentMenuItem<TParentMenu>(CommandBarControls parent, int beforeIndex, IEnumerable<IMenuItem> items)
{
_kernel.Bind<IParentMenuItem>().To(typeof(TParentMenu))
.InSingletonScope()
.WithConstructorArgument("items", items)
.WithConstructorArgument("beforeIndex", beforeIndex)
.WithPropertyValue("Parent", parent);
}
private static int FindRubberduckMenuInsertionIndex(CommandBarControls controls, int beforeId)
{
for (var i = 1; i <= controls.Count; i++)
{
if (controls[i].BuiltIn && controls[i].Id == beforeId)
{
return i;
}
}
return controls.Count;
}
private void BindCommandsToMenuItems()
{
//_kernel.Bind<ICommand>().To<NavigateCommand>().InSingletonScope();
_kernel.Bind<IDeclarationNavigator>().To<NavigateAllImplementations>().WhenTargetHas<FindImplementationsAttribute>().InSingletonScope();
_kernel.Bind<IDeclarationNavigator>().To<NavigateAllReferences>().WhenTargetHas<FindReferencesAttribute>().InSingletonScope();
var types = Assembly.GetExecutingAssembly().GetTypes()
.Where(type => type.Namespace != null && type.Namespace.StartsWith(typeof(CommandBase).Namespace ?? string.Empty))
.ToList();
// note: ICommand naming convention: [Foo]Command
var commands = types.Where(type => type.IsClass && type.BaseType == typeof(CommandBase) && type.Name.EndsWith("Command"));
foreach (var command in commands)
{
var commandName = command.Name.Substring(0, command.Name.Length - "Command".Length);
try
{
// note: ICommandMenuItem naming convention for [Foo]Command: [Foo]CommandMenuItem
var item = types.SingleOrDefault(type => type.Name == commandName + "CommandMenuItem");
if (item != null)
{
_kernel.Bind(item).ToSelf().InSingletonScope();
_kernel.Bind<ICommand>().To(command).WhenInjectedInto(item).InSingletonScope();
}
}
catch (InvalidOperationException exception)
{
// rename one of the classes, "FooCommand" is expected to match exactly 1 "FooBarXyzCommandMenuItem"
}
}
}
private IEnumerable<IMenuItem> GetRubberduckMenuItems()
{
return new IMenuItem[]
{
_kernel.Get<AboutCommandMenuItem>(),
_kernel.Get<OptionsCommandMenuItem>(),
_kernel.Get<RunCodeInspectionsCommandMenuItem>(),
_kernel.Get<ShowSourceControlPanelCommandMenuItem>(),
GetUnitTestingParentMenu(),
GetRefactoringsParentMenu(),
GetNavigateParentMenu(),
};
}
private IMenuItem GetUnitTestingParentMenu()
{
var items = new IMenuItem[]
{
_kernel.Get<RunAllTestsCommandMenuItem>(),
_kernel.Get<TestExplorerCommandMenuItem>(),
_kernel.Get<AddTestModuleCommandMenuItem>(),
_kernel.Get<AddTestMethodCommandMenuItem>(),
_kernel.Get<AddTestMethodExpectedErrorCommandMenuItem>(),
};
return new UnitTestingParentMenu(items);
}
private IMenuItem GetRefactoringsParentMenu()
{
var items = new IMenuItem[]
{
_kernel.Get<RefactorRenameCommandMenuItem>(),
_kernel.Get<RefactorExtractMethodCommandMenuItem>(),
_kernel.Get<RefactorReorderParametersCommandMenuItem>(),
_kernel.Get<RefactorRemoveParametersCommandMenuItem>(),
};
return new RefactoringsParentMenu(items);
}
private IMenuItem GetNavigateParentMenu()
{
var items = new IMenuItem[]
{
_kernel.Get<CodeExplorerCommandMenuItem>(),
_kernel.Get<ToDoExplorerCommandMenuItem>(),
_kernel.Get<RegexSearchReplaceCommandMenuItem>(),
_kernel.Get<FindSymbolCommandMenuItem>(),
_kernel.Get<FindAllReferencesCommandMenuItem>(),
_kernel.Get<FindAllImplementationsCommandMenuItem>(),
};
return new NavigateParentMenu(items);
}
private IEnumerable<IMenuItem> GetCodePaneContextMenuItems()
{
return new IMenuItem[]
{
GetRefactoringsParentMenu(),
_kernel.Get<RegexSearchReplaceCommandMenuItem>(),
_kernel.Get<FindSymbolCommandMenuItem>(),
_kernel.Get<FindAllReferencesCommandMenuItem>(),
_kernel.Get<FindAllImplementationsCommandMenuItem>(),
};
}
private IEnumerable<IMenuItem> GetFormDesignerContextMenuItems()
{
return new IMenuItem[]
{
_kernel.Get<RefactorRenameCommandMenuItem>(),
};
}
private IEnumerable<IMenuItem> GetProjectWindowContextMenuItems()
{
return new IMenuItem[]
{
_kernel.Get<RefactorRenameCommandMenuItem>(),
_kernel.Get<FindSymbolCommandMenuItem>(),
_kernel.Get<FindAllReferencesCommandMenuItem>(),
_kernel.Get<FindAllImplementationsCommandMenuItem>(),
};
}
}
}