Click here to Skip to main content
Click here to Skip to main content
Go to top

Visual Studio Extensibility

, 3 Sep 2012
Rate this:
Please Sign up or sign in to vote.
Just wrote a quick macro for Visual Studio that would replace with >, etc.

Just wrote a quick macro for Visual Studio that would replace < with < and > with >, etc. I am probably the 1000th person to do that. Yet, it proved to be surprisingly hard.

  • DTE.ActiveDocument.Selection is an Object. No methods, no nothing. Need to cast to TextSelection to do anything useful with it.
  • TextSelection.ReplacePattern() happily replaces one pattern, then loses selection. Attempts to artificially restore selection failed, as TopPoint and BottomPoint properties are read only. MoveToPoint() did not do the trick either. I am still not sure how to tell the IDE to select the text from here to there.
  • If you attempt to do something like Selection.Text = Selection.Text.Replace((...) it works fine for one line, but for multiline selection it inserts lots of spaces and generally looks very weird. It also takes a lot of time and occasionally throws exceptions. A non starter.
  • Tried to use DTE.Find object, could not find a way to close programmatically the damn Find window. Fortunately DTE.Find.FindReplace() works without opening the window.

Oh yeah, if you try Googling for it – forget about it. You are drowning in crap. Bing is slightly better, but still no help there.

Final solution:

    Sub EscapeXmlChars()
        Replace("&", "&")
        Replace("<", "<")
        Replace(">", ">")
    End Sub

    Sub UnescapeXmlChars()
        Replace("<", "<")
        Replace(">", ">")
        Replace("&", "&")
    End Sub

    Private Sub Replace(ByVal find As String, ByVal replace As String)
        DTE.ActiveDocument.Activate()
        DTE.Find.FindReplace( _
        vsFindAction.vsFindActionReplaceAll, _
        find, _
        vsFindOptions.vsFindOptionsMatchInHiddenText, _
        replace, _
        vsFindTarget.vsFindTargetCurrentDocumentSelection)
    End Sub

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0

Share

About the Author

Ivan Krivyakov
Technical Lead Thomson Reuters
United States United States
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.
 
Please visit my web site: www.ikriv.com.

Comments and Discussions

 
Questionшутка дня! PinmemberThornik5-Sep-12 0:01 
AnswerRe: шутка дня! PinmemberIvan Krivyakov6-Sep-12 10:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 3 Sep 2012
Article Copyright 2012 by Ivan Krivyakov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid