Skip to content

Commit

Permalink
Maintain selection after indenting. Closes #4452
Browse files Browse the repository at this point in the history
  • Loading branch information
comintern committed Oct 24, 2018
1 parent ebdd5c5 commit a5b1b88
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
56 changes: 48 additions & 8 deletions Rubberduck.SmartIndenter/Indenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public void IndentCurrentProcedure()
return;
}

var initialSelection = GetSelection(pane).Collapse();

using (var module = pane.CodeModule)
{
var selection = GetSelection(pane);
Expand All @@ -49,8 +51,9 @@ public void IndentCurrentProcedure()
using (var component = module.Parent)
{
Indent(component, selection, true);
}
}
}
ResetSelection(pane, initialSelection);
}
}

Expand All @@ -66,13 +69,17 @@ public void IndentCurrentModule()
return;
}

var initialSelection = GetSelection(pane).Collapse();

using (var module = pane.CodeModule)
{
using (var component = module.Parent)
{
Indent(component);
}
}
}
}

ResetSelection(pane, initialSelection);
}
}

Expand All @@ -81,14 +88,47 @@ public void IndentCurrentModule()
/// </summary>
public void IndentCurrentProject()
{
var project = _vbe.ActiveVBProject;
if (project.Protection == ProjectProtection.Locked)
using (var pane = _vbe.ActiveCodePane)
{
var initialSelection = pane == null || pane.IsWrappingNullReference ? default : GetSelection(pane).Collapse();

var project = _vbe.ActiveVBProject;
if (project.Protection == ProjectProtection.Locked)
{
return;
}

foreach (var component in project.VBComponents)
{
Indent(component);
}

ResetSelection(pane, initialSelection);
}
}

private void ResetSelection(ICodePane codePane, Selection initialSelection)
{
using (var window = _vbe.ActiveWindow)
{
return;
if (initialSelection == default || codePane == null || window == null ||
window.IsWrappingNullReference || window.Type != WindowKind.CodeWindow ||
codePane.IsWrappingNullReference)
{
return;
}
}
foreach (var component in project.VBComponents)

using (var module = codePane.CodeModule)
{
Indent(component);
// This will only "ballpark it" for now - it sets the absolute line in the module, not necessarily
// the specific LoC. That will be a TODO when the parse tree is used to indent. For the time being,
// maintaining that is ridiculously difficult vis-a-vis the payoff if the vertical spacing is
// changed.
var lines = module.CountOfLines;
codePane.Selection = lines < initialSelection.StartLine
? new Selection(lines, initialSelection.StartColumn, lines, initialSelection.StartColumn)
: initialSelection;
}
}

Expand Down
3 changes: 3 additions & 0 deletions Rubberduck.VBEEditor/Selection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public Selection(int startLine, int startColumn, int endLine, int endColumn)
public Selection ShiftLeft(int positions = 1) =>
new Selection(StartLine, Math.Max(1, StartColumn - positions), EndLine, Math.Max(1, StartColumn - positions));

public Selection Collapse() =>
new Selection(EndLine, EndColumn, EndLine, EndColumn);

public bool IsEmpty()
{
return Equals(Empty);
Expand Down

0 comments on commit a5b1b88

Please sign in to comment.