locked
VS Shell: How does 'Find All' know the location of all files within a solution to search? RRS feed

  • Question

  • Summary:
    Within our own VSPackage, how does 'Find All' know the location of all files within a solution to search?
    Is there some way for us to override default behaviour, so that we can alo search within our data in memory, not just only in files?  

    Details:
    - Using VS Shell
    - Using VS2008 SP1, VS2008 SDK
    - Within a VSPackage: open a solution, and don't yet open any editors. 
    - Use "Find and Replace", using steps:
        Edit > Find And Replace > Find in Files
        Find what = "some search text"
        Look in = Entire Solution
        Look at these files types = *.*
        Select button: "Find All"

    Desired results:
    We search for "some search text" within all '*.*' files of our solution, and also search within some additional solution/library objects in memory.

    Since we don't have any editors or tool window open, we therefore don't have a window open that implements IVsFindTarget.  Under these conditions, how can we "hook into" the 'Find All' request?

    Thursday, April 15, 2010 3:32 PM

Answers

  • Hi Duke,

    I think when you use "Find All" action in Find In Files, DTE.find will search the flag defined in DTE.Find.Target, for example, if the flag is vsFindTarget.vsFindTargetFiles, it will search all files in Solution Hierachy; if the flag is Current Document, this flag should be vsFindTargetCurrentDocument.For details, please refer the Macro Code in my previous reply. 

    And this search is related to Solution Hierachy, but it's not related to the folder of the solution. It just search the items under Solution Hierachy, but not the files in solution folder or subfolder.

    Regards,

    Nancy Shao [MSFT]
    MSDN Subscriber Supportin Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Duke Huron Monday, April 19, 2010 4:16 PM
    Monday, April 19, 2010 1:47 AM

All replies

  • Hi Duke,

    Based on my research, we can't hook Find All event in VSPackage, but we can hook Find In Files event in VSPackage. To achieve this objective, I recommend you write customFind and Replace Dialog when user click Find In Files inEdit Menu, it will trigger your custom Find and Replace Dialog , and you can do everything you want when user clickFind All Button. Please refer to following code to hook Find In Files event in VSPackage:

          dte2 = (EnvDTE80.DTE2)Package.GetGlobalService(typeof(SDTE));
    findinfilesEvent = dte2.Events.get_CommandEvents("{5EFC7975-14BC-11CF-9B2B-00AA00573819}", 277);
    // Guid and CmdId of Edit.FindInFiles command
    findinfilesEvent.BeforeExecute += new _dispCommandEvents_BeforeExecuteEventHandler(findinfilesEvent_BeforeExecute);
    findinfilesEvent.AfterExecute += new _dispCommandEvents_AfterExecuteEventHandler(findinfilesEvent_AfterExecute);

    void findinfilesEvent_AfterExecute(string Guid, int ID, object CustomIn, object CustomOut)
    {

    //if (dte2.Find.Action == vsFindAction.vsFindActionFind)
    //{
    // MessageBox.Show("capture");
    //}
    }

    void findinfilesEvent_BeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
    {
    // Cancel Find in Files event by setting CancelDefault = true
    //CancelDefault = true
    //throw new NotImplementedException(); } private DTE2 dte2; private CommandEvents findinfilesEvent;

     

    Don't forget to add Envdte.dll and Envdte80.dll as references. For you reference, I record a macro about how Visual Studio do this action, For details, please refer to the code below:

      Sub TemporaryMacro()
        DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Activate() 'Find and Replace
        DTE.ExecuteCommand("Edit.FindinFiles")
        DTE.Find.FindWhat = "//"
        DTE.Find.SearchPath = "Entire Solution"
        DTE.Find.MatchInHiddenText = True
        DTE.Find.Action = vsFindAction.vsFindActionFindAll
        DTE.Windows.Item(Constants.vsWindowKindFindResults1).Activate()
        DTE.Find.FindWhat = "//"
        DTE.Find.Target = vsFindTarget.vsFindTargetFiles
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.SearchPath = "Entire Solution"
        DTE.Find.SearchSubfolders = True
        DTE.Find.FilesOfType = ""
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResults1
        DTE.Find.Action = vsFindAction.vsFindActionFindAll
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
          Throw New System.Exception("vsFindResultNotFound")
        End If
        DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Close()
      End Sub

    If I misunderstood you, or you have any comments, please feel free to let me know.

    Regards,

    Nancy Shao [MSFT]
    MSDN Subscriber Supportin Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, April 16, 2010 9:08 AM
  • Hi Nancy: thanks for the reply!

    Thanks also, for letting me know that we can't hook into 'Find All' in VSPackage, and for your tips on how to programatically execute a 'Find All' command. We'll certainly consider that as one of our options.

    Part of my original question remains: when using the default 'Find All' action, how does the VSPackage know where (which files) to perform its search? 

    By example, if I create a C# project and solution, and perform a 'Find All', the IDE knows where all files within that project are all located, even if they are located in different paths other than within subfolder of solution root path.  Does the VSPackage queries all hierachy items for their file locations?

    Friday, April 16, 2010 3:56 PM
  • Hi Duke,

    I think when you use "Find All" action in Find In Files, DTE.find will search the flag defined in DTE.Find.Target, for example, if the flag is vsFindTarget.vsFindTargetFiles, it will search all files in Solution Hierachy; if the flag is Current Document, this flag should be vsFindTargetCurrentDocument.For details, please refer the Macro Code in my previous reply. 

    And this search is related to Solution Hierachy, but it's not related to the folder of the solution. It just search the items under Solution Hierachy, but not the files in solution folder or subfolder.

    Regards,

    Nancy Shao [MSFT]
    MSDN Subscriber Supportin Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Duke Huron Monday, April 19, 2010 4:16 PM
    Monday, April 19, 2010 1:47 AM
  • > "It just search the items under Solution Hierachy, but not the files in solution folder or subfolder"

    Aha, that's what I was after.

    Thanks Nancy!

    Monday, April 19, 2010 4:17 PM
  • Hi Nancy,

    It would be grateful, if you can give me some solution for my issue 

    http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/0d6a9da2-83f5-4195-a887-1173d7e8a459

    I am basically trying to find some keyword and read the results from the FindResult1 window. Find works fine but unable to get the results.

    THanks

    Asif


    Ashi

    Monday, October 15, 2012 11:50 AM