Skip to content

Support smart escaping of text when pasting into string expressions (part2).#61111

Merged
CyrusNajmabadi merged 87 commits intodotnet:mainfrom
CyrusNajmabadi:stringCopyPaste2
May 7, 2022
Merged

Support smart escaping of text when pasting into string expressions (part2).#61111
CyrusNajmabadi merged 87 commits intodotnet:mainfrom
CyrusNajmabadi:stringCopyPaste2

Conversation

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

Followup to #60036. This adds support for understanding copy/paste from an existing string literal.

Initial support for a smart-string-copy-paste system. The general ideas here are:

  1. if you copy code from VS, we can smartly determine what the original context was, to determine how to interpret characters. For example, if the user copies "" from inside a verbatim string (like @" goo "" bar "" baz "), then we can know this represents a single quote character.
  2. when pasting code into a string literal we can use the above information to smartly determine to if we should escape the contents, and how to escape them. For example, if we see that we are pasting text whose value is a single quote character into a verbatim string, then we can double the quote to keep things legal. This allows copy/paste across different string types, while having the contents of the string stay the same, even if the escaping changes.
  3. if pasting come in from an external source, we can't know the context of the text to know what values it actually has. However, we can still ensure that things are properly escaped to keep code parsable.

For all of the above, we ensure that any special work we do happens as a second edit transaction. THat way if the user ever doesn't want the contextual-pasting result, they can undo and get back to exactly the code that pastes normally.

This PR effectively adds the italicized item in this table:

First position of copied region Pasting outside a literal Pasting inside a literal
Inside a text literal Unescape on paste Correct escape sequences that change meaning
Outside a text literal As-is Escape on paste
Raw text (e.g. browser) As-is Escape on paste

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

@davidwengier This is has been substantially rewritten now based on your suggestion. The general approach now is that when the cut/copy command goes through we capture the 'processed' information we need about the string literal the user copied from. Then, when a paste occurs, we use that information directly instead of trying to back infer what the text changes would have meant in the original document.


using static StringCopyPasteHelpers;

internal readonly struct StringInfo
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this is information we were computing before, but storing as separate fields. i aggregated into into a single type to keep things cleaner.

@"var x = $""""""""
""""""
""[||]""""""",
[||]""""""""",
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this is an improvement over the prior behavior. the caret now stays before the delimiters instead of moving partway into the closing delimiters.

@CyrusNajmabadi CyrusNajmabadi enabled auto-merge May 6, 2022 23:15
@CyrusNajmabadi CyrusNajmabadi merged commit 1172338 into dotnet:main May 7, 2022
@ghost ghost added this to the Next milestone May 7, 2022
@CyrusNajmabadi CyrusNajmabadi deleted the stringCopyPaste2 branch May 7, 2022 01:58
@Cosifne Cosifne modified the milestones: Next, 17.3 P2 May 31, 2022
@NXTwoThou
Copy link
Copy Markdown

How does one -disable- this feature. It's been a usability nightmare for me. Sure, the feature is "cool", but it's not always wanted(in my case, -very- infrequently desired). Changes to basic functions absolutely require options to be able to enable or disable. I can't find anything in VS17.3.6 options for preventing the behavior where on every ctrl+v I have to do a ctrl+z to get what I intended to paste.

Please add a "Smart escape characters on paste” option so pasting behavior can revert back for those of us that need it. Also introduce “Edit->Paste Special->Smart escape paste” for those of us that do disable the feature for ctrl+v, but have opportunities where it would be beneficial.

VS Developer community

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

Disable 'fix text pasted into string literals':

image

@NXTwoThou
Copy link
Copy Markdown

Disable 'fix text pasted into string literals':

Yay! Exactly what I needed. Thank you.

That brings up a different issue then. That option doesn't appear in "Search Options" I typed in "paste"(I tried several different searches looking for an option to disable). It is because it's experimental?

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

Not sure. @davidwengier i think you added some sort of searching support to Tools | Options. Do you know why it wouldn't be picking this up?

@davidwengier
Copy link
Copy Markdown
Member

I made the search term highlight on the page, if the page shows up in the search results, but sadly the act of showing up in the search results is 20 year old technology. The search matches on any word in this resource string which needs to be manually updated when a new option is added.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants