Skip to content

[Sonic] Support multiple C# documents in document highlight - #84025

Merged
davidwengier merged 6 commits into
features/sonicfrom
dev/dawengie/SonicDocumentHighlight
Jun 6, 2026
Merged

[Sonic] Support multiple C# documents in document highlight#84025
davidwengier merged 6 commits into
features/sonicfrom
dev/dawengie/SonicDocumentHighlight

Conversation

@davidwengier

@davidwengier davidwengier commented Jun 4, 2026

Copy link
Copy Markdown
Member

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

Copilot AI review requested due to automatic review settings June 4, 2026 23:35
@davidwengier
davidwengier requested review from a team as code owners June 4, 2026 23:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

var highlightingService = document.Project.Solution.Services.ExportProvider.GetService<IHighlightingService>();
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

var keywordSpans = new List<TextSpan>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pool?

@dibarbet dibarbet left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

roslyn side lgtm

@davidwengier
davidwengier enabled auto-merge June 6, 2026 05:31
@davidwengier
davidwengier merged commit bd4b481 into features/sonic Jun 6, 2026
24 of 25 checks passed
@davidwengier
davidwengier deleted the dev/dawengie/SonicDocumentHighlight branch June 6, 2026 06:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants