Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +54,43 @@ public SearchResultViewBase CreateSearchResultView(SearchResult searchResult)

public Task<IReadOnlyList<SearchResultPreviewPanelBase>> GetPreviewPanelsAsync(SearchResult searchResult, SearchResultViewBase searchResultView)
{
return Task.FromResult<IReadOnlyList<SearchResultPreviewPanelBase>>(Array.Empty<SearchResultPreviewPanelBase>());
IReadOnlyList<SearchResultPreviewPanelBase> panels = Array.Empty<SearchResultPreviewPanelBase>();
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);
}
}

/// <summary>
/// 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.
/// </summary>
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<TextDocumentLocation>(...), 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<Task<TextDocumentLocation>> 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 });
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/VisualStudio/LanguageService/Generic/DropDownClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ public XSharpDropDownClient(IVsDropdownBarManager manager, XFile file)
});
}

/// <summary>The role of the editor in the preview of the Code Search window.</summary>
private const string SearchPreviewRole = "SEARCH_RESULT_PREVIEW";

internal void addTextView(ITextView textView, IVsTextView textViewAdapter)
{
if (!_textViews.ContainsKey(textView))
Expand All @@ -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
Expand Down