-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bring along imports when copy and pasting code across files #50187
Comments
The Copy With Imports extension seems to work pretty well at addressing this issue. Thanks to @lukeapage for mentioning this extension in microsoft/vscode#30066. |
The Copy With Imports extension works by overriding the copy and paste keybindings in a kind of hacky way. And having VSCode add an extension into copying introduces a delay that can result in the wrong line of code being copied if you don't wait for the copy command to be invoked. Copy With Imports as an extension is an ok stopgap. But this should definitely be handled natively in TypeScript and VSCode. |
@dantman Ah, good to know. Thanks for the info. Hopefully, native support will be added soon. It seems like a good sign that the TypeScript 5.3 Iteration Plan mentions "Investigate Carrying Imports Across Paste". |
|
So, can we also support |
Feature request
From microsoft/vscode#66236
When copy and pasting code between two TS/JS files, we should support adding the required imports on paste. For example, with the following code:
If I copy
fs.readFileSync('bla')
and paste it into a new file, an import forfs
should also be added to the top of that file:TS Server Protocol changes
VS Code is actively working on an API that lets extensions hook into copy and paste: microsoft/vscode#30066
Using this api, here's what I imagine the flow between VS Code and TS server would look like:
User copies code
VS Code tells TS Server about the copied range
TS Server goes and writes whatever state it needs to re-create the imports. This state needs to be JSON serializable (so that TS Server can send it to VS Code). Just as an example, this could be something like (note! just for demonstration purposes, not intended to be what this actually looks like):
TS Server returns this state to the VS Code extension which attaches it to the copied text (this can happen asynchronously)
The user now pastes the code in another file
VS Code sends TS Server a request with the following:
TS Server returns a text edit that adds the imports to the file
The text was updated successfully, but these errors were encountered: