Skip to content

Commit 68434fa

Browse files
committed
Merge pull request #1339 from retailcoder/next
Finally got a reliable ProjectId!
2 parents 1f04bb6 + 7e8ec60 commit 68434fa

File tree

45 files changed

+414
-269
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+414
-269
lines changed

RetailCoder.VBE/App.cs

Lines changed: 84 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ public class App : IDisposable
4141
private readonly IConnectionPoint _projectsEventsConnectionPoint;
4242
private readonly int _projectsEventsCookie;
4343

44-
private readonly IDictionary<VBComponents, Tuple<IConnectionPoint, int>> _componentsEventsConnectionPoints =
45-
new Dictionary<VBComponents, Tuple<IConnectionPoint, int>>();
44+
private readonly IDictionary<VBProjectsEventsSink, Tuple<IConnectionPoint, int>> _componentsEventsConnectionPoints =
45+
new Dictionary<VBProjectsEventsSink, Tuple<IConnectionPoint, int>>();
46+
private readonly IDictionary<VBProjectsEventsSink, Tuple<IConnectionPoint, int>> _referencesEventsConnectionPoints =
47+
new Dictionary<VBProjectsEventsSink, Tuple<IConnectionPoint, int>>();
4648

4749
private readonly IDictionary<Type, Action> _hookActions;
4850

@@ -145,29 +147,46 @@ public void Startup()
145147
{
146148
_parser.State.OnParseRequested(this);
147149
});
148-
}, new StaTaskScheduler()).ConfigureAwait(false);
150+
}).ConfigureAwait(false);
149151

150152
_hooks.HookHotkeys();
151153
}
152154

153155
#region sink handlers. todo: move to another class
154156
async void sink_ProjectRemoved(object sender, DispatcherEventArgs<VBProject> e)
155157
{
158+
var sink = (VBProjectsEventsSink)sender;
159+
_componentsEventSinks.Remove(sink);
160+
_referencesEventsSinks.Remove(sink);
156161
_parser.State.RemoveProject(e.Item);
157162

158163
Debug.WriteLine(string.Format("Project '{0}' was removed.", e.Item.Name));
159-
Tuple<IConnectionPoint, int> value;
160-
if (_componentsEventsConnectionPoints.TryGetValue(e.Item.VBComponents, out value))
164+
Tuple<IConnectionPoint, int> componentsTuple;
165+
if (_componentsEventsConnectionPoints.TryGetValue(sink, out componentsTuple))
161166
{
162-
value.Item1.Unadvise(value.Item2);
163-
_componentsEventsConnectionPoints.Remove(e.Item.VBComponents);
167+
componentsTuple.Item1.Unadvise(componentsTuple.Item2);
168+
_componentsEventsConnectionPoints.Remove(sink);
169+
}
164170

165-
_parser.State.ClearDeclarations(e.Item);
171+
Tuple<IConnectionPoint, int> referencesTuple;
172+
if (_referencesEventsConnectionPoints.TryGetValue(sink, out referencesTuple))
173+
{
174+
referencesTuple.Item1.Unadvise(referencesTuple.Item2);
175+
_referencesEventsConnectionPoints.Remove(sink);
166176
}
177+
178+
_parser.State.ClearDeclarations(e.Item);
167179
}
168180

181+
private readonly IDictionary<VBProjectsEventsSink, VBComponentsEventsSink> _componentsEventSinks =
182+
new Dictionary<VBProjectsEventsSink, VBComponentsEventsSink>();
183+
184+
private readonly IDictionary<VBProjectsEventsSink, ReferencesEventsSink> _referencesEventsSinks =
185+
new Dictionary<VBProjectsEventsSink, ReferencesEventsSink>();
186+
169187
async void sink_ProjectAdded(object sender, DispatcherEventArgs<VBProject> e)
170188
{
189+
var sink = (VBProjectsEventsSink)sender;
171190
_parser.State.AddProject(e.Item);
172191

173192
if (!_parser.State.AllDeclarations.Any())
@@ -179,25 +198,67 @@ async void sink_ProjectAdded(object sender, DispatcherEventArgs<VBProject> e)
179198
}
180199

181200
Debug.WriteLine(string.Format("Project '{0}' was added.", e.Item.Name));
182-
var connectionPointContainer = (IConnectionPointContainer)e.Item.VBComponents;
183-
var interfaceId = typeof(_dispVBComponentsEvents).GUID;
184-
201+
RegisterComponentsEventSink(e, sink);
202+
RegisterReferencesEventsSink(e, sink);
203+
204+
_parser.State.OnParseRequested(sender);
205+
}
206+
207+
private void RegisterComponentsEventSink(DispatcherEventArgs<VBProject> e, VBProjectsEventsSink sink)
208+
{
209+
var connectionPointContainer = (IConnectionPointContainer) e.Item.VBComponents;
210+
var interfaceId = typeof (_dispVBComponentsEvents).GUID;
211+
185212
IConnectionPoint connectionPoint;
186213
connectionPointContainer.FindConnectionPoint(ref interfaceId, out connectionPoint);
187214

188-
var sink = new VBComponentsEventsSink();
189-
sink.ComponentActivated += sink_ComponentActivated;
190-
sink.ComponentAdded += sink_ComponentAdded;
191-
sink.ComponentReloaded += sink_ComponentReloaded;
192-
sink.ComponentRemoved += sink_ComponentRemoved;
193-
sink.ComponentRenamed += sink_ComponentRenamed;
194-
sink.ComponentSelected += sink_ComponentSelected;
215+
var componentsSink = new VBComponentsEventsSink();
216+
componentsSink.ComponentActivated += sink_ComponentActivated;
217+
componentsSink.ComponentAdded += sink_ComponentAdded;
218+
componentsSink.ComponentReloaded += sink_ComponentReloaded;
219+
componentsSink.ComponentRemoved += sink_ComponentRemoved;
220+
componentsSink.ComponentRenamed += sink_ComponentRenamed;
221+
componentsSink.ComponentSelected += sink_ComponentSelected;
222+
_componentsEventSinks.Add(sink, componentsSink);
195223

196224
int cookie;
197-
connectionPoint.Advise(sink, out cookie);
225+
connectionPoint.Advise(componentsSink, out cookie);
198226

199-
_componentsEventsConnectionPoints.Add(e.Item.VBComponents, Tuple.Create(connectionPoint, cookie));
200-
_parser.State.OnParseRequested(sender);
227+
_componentsEventsConnectionPoints.Add(sink, Tuple.Create(connectionPoint, cookie));
228+
}
229+
230+
private void RegisterReferencesEventsSink(DispatcherEventArgs<VBProject> e, VBProjectsEventsSink sink)
231+
{
232+
var connectionPointContainer = (IConnectionPointContainer)e.Item.References;
233+
var interfaceId = typeof(_dispReferencesEvents).GUID;
234+
235+
IConnectionPoint connectionPoint;
236+
connectionPointContainer.FindConnectionPoint(ref interfaceId, out connectionPoint);
237+
238+
var referencesSink = new ReferencesEventsSink();
239+
referencesSink.ReferenceAdded += referencesSink_ReferenceAdded;
240+
referencesSink.ReferenceRemoved += referencesSink_ReferenceRemoved;
241+
_referencesEventsSinks.Add(sink, referencesSink);
242+
243+
int cookie;
244+
connectionPoint.Advise(referencesSink, out cookie);
245+
_referencesEventsConnectionPoints.Add(sink, Tuple.Create(connectionPoint, cookie));
246+
}
247+
248+
private void referencesSink_ReferenceRemoved(object sender, DispatcherEventArgs<Reference> e)
249+
{
250+
Debug.WriteLine(string.Format("Reference '{0}' was removed.", e.Item.Name));
251+
var state = _parser.State.Status;
252+
_parser.UnloadComReference(e.Item);
253+
_parser.State.SetModuleState(state);
254+
}
255+
256+
private void referencesSink_ReferenceAdded(object sender, DispatcherEventArgs<Reference> e)
257+
{
258+
Debug.WriteLine(string.Format("Reference '{0}' was added.", e.Item.Name));
259+
var state = _parser.State.Status;
260+
_parser.LoadComReference(e.Item);
261+
_parser.State.SetModuleState(state);
201262
}
202263

203264
async void sink_ComponentSelected(object sender, DispatcherEventArgs<VBComponent> e)
@@ -274,8 +335,8 @@ async void sink_ProjectRenamed(object sender, DispatcherRenamedEventArgs<VBProje
274335
return;
275336
}
276337

277-
Debug.WriteLine(string.Format("Project '{0}' was renamed.", e.Item.Name));
278-
_parser.State.ClearDeclarations(e.Item);
338+
Debug.WriteLine("Project '{0}' (ID {1}) was renamed to '{2}'.", e.OldName, e.Item.HelpFile, e.Item.Name);
339+
_parser.State.RemoveProject(e.Item.HelpFile);
279340
_parser.State.OnParseRequested(sender);
280341
}
281342

