能否开发Visual Studio 2017扩展修改Go to All搜索结果并支持路径排除?
Absolutely! You can absolutely create a Visual Studio 2017 extension to customize Go to All (Ctrl+T/Ctrl+,) results by excluding paths via user-defined glob patterns set in Tools -> Options. Let’s walk through the feasibility, key APIs, and two common approaches to implement this.
Feasibility
This is totally doable. Visual Studio’s extensibility model exposes hooks into its search infrastructure, letting you either filter the locations searched upfront or post-process the results after they’re generated.
Key APIs & Components You’ll Need
First, here are the core APIs and tools to leverage:
DialogPage(fromMicrosoft.VisualStudio.Shell): Use this to create a custom Options page where users can add/remove glob patterns. Mark your derived class with[ProvideOptionPage]to register it in the Tools -> Options menu.GlobPattern(fromMicrosoft.VisualStudio.Globbing): VS provides built-in glob parsing and matching—no need to roll your own. This class handles patterns like**/bin/**,*.obj, etc.- Visual Studio Search Infrastructure APIs:
IVsSearchProvider: Each category in Go to All (files, symbols, types, etc.) uses a search provider. You can wrap or extend existing providers to inject path filtering before results are fetched.IVsSearchResult&IVsSearchCallback: If you prefer post-processing results, use these to intercept returned results and filter out matches against your glob patterns.IVsHierarchy: Helps you retrieve full paths of project items/files to compare against glob patterns.
Implementation Approaches
1. Pre-Filter: Restrict Search Locations Upfront
This approach modifies which locations VS searches in the first place, avoiding unnecessary work generating results you’ll just discard later.
- Create a custom search provider that wraps the default provider (e.g., for file searches, wrap
FileSearchProvider). - In your provider’s query execution logic, use
IVsHierarchyto enumerate potential files/locations, then check each path against the user’s glob patterns usingGlobPattern.IsMatch(). - Only include paths that don’t match any exclusion patterns when passing the query to the underlying provider.
- Register your custom provider to replace or supplement the default one via VS’s extension registration attributes.
2. Post-Filter: Process Results After Search
If wrapping providers feels too heavy, you can intercept results after they’re generated and filter them:
- Implement
IVsSearchCallbackand register it to listen for search completion events from Go to All. - When results are returned, iterate over each
IVsSearchResult, extract the corresponding file/item path (you may need to useIVsHierarchyor result properties to get this). - Check if the path matches any of the user’s exclusion glob patterns. If it does, remove the result from the displayed list.
- Note: This requires careful handling to ensure the UI updates correctly after filtering.
Example Snippet: Glob Pattern Matching
Here’s a quick example of how to use GlobPattern to check a file path against an exclusion pattern:
using Microsoft.VisualStudio.Globbing; // Get user-defined patterns from your Options page var excludePatterns = MyOptionsPage.Instance.ExcludeGlobPatterns; var filePath = @"C:\MyProject\bin\Debug\MyAssembly.dll"; foreach (var pattern in excludePatterns) { var glob = new GlobPattern(pattern); if (glob.IsMatch(filePath)) { // Exclude this file from results return false; } } // Include the file return true;
Notes for VS2017 Compatibility
- Ensure your VSIX project targets Visual Studio 2017 (version 15.x) and references the correct version of the Visual Studio Shell assemblies (e.g.,
Microsoft.VisualStudio.Shell.15.0). - Test thoroughly across different Go to All categories (files, symbols, etc.) to make sure your filtering applies consistently.
内容的提问来源于stack exchange,提问作者Dan Pettersson

