Copy Code in HTML format with Visual Studio 2010

Today Jason has announced the Visual Studio 2010 Productivity Power Tools – a set of VS 2010 extensions that we released to complement and enhance the built-in 2010 features. You can either install the Pro Power Tools from here or just go to the Tools –> Extension Manager and type "Pro Power Tools" in the search box:

image

For the Pro Power Tools, I wrote a feature to copy formatted source code to clipboard in HTML format. The traditional VS editor only copies the plain text and RTF formats to clipboard, and there are numerous tools available to convert RTF to HTML. With the Pro Power Tools installed, pressing Ctrl+C or otherwise copying the code editor selection to clipboard, it will place the formatted and colorized HTML fragment on clipboard, alongsize the plain text and RTF format.

Then you can paste the code anywhere HTML is supported, such as Windows Live Writer:

image

Ctrl+Shift+V (Paste Special), Alt+K (Keep Formatting) and Enter to insert the colorized code to Live Writer.

In a future version of the power tools I will introduce customization – you will be able to fully control what HTML gets generated:

  1. Prepend any HTML to the code fragment
  2. Append any HTML to the code fragment
  3. Prepend any HTML before every line of code
  4. Append any HTML after every line of code
  5. Include line numbers and the format string for them
  6. Replace the space with  
  7. Replace the carriage return line feed with <br />
  8. Emit <span style="color: blue">void</span> vs. <span class="keyword">void</span>

Also, the current version has a bug: Cut (Ctrl+X) doesn't place formatted HTML to clipboard, only Copy does. I forgot to hook up the Edit.Cut command and never got around to fixing it. I'll fix it for the next release though.

Hope you like it, and as always, your feedback is appreciated!

P.S. Here's the sample code formatted using the extension:

         private static string IntersperseLineBreaks(string text)
        {
            text = text.Replace("\n\r", "\n \r");
            return text;
        }

You can View Source on this page, look for IntersperseLineBreaks and view the HTML that I generate. In a future release you will be able to customize the output of the tool, but for now this is what it defaults to.