Skip to content

Convert generic brackets to braces when pasting into cref - #85787

Merged
jasonmalinowski merged 3 commits into
dotnet:mainfrom
Davide244:Davide244/Implement19197
Oct 2, 2026
Merged

jasonmalinowski merged 3 commits into
dotnet:mainfrom
Davide244:Davide244/Implement19197

Conversation

@Davide244

@Davide244 Davide244 commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #19197

When pasting into the value of a cref attribute in a C# documentation comment, < and > in the pasted text are converted to { and }.

/// <see cref="$$"/>        // paste: Dictionary<string, List<int>>
/// <see cref="Dictionary{string, List{int}}"/>

Implementation

New CrefPasteCommandHandler (C# only), modeled on StringCopyPasteCommandHandler:

  • Lets the normal paste go through, then applies the fix as a separate undo transaction, so a single undo restores the raw pasted text.
  • Only acts when every edit from the paste lies inside a single cref attribute value (between the quotes).
  • Does nothing if the pasted text contains quotes or line breaks, or if another component also edited the buffer.

VB is not included, since (Of T) is already valid in crefs.

Tests

CrefPasteCommandHandlerTests covers empty and partially filled crefs, nested generics, generic methods, pasting over a selection, single-quoted crefs and undo. Negative cases: doc comment text, other attributes, before the attribute value, pasted quotes, regular code and string literals.

Microsoft Reviewers: Open in CodeFlow

@Davide244
Davide244 requested a review from a team as a code owner September 25, 2026 14:01
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service dotnet-policy-service Bot added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Sep 25, 2026
@akhera99

This comment was marked as resolved.

@jasonmalinowski jasonmalinowski self-assigned this Oct 1, 2026
@Davide244

Copy link
Copy Markdown
Contributor Author

This looks really good! You mentioned adding an option to disable in the issue linked. I feel that this should be the default behavior, but an opt-out option would follow the precedent for existing features, like "format on paste" and "add import on paste".

Thanks! Agreed, I'll add an opt-out option (so default on) and follow the existing dotnet_fix_string_contents_on_paste setting (a PerLanguageOption2 checked by the handler, with a checkbox on the C# Advanced options page and VS options storage/automation registration)

@jasonmalinowski jasonmalinowski 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.

Great feature! some small tweaks/additions for tests, but this is otherwise quite close.

@Davide244

Copy link
Copy Markdown
Contributor Author

I have added the opt-out option csharp_fix_cref_on_paste (default on) with a checkbox under Tools > Options > C# > Advanced, next to "Fix text pasted into string literals".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee. Needs UX Triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pasting generic type names in doc comments should escape '<' and '>'

3 participants