Skip to content

Conversation

davidwengier
Copy link
Member

@davidwengier davidwengier commented Jul 25, 2025

Part of dotnet/razor#9519
Half of the fix for dotnet/razor#12054, along with the Razor side at dotnet/razor#12055

Previously I allowed rename to work on any source generated document, but made it only opt in for when the rename request came from Razor. Now that we have a little more time for development, it's time to finish the feature off properly. This PR:

  • Rename will only consider source generated documents if they're from Razor
  • Any rename operation will automatically do so, without opt in
  • Before applying changes to the workspace, we call in to Razor to map edits
    • This matches the behaviour of non-cohosting, but that does edit mapping via a document service. Since with cohosting Razor doesn't control the document creation, there is no way to set up a document service, so there is just a workspace service that does the same thing.

There will still need to be more PRs to make this work in VS Code, and to hook up the span mapping aspect of the service, but I didn't want this PR to get too big.

@davidwengier davidwengier requested a review from a team July 25, 2025 12:18
@davidwengier davidwengier requested review from a team as code owners July 25, 2025 12:18
@davidwengier davidwengier changed the title Add helper method Rename Razor source generated documents in all scenarios, and map edits Jul 25, 2025
@davidwengier davidwengier removed the request for review from a team July 25, 2025 12:21
/// We check for the presence of the razor SG by full name
/// so we have to make sure this is the right name in the right namespace.
/// </summary>
internal sealed class RazorSourceGenerator(Action<GeneratorExecutionContext> execute) : ISourceGenerator
Copy link
Member Author

Choose a reason for hiding this comment

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

Copied this from #79510

_renameLocationSet.Solution.GetRequiredDocument(location.DocumentId), location.Location.SourceSpan);
var document = await solution.GetRequiredDocumentAsync(location.DocumentId, includeSourceGenerated: true, cancellationToken).ConfigureAwait(false);

Contract.ThrowIfTrue(location.DocumentId.IsSourceGenerated && !document.IsRazorSourceGeneratedDocument());
Copy link
Member Author

Choose a reason for hiding this comment

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

It's a little odd that these contracts come after the possibly-expensive method, but sadly thats the way it has to be. They do prove that at least if the GetRequiredDocumentAsync had to go async, then it was for Razor which is what we want anyway.

var changes = _baseSolution.GetChanges(newSolution);
var changes = newSolution.GetChanges(_baseSolution);
Copy link
Member Author

Choose a reason for hiding this comment

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

These parameters were backwards, and it was a huge pain to find! For regular documents it turns out not to matter.

Comment on lines -192 to +195
new A().{|renamed:M|}();
new A().M();
Copy link
Member Author

Choose a reason for hiding this comment

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

This is not a Razor generated document, just a normal one, so these spans are no longer found by rename. Efficiency!

if (!documentId.IsSourceGenerated)
{
finalSolution = finalSolution.WithDocumentName(documentId, newName);
}
Copy link
Member

Choose a reason for hiding this comment

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

could you even have a case where you had an SG doc, and it gota new name? is this just being paranoid (which i'm fine with). might be good to doc.

Copy link
Member Author

Choose a reason for hiding this comment

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

Source generated documents don't support modifying document attributes, which this call does. Just like WithDocumentText and WithSyntaxRoot weren't supported until I added it a few months ago.

It's a bit of an annoying chicken and egg - it would be nice if this call could no-op if the attributes haven't actually changed, regardless of document types, but it means adding support for source generated documents in order to get far enough to know whether that is what is happening.

Copy link
Member Author

Choose a reason for hiding this comment

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

Docced. I doubt there is a scenario were a SG doc would want to get renamed here, and restricting it to Razor makes it entirely impossible - Roslyn would have to know to rename a seemingly arbitrary additional document to cause a change of Razor SG doc name.

@davidwengier davidwengier enabled auto-merge July 27, 2025 04:27
@davidwengier davidwengier disabled auto-merge July 28, 2025 21:41
@davidwengier davidwengier merged commit a461c84 into dotnet:main Jul 28, 2025
28 checks passed
@davidwengier davidwengier deleted the RenameRazorSourceGennedDocuments branch July 28, 2025 21:41
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Jul 28, 2025
davidwengier added a commit that referenced this pull request Jul 30, 2025
Follow up to #79604
Part of dotnet/razor#9519

This is the follow up to the above PR to support rename in LSP, but
thought I'd make the API slightly stronger while I was here since we
haven't consumed it on Razor yet. I also misunderstood the span mapping
API, and it needing to return an array of the same length as the input.
davidwengier added a commit to dotnet/razor that referenced this pull request Aug 6, 2025
Fixes #12054
Part of #9519
Needs ~dotnet/roslyn#79604 and~
dotnet/roslyn#79677 to insert before this will
build

This PR implements the workspace version of our span mapping service,
since the old one is hooked up via a document service, which we can't do
in cohosting.
@RikkiGibson RikkiGibson modified the milestones: Next, 18.0 P1 Aug 19, 2025
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.

3 participants