Click here to Skip to main content
15,885,309 members
Articles / Programming Languages / C#
Technical Blog

Visual Studio Extensibility

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
3 Sep 2012Apache1 min read 8.8K   2   2
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

This article was originally posted at http://www.ikriv.com/blog?p=1055

License

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


Written By
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шутка дня! Pin
Thornik5-Sep-12 0:01
Thornik5-Sep-12 0:01 
AnswerRe: шутка дня! Pin
Ivan Krivyakov6-Sep-12 10:40
Ivan Krivyakov6-Sep-12 10:40 

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

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