From a60a356797ea60c8dd26574e02084f68cb9dcacf Mon Sep 17 00:00:00 2001 From: CyrusNajmabadi Date: Fri, 18 Nov 2016 12:32:23 -0800 Subject: [PATCH] Remove the synchronous GoToImplementation path for C#/VB. --- .../CSharpGoToImplementationService.cs | 6 +- .../AbstractGoToImplementationService.cs | 4 +- .../GoToDefinition/GoToDefinitionTests.vb | 83 +++++++++++++- .../GoToImplementationTests.vb | 50 +++++++-- .../TestUtilities/Workspaces/TestWorkspace.cs | 3 +- .../Utilities/GoToHelpers/GoToTestHelpers.vb | 106 ++++-------------- .../VisualBasicGoToImplementationService.vb | 4 +- 7 files changed, 149 insertions(+), 107 deletions(-) diff --git a/src/EditorFeatures/CSharp/GoToImplementation/CSharpGoToImplementationService.cs b/src/EditorFeatures/CSharp/GoToImplementation/CSharpGoToImplementationService.cs index 76cb1e814eb..466c2fb652c 100644 --- a/src/EditorFeatures/CSharp/GoToImplementation/CSharpGoToImplementationService.cs +++ b/src/EditorFeatures/CSharp/GoToImplementation/CSharpGoToImplementationService.cs @@ -1,4 +1,5 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#if false +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; @@ -19,4 +20,5 @@ internal sealed class CSharpGoToImplementationService : AbstractGoToImplementati { } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/EditorFeatures/Core/GoToImplementation/AbstractGoToImplementationService.cs b/src/EditorFeatures/Core/GoToImplementation/AbstractGoToImplementationService.cs index 5ec0bf6bcd3..97d57a74052 100644 --- a/src/EditorFeatures/Core/GoToImplementation/AbstractGoToImplementationService.cs +++ b/src/EditorFeatures/Core/GoToImplementation/AbstractGoToImplementationService.cs @@ -1,3 +1,4 @@ +#if false // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; @@ -96,4 +97,5 @@ private static IEnumerable CreateItemsForImplementation(ISymbol displayTaggedParts: symbolDisplayService.ToDisplayParts(implementation).ToTaggedText()); } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/EditorFeatures/Test2/GoToDefinition/GoToDefinitionTests.vb b/src/EditorFeatures/Test2/GoToDefinition/GoToDefinitionTests.vb index 023a56338fa..e01d0e45e0f 100644 --- a/src/EditorFeatures/Test2/GoToDefinition/GoToDefinitionTests.vb +++ b/src/EditorFeatures/Test2/GoToDefinition/GoToDefinitionTests.vb @@ -1,16 +1,93 @@ ' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. Imports System.Threading +Imports System.Threading.Tasks Imports Microsoft.CodeAnalysis.Editor.CSharp.GoToDefinition Imports Microsoft.CodeAnalysis.Editor.Host -Imports Microsoft.CodeAnalysis.Editor.VisualBasic.GoToDefinition Imports Microsoft.CodeAnalysis.Editor.UnitTests.Utilities.GoToHelpers -Imports System.Threading.Tasks +Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces +Imports Microsoft.CodeAnalysis.Editor.VisualBasic.GoToDefinition +Imports Microsoft.CodeAnalysis.Navigation +Imports Microsoft.VisualStudio.Composition +Imports Microsoft.VisualStudio.Text Namespace Microsoft.CodeAnalysis.Editor.UnitTests.GoToDefinition Public Class GoToDefinitionTests + Friend Async Function TestAsync(workspaceDefinition As XElement, + expectedResult As Boolean, + executeOnDocument As Func(Of Document, Integer, IEnumerable(Of Lazy(Of INavigableItemsPresenter)), Boolean)) As System.Threading.Tasks.Task + Using workspace = Await TestWorkspace.CreateAsync( + workspaceDefinition, exportProvider:=GoToTestHelpers.ExportProvider) + Dim solution = workspace.CurrentSolution + Dim cursorDocument = workspace.Documents.First(Function(d) d.CursorPosition.HasValue) + Dim cursorPosition = cursorDocument.CursorPosition.Value + + Dim items As IList(Of INavigableItem) = Nothing + + ' Set up mocks. The IDocumentNavigationService should be called if there is one, + ' location and the INavigableItemsPresenter should be called if there are + ' multiple locations. + + ' prepare a notification listener + Dim textView = cursorDocument.GetTextView() + Dim textBuffer = textView.TextBuffer + textView.Caret.MoveTo(New SnapshotPoint(textBuffer.CurrentSnapshot, cursorPosition)) + + Dim cursorBuffer = cursorDocument.TextBuffer + Dim document = workspace.CurrentSolution.GetDocument(cursorDocument.Id) + + Dim mockDocumentNavigationService = DirectCast(workspace.Services.GetService(Of IDocumentNavigationService)(), MockDocumentNavigationService) + + Dim presenter = New MockNavigableItemsPresenter(Sub(i) items = i) + Dim presenters = {New Lazy(Of INavigableItemsPresenter)(Function() presenter)} + Dim actualResult = executeOnDocument(document, cursorPosition, presenters) + + Assert.Equal(expectedResult, actualResult) + + Dim expectedLocations As New List(Of FilePathAndSpan) + + For Each testDocument In workspace.Documents + For Each selectedSpan In testDocument.SelectedSpans + expectedLocations.Add(New FilePathAndSpan(testDocument.FilePath, selectedSpan)) + Next + Next + + expectedLocations.Sort() + + If expectedResult Then + If mockDocumentNavigationService._triedNavigationToSpan Then + Dim definitionDocument = workspace.GetTestDocument(mockDocumentNavigationService._documentId) + Assert.Single(definitionDocument.SelectedSpans) + Assert.Equal(definitionDocument.SelectedSpans.Single(), mockDocumentNavigationService._span) + + ' The INavigableItemsPresenter should not have been called + Assert.Null(items) + Else + Assert.False(mockDocumentNavigationService._triedNavigationToPosition) + Assert.False(mockDocumentNavigationService._triedNavigationToLineAndOffset) + Assert.NotEmpty(items) + + Dim actualLocations As New List(Of FilePathAndSpan) + + For Each location In items + actualLocations.Add(New FilePathAndSpan(location.Document.FilePath, location.SourceSpan)) + Next + + actualLocations.Sort() + Assert.Equal(expectedLocations, actualLocations) + + ' The IDocumentNavigationService should not have been called + Assert.Null(mockDocumentNavigationService._documentId) + End If + Else + Assert.Null(mockDocumentNavigationService._documentId) + Assert.True(items Is Nothing OrElse items.Count = 0) + End If + End Using + End Function + Private Function TestAsync(workspaceDefinition As XElement, Optional expectedResult As Boolean = True) As Tasks.Task - Return GoToTestHelpers.TestAsync(workspaceDefinition, expectedResult, + Return TestAsync(workspaceDefinition, expectedResult, Function(document As Document, cursorPosition As Integer, presenters As IEnumerable(Of Lazy(Of INavigableItemsPresenter))) Dim goToDefService = If(document.Project.Language = LanguageNames.CSharp, DirectCast(New CSharpGoToDefinitionService(presenters, {}), IGoToDefinitionService), diff --git a/src/EditorFeatures/Test2/GoToImplementation/GoToImplementationTests.vb b/src/EditorFeatures/Test2/GoToImplementation/GoToImplementationTests.vb index 5171e791667..1e60189d168 100644 --- a/src/EditorFeatures/Test2/GoToImplementation/GoToImplementationTests.vb +++ b/src/EditorFeatures/Test2/GoToImplementation/GoToImplementationTests.vb @@ -2,23 +2,49 @@ Imports System.Threading Imports System.Threading.Tasks -Imports Microsoft.CodeAnalysis.Editor.CSharp.GoToImplementation +Imports Microsoft.CodeAnalysis.Editor.FindUsages Imports Microsoft.CodeAnalysis.Editor.Host Imports Microsoft.CodeAnalysis.Editor.UnitTests.Utilities.GoToHelpers -Imports Microsoft.CodeAnalysis.Editor.VisualBasic.GoToImplementation +Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces Namespace Microsoft.CodeAnalysis.Editor.UnitTests.GoToImplementation Public Class GoToImplementationTests - Private Function TestAsync(workspaceDefinition As XElement, Optional shouldSucceed As Boolean = True) As Tasks.Task - Return GoToTestHelpers.TestAsync(workspaceDefinition, shouldSucceed, - Function(document As Document, cursorPosition As Integer, presenters As IEnumerable(Of Lazy(Of INavigableItemsPresenter))) - Dim service = If(document.Project.Language = LanguageNames.CSharp, - DirectCast(New CSharpGoToImplementationService(presenters), IGoToImplementationService), - New VisualBasicGoToImplementationService(presenters)) - - Dim message As String = Nothing - Return service.TryGoToImplementation(document, cursorPosition, CancellationToken.None, message) - End Function) + Private Async Function TestAsync(workspaceDefinition As XElement, Optional shouldSucceed As Boolean = True) As Tasks.Task + Using workspace = Await TestWorkspace.CreateAsync(workspaceDefinition) + Dim documentWithCursor = workspace.DocumentWithCursor + Dim position = documentWithCursor.CursorPosition.Value + + Dim document = workspace.CurrentSolution.GetDocument(documentWithCursor.Id) + Dim findUsagesService = document.GetLanguageService(Of IFindUsagesService) + + Dim context = New SimpleFindUsagesContext(CancellationToken.None) + Await findUsagesService.FindImplementationsAsync(document, position, context) + + If Not shouldSucceed Then + Assert.NotNull(context.Message) + Else + Dim actualDefinitions = context.GetDefinitions(). + SelectMany(Function(d) d.SourceSpans). + Select(Function(ss) New FilePathAndSpan(ss.Document.FilePath, ss.SourceSpan)). + ToList() + actualDefinitions.Sort() + + Dim expectedDefinitions = workspace.Documents.SelectMany( + Function(d) d.SelectedSpans.Select(Function(ss) New FilePathAndSpan(d.FilePath, ss))).ToList() + + expectedDefinitions.Sort() + + Assert.Equal(actualDefinitions.Count, expectedDefinitions.Count) + + For i = 0 To actualDefinitions.Count - 1 + Dim actual = actualDefinitions(i) + Dim expected = expectedDefinitions(i) + + Assert.True(actual.CompareTo(expected) = 0, + $"Expected: ({expected}) but got: ({actual})") + Next + End If + End Using End Function diff --git a/src/EditorFeatures/TestUtilities/Workspaces/TestWorkspace.cs b/src/EditorFeatures/TestUtilities/Workspaces/TestWorkspace.cs index 93effa73a7a..3c4cd8e3348 100644 --- a/src/EditorFeatures/TestUtilities/Workspaces/TestWorkspace.cs +++ b/src/EditorFeatures/TestUtilities/Workspaces/TestWorkspace.cs @@ -93,7 +93,8 @@ protected internal override bool PartialSemanticsEnabled get { return _backgroundCompiler != null; } } - public TestHostDocument DocumentWithCursor { get { return Documents.Single(d => d.CursorPosition.HasValue && !d.IsLinkFile); } } + public TestHostDocument DocumentWithCursor + => Documents.Single(d => d.CursorPosition.HasValue && !d.IsLinkFile); protected override void OnDocumentTextChanged(Document document) { diff --git a/src/EditorFeatures/TestUtilities2/Utilities/GoToHelpers/GoToTestHelpers.vb b/src/EditorFeatures/TestUtilities2/Utilities/GoToHelpers/GoToTestHelpers.vb index 98c4a888135..ed64b669b34 100644 --- a/src/EditorFeatures/TestUtilities2/Utilities/GoToHelpers/GoToTestHelpers.vb +++ b/src/EditorFeatures/TestUtilities2/Utilities/GoToHelpers/GoToTestHelpers.vb @@ -17,100 +17,32 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Utilities.GoToHelpers Public ReadOnly ExportProvider As ExportProvider = MinimalTestExportProvider.CreateExportProvider(Catalog) - Public Async Function TestAsync(workspaceDefinition As XElement, expectedResult As Boolean, executeOnDocument As Func(Of Document, Integer, IEnumerable(Of Lazy(Of INavigableItemsPresenter)), Boolean)) As System.Threading.Tasks.Task - Using workspace = Await TestWorkspace.CreateAsync(workspaceDefinition, exportProvider:=ExportProvider) - Dim solution = workspace.CurrentSolution - Dim cursorDocument = workspace.Documents.First(Function(d) d.CursorPosition.HasValue) - Dim cursorPosition = cursorDocument.CursorPosition.Value - - Dim items As IList(Of INavigableItem) = Nothing - - ' Set up mocks. The IDocumentNavigationService should be called if there is one, - ' location and the INavigableItemsPresenter should be called if there are - ' multiple locations. - - ' prepare a notification listener - Dim textView = cursorDocument.GetTextView() - Dim textBuffer = textView.TextBuffer - textView.Caret.MoveTo(New SnapshotPoint(textBuffer.CurrentSnapshot, cursorPosition)) - - Dim cursorBuffer = cursorDocument.TextBuffer - Dim document = workspace.CurrentSolution.GetDocument(cursorDocument.Id) - - Dim mockDocumentNavigationService = DirectCast(workspace.Services.GetService(Of IDocumentNavigationService)(), MockDocumentNavigationService) - - Dim presenter = New MockNavigableItemsPresenter(Sub(i) items = i) - Dim presenters = {New Lazy(Of INavigableItemsPresenter)(Function() presenter)} - Dim actualResult = executeOnDocument(document, cursorPosition, presenters) - - Assert.Equal(expectedResult, actualResult) - - Dim expectedLocations As New List(Of FilePathAndSpan) - - For Each testDocument In workspace.Documents - For Each selectedSpan In testDocument.SelectedSpans - expectedLocations.Add(New FilePathAndSpan(testDocument.FilePath, selectedSpan)) - Next - Next - - expectedLocations.Sort() + End Module - If expectedResult Then - If mockDocumentNavigationService._triedNavigationToSpan Then - Dim definitionDocument = workspace.GetTestDocument(mockDocumentNavigationService._documentId) - Assert.Single(definitionDocument.SelectedSpans) - Assert.Equal(definitionDocument.SelectedSpans.Single(), mockDocumentNavigationService._span) + Friend Structure FilePathAndSpan + Implements IComparable(Of FilePathAndSpan) - ' The INavigableItemsPresenter should not have been called - Assert.Null(items) - Else - Assert.False(mockDocumentNavigationService._triedNavigationToPosition) - Assert.False(mockDocumentNavigationService._triedNavigationToLineAndOffset) - Assert.NotEmpty(items) + Public ReadOnly Property FilePath As String + Public ReadOnly Property Span As TextSpan - Dim actualLocations As New List(Of FilePathAndSpan) + Public Sub New(filePath As String, span As TextSpan) + Me.FilePath = filePath + Me.Span = span + End Sub - For Each location In items - actualLocations.Add(New FilePathAndSpan(location.Document.FilePath, location.SourceSpan)) - Next + Public Function CompareTo(other As FilePathAndSpan) As Integer Implements IComparable(Of FilePathAndSpan).CompareTo + Dim result = String.CompareOrdinal(FilePath, other.FilePath) - actualLocations.Sort() - Assert.Equal(expectedLocations, actualLocations) + If result <> 0 Then + Return result + End If - ' The IDocumentNavigationService should not have been called - Assert.Null(mockDocumentNavigationService._documentId) - End If - Else - Assert.Null(mockDocumentNavigationService._documentId) - Assert.True(items Is Nothing OrElse items.Count = 0) - End If - End Using + Return Span.CompareTo(other.Span) End Function - Private Structure FilePathAndSpan - Implements IComparable(Of FilePathAndSpan) - - Public ReadOnly Property FilePath As String - Public ReadOnly Property Span As TextSpan - - Public Sub New(filePath As String, span As TextSpan) - Me.FilePath = filePath - Me.Span = span - End Sub - - Public Function CompareTo(other As FilePathAndSpan) As Integer Implements IComparable(Of FilePathAndSpan).CompareTo - Dim result = String.CompareOrdinal(FilePath, other.FilePath) - - If result <> 0 Then - Return result - End If - - Return Span.CompareTo(other.Span) - End Function + Public Overrides Function ToString() As String + Return $"{FilePath}, {Span}" + End Function + End Structure - Public Overrides Function ToString() As String - Return $"{FilePath}, {Span}" - End Function - End Structure - End Module End Namespace \ No newline at end of file diff --git a/src/EditorFeatures/VisualBasic/GoToImplementation/VisualBasicGoToImplementationService.vb b/src/EditorFeatures/VisualBasic/GoToImplementation/VisualBasicGoToImplementationService.vb index 122cecd148f..a76997e9e01 100644 --- a/src/EditorFeatures/VisualBasic/GoToImplementation/VisualBasicGoToImplementationService.vb +++ b/src/EditorFeatures/VisualBasic/GoToImplementation/VisualBasicGoToImplementationService.vb @@ -1,3 +1,4 @@ +#If False Then ' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. Imports System.Composition @@ -15,4 +16,5 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.GoToImplementation MyBase.New(presenters) End Sub End Class -End Namespace \ No newline at end of file +End Namespace +#end if \ No newline at end of file -- GitLab