microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.94k stars 12.47k forks source link

Add `preparePaste` method to check if copied text should potentially have smart paste enabled or not #59881

Closed mjbvz closed 1 month ago

mjbvz commented 1 month ago

🔍 Search Terms

✅ Viability Checklist

⭐ Suggestion

Add a new preparePasteEdits api that lets editors quickly check if copied code may have a paste edit or not. preparePasteEdits would be called on copy, while today getPasteEdits is called on paste

This would align with the VS Code paste API proposal: https://github.com/microsoft/vscode/blob/cbcf121deb9f7195e7dc1d63b298b98b254bba5a/src/vscode-dts/vscode.proposed.documentPaste.d.ts#L85

📃 Motivating Example

On paste, VS Code shows UI that lets users select how the content should be pasted. For TS, we now show a paste with imports:

Image

In order to keep the editor response, we need to show this paste UI quickly after paste. However the TS paste edit may take some time. To mitigate this, we've:

However the UX for TS pasting is still too slow to enable by default. A big cause of this is that paste with imports gets triggered even when pasting source that does not have any importable symbols, such as text from a comment or string. We should be able to determine this when copying and avoid showing paste with import at all in these cases

Here's what I think the vscode <-> TS flow should look like:

mjbvz commented 1 month ago

I'm not sure it's needed, but we could also add a resolvePasteEdit call that let us delay computing the workspace edit. This would align with how the VS Code copy paste api is designed

As a quick overview of how the VS Code paste api works:

The main use case for the resolve step is for when you know a paste edit exists but determining how to apply it takes time. A good example is if we need to go out to a llm to get the edit. I'm not sure if this is a perf issue for TS too