Skip to content

Commit f5a0ed8

Browse files
authored
Merge pull request #223 from rubberduck-vba/next
sync with main repo
2 parents 8dde4de + ed27dce commit f5a0ed8

File tree

14 files changed

+193
-126
lines changed

14 files changed

+193
-126
lines changed

RetailCoder.VBE/App.cs

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public App(IVBE vbe,
6363
_version = version;
6464
_checkVersionCommand = checkVersionCommand;
6565

66-
VBEEvents.SelectionChanged += _vbe_SelectionChanged;
67-
VBEEvents.WindowFocusChange += _vbe_FocusChanged;
66+
VBENativeServices.SelectionChanged += _vbe_SelectionChanged;
67+
VBENativeServices.WindowFocusChange += _vbe_FocusChanged;
6868

6969
_configService.SettingsChanged += _configService_SettingsChanged;
7070
_parser.State.StateChanged += Parser_StateChanged;
@@ -73,6 +73,9 @@ public App(IVBE vbe,
7373
UiDispatcher.Initialize();
7474
}
7575

76+
//TODO - This should be able to move to the appropriate handling classes now.
77+
#region Statusbar
78+
7679
private void State_StatusMessageUpdate(object sender, RubberduckStatusMessageEventArgs e)
7780
{
7881
var message = e.Message;
@@ -92,90 +95,79 @@ private void _vbe_SelectionChanged(object sender, SelectionChangedEventArgs e)
9295

9396
private void _vbe_FocusChanged(object sender, WindowChangedEventArgs e)
9497
{
95-
if (e.EventType == WindowChangedEventArgs.FocusType.GotFocus)
98+
if (e.EventType == FocusType.GotFocus)
9699
{
97100
switch (e.Window.Type)
98101
{
99102
case WindowKind.Designer:
103+
//Designer or control on designer form selected.
100104
RefreshSelection(e.Window);
101105
break;
102106
case WindowKind.CodeWindow:
107+
//Caret changed in a code pane.
103108
RefreshSelection(e.CodePane);
104109
break;
105110
}
106-
}
111+
}
112+
else if (e.EventType == FocusType.ChildFocus)
113+
{
114+
//Treeview selection changed in project window.
115+
RefreshSelection();
116+
}
107117
}
108118

109119
private ParserState _lastStatus;
110120
private Declaration _lastSelectedDeclaration;
111121
private void RefreshSelection(ICodePane pane)
112122
{
113-
Declaration selectedDeclaration = null;
114-
if (!pane.IsWrappingNullReference)
123+
if (pane == null || pane.IsWrappingNullReference)
115124
{
116-
selectedDeclaration = _parser.State.FindSelectedDeclaration(pane);
117-
var refCount = selectedDeclaration == null ? 0 : selectedDeclaration.References.Count();
118-
var caption = _stateBar.GetContextSelectionCaption(_vbe.ActiveCodePane, selectedDeclaration);
119-
_stateBar.SetContextSelectionCaption(caption, refCount);
120-
}
121-
122-
var currentStatus = _parser.State.Status;
123-
if (ShouldEvaluateCanExecute(selectedDeclaration, currentStatus))
124-
{
125-
_appMenus.EvaluateCanExecute(_parser.State);
126-
_stateBar.EvaluateCanExecute(_parser.State);
125+
return;
127126
}
128127

129-
_lastStatus = currentStatus;
130-
_lastSelectedDeclaration = selectedDeclaration;
128+
var selectedDeclaration = _parser.State.FindSelectedDeclaration(pane);
129+
var caption = _stateBar.GetContextSelectionCaption(_vbe.ActiveCodePane, selectedDeclaration);
130+
UpdateStatusbarAndCommandState(caption, selectedDeclaration);
131131
}
132132

133133
private void RefreshSelection(IWindow window)
134134
{
135-
if (window.IsWrappingNullReference || window.Type != WindowKind.Designer)
135+
if (window == null || window.IsWrappingNullReference || window.Type != WindowKind.Designer)
136136
{
137137
return;
138138
}
139-
var caption = String.Empty;
140-
var refCount = 0;
141139

142-
WindowKind windowKind = _vbe.ActiveWindow.Type;
143-
var pane = _vbe.ActiveCodePane;
144140
var component = _vbe.SelectedVBComponent;
141+
var caption = GetComponentControlsCaption(component);
142+
//TODO: Need to find the selected declaration for the Form\Control.
143+
UpdateStatusbarAndCommandState(caption, null);
144+
}
145145

146-
Declaration selectedDeclaration = null;
147-
148-
//TODO - I doubt this is the best way to check if the SelectedVBComponent and the ActiveCodePane are the same component.
149-
if (windowKind == WindowKind.CodeWindow || (!_vbe.SelectedVBComponent.IsWrappingNullReference
150-
&& component.ParentProject.ProjectId == pane.CodeModule.Parent.ParentProject.ProjectId
151-
&& component.Name == pane.CodeModule.Parent.Name))
152-
{
153-
selectedDeclaration = _parser.State.FindSelectedDeclaration(pane);
154-
refCount = selectedDeclaration == null ? 0 : selectedDeclaration.References.Count();
155-
caption = _stateBar.GetContextSelectionCaption(_vbe.ActiveCodePane, selectedDeclaration);
156-
}
157-
else if (windowKind == WindowKind.Designer)
146+
private void RefreshSelection()
147+
{
148+
var caption = string.Empty;
149+
var component = _vbe.SelectedVBComponent;
150+
if (component == null || component.IsWrappingNullReference)
158151
{
159-
caption = GetComponentControlsCaption(component);
152+
//The user might have selected the project node in Project Explorer
153+
//If they've chosen a folder, we'll return the project anyway
154+
caption = !_vbe.ActiveVBProject.IsWrappingNullReference
155+
? _vbe.ActiveVBProject.Name
156+
: null;
160157
}
161158
else
162159
{
163-
if (_vbe.SelectedVBComponent.IsWrappingNullReference)
164-
{
165-
//The user might have selected the project node in Project Explorer
166-
//If they've chosen a folder, we'll return the project anyway
167-
caption = !_vbe.ActiveVBProject.IsWrappingNullReference
168-
? _vbe.ActiveVBProject.Name
169-
: null;
170-
}
171-
else
172-
{
173-
caption = component.Type == ComponentType.UserForm && component.HasOpenDesigner
174-
? GetComponentControlsCaption(component)
175-
: String.Format("{0}.{1} ({2})", component.ParentProject.Name, component.Name, component.Type);
176-
}
160+
caption = component.Type == ComponentType.UserForm && component.HasOpenDesigner
161+
? GetComponentControlsCaption(component)
162+
: string.Format("{0}.{1} ({2})", component.ParentProject.Name, component.Name, component.Type);
177163
}
164+
//TODO: Need to find the selected declaration for the selected treeview item.
165+
UpdateStatusbarAndCommandState(caption, null);
166+
}
178167

168+
private void UpdateStatusbarAndCommandState(string caption, Declaration selectedDeclaration)
169+
{
170+
var refCount = selectedDeclaration == null ? 0 : selectedDeclaration.References.Count();
179171
_stateBar.SetContextSelectionCaption(caption, refCount);
180172

181173
var currentStatus = _parser.State.Status;
@@ -186,7 +178,7 @@ private void RefreshSelection(IWindow window)
186178
}
187179

188180
_lastStatus = currentStatus;
189-
_lastSelectedDeclaration = selectedDeclaration;
181+
_lastSelectedDeclaration = selectedDeclaration;
190182
}
191183

192184
private string GetComponentControlsCaption(IVBComponent component)
@@ -215,6 +207,8 @@ private bool ShouldEvaluateCanExecute(Declaration selectedDeclaration, ParserSta
215207
(selectedDeclaration == null && _lastSelectedDeclaration != null);
216208
}
217209

210+
#endregion
211+
218212
private void _configService_SettingsChanged(object sender, ConfigurationChangedEventArgs e)
219213
{
220214
_config = _configService.LoadConfiguration();
@@ -366,8 +360,8 @@ public void Dispose()
366360
_parser.State.StatusMessageUpdate -= State_StatusMessageUpdate;
367361
}
368362

369-
VBEEvents.SelectionChanged += _vbe_SelectionChanged;
370-
VBEEvents.WindowFocusChange += _vbe_FocusChanged;
363+
VBENativeServices.SelectionChanged += _vbe_SelectionChanged;
364+
VBENativeServices.WindowFocusChange += _vbe_FocusChanged;
371365

372366
if (_configService != null)
373367
{

RetailCoder.VBE/Extension.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void OnConnection(object Application, ext_ConnectMode ConnectMode, object
5656
{
5757
var vbe = (Microsoft.Vbe.Interop.VBE) Application;
5858
_ide = new VBEditor.SafeComWrappers.VBA.VBE(vbe);
59-
VBEEvents.HookEvents(_ide);
59+
VBENativeServices.HookEvents(_ide);
6060

6161
var addin = (Microsoft.Vbe.Interop.AddIn)AddInInst;
6262
_addin = new VBEditor.SafeComWrappers.VBA.AddIn(addin) { Object = this };
@@ -221,7 +221,7 @@ private void Startup()
221221

222222
private void ShutdownAddIn()
223223
{
224-
VBEEvents.UnhookEvents();
224+
VBENativeServices.UnhookEvents();
225225

226226
var currentDomain = AppDomain.CurrentDomain;
227227
currentDomain.AssemblyResolve -= LoadFromSameFolder;

0 commit comments

Comments
 (0)