Skip to content

Commit a02424e

Browse files
committed
Introduce the ISelection service
This service is a provides access to the active selection and selection of individual modules without involving COM wrappers directly; the interface only uses QMNs and selections.
1 parent 81a2626 commit a02424e

File tree

3 files changed

+572
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)