RetailCoder.VBE/Common/DeclarationExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public static IEnumerable<Declaration> FindEventProcedures(this IEnumerable<Decl
354354
var handlerNames = events.Select(e => withEventsDeclaration.IdentifierName + '_' + e.IdentifierName);
355355

356356
return items.Where(item => item.Project != null
357-
&& item.ProjectName == withEventsDeclaration.ProjectName
357+
&& item.ProjectId == withEventsDeclaration.ProjectId
358358
&& item.ParentScope == withEventsDeclaration.ParentScope
359359
&& item.DeclarationType == DeclarationType.Procedure
360360
&& handlerNames.Any(name => item.IdentifierName == name))
@@ -363,7 +363,7 @@ public static IEnumerable<Declaration> FindEventProcedures(this IEnumerable<Decl
363363

364364
private static IEnumerable<Declaration> GetTypeMembers(this IEnumerable<Declaration> declarations, Declaration type)
365365
{
366-
return declarations.Where(item => item.Project != null && item.ProjectName == type.ProjectName && item.ParentScope == type.Scope);
366+
return declarations.Where(item => item.Project != null && item.ProjectId == type.ProjectId && item.ParentScope == type.Scope);
367367
}
368368

369369
/// <summary>
@@ -394,7 +394,7 @@ public static Declaration FindInterfaceMember(this IEnumerable<Declaration> decl
394394
var matches = members.Where(m => !m.IsBuiltIn && implementation.IdentifierName == m.ComponentName + '_' + m.IdentifierName).ToList();
395395

396396
return matches.Count > 1
397-
? matches.SingleOrDefault(m => m.ProjectName == implementation.ProjectName)
397+
? matches.SingleOrDefault(m => m.ProjectId == implementation.ProjectId)
398398
: matches.First();
399399
}
400400

RetailCoder.VBE/Inspections/InspectionResultBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public override string ToString()
103103
InspectionsUI.QualifiedSelectionInspection,
104104
Inspection.Severity,
105105
Description,
106-
module.ProjectName,
106+
module.ProjectId,
107107
module.ComponentName,
108108
QualifiedSelection.Selection.StartLine);
109109
}
@@ -121,7 +121,7 @@ public int CompareTo(object obj)
121121
public object[] ToArray()
122122
{
123123
var module = QualifiedSelection.QualifiedName;
124-
return new object[] {Inspection.Severity.ToString(), Description, module.ProjectName, module.ComponentName, QualifiedSelection.Selection.StartLine };
124+
return new object[] {Inspection.Severity.ToString(), Description, module.ProjectId, module.ComponentName, QualifiedSelection.Selection.StartLine };
125125
}
126126

127127
public string ToCsvString()
@@ -131,7 +131,7 @@ public string ToCsvString()
131131
"\"{0}\",\"{1}\",\"{2}\",\"{3}\",{4}",
132132
Inspection.Severity,
133133
Description,
134-
module.ProjectName,
134+
module.ProjectId,
135135
module.ComponentName,
136136
QualifiedSelection.Selection.StartLine);
137137
}

RetailCoder.VBE/Inspections/ProcedureNotUsedInspection.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private bool IsPublicModuleMember(IEnumerable<Declaration> modules, Declaration
8383
return false;
8484
}
8585

86-
var parent = modules.Where(item => item.ProjectName == procedure.ProjectName)
86+
var parent = modules.Where(item => item.ProjectId == procedure.ProjectId)
8787
.SingleOrDefault(item => item.IdentifierName == procedure.ComponentName);
8888

8989
return parent != null;
@@ -102,7 +102,7 @@ private bool IsClassLifeCycleHandler(IEnumerable<Declaration> classes, Declarati
102102
return false;
103103
}
104104

105-
var parent = classes.Where(item => item.ProjectName == procedure.ProjectName)
105+
var parent = classes.Where(item => item.ProjectId == procedure.ProjectId)
106106
.SingleOrDefault(item => item.IdentifierName == procedure.ComponentName);
107107

