Skip to content

Commit fbd01e7

Browse files
authored
Merge pull request #2095 from Hosch250/Issue2077
Set selection after finishing refactoring
2 parents cd6d4d2 + 35a0d1f commit fbd01e7

File tree

9 files changed

+134
-1
lines changed

9 files changed

+134
-1
lines changed

RetailCoder.VBE/Refactorings/EncapsulateField/EncapsulateFieldRefactoring.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,22 @@ public void Refactor()
2929
}
3030

3131
_model = presenter.Show();
32-
3332
if (_model == null) { return; }
3433

34+
QualifiedSelection? oldSelection = null;
35+
if (_vbe.ActiveCodePane != null)
36+
{
37+
oldSelection = _vbe.ActiveCodePane.CodeModule.GetSelection();
38+
}
39+
3540
AddProperty();
3641

42+
if (oldSelection.HasValue)
43+
{
44+
oldSelection.Value.QualifiedName.Component.CodeModule.SetSelection(oldSelection.Value.Selection);
45+
oldSelection.Value.QualifiedName.Component.CodeModule.CodePane.ForceFocus();
46+
}
47+
3748
_model.State.OnParseRequested(this);
3849
}
3950

RetailCoder.VBE/Refactorings/ExtractInterface/ExtractInterfaceRefactoring.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,20 @@ public void Refactor()
4343
return;
4444
}
4545

46+
QualifiedSelection? oldSelection = null;
47+
if (_vbe.ActiveCodePane != null)
48+
{
49+
oldSelection = _vbe.ActiveCodePane.CodeModule.GetSelection();
50+
}
51+
4652
AddInterface();
4753

54+
if (oldSelection.HasValue)
55+
{
56+
oldSelection.Value.QualifiedName.Component.CodeModule.SetSelection(oldSelection.Value.Selection);
57+
oldSelection.Value.QualifiedName.Component.CodeModule.CodePane.ForceFocus();
58+
}
59+
4860
_state.OnParseRequested(this);
4961
}
5062

RetailCoder.VBE/Refactorings/ImplementInterface/ImplementInterfaceRefactoring.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Rubberduck.Refactorings.ImplementInterface
1313
{
14+
using VBEditor.Extensions;
15+
1416
public class ImplementInterfaceRefactoring : IRefactoring
1517
{
1618
private readonly VBE _vbe;
@@ -66,8 +68,20 @@ public void Refactor(QualifiedSelection selection)
6668
return;
6769
}
6870

71+
QualifiedSelection? oldSelection = null;
72+
if (_vbe.ActiveCodePane != null)
73+
{
74+
oldSelection = _vbe.ActiveCodePane.CodeModule.GetSelection();
75+
}
76+
6977
ImplementMissingMembers();
7078

79+
if (oldSelection.HasValue)
80+
{
81+
oldSelection.Value.QualifiedName.Component.CodeModule.SetSelection(oldSelection.Value.Selection);
82+
oldSelection.Value.QualifiedName.Component.CodeModule.CodePane.ForceFocus();
83+
}
84+
7185
_state.OnParseRequested(this);
7286
}
7387

RetailCoder.VBE/Refactorings/IntroduceField/IntroduceFieldRefactoring.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,21 @@ private void PromoteVariable(Declaration target)
8080
return;
8181
}
8282

83+
QualifiedSelection? oldSelection = null;
84+
if (_vbe.ActiveCodePane != null)
85+
{
86+
oldSelection = _vbe.ActiveCodePane.CodeModule.GetSelection();
87+
}
88+
8389
RemoveVariable(target);
8490
AddField(target);
8591

92+
if (oldSelection.HasValue)
93+
{
94+
oldSelection.Value.QualifiedName.Component.CodeModule.SetSelection(oldSelection.Value.Selection);
95+
oldSelection.Value.QualifiedName.Component.CodeModule.CodePane.ForceFocus();
96+
}
97+
8698
_state.OnParseRequested(this);
8799
}
88100

RetailCoder.VBE/Refactorings/IntroduceParameter/IntroduceParameterRefactoring.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,21 @@ private void PromoteVariable(Declaration target)
9595
return;
9696
}
9797

98+
QualifiedSelection? oldSelection = null;
99+
if (_vbe.ActiveCodePane != null)
100+
{
101+
oldSelection = _vbe.ActiveCodePane.CodeModule.GetSelection();
102+
}
103+
98104
RemoveVariable(target);
99105
UpdateSignature(target);
100106

107+
if (oldSelection.HasValue)
108+
{
109+
oldSelection.Value.QualifiedName.Component.CodeModule.SetSelection(oldSelection.Value.Selection);
110+
oldSelection.Value.QualifiedName.Component.CodeModule.CodePane.ForceFocus();
111+
}
112+
101113
_state.OnParseRequested(this);
102114
}
103115

RetailCoder.VBE/Refactorings/MoveCloserToUsage/MoveCloserToUsageRefactoring.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,21 @@ private void MoveCloserToUsage()
101101
return;
102102
}
103103

104+
QualifiedSelection? oldSelection = null;
105+
if (_vbe.ActiveCodePane != null)
106+
{
107+
oldSelection = _vbe.ActiveCodePane.CodeModule.GetSelection();
108+
}
109+
104110
// it doesn't make sense to do it backwards, but we need to work from the bottom up so our selections are accurate
105111
InsertDeclaration();
106112

