diff --git a/src/EditorFeatures/Test/Options/GlobalOptionsTests.cs b/src/EditorFeatures/Test/Options/GlobalOptionsTests.cs index 1260e97336794..76ab16db3ae76 100644 --- a/src/EditorFeatures/Test/Options/GlobalOptionsTests.cs +++ b/src/EditorFeatures/Test/Options/GlobalOptionsTests.cs @@ -203,7 +203,7 @@ static void Recurse(Type type, object options, object defaultOptions, string lan private static TestWorkspace CreateWorkspace(out TestGlobalOptions globalOptions) { - var composition = EditorTestCompositions.LanguageServerProtocol. + var composition = EditorTestCompositions.LanguageServerProtocolEditorFeatures. AddExcludedPartTypes(typeof(GlobalOptionService)). AddParts(typeof(TestGlobalOptions)); diff --git a/src/EditorFeatures/TestUtilities/EditorTestCompositions.cs b/src/EditorFeatures/TestUtilities/EditorTestCompositions.cs index 75d7b2115b31e..1057ae414af29 100644 --- a/src/EditorFeatures/TestUtilities/EditorTestCompositions.cs +++ b/src/EditorFeatures/TestUtilities/EditorTestCompositions.cs @@ -76,6 +76,10 @@ public static class EditorTestCompositions .AddParts( typeof(TestInteractiveWindowEditorFactoryService)); - public static readonly TestComposition LanguageServerProtocol = EditorFeatures; + public static readonly TestComposition LanguageServerProtocol = FeaturesTestCompositions.Features + .AddAssemblies(typeof(LanguageServerResources).Assembly); + + public static readonly TestComposition LanguageServerProtocolEditorFeatures = EditorFeatures + .AddAssemblies(typeof(LanguageServerResources).Assembly); } } diff --git a/src/EditorFeatures/TestUtilities/LanguageServer/AbstractLanguageServerProtocolTests.cs b/src/EditorFeatures/TestUtilities/LanguageServer/AbstractLanguageServerProtocolTests.cs index eaddc8a4e7021..bbedf39a263a9 100644 --- a/src/EditorFeatures/TestUtilities/LanguageServer/AbstractLanguageServerProtocolTests.cs +++ b/src/EditorFeatures/TestUtilities/LanguageServer/AbstractLanguageServerProtocolTests.cs @@ -51,7 +51,7 @@ protected AbstractLanguageServerProtocolTests(ITestOutputHelper? testOutputHelpe TestOutputLspLogger = testOutputHelper != null ? new TestOutputLspLogger(testOutputHelper) : NoOpLspLogger.Instance; } - private static readonly TestComposition s_composition = EditorTestCompositions.LanguageServerProtocol + private static readonly TestComposition s_composition = EditorTestCompositions.LanguageServerProtocolEditorFeatures .AddParts(typeof(TestDocumentTrackingService)) .AddParts(typeof(TestWorkspaceRegistrationService)); diff --git a/src/Features/LanguageServer/Protocol/Handler/References/FindAllReferencesHandler.cs b/src/Features/LanguageServer/Protocol/Handler/References/FindAllReferencesHandler.cs index c2b954e599941..c2c4372be3f54 100644 --- a/src/Features/LanguageServer/Protocol/Handler/References/FindAllReferencesHandler.cs +++ b/src/Features/LanguageServer/Protocol/Handler/References/FindAllReferencesHandler.cs @@ -50,9 +50,6 @@ public FindAllReferencesHandler( RequestContext context, CancellationToken cancellationToken) { - var clientCapabilities = context.GetRequiredClientCapabilities(); - Debug.Assert(clientCapabilities.HasVisualStudioLspCapability()); - var document = context.Document; var workspace = context.Workspace; Contract.ThrowIfNull(document); diff --git a/src/Features/LanguageServer/ProtocolUnitTests/DocumentChanges/DocumentChangesTests.WithFindAllReferences.cs b/src/Features/LanguageServer/ProtocolUnitTests/DocumentChanges/DocumentChangesTests.WithFindAllReferences.cs index daaab4638e958..eb4afa0d20854 100644 --- a/src/Features/LanguageServer/ProtocolUnitTests/DocumentChanges/DocumentChangesTests.WithFindAllReferences.cs +++ b/src/Features/LanguageServer/ProtocolUnitTests/DocumentChanges/DocumentChangesTests.WithFindAllReferences.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.LanguageServer.UnitTests.References; +using Microsoft.VisualStudio.LanguageServer.Protocol; using Xunit; namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests.DocumentChanges @@ -36,7 +37,7 @@ void M2() await DidOpen(testLspServer, locationTyped.Uri); - var findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync(testLspServer, locationTyped); + var findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync(testLspServer, locationTyped); Assert.Single(findResults); Assert.Equal("A", findResults[0].ContainingType); @@ -44,7 +45,7 @@ void M2() // Declare a local inside A.M() await DidChange(testLspServer, locationTyped.Uri, (5, 0, "var i = someInt + 1;\r\n")); - findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync(testLspServer, locationTyped); + findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync(testLspServer, locationTyped); Assert.Equal(2, findResults.Length); Assert.Equal("A", findResults[0].ContainingType); @@ -53,7 +54,7 @@ void M2() // Declare a field in B await DidChange(testLspServer, locationTyped.Uri, (10, 0, "int someInt = A.someInt + 1;\r\n")); - findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync(testLspServer, locationTyped); + findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync(testLspServer, locationTyped); Assert.Equal(3, findResults.Length); Assert.Equal("A", findResults[0].ContainingType); @@ -63,7 +64,7 @@ void M2() // Declare a local inside B.M2() await DidChange(testLspServer, locationTyped.Uri, (13, 0, "var j = someInt + A.someInt;\r\n")); - findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync(testLspServer, locationTyped); + findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync(testLspServer, locationTyped); Assert.Equal(4, findResults.Length); Assert.Equal("A", findResults[0].ContainingType); @@ -78,7 +79,7 @@ void M2() // the updated document, so if we regress and get lucky, we still know about it. await DidClose(testLspServer, locationTyped.Uri); - findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync(testLspServer, locationTyped); + findResults = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync(testLspServer, locationTyped); Assert.Single(findResults); Assert.Equal("A", findResults[0].ContainingType); diff --git a/src/Features/LanguageServer/ProtocolUnitTests/References/FindAllReferencesHandlerFeaturesTests.cs b/src/Features/LanguageServer/ProtocolUnitTests/References/FindAllReferencesHandlerFeaturesTests.cs new file mode 100644 index 0000000000000..be0106e1ac8a7 --- /dev/null +++ b/src/Features/LanguageServer/ProtocolUnitTests/References/FindAllReferencesHandlerFeaturesTests.cs @@ -0,0 +1,54 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis.Editor.Test; +using Microsoft.CodeAnalysis.Editor.UnitTests; +using Microsoft.CodeAnalysis.Test.Utilities; +using Roslyn.Test.Utilities; +using Xunit; +using Xunit.Abstractions; +using LSP = Microsoft.VisualStudio.LanguageServer.Protocol; + +namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests.References; +public class FindAllReferencesHandlerFeaturesTests : AbstractLanguageServerProtocolTests +{ + public FindAllReferencesHandlerFeaturesTests(ITestOutputHelper? testOutputHelper) : base(testOutputHelper) + { + } + + protected override TestComposition Composition => EditorTestCompositions.LanguageServerProtocol + .AddParts(typeof(TestDocumentTrackingService)) + .AddParts(typeof(TestWorkspaceRegistrationService)); + + [Fact] + public async Task TestFindAllReferencesAsync_DoesNotUseVSTypes() + { + var markup = +@"class A +{ + public int {|reference:someInt|} = 1; + void M() + { + var i = {|reference:someInt|} + 1; + } +} +class B +{ + int someInt = A.{|reference:someInt|} + 1; + void M2() + { + var j = someInt + A.{|caret:|}{|reference:someInt|}; + } +}"; + await using var testLspServer = await CreateTestLspServerAsync(markup, new LSP.ClientCapabilities()); + + var results = await FindAllReferencesHandlerTests.RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); + AssertLocationsEqual(testLspServer.GetLocations("reference"), results.Select(result => result)); + } +} diff --git a/src/Features/LanguageServer/ProtocolUnitTests/References/FindAllReferencesHandlerTests.cs b/src/Features/LanguageServer/ProtocolUnitTests/References/FindAllReferencesHandlerTests.cs index 45112ce1e8be5..46b6e6706b33c 100644 --- a/src/Features/LanguageServer/ProtocolUnitTests/References/FindAllReferencesHandlerTests.cs +++ b/src/Features/LanguageServer/ProtocolUnitTests/References/FindAllReferencesHandlerTests.cs @@ -27,7 +27,7 @@ public FindAllReferencesHandlerTests(ITestOutputHelper testOutputHelper) : base( { } - [WpfFact] + [Fact] public async Task TestFindAllReferencesAsync() { var markup = @@ -49,7 +49,7 @@ void M2() }"; await using var testLspServer = await CreateTestLspServerAsync(markup, CapabilitiesWithVSExtensions); - var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); + var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); AssertLocationsEqual(testLspServer.GetLocations("reference"), results.Select(result => result.Location)); // Results are returned in a non-deterministic order, so we order them by location @@ -63,7 +63,7 @@ void M2() AssertHighlightCount(results, expectedDefinitionCount: 1, expectedWrittenReferenceCount: 0, expectedReferenceCount: 3); } - [WpfFact] + [Fact] public async Task TestFindAllReferencesAsync_Streaming() { var markup = @@ -87,7 +87,7 @@ void M2() using var progress = BufferedProgress.Create(null); - var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First(), progress); + var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First(), progress); Assert.Null(results); @@ -113,7 +113,7 @@ void M2() AssertHighlightCount(results, expectedDefinitionCount: 1, expectedWrittenReferenceCount: 0, expectedReferenceCount: 3); } - [WpfFact] + [Fact] public async Task TestFindAllReferencesAsync_Class() { var markup = @@ -135,7 +135,7 @@ void M2() }"; await using var testLspServer = await CreateTestLspServerAsync(markup, CapabilitiesWithVSExtensions); - var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); + var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); AssertLocationsEqual(testLspServer.GetLocations("reference"), results.Select(result => result.Location)); var textElement = results[0].Text as ClassifiedTextElement; @@ -154,7 +154,7 @@ void M2() AssertHighlightCount(results, expectedDefinitionCount: 1, expectedWrittenReferenceCount: 0, expectedReferenceCount: 2); } - [WpfFact] + [Fact] public async Task TestFindAllReferencesAsync_MultipleDocuments() { var markups = new string[] { @@ -178,7 +178,7 @@ void M2() await using var testLspServer = await CreateTestLspServerAsync(markups, new InitializationOptions { ClientCapabilities = CapabilitiesWithVSExtensions }); - var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); + var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); AssertLocationsEqual(testLspServer.GetLocations("reference"), results.Select(result => result.Location)); // Results are returned in a non-deterministic order, so we order them by location @@ -192,7 +192,7 @@ void M2() AssertHighlightCount(results, expectedDefinitionCount: 1, expectedWrittenReferenceCount: 0, expectedReferenceCount: 3); } - [WpfFact] + [Fact] public async Task TestFindAllReferencesAsync_InvalidLocation() { var markup = @@ -202,11 +202,11 @@ public async Task TestFindAllReferencesAsync_InvalidLocation() }"; await using var testLspServer = await CreateTestLspServerAsync(markup, CapabilitiesWithVSExtensions); - var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); + var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); Assert.Empty(results); } - [WpfFact] + [Fact] public async Task TestFindAllReferencesMetadataDefinitionAsync() { var markup = @@ -221,12 +221,12 @@ void M() }"; await using var testLspServer = await CreateTestLspServerAsync(markup, CapabilitiesWithVSExtensions); - var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); + var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); Assert.NotNull(results[0].Location.Uri); AssertHighlightCount(results, expectedDefinitionCount: 0, expectedWrittenReferenceCount: 0, expectedReferenceCount: 1); } - [WpfFact, WorkItem(1240061, "https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1240061/")] + [Fact, WorkItem(1240061, "https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1240061/")] public async Task TestFindAllReferencesAsync_Namespace() { var markup = @@ -243,7 +243,7 @@ void M() "; await using var testLspServer = await CreateTestLspServerAsync(markup, CapabilitiesWithVSExtensions); - var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); + var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); // Namespace definitions should not have a location Assert.True(results.Any(r => r.DefinitionText != null && r.Location == null)); @@ -255,7 +255,7 @@ void M() AssertHighlightCount(results, expectedDefinitionCount: 0, expectedWrittenReferenceCount: 0, expectedReferenceCount: 2); } - [WpfFact, WorkItem(1245616, "https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1245616/")] + [Fact, WorkItem(1245616, "https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1245616/")] public async Task TestFindAllReferencesAsync_Highlights() { var markup = @@ -273,11 +273,11 @@ void M() "; await using var testLspServer = await CreateTestLspServerAsync(markup, CapabilitiesWithVSExtensions); - var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); + var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); AssertHighlightCount(results, expectedDefinitionCount: 1, expectedWrittenReferenceCount: 1, expectedReferenceCount: 1); } - [WpfFact] + [Fact] public async Task TestFindAllReferencesAsync_StaticClassification() { var markup = @@ -285,7 +285,7 @@ public async Task TestFindAllReferencesAsync_StaticClassification() "; await using var testLspServer = await CreateTestLspServerAsync(markup, CapabilitiesWithVSExtensions); - var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); + var results = await RunFindAllReferencesAsync(testLspServer, testLspServer.GetLocations("caret").First()); // Ensure static definitions and references are only classified once var textRuns = ((ClassifiedTextElement)results.First().Text).Runs; @@ -301,11 +301,11 @@ private static LSP.ReferenceParams CreateReferenceParams(LSP.Location caret, IPr PartialResultToken = progress }; - internal static async Task RunFindAllReferencesAsync(TestLspServer testLspServer, LSP.Location caret, IProgress progress = null) + internal static async Task RunFindAllReferencesAsync(TestLspServer testLspServer, LSP.Location caret, IProgress progress = null) { - var results = await testLspServer.ExecuteRequestAsync(LSP.Methods.TextDocumentReferencesName, + var results = await testLspServer.ExecuteRequestAsync(LSP.Methods.TextDocumentReferencesName, CreateReferenceParams(caret, progress), CancellationToken.None); - return results?.Cast()?.ToArray(); + return results?.Cast()?.ToArray(); } private static void AssertValidDefinitionProperties(LSP.VSInternalReferenceItem[] referenceItems, int definitionIndex, Glyph definitionGlyph) diff --git a/src/VisualStudio/CSharp/Test/DocumentOutline/DocumentOutlineTestsBase.cs b/src/VisualStudio/CSharp/Test/DocumentOutline/DocumentOutlineTestsBase.cs index 92b99e6c09c9c..64aa99e48d58b 100644 --- a/src/VisualStudio/CSharp/Test/DocumentOutline/DocumentOutlineTestsBase.cs +++ b/src/VisualStudio/CSharp/Test/DocumentOutline/DocumentOutlineTestsBase.cs @@ -72,7 +72,7 @@ public ValueTask DisposeAsync() => _disposable.DisposeAsync(); } - private static readonly TestComposition s_composition = EditorTestCompositions.LanguageServerProtocol + private static readonly TestComposition s_composition = EditorTestCompositions.LanguageServerProtocolEditorFeatures .AddParts(typeof(TestDocumentTrackingService)) .AddParts(typeof(TestWorkspaceRegistrationService)) .RemoveParts(typeof(MockWorkspaceEventListenerProvider));