Collapse all but this

On this morning's little public editor conference call, someone remarked (and others agreed) that they wanted a way to do "collapse everything in this file but where the cursor is". Seems like an interesting enough scenario, and it's rather simple to write as an extension, so I went ahead and wrote one during the conference call:

CollapseAllButThis.cs on

One of the things it doesn't really do is handle commands (partially due to a desire for brevity, and partially because the MEF extensibility isn't there for that). It uses a keyprocessor to handle Ctrl+1 as the command for "collapse all but this", which has a few issues inside VS, which is why you should avoid using key processors inside visual studio:

  • If there is a command bound to ctrl+1, the key processor will never see the key presses.
  • There isn't an actual "command" that people could listen to or handle differently.
  • You can't rebind the command to something else (except by changing and re-compiling the code.

I have a work item to make a MEF extension for handling existing commands in the editor, but it doesn't include the ability to register new commands. That's something you can do through DTE, but only if there is an add-in loaded. I'm mulling over creating a prototype extension for letting you create commands via MEF, but it's unfortunately not straightforward. Technically, it's also not really an editor thing, but it still seems like a useful thing to be able to do through MEF.

If you want to try this extension out, you can do what I do:

  1. Download VS 2010 Beta 1 and the Beta 1 SDK
  2. In VS, create a new project of type Editor Classifier (you can really use any of editor extension templates, I just tend to go to this one first). You can search for it by typing "editor" in the New Project dialog.
  3. Delete all the code files except for one (or just delete all the code files and add a new one).
  4. Replace the contents of the code file with the pasted gist.

...and you should be off and running. Let me know if that doesn't work for you in the comments, and please feel free to take it and change it however you like. At some point in the future, I'll write an article about how outlining works in the new editor, which should shine more light on the types of things you can do like this (in 100 lines of code or less - this one clocks in around 50, I think).

Comments and questions welcome!

Comments (0)

Skip to main content