Skip to content

Commit

Permalink
Use GoToDefinition to navigate to a symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzngard committed Sep 23, 2021
1 parent d2121b9 commit 803026d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
35 changes: 16 additions & 19 deletions src/VisualStudio/Core/Def/StackTraceExplorer/StackFrameViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
using System.Windows.Documents;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.Editor;
using Microsoft.CodeAnalysis.Editor.GoToDefinition;
using Microsoft.CodeAnalysis.Editor.Host;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Editor.StackTraceExplorer;
Expand All @@ -32,6 +35,7 @@ internal class StackFrameViewModel : FrameViewModel
private readonly ParsedStackFrame _frame;
private readonly IThreadingContext _threadingContext;
private readonly Workspace _workspace;
private readonly IStreamingFindUsagesPresenter _streamingPresenter;

private ISymbol? _cachedSymbol;
private Document? _cachedDocument;
Expand All @@ -42,12 +46,14 @@ public StackFrameViewModel(
IThreadingContext threadingContext,
Workspace workspace,
IClassificationFormatMap formatMap,
ClassificationTypeMap typeMap)
ClassificationTypeMap typeMap,
IStreamingFindUsagesPresenter streamingPresenter)
: base(formatMap, typeMap)
{
_frame = frame;
_threadingContext = threadingContext;
_workspace = workspace;
_streamingPresenter = streamingPresenter;
}

public override bool ShowMouseOver => true;
Expand Down Expand Up @@ -119,28 +125,19 @@ public async Task NavigateToMethodAsync(CancellationToken cancellationToken)
return;
}

var sourceLocation = symbol.Locations.FirstOrDefault(l => l.IsInSource);
if (sourceLocation is null || sourceLocation.SourceTree is null)
{
// Show some dialog?
return;
}
var success = GoToDefinitionHelpers.TryGoToDefinition(
symbol,
_workspace.CurrentSolution,
_threadingContext,
_streamingPresenter,
thirdPartyNavigationAllowed: true,
cancellationToken: cancellationToken);

var navigationService = _workspace.Services.GetService<IDocumentNavigationService>();
if (navigationService is null)
if (!success)
{
// Show some dialog?
return;
}

// While navigating do not activate the tab, which will change focus from the tool window
var options = _workspace.Options
.WithChangedOption(new OptionKey(NavigationOptions.PreferProvisionalTab), true)
.WithChangedOption(new OptionKey(NavigationOptions.ActivateTab), false);

var document = _workspace.CurrentSolution.GetRequiredDocument(sourceLocation.SourceTree);

await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
navigationService.TryNavigateToSpan(_workspace, document.Id, sourceLocation.SourceSpan, options, cancellationToken);
}
catch (Exception ex) when (FatalError.ReportAndCatchUnlessCanceled(ex, cancellationToken))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using Microsoft.CodeAnalysis.Editor.Host;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.VisualStudio.LanguageServices.Utilities;
using Microsoft.VisualStudio.Text.Classification;
Expand All @@ -17,13 +18,15 @@ internal class StackTraceExplorerRootViewModel : ViewModelBase
private readonly IClassificationFormatMap _formatMap;
private readonly ClassificationTypeMap _typeMap;
private readonly IThreadingContext _threadingContext;
private readonly IStreamingFindUsagesPresenter _streamingFindUsagesPresenter;

public StackTraceExplorerRootViewModel(IThreadingContext threadingContext, VisualStudioWorkspace workspace, IClassificationFormatMap formatMap, ClassificationTypeMap typeMap)
public StackTraceExplorerRootViewModel(IThreadingContext threadingContext, VisualStudioWorkspace workspace, IClassificationFormatMap formatMap, ClassificationTypeMap typeMap, IStreamingFindUsagesPresenter streamingFindUsagesPresenter)
{
_threadingContext = threadingContext;
_workspace = workspace;
_formatMap = formatMap;
_typeMap = typeMap;
_streamingFindUsagesPresenter = streamingFindUsagesPresenter;
}

public ObservableCollection<StackTraceExplorerTab> Tabs { get; } = new();
Expand All @@ -42,7 +45,7 @@ public void AddNewTab(bool triggerPaste)
? 0
: Tabs.Max(t => t.NameIndex);

var newTab = new StackTraceExplorerTab(_threadingContext, _workspace, _formatMap, _typeMap, highestIndex + 1);
var newTab = new StackTraceExplorerTab(_threadingContext, _workspace, _formatMap, _typeMap, _streamingFindUsagesPresenter, highestIndex + 1);
Tabs.Add(newTab);

