Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@noahrichards
Created June 12, 2009 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noahrichards/128747 to your computer and use it in GitHub Desktop.
Save noahrichards/128747 to your computer and use it in GitHub Desktop.
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Utilities;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Outlining;
using System.Windows.Input;
using Microsoft.VisualStudio.Text;
namespace CollapseAllButThis
{
[Export(typeof(IKeyProcessorProvider))]
[ContentType("code")]
[Name("CollapseAllButThisKeyProc")]
[Order(Before = "VisualStudioKeyProcessor")]
internal sealed class CollapseAllKeyProvider : IKeyProcessorProvider
{
[Import]
internal IOutliningManagerService OutliningManagerService;
public KeyProcessor GetAssociatedProcessor(IWpfTextViewHost wpfTextViewHost)
{
var outliningManager = OutliningManagerService.GetOutliningManager(wpfTextViewHost.TextView.TextViewModel);
if (outliningManager != null)
return new CollapseAllKeyProc(wpfTextViewHost.TextView, outliningManager);
return null;
}
}
internal sealed class CollapseAllKeyProc : KeyProcessor
{
private ITextView textView;
private IOutliningManager outliningManager;
public CollapseAllKeyProc(ITextView textView, IOutliningManager outliningManager)
{
this.textView = textView;
this.outliningManager = outliningManager;
}
public override void KeyDown(KeyEventArgs args)
{
if (args.Key == Key.D1 &&
(Keyboard.Modifiers & ModifierKeys.Control) != 0)
{
var point = textView.Caret.Position.BufferPosition;
outliningManager.CollapseAll(new SnapshotSpan(textView.TextSnapshot, 0, textView.TextSnapshot.Length),
collapsible => !collapsible.Extent.GetSpan(textView.TextSnapshot).Contains(point));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment