From d6340a5e785664127db145519ad1ef12cf32710d Mon Sep 17 00:00:00 2001 From: David Wengier Date: Fri, 5 Jun 2026 15:04:57 +1000 Subject: [PATCH 1/7] Move files to Remote.Razor and update namespaces May as well do this in the feature branch, since doing it in main would be conflict city --- .../Hover/ComponentAvailabilityService.cs | 2 +- .../Hover/HoverDisplayOptions.cs | 2 +- .../Hover/HoverFactory.cs | 2 +- .../Hover/RemoteHoverService.cs | 16 ++++++++-------- .../Hover/HoverFactoryTest.cs | 3 ++- 5 files changed, 13 insertions(+), 12 deletions(-) rename src/Razor/src/Razor/src/{Microsoft.CodeAnalysis.Razor.Workspaces => Microsoft.CodeAnalysis.Remote.Razor}/Hover/HoverDisplayOptions.cs (91%) rename src/Razor/src/Razor/src/{Microsoft.CodeAnalysis.Razor.Workspaces => Microsoft.CodeAnalysis.Remote.Razor}/Hover/HoverFactory.cs (99%) rename src/Razor/src/Razor/test/{Microsoft.CodeAnalysis.Razor.Workspaces.UnitTests => Microsoft.CodeAnalysis.Remote.Razor.UnitTests}/Hover/HoverFactoryTest.cs (99%) diff --git a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/ComponentAvailabilityService.cs b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/ComponentAvailabilityService.cs index b204c551220d4..cd93ace257ef4 100644 --- a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/ComponentAvailabilityService.cs +++ b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/ComponentAvailabilityService.cs @@ -6,7 +6,7 @@ using Microsoft.CodeAnalysis.Razor.Tooltip; using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem; -namespace Microsoft.CodeAnalysis.Remote.Razor; +namespace Microsoft.CodeAnalysis.Remote.Razor.Hover; internal sealed class ComponentAvailabilityService(RemoteSolutionSnapshot solutionSnapshot) : AbstractComponentAvailabilityService { diff --git a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Hover/HoverDisplayOptions.cs b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/HoverDisplayOptions.cs similarity index 91% rename from src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Hover/HoverDisplayOptions.cs rename to src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/HoverDisplayOptions.cs index ede95efe58c67..7547f9bc36a10 100644 --- a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Hover/HoverDisplayOptions.cs +++ b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/HoverDisplayOptions.cs @@ -1,7 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -namespace Microsoft.CodeAnalysis.Razor.Hover; +namespace Microsoft.CodeAnalysis.Remote.Razor.Hover; internal readonly record struct HoverDisplayOptions(MarkupKind MarkupKind, bool SupportsVisualStudioExtensions) { diff --git a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Hover/HoverFactory.cs b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/HoverFactory.cs similarity index 99% rename from src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Hover/HoverFactory.cs rename to src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/HoverFactory.cs index 6dc522198e362..59d35221da5e5 100644 --- a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Hover/HoverFactory.cs +++ b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/HoverFactory.cs @@ -19,7 +19,7 @@ using Microsoft.VisualStudio.Editor.Razor; using Roslyn.Text.Adornments; -namespace Microsoft.CodeAnalysis.Razor.Hover; +namespace Microsoft.CodeAnalysis.Remote.Razor.Hover; internal static class HoverFactory { diff --git a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/RemoteHoverService.cs b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/RemoteHoverService.cs index 63ceea4b8e9f7..d90637afaefc9 100644 --- a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/RemoteHoverService.cs +++ b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/RemoteHoverService.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; @@ -10,9 +10,9 @@ using Microsoft.CodeAnalysis.LanguageServer.Handler; using Microsoft.CodeAnalysis.Options; using Microsoft.CodeAnalysis.Razor.DocumentMapping; -using Microsoft.CodeAnalysis.Razor.Hover; using Microsoft.CodeAnalysis.Razor.Protocol; using Microsoft.CodeAnalysis.Razor.Remote; +using Microsoft.CodeAnalysis.Remote.Razor.Hover; using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem; using static Microsoft.CodeAnalysis.Razor.Remote.RemoteResponse; @@ -30,7 +30,7 @@ protected override IRemoteHoverService CreateService(in ServiceArgs args) protected override IDocumentPositionInfoStrategy DocumentPositionInfoStrategy => PreferAttributeNameDocumentPositionInfoStrategy.Instance; - public ValueTask> GetHoverAsync( + public ValueTask> GetHoverAsync( JsonSerializableRazorSolutionWrapper solutionInfo, JsonSerializableDocumentId documentId, Position position, @@ -41,7 +41,7 @@ protected override IRemoteHoverService CreateService(in ServiceArgs args) context => GetHoverAsync(context, position, cancellationToken), cancellationToken); - private async ValueTask> GetHoverAsync( + private async ValueTask> GetHoverAsync( RemoteDocumentContext context, Position position, CancellationToken cancellationToken) @@ -112,7 +112,7 @@ protected override IRemoteHoverService CreateService(in ServiceArgs args) } // As there is a C# hover, stop further handling. - return new RemoteResponse(StopHandling: true, Result: csharpHover); + return new RemoteResponse(StopHandling: true, Result: csharpHover); } if (positionInfo.LanguageKind is not (RazorLanguageKind.Html or RazorLanguageKind.Razor)) @@ -153,7 +153,7 @@ protected override IRemoteHoverService CreateService(in ServiceArgs args) /// /// Once Razor moves wholly over to Roslyn.LanguageServer.Protocol, this method can be removed. /// - private static Hover ConvertHover(Hover hover) + private static LspHover ConvertHover(LspHover hover) { // Note: Razor only ever produces a Hover with MarkupContent or a VSInternalHover with RawContents. // Both variants return a Range. @@ -166,12 +166,12 @@ private static Hover ConvertHover(Hover hover) Contents = string.Empty, RawContent = rawContent }, - Hover { Range: var range, Contents.Fourth: MarkupContent contents } => new Hover() + LspHover { Range: var range, Contents.Fourth: MarkupContent contents } => new LspHover() { Range = range, Contents = contents }, - _ => Assumed.Unreachable(), + _ => Assumed.Unreachable(), }; } } diff --git a/src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.UnitTests/Hover/HoverFactoryTest.cs b/src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Remote.Razor.UnitTests/Hover/HoverFactoryTest.cs similarity index 99% rename from src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.UnitTests/Hover/HoverFactoryTest.cs rename to src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Remote.Razor.UnitTests/Hover/HoverFactoryTest.cs index 5d53efcda7dfb..7e447f07f3362 100644 --- a/src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.UnitTests/Hover/HoverFactoryTest.cs +++ b/src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Remote.Razor.UnitTests/Hover/HoverFactoryTest.cs @@ -12,8 +12,9 @@ using Moq; using Xunit; using Xunit.Abstractions; +using Roslyn.LanguageServer.Protocol; -namespace Microsoft.CodeAnalysis.Razor.Hover; +namespace Microsoft.CodeAnalysis.Remote.Razor.Hover; public class HoverFactoryTest(ITestOutputHelper testOutput) : ToolingTestBase(testOutput) { From 1e13455643fd589f4b1b08a8e3318238a7b46b68 Mon Sep 17 00:00:00 2001 From: David Wengier Date: Thu, 4 Jun 2026 21:08:37 +1000 Subject: [PATCH 2/7] Oops, missed committing this in all of the mess of my machine --- .../IDocumentMappingServiceExtensions.cs | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/DocumentMapping/IDocumentMappingServiceExtensions.cs b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/DocumentMapping/IDocumentMappingServiceExtensions.cs index d032dfd11afbf..5d5a7315487e2 100644 --- a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/DocumentMapping/IDocumentMappingServiceExtensions.cs +++ b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/DocumentMapping/IDocumentMappingServiceExtensions.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; @@ -91,4 +91,38 @@ public static bool TryMapToCSharpPositionOrNext(this IDocumentMappingService ser csharpPosition = result ? csharpLinePosition.ToPosition() : null; return result; } + + /// + /// Convenience method to map from Razor to C#, which checks both impl and decl documents + /// + /// + /// A position in a Razor document could map to one of two different C# documents, but the only situation + /// where it would map to both, is when the resulting position in the C# document is semantically equivalent. + /// ie, a Razor using or namespace directive would map to both the decl and impl documents, but in either case + /// it ends up at a C# using or namespace directive, so it doesn't matter which one we get back. + /// + /// For all other positions in the Razor document, only one document will be mappable. + /// + /// Note that the same is NOT true in reverse: A mappable position in a C# document might be unique to either + /// the decl or impl document, but that would only be a coincidence. Part of the reason we emit inDeclDocument + /// as an out parameter is because in order to map back to Razor later, we must know which document the C# position + /// came from. + /// + public static bool TryMapToCSharpDocumentLinePosition(this IDocumentMappingService service, RazorCodeDocument codeDocument, int razorIndex, out LinePosition csharpPosition, out int csharpIndex, out bool inDeclDocument) + { + inDeclDocument = false; + if (service.TryMapToCSharpDocumentPosition(codeDocument.GetRequiredCSharpDocument(declarationDocument: false), razorIndex, out csharpPosition, out csharpIndex)) + { + return true; + } + + inDeclDocument = true; + if (codeDocument.GetCSharpDocument(declarationDocument: true) is { } declDocument && + service.TryMapToCSharpDocumentPosition(declDocument, razorIndex, out csharpPosition, out csharpIndex)) + { + return true; + } + + return false; + } } From 66c5ecd3f5f5a751e98917601ca36e414448209a Mon Sep 17 00:00:00 2001 From: David Wengier Date: Fri, 5 Jun 2026 15:13:20 +1000 Subject: [PATCH 3/7] Track which C# document we found the mapped position in This is going to be generally useful in future --- .../Completion/Delegation/DelegatedCompletionHelper.cs | 5 +++-- .../DocumentMapping/IDocumentMappingServiceExtensions.cs | 7 ++++--- .../RazorDocumentServiceBase.cs | 6 +++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Completion/Delegation/DelegatedCompletionHelper.cs b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Completion/Delegation/DelegatedCompletionHelper.cs index 7bef16c252cd4..0402b740fe9b2 100644 --- a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Completion/Delegation/DelegatedCompletionHelper.cs +++ b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Completion/Delegation/DelegatedCompletionHelper.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; @@ -197,7 +197,8 @@ public static bool TryGetProvisionalCompletionInfo( LspFactory.CreatePosition( previousPosition.Line, previousPosition.Character + 1), - previousCharacterPositionInfo.HostDocumentIndex + 1); + previousCharacterPositionInfo.HostDocumentIndex + 1, + originalPositionInfo.InDeclDocument); result = new CompletionPositionInfo(addProvisionalDot, provisionalPositionInfo, ShouldIncludeDelegationSnippets: false); return true; diff --git a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/DocumentMapping/IDocumentMappingServiceExtensions.cs b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/DocumentMapping/IDocumentMappingServiceExtensions.cs index 5d5a7315487e2..14c5637480cf1 100644 --- a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/DocumentMapping/IDocumentMappingServiceExtensions.cs +++ b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/DocumentMapping/IDocumentMappingServiceExtensions.cs @@ -36,14 +36,15 @@ public static DocumentPositionInfo GetPositionInfo( var position = sourceText.GetPosition(razorIndex); + var inDeclDocument = false; var languageKind = codeDocument.GetLanguageKind(razorIndex, rightAssociative: false); if (languageKind is RazorLanguageKind.CSharp) { - if (service.TryMapToCSharpDocumentPosition(codeDocument.GetRequiredImplCSharpDocument(), razorIndex, out Position? mappedPosition, out _)) + if (service.TryMapToCSharpDocumentLinePosition(codeDocument, razorIndex, out var mappedPosition, out _, out inDeclDocument)) { // For C# locations, we attempt to return the corresponding position // within the projected document - position = mappedPosition; + position = mappedPosition.ToPosition(); } else { @@ -54,7 +55,7 @@ public static DocumentPositionInfo GetPositionInfo( } } - return new DocumentPositionInfo(languageKind, position, razorIndex); + return new DocumentPositionInfo(languageKind, position, razorIndex, inDeclDocument); } public static bool TryMapToRazorDocumentRange(this IDocumentMappingService service, RazorCSharpDocument csharpDocument, LspRange csharpRange, MappingBehavior mappingBehavior, [NotNullWhen(true)] out LspRange? razorRange) diff --git a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/RazorDocumentServiceBase.cs b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/RazorDocumentServiceBase.cs index 38499f8df8999..a875d1f35a71b 100644 --- a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/RazorDocumentServiceBase.cs +++ b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/RazorDocumentServiceBase.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; @@ -31,13 +31,13 @@ protected DocumentPositionInfo GetPositionInfo(RazorCodeDocument codeDocument, i // Sometimes Html can actually be mapped to C#, like for example component attributes, which map to // C# properties, even though they appear entirely in a Html context. Since remapping is pretty cheap // it's easier to just try mapping, and see what happens, rather than checking for specific syntax nodes. - if (DocumentMappingService.TryMapToCSharpDocumentPosition(codeDocument.GetRequiredImplCSharpDocument(), positionInfo.HostDocumentIndex, out Position? csharpPosition, out _)) + if (DocumentMappingService.TryMapToCSharpDocumentLinePosition(codeDocument, positionInfo.HostDocumentIndex, out var csharpPosition, out _, out var inDeclDocument)) { // We're just gonna pretend this mapped perfectly normally onto C#. Moving this logic to the actual position info // calculating code is possible, but could have untold effects, so opt-in is better (for now?) // TODO: Not using a with operator here because it doesn't work in OOP for some reason. - positionInfo = new DocumentPositionInfo(RazorLanguageKind.CSharp, csharpPosition, positionInfo.HostDocumentIndex); + positionInfo = new DocumentPositionInfo(RazorLanguageKind.CSharp, csharpPosition.ToPosition(), positionInfo.HostDocumentIndex, inDeclDocument); } } From 4a18e2f27c119cec5b51f44d2d6b26b9d10a9e3f Mon Sep 17 00:00:00 2001 From: David Wengier Date: Fri, 5 Jun 2026 15:28:50 +1000 Subject: [PATCH 4/7] Make hover service multi-document aware --- .../Hover/RemoteHoverService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/RemoteHoverService.cs b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/RemoteHoverService.cs index d90637afaefc9..b28f383f60665 100644 --- a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/RemoteHoverService.cs +++ b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/RemoteHoverService.cs @@ -65,7 +65,7 @@ protected override IRemoteHoverService CreateService(in ServiceArgs args) if (positionInfo.LanguageKind == RazorLanguageKind.CSharp) { var generatedDocument = await context.Snapshot - .GetGeneratedDocumentAsync(cancellationToken) + .GetGeneratedDocumentAsync(positionInfo.InDeclDocument, cancellationToken) .ConfigureAwait(false); var globalOptions = generatedDocument.Project.Solution.Services.ExportProvider.GetService(); @@ -85,7 +85,7 @@ protected override IRemoteHoverService CreateService(in ServiceArgs args) // Map the hover range back to the host document if (csharpHover.Range is { } range && - DocumentMappingService.TryMapToRazorDocumentRange(codeDocument.GetRequiredImplCSharpDocument(), range.ToLinePositionSpan(), out var hostDocumentSpan)) + DocumentMappingService.TryMapToRazorDocumentRange(codeDocument.GetRequiredCSharpDocument(positionInfo.InDeclDocument), range.ToLinePositionSpan(), out var hostDocumentSpan)) { csharpHover.Range = LspFactory.CreateRange(hostDocumentSpan); } From 06ab645d957a2656c086e713cb301567b9217316 Mon Sep 17 00:00:00 2001 From: David Wengier Date: Fri, 5 Jun 2026 15:29:18 +1000 Subject: [PATCH 5/7] Fix build from previous moves --- .../Completion/RemoteCompletionService.cs | 1 + .../ProjectAvailabilityTests.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Completion/RemoteCompletionService.cs b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Completion/RemoteCompletionService.cs index 2a05ba9c280a6..36438b99aab42 100644 --- a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Completion/RemoteCompletionService.cs +++ b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Completion/RemoteCompletionService.cs @@ -25,6 +25,7 @@ using Microsoft.CodeAnalysis.Razor.Remote; using Microsoft.CodeAnalysis.Razor.Telemetry; using Microsoft.CodeAnalysis.Razor.Workspaces.Settings; +using Microsoft.CodeAnalysis.Remote.Razor.Hover; using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem; using Microsoft.CodeAnalysis.Text; using CompletionResponse = Microsoft.CodeAnalysis.Razor.Remote.RemoteResponse; diff --git a/src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/ProjectAvailabilityTests.cs b/src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/ProjectAvailabilityTests.cs index 95f5e3f7eb0a9..a603af8560eb1 100644 --- a/src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/ProjectAvailabilityTests.cs +++ b/src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/ProjectAvailabilityTests.cs @@ -3,7 +3,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Razor.Test.Common; -using Microsoft.CodeAnalysis.Remote.Razor; +using Microsoft.CodeAnalysis.Remote.Razor.Hover; using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem; using Microsoft.VisualStudio.Razor.LanguageClient.Cohost; using Roslyn.Test.Utilities; From 7954cd5aa68661e102cdfcced810a9f09523d1ce Mon Sep 17 00:00:00 2001 From: David Wengier Date: Fri, 5 Jun 2026 15:38:41 +1000 Subject: [PATCH 6/7] Add test coverage for more C# documents --- .../Endpoints/CohostHoverEndpointTest.cs | 76 ++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/Endpoints/CohostHoverEndpointTest.cs b/src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/Endpoints/CohostHoverEndpointTest.cs index 30105dbe797a0..66d1b3269d2fd 100644 --- a/src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/Endpoints/CohostHoverEndpointTest.cs +++ b/src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/Endpoints/CohostHoverEndpointTest.cs @@ -146,7 +146,7 @@ public async Task Html_EndTag() } [ConditionalFact(typeof(IsEnglishLocal))] - public async Task CSharp() + public async Task CSharp_Impl() { TestCode code = """ @@ -178,6 +178,80 @@ await VerifyHoverAsync(code, async (hover, document) => }); } + [ConditionalFact(typeof(IsEnglishLocal))] + public async Task CSharp_Decl() + { + TestCode code = """ + +
+ + @code + { + public void M() + { + var $$[|myVariable|] = "Hello"; + + var length = myVariable.Length; + } + } + """; + + await VerifyHoverAsync(code, async (hover, document) => + { + await VerifyRangeAsync(hover, code.Span, document); + + hover.VerifyContents( + Container( + Container( + Image, + ClassifiedText( // (local variable) string myVariable + Punctuation("("), + Text("local variable"), + Punctuation(")"), + WhiteSpace(" "), + Keyword("string"), + WhiteSpace(" "), + LocalName("myVariable"))))); + }); + } + + [ConditionalFact(typeof(IsEnglishLocal))] + public async Task CSharp_Legacy() + { + TestCode code = """ + +
+ + @functions + { + public void M() + { + var $$[|myVariable|] = "Hello"; + + var length = myVariable.Length; + } + } + """; + + await VerifyHoverAsync(code, fileKind: RazorFileKind.Legacy, htmlResponse: null, async (hover, document) => + { + await VerifyRangeAsync(hover, code.Span, document); + + hover.VerifyContents( + Container( + Container( + Image, + ClassifiedText( // (local variable) string myVariable + Punctuation("("), + Text("local variable"), + Punctuation(")"), + WhiteSpace(" "), + Keyword("string"), + WhiteSpace(" "), + LocalName("myVariable"))))); + }); + } + [Fact] public async Task ComponentAttribute() { From 704df7174556f357f79d28a678f2ab54a3e77de3 Mon Sep 17 00:00:00 2001 From: David Wengier Date: Fri, 5 Jun 2026 16:25:47 +1000 Subject: [PATCH 7/7] PR Feedback --- .../Delegation/DelegatedCompletionHelper.cs | 2 +- .../Hover/RemoteHoverService.cs | 34 +------------------ 2 files changed, 2 insertions(+), 34 deletions(-) diff --git a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Completion/Delegation/DelegatedCompletionHelper.cs b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Completion/Delegation/DelegatedCompletionHelper.cs index 0402b740fe9b2..f48ba6054bb87 100644 --- a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Completion/Delegation/DelegatedCompletionHelper.cs +++ b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Completion/Delegation/DelegatedCompletionHelper.cs @@ -198,7 +198,7 @@ public static bool TryGetProvisionalCompletionInfo( previousPosition.Line, previousPosition.Character + 1), previousCharacterPositionInfo.HostDocumentIndex + 1, - originalPositionInfo.InDeclDocument); + previousCharacterPositionInfo.InDeclDocument); result = new CompletionPositionInfo(addProvisionalDot, provisionalPositionInfo, ShouldIncludeDelegationSnippets: false); return true; diff --git a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/RemoteHoverService.cs b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/RemoteHoverService.cs index b28f383f60665..faec82fa616e9 100644 --- a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/RemoteHoverService.cs +++ b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/RemoteHoverService.cs @@ -5,7 +5,6 @@ using System.Diagnostics; using System.Threading; using System.Threading.Tasks; -using Microsoft.AspNetCore.Razor; using Microsoft.AspNetCore.Razor.Language; using Microsoft.CodeAnalysis.LanguageServer.Handler; using Microsoft.CodeAnalysis.Options; @@ -141,37 +140,6 @@ protected override IRemoteHoverService CreateService(in ServiceArgs args) return CallHtml; } - // Ensure that we convert our Hover to a Roslyn Hover. - var resultHover = ConvertHover(razorHover); - - return Results(resultHover); - } - - /// - /// Converts a to a . - /// - /// - /// Once Razor moves wholly over to Roslyn.LanguageServer.Protocol, this method can be removed. - /// - private static LspHover ConvertHover(LspHover hover) - { - // Note: Razor only ever produces a Hover with MarkupContent or a VSInternalHover with RawContents. - // Both variants return a Range. - - return hover switch - { - VSInternalHover { Range: var range, RawContent: { } rawContent } => new VSInternalHover() - { - Range = range, - Contents = string.Empty, - RawContent = rawContent - }, - LspHover { Range: var range, Contents.Fourth: MarkupContent contents } => new LspHover() - { - Range = range, - Contents = contents - }, - _ => Assumed.Unreachable(), - }; + return Results(razorHover); } }