Skip to content

Commit 5213d7c

Browse files
committed
Clean up the ParseErrorEventArgs
1 parent 0f2351e commit 5213d7c

File tree

4 files changed

+65
-27
lines changed

4 files changed

+65
-27
lines changed

Rubberduck.Core/UI/ParserErrors/ParseErrorListItem.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Rubberduck.Parsing.VBA;
2+
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
23

34
namespace Rubberduck.UI.ParserErrors
45
{
@@ -20,9 +21,9 @@ public ParseErrorListItem(ParseErrorEventArgs error)
2021

2122
public string Value => ToString();
2223

23-
public void Navigate()
24+
public void Navigate(IVBE vbe)
2425
{
25-
_error.Navigate();
26+
_error.Navigate(vbe);
2627
}
2728

2829
public override string ToString()

Rubberduck.Core/UI/ParserErrors/ParserErrorsPresenter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ public interface IParserErrorsPresenter
1717

1818
public class ParserErrorsPresenter : DockableToolwindowPresenter, IParserErrorsPresenter
1919
{
20+
private readonly IVBE _vbe;
21+
2022
public ParserErrorsPresenter(IVBE vbe, IAddIn addin)
2123
: base(vbe, addin, new SimpleListControl(RubberduckUI.ParseErrors_Caption), null)
2224
{
25+
_vbe = vbe;
2326
_source = new BindingList<ParseErrorListItem>();
2427
var control = UserControl as SimpleListControl;
2528
Debug.Assert(control != null);
@@ -29,7 +32,7 @@ public ParserErrorsPresenter(IVBE vbe, IAddIn addin)
2932
private void Control_Navigate(object sender, ListItemActionEventArgs e)
3033
{
3134
var selection = (ParseErrorListItem) e.SelectedItem;
32-
selection.Navigate();
35+
selection.Navigate(_vbe);
3336
}
3437

3538
private readonly IBindingList _source;

Rubberduck.Parsing/VBA/ParseErrorEventArgs.cs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using Rubberduck.Parsing.Symbols;
32
using Rubberduck.Parsing.VBA.Parsing.ParsingExceptions;
43
using Rubberduck.VBEditor;
54
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
@@ -8,38 +7,33 @@ namespace Rubberduck.Parsing.VBA
87
{
98
public class ParseErrorEventArgs : EventArgs
109
{
11-
public ParseErrorEventArgs(SyntaxErrorException exception, IVBComponent component)
10+
private readonly QualifiedModuleName _moduleName;
11+
12+
public ParseErrorEventArgs(SyntaxErrorException exception, QualifiedModuleName moduleName)
1213
{
13-
_exception = exception;
14-
_component = component;
14+
Exception = exception;
15+
_moduleName = moduleName;
1516
}
1617

17-
private readonly SyntaxErrorException _exception;
18-
public SyntaxErrorException Exception { get { return _exception; } }
18+
public SyntaxErrorException Exception { get; }
19+
20+
public string ComponentName => _moduleName.ComponentName;
21+
public string ProjectName => _moduleName.ProjectName;
1922

20-
private readonly IVBComponent _component;
21-
public string ComponentName { get { return _component.Name; } }
22-
public string ProjectName
23+
public void Navigate(IVBE vbe)
2324
{
24-
get
25+
var selection = new Selection(Exception.LineNumber, Exception.Position, Exception.LineNumber, Exception.Position + Exception.OffendingSymbol.Text.Length - 1);
26+
27+
if (!_moduleName.TryGetComponent(vbe, out var component))
2528
{
26-
using (var collection = _component.Collection)
27-
using (var parent = collection.Parent)
28-
{
29-
return parent.Name;
30-
}
29+
return;
3130
}
32-
}
3331

34-
public void Navigate()
35-
{
36-
var selection = new Selection(_exception.LineNumber, _exception.Position, _exception.LineNumber, _exception.Position + _exception.OffendingSymbol.Text.Length - 1);
37-
using (var module = _component.CodeModule)
32+
using (component)
33+
using (var module = component.CodeModule)
34+
using (var pane = module.CodePane)
3835
{
39-
using (var pane = module.CodePane)
40-
{
41-
pane.Selection = selection;
42-
}
36+
pane.Selection = selection;
4337
}
4438
}
4539
}

Rubberduck.VBEEditor/QualifiedModuleName.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,44 @@ public override bool Equals(object obj)
149149
return !a.Equals(b);
150150
}
151151
}
152+
153+
public static class QualifiedModuleNameExtensions
154+
{
155+
156+
public static bool TryGetProject(this QualifiedModuleName moduleName, IVBE vbe, out IVBProject project)
157+
{
158+
using (var projects = vbe.VBProjects)
159+
{
160+
foreach (var item in projects)
161+
{
162+
if (item.ProjectId == moduleName.ProjectId && item.Name == moduleName.ProjectName)
163+
{
164+
project = item;
165+
return true;
166+
}
167+
168+
item.Dispose();
169+
}
170+
171+
project = null;
172+
return false;
173+
}
174+
}
175+
176+
public static bool TryGetComponent(this QualifiedModuleName moduleName, IVBE vbe, out IVBComponent component)
177+
{
178+
if (TryGetProject(moduleName, vbe, out var project))
179+
{
180+
using (project)
181+
using (var components = project.VBComponents)
182+
{
183+
component = components[moduleName.ComponentName];
184+
return true;
185+
}
186+
}
187+
188+
component = null;
189+
return false;
190+
}
191+
}
152192
}

0 commit comments

Comments
 (0)