Skip to content

Commit

Permalink
Merge pull request #4455 from comintern/fixes
Browse files Browse the repository at this point in the history
 Maintain selection after indenting.
  • Loading branch information
retailcoder committed Oct 26, 2018
2 parents e00250c + a5b1b88 commit 711c55d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,5 @@ $RECYCLE.BIN/
Installers/
*.xlsx

CodeGraphData/
CodeGraphData/
/Rubberduck.Deployment/Properties/launchSettings.json
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 711c55d

Please sign in to comment.