SelectedTab = newTab;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Windows;
using System.Windows.Input;
using Microsoft.CodeAnalysis.Editor.Host;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.VisualStudio.LanguageServices.Utilities;
using Microsoft.VisualStudio.Text.Classification;
Expand All @@ -22,11 +23,11 @@ internal class StackTraceExplorerTab
public event EventHandler? OnClosed;
public bool IsEmpty => _stackExplorerVM.Frames.Count == 0;

public StackTraceExplorerTab(IThreadingContext threadingContext, VisualStudioWorkspace workspace, IClassificationFormatMap formatMap, ClassificationTypeMap typeMap, int nameIndex)
public StackTraceExplorerTab(IThreadingContext threadingContext, VisualStudioWorkspace workspace, IClassificationFormatMap formatMap, ClassificationTypeMap typeMap, IStreamingFindUsagesPresenter streamingFindUsagesPresenter, int nameIndex)
{
NameIndex = nameIndex;

_stackExplorerVM = new StackTraceExplorerViewModel(threadingContext, workspace, typeMap, formatMap);
_stackExplorerVM = new StackTraceExplorerViewModel(threadingContext, workspace, typeMap, formatMap, streamingFindUsagesPresenter);
Content = new StackTraceExplorer(_stackExplorerVM);

CloseClick = new DelegateCommand(_ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using Microsoft.CodeAnalysis.Editor.Host;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.VisualStudio.Imaging;
using Microsoft.VisualStudio.LanguageServices.Setup;
Expand Down Expand Up @@ -46,8 +47,9 @@ public void InitializeIfNeeded(RoslynPackage roslynPackage)
var formatMap = formatMapService.GetClassificationFormatMap(StandardContentTypeNames.Text);
var typeMap = roslynPackage.ComponentModel.GetService<ClassificationTypeMap>();
var threadingContext = roslynPackage.ComponentModel.GetService<IThreadingContext>();
var streamingFindUsagesPresenter = roslynPackage.ComponentModel.GetService<IStreamingFindUsagesPresenter>();

Root = new StackTraceExplorerRoot(new StackTraceExplorerRootViewModel(threadingContext, workspace, formatMap, typeMap))
Root = new StackTraceExplorerRoot(new StackTraceExplorerRootViewModel(threadingContext, workspace, formatMap, typeMap, streamingFindUsagesPresenter))
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
using Microsoft.VisualStudio.LanguageServices.Utilities;
using Microsoft.VisualStudio.Text.Classification;
using Roslyn.Utilities;
using Microsoft.CodeAnalysis.Editor.Host;

namespace Microsoft.VisualStudio.LanguageServices.StackTraceExplorer
{
internal class StackTraceExplorerViewModel : ViewModelBase
{
private readonly IThreadingContext _threadingContext;
private readonly Workspace _workspace;

private readonly IStreamingFindUsagesPresenter _streamingFindUsagesPresenter;
public ObservableCollection<FrameViewModel> Frames { get; } = new();

private bool _isLoading;
Expand Down Expand Up @@ -49,14 +50,15 @@ internal void OnClear()

public string InstructionText => ServicesVSResources.Paste_valid_stack_trace;

public StackTraceExplorerViewModel(IThreadingContext threadingContext, Workspace workspace, ClassificationTypeMap classificationTypeMap, IClassificationFormatMap formatMap)
public StackTraceExplorerViewModel(IThreadingContext threadingContext, Workspace workspace, ClassificationTypeMap classificationTypeMap, IClassificationFormatMap formatMap, IStreamingFindUsagesPresenter streamingFindUsagesPresenter)
{
_threadingContext = threadingContext;
_workspace = workspace;

workspace.WorkspaceChanged += Workspace_WorkspaceChanged;
_classificationTypeMap = classificationTypeMap;
_formatMap = formatMap;
_streamingFindUsagesPresenter = streamingFindUsagesPresenter;

Frames.CollectionChanged += CallstackLines_CollectionChanged;
}
Expand Down Expand Up @@ -118,7 +120,7 @@ private FrameViewModel GetViewModel(ParsedFrame frame)
=> frame switch
{
IgnoredFrame ignoredFrame => new IgnoredFrameViewModel(ignoredFrame, _formatMap, _classificationTypeMap),
ParsedStackFrame stackFrame => new StackFrameViewModel(stackFrame, _threadingContext, _workspace, _formatMap, _classificationTypeMap),
ParsedStackFrame stackFrame => new StackFrameViewModel(stackFrame, _threadingContext, _workspace, _formatMap, _classificationTypeMap, _streamingFindUsagesPresenter),
_ => throw ExceptionUtilities.UnexpectedValue(frame)
};
}
Expand Down

0 comments on commit 803026d

Please sign in to comment.