108108
return parent != null;
@@ -118,7 +118,7 @@ private bool IsInterfaceMember(IEnumerable<Declaration> declarations, IEnumerabl
118118
{
119119
// get the procedure's parent module
120120
var enumerable = classes as IList<Declaration> ?? classes.ToList();
121-
var parent = enumerable.Where(item => item.ProjectName == procedure.ProjectName)
121+
var parent = enumerable.Where(item => item.ProjectId == procedure.ProjectId)
122122
.SingleOrDefault(item => item.IdentifierName == procedure.ComponentName);
123123

124124
if (parent == null)

RetailCoder.VBE/Refactorings/ExtractInterface/ExtractInterfaceModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public ExtractInterfaceModel(RubberduckParserState state, QualifiedSelection sel
4949
InterfaceName = "I" + TargetDeclaration.IdentifierName;
5050

5151
_members = declarations.Where(item => !item.IsBuiltIn
52-
&& item.ProjectName == _targetDeclaration.ProjectName
52+
&& item.ProjectId == _targetDeclaration.ProjectId
5353
&& item.ComponentName == _targetDeclaration.ComponentName
5454
&& (item.Accessibility == Accessibility.Public || item.Accessibility == Accessibility.Implicit)
5555
&& MemberTypes.Contains(item.DeclarationType))

RetailCoder.VBE/Refactorings/ExtractMethod/ExtractMethodPresenter.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Windows.Forms;
55
using Rubberduck.Parsing.Grammar;
6+
using Rubberduck.SmartIndenter;
67

78
namespace Rubberduck.Refactorings.ExtractMethod
89
{
@@ -15,11 +16,13 @@ public class ExtractMethodPresenter : IExtractMethodPresenter
1516
{
1617
private readonly IExtractMethodDialog _view;
1718
private readonly ExtractMethodModel _model;
19+
private readonly IIndenter _indenter;
1820

19-
public ExtractMethodPresenter(IExtractMethodDialog view, ExtractMethodModel model)
21+
public ExtractMethodPresenter(IExtractMethodDialog view, ExtractMethodModel model, IIndenter indenter)
2022
{
2123
_view = view;
2224
_model = model;
25+
_indenter = indenter;
2326
}
2427

2528
public ExtractMethodModel Show()
@@ -92,7 +95,10 @@ private void GeneratePreview()
9295
_model.Method.ReturnValue = _view.ReturnValue;
9396
_model.Method.SetReturnValue = _view.SetReturnValue;
9497

95-
_view.Preview = ExtractMethodRefactoring.GetExtractedMethod(_model);
98+
var code = ExtractMethodRefactoring.GetExtractedMethod(_model).Split(new[]{Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
99+
_indenter.Indent(code, "Preview", false);
100+
101+
_view.Preview = string.Join(Environment.NewLine, code);
96102
}
97103
}
98104
}

RetailCoder.VBE/Refactorings/ExtractMethod/ExtractMethodPresenterFactory.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using Rubberduck.Parsing.Symbols;
4+
using Rubberduck.SmartIndenter;
45
using Rubberduck.UI.Refactorings;
56
using Rubberduck.VBEditor;
67

@@ -10,11 +11,13 @@ public class ExtractMethodPresenterFactory : IRefactoringPresenterFactory<IExtra
1011
{
1112
private readonly IActiveCodePaneEditor _editor;
1213
private readonly IEnumerable<Declaration> _declarations;
14+
private readonly IIndenter _indenter;
1315

14-
public ExtractMethodPresenterFactory(IActiveCodePaneEditor editor, IEnumerable<Declaration> declarations)
16+
public ExtractMethodPresenterFactory(IActiveCodePaneEditor editor, IEnumerable<Declaration> declarations, IIndenter indenter)
1517
{
1618
_editor = editor;
1719
_declarations = declarations;
20+
_indenter = indenter;
1821
}
1922

2023
public IExtractMethodPresenter Create()
@@ -36,7 +39,7 @@ public IExtractMethodPresenter Create()
3639
}
3740

3841
var view = new ExtractMethodDialog();
39-
return new ExtractMethodPresenter(view, model);
42+
return new ExtractMethodPresenter(view, model, _indenter);
4043
}
4144
}
4245
}

RetailCoder.VBE/Refactorings/ImplementInterface/ImplementInterfaceRefactoring.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ private IEnumerable<Declaration> GetInterfaceMembers()
206206
private IEnumerable<Declaration> GetImplementedMembers()
207207
{
208208
return _declarations.FindInterfaceImplementationMembers()
209-
.Where(item => item.ProjectName == _targetInterface.ProjectName
209+
.Where(item => item.ProjectId == _targetInterface.ProjectId
210210
&& item.ComponentName == _targetClass.IdentifierName
211211
&& item.IdentifierName.StartsWith(_targetInterface.ComponentName + "_")
212212
&& !item.Equals(_targetClass))

RetailCoder.VBE/Refactorings/IntroduceParameter/IntroduceParameterRefactoring.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private void UpdateSignature(Declaration targetVariable)
148148
UpdateSignature(interfaceImplementation, targetVariable);
149149

150150
var interfaceImplementations = _declarations.FindInterfaceImplementationMembers()
151-
.Where(item => item.ProjectName == interfaceImplementation.ProjectName
151+
.Where(item => item.ProjectId == interfaceImplementation.ProjectId
152152
&& item.IdentifierName == interfaceImplementation.ComponentName + "_" + interfaceImplementation.IdentifierName
153153
&& !item.Equals(functionDeclaration));
154154

RetailCoder.VBE/Refactorings/RemoveParameters/RemoveParametersModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private IEnumerable<Declaration> GetParameters()
6767

6868
return Declarations.Where(d => d.DeclarationType == DeclarationType.Parameter
6969
&& d.ComponentName == TargetDeclaration.ComponentName
70-
&& d.ProjectName == TargetDeclaration.ProjectName
70+
&& d.ProjectId == TargetDeclaration.ProjectId
7171
&& targetSelection.Contains(d.Selection))
7272
.OrderBy(item => item.Selection.StartLine)
7373
.ThenBy(item => item.Selection.StartColumn);

0 commit comments

Comments
 (0)