Visual Studio Find and Replace Macro

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Prelates, Moderators General

Visual Studio Find and Replace Macro

Postby MrGee » Wed Mar 14, 2012 1:13 am UTC

I've asked several people this question and they all seemed stumped. The issue is this: I need a Visual Studio 2010 macro that will scan my solution, take the code in the [source][/source] tags, and put it between the [destination][/destination] tags. Sounds easy right? Unfortunately I can't figure out how to capture the results of a find in VS. Here is what the code should sort of look like:
Code: Select all
Public Sub FindAndReplace()
        DTE.Find.SearchPath = "Entire Solution"
        DTE.Find.FindWhat = "[source]{.*}[/source]"
        DTE.Find.Target = vsFindTarget.vsFindTargetSolution
        DTE.Find.MatchCase = False
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr
        DTE.Find.Action = vsFindAction.vsFindActionFind
        Dim results = DTE.Find.Execute           //this doesn't actually return the results; how do I capture them?

    DTE.Find.FindWhat =  "[destination]{.*}[/destination]"
    DTE.Find.FindAction = vsFindActionReplaceAll
    DTE.FindTarget = vsFindTargetSolution
    DTE.Find.ReplaceWith = results
    DTE.Find.Execute

    End Sub

I've tried everything I can think of and I've searched several forums....it's all just people asking how a regex works. Any thoughts?
MrGee
 
Posts: 998
Joined: Sat Jun 14, 2008 9:33 pm UTC

Re: Visual Studio Find and Replace Macro

Postby Sc4Freak » Wed Mar 14, 2012 3:17 am UTC

Could you give an example of what you actually want to achieve? This sounds like something that can be done simply with a regex.
User avatar
Sc4Freak
 
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Visual Studio Find and Replace Macro

Postby MrGee » Wed Mar 14, 2012 8:58 pm UTC

Code: Select all

public class Foo {
    //[source1]
    public string ToString() {
        return "I am overriding ToString!"
    }
    //[source1]
}

public class Bar {
    //[destination1]
    //[/destination1]
}

Objective: have Bar override ToString(). And no, I can't just copy-paste, because there are tons of sources and tons of destinations and they all have to stay in sync.
MrGee
 
Posts: 998
Joined: Sat Jun 14, 2008 9:33 pm UTC

Re: Visual Studio Find and Replace Macro

Postby Zabaron » Thu Mar 15, 2012 3:19 am UTC

Erm, this seems like a job for inheritance. Just have
Code: Select all
public class FooBase {
    public string ToString() {
        return "I am overriding ToString!"
    }
}

public class Foo : FooBase {
    //Foo specific functionality.
}
public class Bar : FooBase {
    //Bar specific functionality.
}


In general, if you have a bunch of blocks of identical code that you need to keep in sync, there's some way you can factor that code out into its own unit and just use it everywhere you need it.

--Zabaron
I love deadlines. I love that wooshing sound they make as they fly by. -Douglas Adams
User avatar
Zabaron
 
Posts: 112
Joined: Wed Jun 04, 2008 3:33 am UTC
Location: Georgia

Re: Visual Studio Find and Replace Macro

Postby Sc4Freak » Thu Mar 15, 2012 5:44 am UTC

Find:
Code: Select all
//\[source1\]{(.|\n)*}//\[/source1\]{(.|\n)*}//\[destination1\](.|\n)*//\[/destination1\]

Replace with:
Code: Select all
//[source1]\n//[/source1]\2//[destination1]\1//[/destination1]

Unreadable, but seems to work just fine.
User avatar
Sc4Freak
 
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Visual Studio Find and Replace Macro

Postby MrGee » Thu Mar 15, 2012 4:30 pm UTC

Sc4Freak wrote:Find:
Code: Select all
//\[source1\]{(.|\n)*}//\[/source1\]{(.|\n)*}//\[destination1\](.|\n)*//\[/destination1\]

Replace with:
Code: Select all
//[source1]\n//[/source1]\2//[destination1]\1//[/destination1]

Unreadable, but seems to work just fine.


So you do the whole thing in one call to DTE.Find.Execute? Not sure I follow the logic, but I'll give it a try.
MrGee
 
Posts: 998
Joined: Sat Jun 14, 2008 9:33 pm UTC

Re: Visual Studio Find and Replace Macro

Postby Sc4Freak » Thu Mar 15, 2012 11:27 pm UTC

It's not a macro, it's just a regex that you use in the Find and Replace window.
User avatar
Sc4Freak
 
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Visual Studio Find and Replace Macro

Postby MrGee » Tue Mar 20, 2012 8:04 pm UTC

Sc4Freak wrote:Find:
Code: Select all
//\[source1\]{(.|\n)*}//\[/source1\]{(.|\n)*}//\[destination1\](.|\n)*//\[/destination1\]

Replace with:
Code: Select all
//[source1]\n//[/source1]\2//[destination1]\1//[/destination1]

Unreadable, but seems to work just fine.


So I tried this out, and it works as a macro. Thanks! There were a few caveats though:

1. It deletes the source code
2. It only replaces one instance of [destination], not all instances
3. It doesn't work if [destination] comes before [source]
4. It doesn't work across files

#1 I fixed as follows:

Code: Select all
DTE.Find.ReplaceWith = "//[source1]\1//[/source1]\2//[destination1]\1//[/destination1]"


#2 and #3 I can probably fix with some effort. But I have no idea how I can make this work between separate files. Any suggestions?
MrGee
 
Posts: 998
Joined: Sat Jun 14, 2008 9:33 pm UTC

Re: Visual Studio Find and Replace Macro

Postby MrGee » Wed Mar 21, 2012 6:19 pm UTC

...I think this would be very easy to do with the .NET Regex class; if I were operating on a string variable. So as an alternate solution, how do I get the entire VS solution into a variable? For example:

Code: Select all
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.ReplacePattern(......)


I should be able to do it like this since I can use standard.NET regex syntax. So how do I get "selection" to be all files in the solution, or to iterate through all the files (hopefully without opening them all in the text editor window)?
MrGee
 
Posts: 998
Joined: Sat Jun 14, 2008 9:33 pm UTC


Return to Coding

Who is online

Users browsing this forum: No registered users and 3 guests