Copying Visual Studio Code Snippets to the Clipboard as HTML

As I mentioned in Formatting HTML code snippets with Ten Ton Wrecking Balls, copying code to your clipboard in Visual Studio is often an excercise in futility if you want anything more than plain vanilla text. VS copies code to the clipboard with bizarro-world RTF formatting instead of the sane, simple HTML markup you might expect. This is true even of the brand spanking new VS.NET 2005.


This is a companion discussion topic for the original blog entry at: http://www.codinghorror.com/blog/2005/10/copying-visual-studio-code-snippets-to-the-clipboard-as-html.html

Here’s a plugin that is quite awesome…
a href="http://www.jtleigh.com/people/colin/blog/archives/2004/10/copysourceashtm.html"http://www.jtleigh.com/people/colin/blog/archives/2004/10/copysourceashtm.html/a

Right, this is the XCOPY and no-UI version of that add-in.

  • no pesky installation
  • no dialogs prompting you for information

it “just works”. In other words, this is the way copy and paste should frickin’ work in VS.NET 2005. But it doesn’t.

Also, one criticism of the code that add-on is based on:

Also, one minor niggle I’ve had with the Manoli formatter is that the xml comments tags and the triplle slashes (such as /// ) should be gray to mimic VS.NET instead of all green. How hard would it be to fix that?

I preserve the color scheme EXACTLY as VS.NET places it in the clipboard.

Plus I remove excessive indentation, which is great when you’re copying one method out of a class.

And I work with a single mapped hotkey, just like CTRL+C does.

So there’s your differentiation :wink:

AWESOME! Thanks for sharing this wonderful snippet.

The only thing that I changed was instead of:
Private Const CodeFontName As String = “Courier New”

I’ve put:
Private Const CodeFontName As String = “fixed-width”

That way it’ll use whatever fixed-width font is set as the default instead of horrible Courier New.

Also, I think I may be missing some necessary references. (I had to add System.Drawing and System.Web) Where is EnvDTE80? Is that for VS.2005?

Thanks!

I had to add System.Drawing and System.Web

Yep, you’ll need those. The install procedure is documented in the earlier post:

http://www.codinghorror.com/blog/archives/000319.html

Where is EnvDTE80? Is that for VS.2005?

Yeah, it’s for VS.2005; it isn’t required for VS.2003.

Private Const CodeFontName As String = “fixed-width”

I agree, that’s what it should be by default! I’ll make that change…

Private Const CodeFontName As String = “fixed-width”

Actually, “fixed-width” doesn’t work. but this does:

http://www.zvon.org/xxl/css1Reference/Output/property_font-family.html

Private Const CodeFontName As String = “monospace”

Excellent…

I just updated the macro to…

  • remove leading whitespace from the Text as well as the Html
  • use the “monospace” generic font family instead of hard-coding “Courier New”
  • fixed a bug with leading whitespace removal for HTML/XML document types

It works amazingly well as a generic replacement for copy when the macro is bound to the standard CTRL+C shortcut.

I also tested it with XML and HTML document types and it now works just as well as with VB.NET and C# code. It’s fairly generic.

It’s a great add-in (which I referenced in the comments to my original post), but it’s still an external add-in; all other things being equal, I prefer a lighter weight IDE macro that does the same thing. Easier to set up, easier to modify, more discoverable, etcetera.

A couple observations on differentiation:

  1. My macro works for XML, HTML, and every other IDE filetype, in VS.NET 2003 and VS.NET 2005. Yours only works for C# or VB.NET code in VS.NET 2003.

  2. You aren’t copying background colors. Try setting a background color (eg, for a string); it won’t show up in the resulting HTML. That’s the same problem I have, and it’s because the RTF inserted in the clipboard doesn’t have that information.

If you could come up with a way to truly retain background colors, I’d definitely be inclined to use your solution :wink:

Jeff,

  1. Versions 1.2.0 and higher parse the RTF generated by VS.NET and can copy anything VS.NET can highlight.

  2. Versions 1.2.1 and higher get font and color information from VS.NET

Application Properties FontsAndColors TextEditor FontFamily
Application Properties FontsAndColors TextEditor FontSize
Application Properties FontsAndColors TextEditor FontsAndColorsItems Plain Text
Application Properties FontsAndColors TextEditor FontsAndColorsItems Line Numbers

and use it to supplement the font and color information from the RTF. They can indeed copy background colors. The release page has an example with some ugly colors:

a href="http://www.jtleigh.com/CopySourceAsHtml/"http://www.jtleigh.com/CopySourceAsHtml//a

  1. I haven’t released a version for VS.NET 2005 yet, but that’s not because it’ll require massive changes, it’s because I’m lazy.

Now, to be fair, your macro does has advantages over CopySourceAsHtml:

  1. It doesn’t need to be installed.

  2. It already works with VS.NET 2005.

  3. It probably works under VPC. I meant to fix this problem, but… uh… well…

  4. You’re less lazy than I am.

But you should still grab the latest version and try it out. :slight_smile:

Cheers,

Colin

your macro does has advantages over CopySourceAsHtml

Well, it’s a Macro. Different strokes. I do like that it’s a lighter weight solution (IMO)

But you should still grab the latest version and try it out. :slight_smile:

But I did try it out! That’s what my last comments were based on.

Versions 1.2.1 and higher get font and color information from VS.NET

I previously installed the latest version 1.2.4 and I definitely didn’t get background colors for strings or numbers (the only two background color settings that I use).

That’s not good. I was particularly proud of those background colors. I’ll have to take some time next week to figure out why it’s not working, solve the VPC problem (it’s really the threading problem you work around in your macro, but I’ve only ever seen it manifest itself under VPC), and make a VS.NET 2005 version. :slight_smile:

Jeff,

CopySourceAsHtml hasn’t used Manoli’s formatter for almost a year. The latest version (1.2.4, July 04, 2005) allows you to

  • copy with (Copy…) or without (Copy) the copy preferences dialog,
  • configure which actions are shown in the edit and context menus,
  • bind actions to hotkeys,
  • remove indentation,
  • copy single-byte, multi-byte, and Unicode characters,
  • copy line numbers with configurable starting line number,
  • copy background colors, line number colors, syntax highlighting colors, etc exactly the way VS.NET does,
  • override font, size, generated CSS, etc,
  • and so on.

Have a look at the release page and try playing with a recent version:

a href="http://www.jtleigh.com/people/colin/software/CopySourceAsHtml/"http://www.jtleigh.com/people/colin/software/CopySourceAsHtml//a

Cheers,

Colin

By doing so, you could remove all “span color:black” from your code (and the “span cleaner”), considering text will use the div’s color.

This is used to “reset” the color from whatever it was back to the default. Conversion from RTF to HTML means directly replacing RTF tags with HTML tags. And since RTF tags don’t have a strict begin/end relationship like those of HTML, it can get a little squirrelly!

Am I wrong ?

No, I think that’s a great suggestion. I tried it in the source, and miraculously, it works. I was afraid there would be mis-matched SPAN tags, but they all match.

I’ll update the source! (EDIT) I spoke too soon. When I tried this on more complex code snippets, I got mismatching SPAN tags. I’m doing a blanket replacement of RTF tags, and that only works if each element is replaced. If I “skip” replacing the cf0 (black) tags, you end up with mismatching SPAN tags and invalid HTML.

Oh well.

I’ve changed some parts of your code to clean up the rendered html.

Why don’t you specify a black fore color like you’ve done for the background-color ?

By doing so, you could remove all “span color:black” from your code (and the “span cleaner”), considering text will use the div’s color.

Am I wrong ?

I found and liked this macro especially as it’s based on the IDE configuration and not a list of keywords, making it truly “WYSIWYG” in that sense.

However, I have problems with the HTML clipboard object it creates. It works fine in that FreeTextBox page, but not in a random text editor you’re using to make web pages? This really makes it less convenient than it could have been for me.

I can understand why one would find a HTML object to be nice, but for my uses, I wouldn’t mind it simply being copied as a text object as HTML code. Right now when I paste it into e.g. Notepad, EditPlus, or TopStyle Pro, I just get plaintext as if the macro didn’t do anything. :frowning:

I might add that this HTML object mode is still very useful for the tools that support it though (I found OpenOffice.org to do), so by no means do I wish the feature should be exchanged. :-/ Probably more WYSIWYG HTML editors do as well, but “text” HTML editors may apparently not.

Right now when I paste it into e.g. Notepad, EditPlus, or TopStyle Pro, I just get plaintext as if the macro didn’t do anything. :frowning:

It’s trivial to edit the macro to do this. Simply change the line…

SetClipboardHtml(RtfToHtml(rtf), text)

To…

Dim html As String = RtfToHtml(rtf)
SetClipboardHtml(html, html)

When I try to build this macro, it shows me some errors. It does not know Drawing class and HttpUtility is not a member of Web.

I try to import System.Drawing but can not find it when I type dot behinds System. And I already import System.Web.

How can I fix it? My VS 2005 only installs C#, not VB.NET.