From 5acefb57401e0d0dbc28b92593905ee9c60551d0 Mon Sep 17 00:00:00 2001 From: Hansjoerg Petriffer Date: Wed, 11 Nov 2026 17:12:07 +0100 Subject: [PATCH 1/2] [VS] Code Search: the preview no longer takes the focus DropDownClient.InitCaret sends the focus to every new view of a file that is already open in an editor. The preview editor of the Code Search (Ctrl+T) is such a view, so the keys typed for the search changed the code. Views with the role SEARCH_RESULT_PREVIEW are left alone now. Co-Authored-By: Claude Fable 5.2 --- .../LanguageService/Generic/DropDownClient.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/VisualStudio/LanguageService/Generic/DropDownClient.cs b/src/VisualStudio/LanguageService/Generic/DropDownClient.cs index 35e8200ab7..d9562ad5ff 100644 --- a/src/VisualStudio/LanguageService/Generic/DropDownClient.cs +++ b/src/VisualStudio/LanguageService/Generic/DropDownClient.cs @@ -174,6 +174,9 @@ public XSharpDropDownClient(IVsDropdownBarManager manager, XFile file) }); } + /// The role of the editor in the preview of the Code Search window. + private const string SearchPreviewRole = "SEARCH_RESULT_PREVIEW"; + internal void addTextView(ITextView textView, IVsTextView textViewAdapter) { if (!_textViews.ContainsKey(textView)) @@ -185,9 +188,10 @@ internal void addTextView(ITextView textView, IVsTextView textViewAdapter) textView.Closed += TextView_Closed; if (textView.Caret != null) textView.Caret.PositionChanged += Caret_PositionChanged; - StartOnIdleAsync(textViewAdapter).FireAndForget(); - - + // The preview of the Code Search (Ctrl+T) must not take the focus (InitCaret sends it to the new + // view when the file is already open in an editor): the keys typed for the search would change the code. + if (!textView.Roles.Contains(SearchPreviewRole)) + StartOnIdleAsync(textViewAdapter).FireAndForget(); } } // This moves the caret to trigger initial drop down load From edf7b617bee36b6914a0ef5aba85b35e7356891d Mon Sep 17 00:00:00 2001 From: Hansjoerg Petriffer Date: Wed, 11 Nov 2026 17:12:08 +0100 Subject: [PATCH 2/2] [VS] Code Search: preview for X# results The X# source of the Code Search returns a preview panel: the file in the editor of the search window, at the declaration, as C# has it. CodeEditorModel is created by reflection: Microsoft.VisualStudio.Search is built against the Microsoft.VisualStudio.Threading of its own Visual Studio, newer than the one of the SDK package (CS1705 on AsyncLazy). Co-Authored-By: Claude Fable 5.2 --- .../NavigateToSearchSource.cs | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/VisualStudio/LanguageService.Search/NavigateToSearchSource.cs b/src/VisualStudio/LanguageService.Search/NavigateToSearchSource.cs index e6971f7d4d..06005ad571 100644 --- a/src/VisualStudio/LanguageService.Search/NavigateToSearchSource.cs +++ b/src/VisualStudio/LanguageService.Search/NavigateToSearchSource.cs @@ -7,10 +7,13 @@ using System.Collections.Generic; using System.ComponentModel.Composition; using System.IO; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.VisualStudio.Core.Imaging; using Microsoft.VisualStudio.Search.Data; +using Microsoft.VisualStudio.Search.UI.PreviewPanel.Models; +using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Threading; using Microsoft.VisualStudio.Utilities; using XSharp.Settings; @@ -51,7 +54,43 @@ public SearchResultViewBase CreateSearchResultView(SearchResult searchResult) public Task> GetPreviewPanelsAsync(SearchResult searchResult, SearchResultViewBase searchResultView) { - return Task.FromResult>(Array.Empty()); + IReadOnlyList panels = Array.Empty(); + if (searchResult is XSharpSearchResult result && !string.IsNullOrEmpty(result.Entry.FilePath)) + { + try + { + panels = new SearchResultPreviewPanelBase[] { new XSharpSearchResultPreviewPanel(result.Entry, searchResultView) }; + } + catch (Exception ex) + { + // a path that is no Uri: no preview + Logger.Exception(ex, "Navigate To: preview failed"); + } + } + return Task.FromResult(panels); + } + } + + /// + /// The preview of a result: the file in the editor of the search window, at the declaration. The search window + /// hosts it, so the focus stays in the search box. + /// + internal sealed class XSharpSearchResultPreviewPanel : SearchResultPreviewPanelBase + { + public override UIBaseModel UserInterface { get; } + + internal XSharpSearchResultPreviewPanel(NavigateToEntry entry, SearchResultViewBase view) + : base(view?.Title?.Text ?? entry.Name, view?.PrimaryIcon ?? default) + { + // lines and columns are 0 based, as in the code model; no project: the file may be in several + var location = new TextDocumentLocation(new Uri(entry.FilePath), Guid.Empty, entry.Line, entry.Column, entry.Line, entry.Column); + // new CodeEditorModel(id, new AsyncLazy(...), isEditable: true), by reflection: + // Microsoft.VisualStudio.Search.dll is built against the Microsoft.VisualStudio.Threading of its own + // Visual Studio, newer than the one of our SDK package, and the compiler refuses that AsyncLazy (CS1705) + var constructor = typeof(CodeEditorModel).GetConstructors().First(c => c.GetParameters().Length == 3); + Func> factory = () => Task.FromResult(location); + var lazy = Activator.CreateInstance(constructor.GetParameters()[1].ParameterType, factory, ThreadHelper.JoinableTaskFactory); + UserInterface = (UIBaseModel)constructor.Invoke(new object[] { nameof(XSharpSearchResultPreviewPanel), lazy, true }); } }