小小飞鹰

     中国人缺少的是步骤,太急。练太极!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

强大的宏--删除所匹配字符串的行

Posted on 2008-11-05 15:51  小小飞鹰  阅读(1981)  评论(2编辑  收藏  举报

      开发过程中经常碰到一种情况,要去除基类的一个属性,而基类去掉后,所有子类的这个属性必须手动删除,多的时候相当郁闷,而下面这个宏可解决。具体使用方面如下:

1 在VS IDE 中按ALT+F8打开宏资源管理器,右键MyMacros项目建立一个模块DeleteLine 。

2 右键这个模块--》编辑会打开宏项目,把下面的代码粘贴上去就建立了这个宏命令。

3 回到项目中,在宏资源管理器中双击DeleteLine -->DeleteAll或右键--》运行会弹出对话框让你输入要查找的字符串,如.Text = "我是中国人",则命令会自动查找当前项目,把含有这串字符的行删除;同时在“输出窗口”把删除的行显示出来以便核对。

4 注意:此命令是针对当前项目,同时要删除时打开项目中一个当前文件,否则可能出现未能查找到的情况。,ENJOY


Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Imports Microsoft.VisualBasic
Imports Microsoft.VisualBasic.ControlChars

Public Module DeleteLine '删除匹配的字符串行

    Dim findWhat = ""
    Dim textDocument As EnvDTE.TextDocument
    Dim outputWindowPane As OutputWindowPane

    Sub DeleteAll()
        Dim lastDocument As Integer = 1

        findWhat = InputBox("请输入要删除行查找字符串!")
        outputWindowPane = GetOutputWindowPane("删除的行")
        OpenFindDocument() '打开含有所要查找字串的窗口

        While (DTE.Documents.Count >= lastDocument)
            textDocument = DTE.Documents.Item(lastDocument).Object("textDocument")
            lastDocument = lastDocument + 1

            textDocument.Parent.Activate() '激活页面作为当前操作界面
            textDocument.StartPoint.CreateEditPoint().StartOfDocument()
            textDocument.Selection.SelectAll()

            Delete() '删除找到的行
        End While
    End Sub

    Sub OpenFindDocument()
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
        DTE.Find.Backwards = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.MatchCase = True
        DTE.Find.MatchInHiddenText = True
        DTE.Find.KeepModifiedDocumentsOpen = True
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentProject
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
        DTE.Find.FindWhat = findWhat
        DTE.Find.ReplaceWith = findWhat
        DTE.Find.Execute()
    End Sub

    Sub Delete()
        Dim textSelection As TextSelection
        Dim textSelectionPointSaved As TextPoint
        Dim findResult As EnvDTE.vsFindResult
        Dim lastFoundAt As Integer = 0

        textSelection = textDocument.Selection

        DTE.Find.MatchWholeWord = False
        DTE.Find.Action = vsFindAction.vsFindActionFind
        DTE.Find.Target = vsFindTarget.vsFindTargetOpenDocuments
        DTE.Find.MatchCase = False
        DTE.Find.Backwards = False
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.FindWhat = findWhat

        textSelection.StartOfDocument()
        textSelectionPointSaved = textSelection.ActivePoint.CreateEditPoint()

        ''清空“输出窗口”
        outputWindowPane.Clear()

        CType(DTE.Find, EnvDTE80.Find2).WaitForFindToComplete = True
        findResult = DTE.Find.Execute()
        While (findResult = vsFindResult.vsFindResultFound)
            If textSelection.AnchorPoint.Line <= lastFoundAt Then
                Exit While
            End If

            textSelection.SelectLine()

            If textSelection.Text.IndexOf(findWhat) >= 0 Then
                outputWindowPane.OutputString(textDocument.Parent.FullName + "(" + textSelection.AnchorPoint.Line.ToString() + "):" + textSelection.Text)
                textSelection.Delete() '删除行
            End If

            lastFoundAt = textSelection.AnchorPoint.Line
            textSelection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
            findResult = DTE.Find.Execute()
        End While

        '' 将插入符号还原到调用此命令之前的位置。
        textSelection.MoveToPoint(textSelectionPointSaved)
    End Sub

    '输出信息到  输出窗口
    Function GetOutputWindowPane(ByVal Name As String, Optional ByVal show As Boolean = True) As OutputWindowPane
        Dim window As Window
        Dim outputWindow As OutputWindow
        Dim outputWindowPane As OutputWindowPane

        window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
        If show Then window.Visible = True
        outputWindow = window.Object
        Try
            outputWindowPane = outputWindow.OutputWindowPanes.Item(Name)
        Catch e As System.Exception
            outputWindowPane = outputWindow.OutputWindowPanes.Add(Name)
        End Try
        outputWindowPane.Activate()
        Return outputWindowPane
    End Function
End Module

 

[注:有点BUG, 修改了一下]