Skip to content

Commit a5b1b88

Browse files
committed
Maintain selection after indenting. Closes #4452
1 parent ebdd5c5 commit a5b1b88

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

Rubberduck.SmartIndenter/Indenter.cs

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public void IndentCurrentProcedure()
3030
return;
3131
}
3232

33+
var initialSelection = GetSelection(pane).Collapse();
34+
3335
using (var module = pane.CodeModule)
3436
{
3537
var selection = GetSelection(pane);
@@ -49,8 +51,9 @@ public void IndentCurrentProcedure()
4951
using (var component = module.Parent)
5052
{
5153
Indent(component, selection, true);
52-
}
54+
}
5355
}
56+
ResetSelection(pane, initialSelection);
5457
}
5558
}
5659

@@ -66,13 +69,17 @@ public void IndentCurrentModule()
6669
return;
6770
}
6871

72+
var initialSelection = GetSelection(pane).Collapse();
73+
6974
using (var module = pane.CodeModule)
7075
{
7176
using (var component = module.Parent)
7277
{
7378
Indent(component);
74-
}
75-
}
79+
}
80+
}
81+
82+
ResetSelection(pane, initialSelection);
7683
}
7784
}
7885

@@ -81,14 +88,47 @@ public void IndentCurrentModule()
8188
/// </summary>
8289
public void IndentCurrentProject()
8390
{
84-
var project = _vbe.ActiveVBProject;
85-
if (project.Protection == ProjectProtection.Locked)
91+
using (var pane = _vbe.ActiveCodePane)
92+
{
93+
var initialSelection = pane == null || pane.IsWrappingNullReference ? default : GetSelection(pane).Collapse();
94+
95+
var project = _vbe.ActiveVBProject;
96+
if (project.Protection == ProjectProtection.Locked)
97+
{
98+
return;
99+
}
100+
101+
foreach (var component in project.VBComponents)
102+
{
103+
Indent(component);
104+
}
105+
106+
ResetSelection(pane, initialSelection);
107+
}
108+
}
109+
110+
private void ResetSelection(ICodePane codePane, Selection initialSelection)
111+
{
112+
using (var window = _vbe.ActiveWindow)
86113
{
87-
return;
114+
if (initialSelection == default || codePane == null || window == null ||
115+
window.IsWrappingNullReference || window.Type != WindowKind.CodeWindow ||
116+
codePane.IsWrappingNullReference)
117+
{
118+
return;
119+
}
88120
}
89-
foreach (var component in project.VBComponents)
121+
122+
using (var module = codePane.CodeModule)
90123
{
91-
Indent(component);
124+
// This will only "ballpark it" for now - it sets the absolute line in the module, not necessarily
125+
// the specific LoC. That will be a TODO when the parse tree is used to indent. For the time being,
126+
// maintaining that is ridiculously difficult vis-a-vis the payoff if the vertical spacing is
127+
// changed.
128+
var lines = module.CountOfLines;
129+
codePane.Selection = lines < initialSelection.StartLine
130+
? new Selection(lines, initialSelection.StartColumn, lines, initialSelection.StartColumn)
131+
: initialSelection;
92132
}
93133
}
94134

Rubberduck.VBEEditor/Selection.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public Selection ShiftRight(int positions = 1) =>
3333
public Selection ShiftLeft(int positions = 1) =>
3434
new Selection(StartLine, Math.Max(1, StartColumn - positions), EndLine, Math.Max(1, StartColumn - positions));
3535

36+
public Selection Collapse() =>
37+
new Selection(EndLine, EndColumn, EndLine, EndColumn);
38+
3639
public bool IsEmpty()
3740
{
3841
return Equals(Empty);

0 commit comments

Comments
 (0)