[Sonic] Support multiple C# documents in document highlight - #84025
Merged
Conversation
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates Razor cohosting document highlight to correctly handle Razor files that project into multiple generated C# documents (implementation + declaration), ensuring reference highlighting can span both generated documents and then map results back to Razor.
Changes:
- Extend remote Razor document highlight to query reference highlights across both generated C# documents (while keeping keyword highlighting single-document).
- Add a document-mapping helper that can map a Razor position to either impl or decl projected C# document (and report which one was used).
- Expand cohosting unit tests to cover impl/decl scenarios and legacy (
@functions) file kind.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/Endpoints/CohostDocumentHighlightEndpointTest.cs | Adds broader test coverage (impl/decl + legacy) for highlight scenarios across projected documents. |
| src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteDocumentSnapshot.cs | Adds a TryGetGeneratedDocumentAsync helper to safely retrieve the “other” (decl/impl) generated document when present. |
| src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/DocumentHighlight/RemoteDocumentHighlightService.cs | Reworks highlight retrieval to combine reference highlights from multiple generated C# documents and map them back to Razor ranges. |
| src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/DocumentMapping/IDocumentMappingServiceExtensions.cs | Adds a convenience mapping method that checks both impl and decl projected C# documents and returns which one matched. |
| src/LanguageServer/Protocol/Handler/Highlights/DocumentHighlightHandler.cs | Refactors highlight logic to use instance state (removing the internal static helper) while keeping overall behavior equivalent. |
ToddGrun
approved these changes
Jun 5, 2026
| var highlightingService = document.Project.Solution.Services.ExportProvider.GetService<IHighlightingService>(); | ||
| var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
|
|
||
| var keywordSpans = new List<TextSpan>(); |
…gie/SonicDocumentHighlight
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I decided to give up on Code Actions, because it depended on formatting and the edit service etc. and pick something simple like Document Highlight.
And then I discovered that Document Highlight was actually broken with multiple C# documents, as the Roslyn LSP handler only ever returns highlight results from a single document, so I had to re-write more than I expected.
I have the worst luck 😁
Microsoft Reviewers: Open in CodeFlow