Skip to content

Commit 841f4bc

Browse files
committed
Introduce SelectedDeclarationService
It is a wrapper for an ISelectionService and the DeclarationFinder to remove the common duplication of code around getting the selected declaration from the code panes.
1 parent 6988ec5 commit 841f4bc

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

Rubberduck.Main/Root/RubberduckIoCInstaller.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ public void Install(IWindsorContainer container, IConfigurationStore store)
125125
RegisterParsingEngine(container);
126126
RegisterTypeLibApi(container);
127127

128+
container.Register(Component.For<ISelectedDeclarationService>()
129+
.ImplementedBy<SelectedDeclarationService>()
130+
.LifestyleSingleton());
131+
128132
container.Register(Component.For<IRewritingManager>()
129133
.ImplementedBy<RewritingManager>()
130134
.LifestyleSingleton());
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Rubberduck.Parsing.Symbols;
2+
using Rubberduck.VBEditor;
3+
4+
namespace Rubberduck.Parsing.VBA
5+
{
6+
public interface ISelectedDeclarationService
7+
{
8+
Declaration SelectedDeclaration();
9+
Declaration SelectedDeclaration(QualifiedModuleName module);
10+
Declaration SelectedDeclaration(QualifiedSelection qualifiedSelection);
11+
}
12+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Rubberduck.Parsing.Symbols;
2+
using Rubberduck.VBEditor;
3+
using Rubberduck.VBEditor.Utility;
4+
5+
namespace Rubberduck.Parsing.VBA
6+
{
7+
public class SelectedDeclarationService : ISelectedDeclarationService
8+
{
9+
private readonly ISelectionService _selectionService;
10+
private readonly IDeclarationFinderProvider _declarationFinderProvider;
11+
12+
public SelectedDeclarationService(ISelectionService selectionService, IDeclarationFinderProvider declarationFinderProvider)
13+
{
14+
_selectionService = selectionService;
15+
_declarationFinderProvider = declarationFinderProvider;
16+
}
17+
18+
public Declaration SelectedDeclaration()
19+
{
20+
var selection = _selectionService.ActiveSelection();
21+
return SelectedDeclaration(selection);
22+
}
23+
24+
private Declaration SelectedDeclaration(QualifiedSelection? qualifiedSelection)
25+
{
26+
return qualifiedSelection.HasValue
27+
? SelectedDeclaration(qualifiedSelection.Value)
28+
: null;
29+
}
30+
31+
public Declaration SelectedDeclaration(QualifiedModuleName module)
32+
{
33+
var selection = _selectionService.Selection(module);
34+
return SelectedDeclaration(module, selection);
35+
}
36+
37+
private Declaration SelectedDeclaration(QualifiedModuleName module, Selection? selection)
38+
{
39+
return selection.HasValue
40+
? SelectedDeclaration(new QualifiedSelection(module, selection.Value))
41+
: null;
42+
}
43+
44+
public Declaration SelectedDeclaration(QualifiedSelection qualifiedSelection)
45+
{
46+
return _declarationFinderProvider.DeclarationFinder?.FindSelectedDeclaration(qualifiedSelection);
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)