views:

67

answers:

1

I often find myself writing small (5-20 lines) files for things like input validation, reading a URL and so on. In fact, I probably write nearly the same files over and over because I either forget that I've already implemented such functionality in another project or I don't want to have to go searching for that file.

I want to know if there is a way to simply add code to a global repository from an IDE (in my case Visual Studio) and simply extract that code into whatever project I'm working on. It should be able to:

  1. Extract code to a new file or to the currently open file in my IDE.
  2. w.r.t. #1 I want smart defaults based on how I added the code to the repository.
  3. Easy to use UI.
  4. I don't need replacement/substitution of variables.

For instance, if I right click on a file I want to add to the repository then the default extraction operation is to create a new file. If, instead, I highlight some text in the currently open file and commit that, then the default is just to paste the snippet into the currently open file.

I've tried VS's snippet manager a few times, always with disappointment and quick abandonment. My biggest complaint is its interface and complexity required to add new code to it, plus the inability to extract the code to a file versus just inside whatever I have open.

+2  A: 

You might want to check out ReSharper's Live Templates.

I don't use them to the extent that you're obviously looking for, but you can definitely create new file-based templates. I use that all the time when I create a new unit testing fixture class - just Add New Item -> From Template -> NUnit Test Fixture, where "NUnit Test Fixture" is one of my defined file templates.

Additionally, all the templates can be assigned a shortcut sequence, so you can auto-replace a given combination of text with a code snippet.

All the template configuration is file based, so you can save them to solutions or global workspaces as well.

womp
I know he didn't say he was looking for a free solution, however if he is, ReSharper has a fee involved. http://www.jetbrains.com/resharper/buy/index.jsp
rockinthesixstring
Yeah, I should have mentioned that, thanks. ReSharper is worth the fee on so many other levels too - the templates feature is such a tiny subset of what it does for your productivity.
womp
FYI @womp... I just had a read through all of it's capabilities, and I purchased it.. LOL. +1 from me on ReSharper.
rockinthesixstring
I'll start looking into Live Templates - thanks! At first glance, it looks pretty similar to VS's template manager in terms of the interface and how it's activated (typing a few characters and then <TAB>). I do like the file templates though.
Chad
@rock - haha... I should ask them for a commission! Seriously though, Resharper is great. Search SO for threads on Resharper tips. It takes about three days to get used to the keyboard shortcuts and then you'll never go back.
womp