Skip to content

Commit

Permalink
Add RegionManagement.Alternate command
Browse files Browse the repository at this point in the history
Support using a single keyboard shortcut to alternate between collapse
all and expand all regions. Add outlining menu entry "Alternate all
regions" with command RegionManagement.Alternate that is associated with
keybinding CTRL+R, CTRL+Num / by default.
  • Loading branch information
mdmower committed Jul 9, 2024
1 parent 7f44687 commit e959d87
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 61 deletions.
101 changes: 49 additions & 52 deletions Common/RegionCommands.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Editor;
Expand All @@ -12,6 +13,14 @@

namespace ToggleRegionsExtension
{
enum Operation
{
Expand,
Collapse,
Toggle,
Alternate
}

/// <summary>
/// Command handler
/// </summary>
Expand All @@ -32,6 +41,11 @@ internal sealed class RegionCommands
/// </summary>
public const int ToggleCommandId = 0x0102;

/// <summary>
/// Alternate Command ID.
/// </summary>
public const int AlternateCommandId = 0x0103;

/// <summary>
/// Command menu group (command set GUID).
/// </summary>
Expand Down Expand Up @@ -64,6 +78,10 @@ private RegionCommands(AsyncPackage package, OleMenuCommandService commandServic
var toggleCommandId = new CommandID(CommandSet, ToggleCommandId);
var toggleMenuItem = new MenuCommand(Toggle, toggleCommandId);
commandService.AddCommand(toggleMenuItem);

var alternateCommandId = new CommandID(CommandSet, AlternateCommandId);
var alternateMenuItem = new MenuCommand(Alternate, alternateCommandId);
commandService.AddCommand(alternateMenuItem);
}

/// <summary>
Expand Down Expand Up @@ -96,61 +114,25 @@ public static async Task InitializeAsync(AsyncPackage package)

private void Expand(object sender, EventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();
_ = _package.JoinableTaskFactory.RunAsync(async () =>
{
if (_package.DisposalToken.IsCancellationRequested)
{
return;
}

try
{
var (manager, regions) = await GetCurrentDocInfoAsync();
foreach (var region in regions)
{
if (region is ICollapsed collapsed)
{
manager.Expand(collapsed);
}
}
}
catch (Exception ex)
{
ActivityLog.LogError(ex.Source, ex.Message);
}
});
HandleOperation(Operation.Expand);
}

private void Collapse(object sender, EventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();
_ = _package.JoinableTaskFactory.RunAsync(async () =>
{
if (_package.DisposalToken.IsCancellationRequested)
{
return;
}

try
{
var (manager, regions) = await GetCurrentDocInfoAsync();
foreach (var region in regions)
{
if (!region.IsCollapsed)
{
manager.TryCollapse(region);
}
}
}
catch (Exception ex)
{
ActivityLog.LogError(ex.Source, ex.Message);
}
});
HandleOperation(Operation.Collapse);
}

private void Toggle(object sender, EventArgs e)
{
HandleOperation(Operation.Toggle);
}

private void Alternate(object sender, EventArgs e)
{
HandleOperation(Operation.Alternate);
}

private void HandleOperation(Operation operation)
{
ThreadHelper.ThrowIfNotOnUIThread();
_ = _package.JoinableTaskFactory.RunAsync(async () =>
Expand All @@ -163,15 +145,30 @@ private void Toggle(object sender, EventArgs e)
try
{
var (manager, regions) = await GetCurrentDocInfoAsync();

bool? shouldExpand = null;
if (operation == Operation.Expand)
{
shouldExpand = true;
}
if (operation == Operation.Collapse)
{
shouldExpand = false;
}
else if (operation == Operation.Alternate)
{
shouldExpand = regions.Any(region => region.IsCollapsed);
}

foreach (var region in regions)
{
if (!region.IsCollapsed)
if (shouldExpand != false && region is ICollapsed collapsed)
{
manager.TryCollapse(region);
manager.Expand(collapsed);
}
else if (region is ICollapsed collapsed)
else if (shouldExpand != true && !region.IsCollapsed)
{
manager.Expand(collapsed);
manager.TryCollapse(region);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions Metadata/RegionCommandsPackage.vsct
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,21 @@
<ButtonText>Toggle all regions</ButtonText>
</Strings>
</Button>
<Button guid="guidRegionCommandsPackageCmdSet" id="RegionAlternateId" priority="0xFFFF" type="Button">
<Parent guid="guidStdEd" id="IDG_VS_EDITOR_OUTLINING_CMDS" />
<Strings>
<LocCanonicalName>RegionManagement.Alternate</LocCanonicalName>
<ButtonText>Alternate all regions</ButtonText>
</Strings>
</Button>
</Buttons>
</Commands>

<KeyBindings>
<KeyBinding guid="guidRegionCommandsPackageCmdSet" id="RegionExpandId" editor="guidVSStd97" key1="R" mod1="Control" key2="VK_ADD" mod2="Control" />
<KeyBinding guid="guidRegionCommandsPackageCmdSet" id="RegionCollapseId" editor="guidVSStd97" key1="R" mod1="Control" key2="VK_SUBTRACT" mod2="Control" />
<KeyBinding guid="guidRegionCommandsPackageCmdSet" id="RegionToggleId" editor="guidVSStd97" key1="R" mod1="Control" key2="VK_MULTIPLY" mod2="Control" />
<KeyBinding guid="guidRegionCommandsPackageCmdSet" id="RegionAlternateId" editor="guidVSStd97" key1="R" mod1="Control" key2="VK_DIVIDE" mod2="Control" />
</KeyBindings>

<Symbols>
Expand All @@ -93,6 +101,7 @@
<IDSymbol name="RegionExpandId" value="0x0100" />
<IDSymbol name="RegionCollapseId" value="0x0101" />
<IDSymbol name="RegionToggleId" value="0x0102" />
<IDSymbol name="RegionAlternateId" value="0x0103" />
</GuidSymbol>

</Symbols>
Expand Down
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,22 @@ Alternatively, a `.vsix` installation file can be downloaded from [Releases](htt

### Configurable Commands

Three commands are added to the Outlining menu: "Expand all regions", "Collapse all regions", and "Toggle all regions".

These commands are exposed to Visual Studio as:

- `RegionManagement.Expand` - Default shortcut: CTRL+R, CTRL+Num +
- `RegionManagement.Collapse` - Default shortcut: CTRL+R, CTRL+Num -
- `RegionManagement.Toggle` - Default shortcut: CTRL+R, CTRL+Num \*

... and can be remapped in Tools > Options > Environment > Keyboard.
The following commands are added to the Outlining menu and can be assigned custom shortcuts:

- Expand all regions
- Command: `RegionManagement.Expand`
- Default shortcut: CTRL+R, CTRL+Num +
- Collapse all regions
- Command: `RegionManagement.Collapse`
- Default shortcut: CTRL+R, CTRL+Num -
- Toggle all regions - expand collapsed regions and collapse expanded regions
- Command: `RegionManagement.Toggle`
- Default shortcut: CTRL+R, CTRL+Num \*
- Alternate all regions - expands all regions if any region is collapsed, otherwise collapses all regions
- Command: `RegionManagement.Alternate`
- Default shortcut: CTRL+R, CTRL+Num /

Remap keyboard shortcuts in Tools > Options > Environment > Keyboard (filter by `RegionManagement`).

### Compatibility

Expand Down

0 comments on commit e959d87

Please sign in to comment.