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 }); } } 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