Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify goto def command handler #59769

Merged
merged 4 commits into from
Feb 25, 2022
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 @@ -2,19 +2,17 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Notification;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Commanding;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor.Commanding.Commands;
using Microsoft.VisualStudio.Utilities;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Editor.GoToDefinition
{
Expand All @@ -32,7 +30,7 @@ public GoToDefinitionCommandHandler()

public string DisplayName => EditorFeaturesResources.Go_to_Definition;

private static (Document, IGoToDefinitionService) GetDocumentAndService(ITextSnapshot snapshot)
private static (Document?, IGoToDefinitionService?) GetDocumentAndService(ITextSnapshot snapshot)
{
var document = snapshot.GetOpenDocumentInCurrentContextWithChanges();
return (document, document?.GetLanguageService<IGoToDefinitionService>());
Expand All @@ -56,33 +54,28 @@ public bool ExecuteCommand(GoToDefinitionCommandArgs args, CommandExecutionConte
// This can be removed once typescript implements LSP support for goto def.
if (service != null && !subjectBuffer.IsInLspEditorContext())
{
Contract.ThrowIfNull(document);
var caretPos = args.TextView.GetCaretPoint(subjectBuffer);
if (caretPos.HasValue && TryExecuteCommand(document, caretPos.Value, service, context))
if (caretPos.HasValue)
{
ExecuteCommand(document, caretPos.Value, service, context);
return true;
}
}

return false;
}

// Internal for testing purposes only.
internal static bool TryExecuteCommand(ITextSnapshot snapshot, int caretPosition, CommandExecutionContext context)
=> TryExecuteCommand(snapshot.GetOpenDocumentInCurrentContextWithChanges(), caretPosition, context);

internal static bool TryExecuteCommand(Document document, int caretPosition, CommandExecutionContext context)
=> TryExecuteCommand(document, caretPosition, document.GetLanguageService<IGoToDefinitionService>(), context);

internal static bool TryExecuteCommand(Document document, int caretPosition, IGoToDefinitionService goToDefinitionService, CommandExecutionContext context)
private static void ExecuteCommand(Document document, int caretPosition, IGoToDefinitionService? goToDefinitionService, CommandExecutionContext context)
{
string errorMessage = null;
string? errorMessage = null;

using (context.OperationContext.AddScope(allowCancellation: true, EditorFeaturesResources.Navigating_to_definition))
{
if (goToDefinitionService != null &&
goToDefinitionService.TryGoToDefinition(document, caretPosition, context.OperationContext.UserCancellationToken))
{
return true;
return;
}

errorMessage = FeaturesResources.Cannot_navigate_to_the_symbol_under_the_caret;
Expand All @@ -95,11 +88,15 @@ internal static bool TryExecuteCommand(Document document, int caretPosition, IGo
// and also will take it into consideration when measuring command handling duration.
context.OperationContext.TakeOwnership();
var workspace = document.Project.Solution.Workspace;
var notificationService = workspace.Services.GetService<INotificationService>();
var notificationService = workspace.Services.GetRequiredService<INotificationService>();
notificationService.SendNotification(errorMessage, title: EditorFeaturesResources.Go_to_Definition, severity: NotificationSeverity.Information);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method always returned 'true', so could just simplify the stack all the way up.


return true;
public static class TestAccessor
{
public static void ExecuteCommand(Document document, int caretPosition, IGoToDefinitionService goToDefinitionService, CommandExecutionContext context)
=> GoToDefinitionCommandHandler.ExecuteCommand(document, caretPosition, goToDefinitionService, context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,29 @@

Imports Microsoft.CodeAnalysis.Editor.CSharp.GoToDefinition
Imports Microsoft.CodeAnalysis.Editor.GoToDefinition
Imports Microsoft.CodeAnalysis.Editor.Host
Imports Microsoft.CodeAnalysis.Editor.Shared.Utilities
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Utilities
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Utilities.GoToHelpers
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.Navigation
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.VisualStudio.Commanding
Imports Microsoft.VisualStudio.Text.Editor.Commanding.Commands
Imports Roslyn.Utilities

Namespace Microsoft.CodeAnalysis.Editor.UnitTests.GoToDefinition
<[UseExportProvider]>
Public Class GoToDefinitionCancellationTests

<WpfFact, Trait(Traits.Feature, Traits.Features.GoToDefinition)>
Public Sub TestCancellation()
' Run without cancelling.
Dim updates As Integer = Cancel(Integer.MaxValue, False)
Dim updates = Cancel(Integer.MaxValue, False)
Assert.InRange(updates, 0, Integer.MaxValue)
Dim i As Integer = 0
While i < updates
Dim n As Integer = Cancel(i, True)
Dim n = Cancel(i, True)
Assert.Equal(n, i + 1)
i = i + 1
i += 1
End While
End Sub

Expand Down Expand Up @@ -66,13 +65,14 @@ class C
Dim mockDocumentNavigationService =
DirectCast(workspace.Services.GetService(Of IDocumentNavigationService)(), MockDocumentNavigationService)

GoToDefinitionCommandHandler.TryExecuteCommand(view.TextSnapshot, baseDocument.CursorPosition.Value, TestCommandExecutionContext.Create())
Dim handler = New GoToDefinitionCommandHandler()
Copy link
Member

@sharwell sharwell Feb 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 This should be obtained through the MEF catalog instead of directly constructed. Looks like it's not the only case where this is happening though, so not a blocker at this point. Just makes it more work when we do eventually clean it up.

handler.ExecuteCommand(New GoToDefinitionCommandArgs(view, baseDocument.GetTextBuffer()), TestCommandExecutionContext.Create())
Assert.True(mockDocumentNavigationService._triedNavigationToSpan)
Assert.Equal(New TextSpan(78, 2), mockDocumentNavigationService._span)

workspace.SetDocumentContext(linkDocument.Id)

GoToDefinitionCommandHandler.TryExecuteCommand(view.TextSnapshot, baseDocument.CursorPosition.Value, TestCommandExecutionContext.Create())
handler.ExecuteCommand(New GoToDefinitionCommandArgs(view, baseDocument.GetTextBuffer()), TestCommandExecutionContext.Create())
Assert.True(mockDocumentNavigationService._triedNavigationToSpan)
Assert.Equal(New TextSpan(121, 2), mockDocumentNavigationService._span)
End Using
Expand Down Expand Up @@ -104,7 +104,7 @@ class C
Dim goToDefService = New CSharpGoToDefinitionService(threadingContext, presenter)

Dim waitContext = New TestUIThreadOperationContext(updatesBeforeCancel)
GoToDefinitionCommandHandler.TryExecuteCommand(document, cursorPosition, goToDefService, New CommandExecutionContext(waitContext))
GoToDefinitionCommandHandler.TestAccessor.ExecuteCommand(document, cursorPosition, goToDefService, New CommandExecutionContext(waitContext))

Assert.Equal(navigatedTo OrElse mockDocumentNavigationService._triedNavigationToSpan, Not expectedCancel)

Expand Down