Skip to content

Commit 0d2da9a

Browse files
authored
Merge pull request #4733 from MDoerner/IntroduceSelectionService
Introduce the ISelectionService
2 parents f34996b + 1e23c30 commit 0d2da9a

File tree

3 files changed

+577
-0
lines changed

3 files changed

+577
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Rubberduck.VBEditor.Utility
2+
{
3+
public interface ISelectionService
4+
{
5+
QualifiedSelection? ActiveSelection();
6+
Selection? Selection(QualifiedModuleName module);
7+
bool TryActivate(QualifiedModuleName module);
8+
bool TrySetActiveSelection(QualifiedSelection selection);
9+
bool TrySetSelection(QualifiedModuleName module, Selection selection);
10+
bool TrySetSelection(QualifiedSelection selection);
11+
}
12+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using System;
2+
using NLog;
3+
using Rubberduck.VBEditor.ComManagement;
4+
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
5+
6+
namespace Rubberduck.VBEditor.Utility
7+
{
8+
public class SelectionService : ISelectionService
9+
{
10+
private readonly IProjectsProvider _projectsProvider;
11+
private readonly IVBE _vbe;
12+
13+
private Logger _logger = LogManager.GetCurrentClassLogger();
14+
15+
public SelectionService(IVBE vbe, IProjectsProvider projectsProvider)
16+
{
17+
_vbe = vbe;
18+
_projectsProvider = projectsProvider;
19+
}
20+
21+
22+
public QualifiedSelection? ActiveSelection()
23+
{
24+
using (var activeCodePane = _vbe.ActiveCodePane)
25+
{
26+
return activeCodePane?.GetQualifiedSelection();
27+
}
28+
}
29+
30+
public Selection? Selection(QualifiedModuleName module)
31+
{
32+
var component = _projectsProvider.Component(module);
33+
if (component == null)
34+
{
35+
return null;
36+
}
37+
38+
using (var codeModule = component.CodeModule)
39+
using (var codePane = codeModule.CodePane)
40+
{
41+
return codePane.Selection;
42+
}
43+
}
44+
45+
public bool TryActivate(QualifiedModuleName module)
46+
{
47+
try
48+
{
49+
var component = _projectsProvider.Component(module);
50+
if (component == null)
51+
{
52+
return false;
53+
}
54+
55+
using (var codeModule = component.CodeModule)
56+
using(var codePane = codeModule.CodePane)
57+
{
58+
_vbe.ActiveCodePane = codePane;
59+
}
60+
61+
return true;
62+
}
63+
catch (Exception exception)
64+
{
65+
_logger.Debug(exception, $"Failed to activate the code pane of module {module}.");
66+
return false;
67+
}
68+
}
69+
70+
public bool TrySetActiveSelection(QualifiedSelection selection)
71+
{
72+
var activeCodePane = _vbe.ActiveCodePane;
73+
74+
if (!TryActivate(selection.QualifiedName))
75+
{
76+
return false;
77+
}
78+
79+
if (!TrySetSelection(selection))
80+
{
81+
TryActivate(activeCodePane.QualifiedModuleName);
82+
return false;
83+
}
84+
85+
return true;
86+
}
87+
88+
public bool TrySetSelection(QualifiedModuleName module, Selection selection)
89+
{
90+
try
91+
{
92+
var component = _projectsProvider.Component(module);
93+
if (component == null)
94+
{
95+
return false;
96+
}
97+
98+
using (var codeModule = component.CodeModule)
99+
using (var codePane = codeModule.CodePane)
100+
{
101+
codePane.Selection = selection;
102+
}
103+
104+
return true;
105+
}
106+
catch (Exception exception)
107+
{
108+
_logger.Debug(exception, $"Failed to set the selection of module {module} to {selection}.");
109+
return false;
110+
}
111+
}
112+
113+
public bool TrySetSelection(QualifiedSelection selection)
114+
{
115+
return TrySetSelection(selection.QualifiedName, selection.Selection);
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)