113+
if (oldSelection.HasValue)
114+
{
115+
oldSelection.Value.QualifiedName.Component.CodeModule.SetSelection(oldSelection.Value.Selection);
116+
oldSelection.Value.QualifiedName.Component.CodeModule.CodePane.ForceFocus();
117+
}
118+
107119
_state.StateChanged += _state_StateChanged;
108120
_state.OnParseRequested(this);
109121
}
@@ -112,6 +124,12 @@ private void _state_StateChanged(object sender, ParserStateEventArgs e)
112124
{
113125
if (e.State != ParserState.Ready) { return; }
114126

127+
QualifiedSelection? oldSelection = null;
128+
if (_vbe.ActiveCodePane != null)
129+
{
130+
oldSelection = _vbe.ActiveCodePane.CodeModule.GetSelection();
131+
}
132+
115133
var newTarget = _state.AllUserDeclarations.FirstOrDefault(
116134
item => item.ComponentName == _target.ComponentName &&
117135
item.IdentifierName == _target.IdentifierName &&
@@ -125,6 +143,12 @@ private void _state_StateChanged(object sender, ParserStateEventArgs e)
125143
RemoveField(newTarget);
126144
}
127145

146+
if (oldSelection.HasValue)
147+
{
148+
oldSelection.Value.QualifiedName.Component.CodeModule.SetSelection(oldSelection.Value.Selection);
149+
oldSelection.Value.QualifiedName.Component.CodeModule.CodePane.ForceFocus();
150+
}
151+
128152
_state.StateChanged -= _state_StateChanged;
129153
_state.OnParseRequested(this);
130154
}

RetailCoder.VBE/Refactorings/RemoveParameters/RemoveParametersRefactoring.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,20 @@ public void Refactor()
4040
return;
4141
}
4242

43+
QualifiedSelection? oldSelection = null;
44+
if (_vbe.ActiveCodePane != null)
45+
{
46+
oldSelection = _vbe.ActiveCodePane.CodeModule.GetSelection();
47+
}
48+
4349
RemoveParameters();
4450

51+
if (oldSelection.HasValue)
52+
{
53+
oldSelection.Value.QualifiedName.Component.CodeModule.SetSelection(oldSelection.Value.Selection);
54+
oldSelection.Value.QualifiedName.Component.CodeModule.CodePane.ForceFocus();
55+
}
56+
4557
_model.State.OnParseRequested(this);
4658
}
4759

RetailCoder.VBE/Refactorings/Rename/RenameRefactoring.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,22 @@ public void Refactor()
3838
var presenter = _factory.Create();
3939
_model = presenter.Show();
4040

41+
QualifiedSelection? oldSelection = null;
42+
if (_vbe.ActiveCodePane != null)
43+
{
44+
oldSelection = _vbe.ActiveCodePane.CodeModule.GetSelection();
45+
}
46+
4147
if (_model != null && _model.Declarations != null)
4248
{
4349
Rename();
4450
}
51+
52+
if (oldSelection.HasValue)
53+
{
54+
oldSelection.Value.QualifiedName.Component.CodeModule.SetSelection(oldSelection.Value.Selection);
55+
oldSelection.Value.QualifiedName.Component.CodeModule.CodePane.ForceFocus();
56+
}
4557
}
4658

4759
public void Refactor(QualifiedSelection target)
@@ -55,10 +67,22 @@ public void Refactor(Declaration target)
5567
var presenter = _factory.Create();
5668
_model = presenter.Show(target);
5769

70+
QualifiedSelection? oldSelection = null;
71+
if (_vbe.ActiveCodePane != null)
72+
{
73+
oldSelection = _vbe.ActiveCodePane.CodeModule.GetSelection();
74+
}
75+
5876
if (_model != null && _model.Declarations != null)
5977
{
6078
Rename();
6179
}
80+
81+
if (oldSelection.HasValue)
82+
{
83+
oldSelection.Value.QualifiedName.Component.CodeModule.SetSelection(oldSelection.Value.Selection);
84+
oldSelection.Value.QualifiedName.Component.CodeModule.CodePane.ForceFocus();
85+
}
6286
}
6387

6488
private Declaration FindDeclarationForIdentifier()

RetailCoder.VBE/Refactorings/ReorderParameters/ReorderParametersRefactoring.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,21 @@ public void Refactor()
4343
return;
4444
}
4545

46+
QualifiedSelection? oldSelection = null;
47+
if (_vbe.ActiveCodePane != null)
48+
{
49+
oldSelection = _vbe.ActiveCodePane.CodeModule.GetSelection();
50+
}
51+
4652
AdjustReferences(_model.TargetDeclaration.References);
4753
AdjustSignatures();
4854

55+
if (oldSelection.HasValue)
56+
{
57+
oldSelection.Value.QualifiedName.Component.CodeModule.SetSelection(oldSelection.Value.Selection);
58+
oldSelection.Value.QualifiedName.Component.CodeModule.CodePane.ForceFocus();
59+
}
60+
4961
_model.State.OnParseRequested(this);
5062
}
5163

0 commit comments

Comments
 (0)