次の方法で共有


How to select in Visual Studio an area between 2 known find expression

Some days ago I had to format this huge file to remove everything between a "start" marking and an "end" marking and to do some other nifty things while doing the removing (reformat, copy some of the text, move it somewhere Else and such).

So a simple regexp replace would not work. The only solution was to get a macro to do the work for me so I don't go crazy (or at least not 100%) :)

Since I'm kind of a noob in VB macros I started looking for some samples of how others did this before but after spending like half an hour browsing through tons of VB macros that did everything except what I wanted them to do I decided that it's time for me to write one of my own.

So here is some of it:

        DTE.ExecuteCommand("Edit.Find")
        DTE.Find.FindWhat = "<the start pattern>"
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.Backwards = False
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr
        DTE.Find.Action = vsFindAction.vsFindActionFind
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
            Throw New System.Exception("vsFindResultNotFound")
        End If

        DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Close()

        Dim startLine As Integer = DTE.ActiveDocument.Selection.CurrentLine

        --- do processing with the start line

        DTE.ExecuteCommand("Edit.Find")
        DTE.Find.FindWhat = "<the end pattern>"
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.Backwards = False
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr
        DTE.Find.Action = vsFindAction.vsFindActionFind
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
            Throw New System.Exception("vsFindResultNotFound")
        End If

        Dim endLine As Integer = DTE.ActiveDocument.Selection.CurrentLine

        --- now you have both the start line and the end line
--- and here's your selection to play with

        Dim sel As TextSelection = DTE.ActiveDocument.Selection

Please let me know if you have any trouble using this.

Thanks,
Ionutz