2

I've asked this question on three different forums and no one can figure it out. I'm trying to write a macro in VS2010 that will copy some code around. So, given this setup:

public class foo {
    //[source1]
    public overrides string ToString() {
        return "Hello from Foo"
    }
    //[/source1]
}

public class bar {
    //[destination1]

    //[/destination1]
}

...the objective is to click the macro and have the code copied to bar, so that it overrides ToString() also. I have a semi-working version

DTE.Find.FindWhat = "(//\[source1\]{(.|\n)*})//\[/source1\])|//\[destination1\]{(.|\n)*}//\[/destination1\]"
    DTE.Find.Target = vsFindTarget.vsFindTargetSolution
    DTE.Find.MatchCase = False
    DTE.Find.MatchInHiddenText = True
    DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr
    DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
    DTE.Find.Action = vsFindAction.vsFindActionReplaceAll

...except that it's completely useless if the classes are in different files. I can't capture the actual matches that are found; I can output them to vsFindResults1, except that I can't select the window text with a macro. I can click it, Ctrl-A -> Ctrl-C and capture it, but when I do this recording a macro it shows nothing!! Very frustrated....any ideas?

Fosco
  • 38,138
  • 7
  • 87
  • 101
MrEff
  • 89
  • 2
  • 11

1 Answers1

0

You can use the VirtualPoint and TextSelection classes to do this sort of operation. Apologies that the following is in C#, but the VB should look very similar. This won't give you the exact answer you need, but at least shows you some of the classes you need to look at (the TextSelection and VirtualPoint classes aren't obvious unless you happen to know about them, I think)

TextSelection sel=ActiveWindow.Selection;
sel.StartOfDocument();

// Use your find options here:
if (sel.FindText(textToFind, (int)vsFindOptions.vsFindOptionsNone)) {
   string matchedSourceText=sel.Text;

  // use your replacement options here. This sets selection to the replacement text
  if (sel.FindText(textToReplace, (int)vsFindOptions.vsFindOptionsNone)) {
    sel.Insert(matchedSourceText, (int)EnvDTE.vsInsertFlags.vsInsertFlagsCollapseToEnd);
}

This shows you how to capture the text that you've searched for from the selection and how to find the replacement text locations and substitute some text in there. What you'll need to do in your case is iterate through all the ProjectItems and replace the text with the matchedSourceText string.

the_mandrill
  • 29,792
  • 6
  • 64
  • 93