Skip to content
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

Import-on-paste implementation question #60194

Closed
DanielRosenwasser opened this issue Oct 10, 2024 · 6 comments
Closed

Import-on-paste implementation question #60194

DanielRosenwasser opened this issue Oct 10, 2024 · 6 comments
Assignees
Labels
Discussion Issues which may not have code impact Question An issue which isn't directly actionable in code

Comments

@DanielRosenwasser
Copy link
Member

Hi, I'm a language server developer. I am very curious about how to implement the Import-on-Paste function you mentioned above?
Here is my idea, is it feasible? (sorry but reading ts source code is a bit difficult for me)

  1. Before pasting the text in the clipboard into the file, maintain a string of the pasted text in the memory
  2. Compile the new code in this memory
  3. If there is no syntax error, if an undefined symbol error is encountered in the line where the paste is located, try to do automatic completion at the location of the symbol
  4. If there is an automatic completion item that completely matches the text of the symbol, complete the import statement corresponding to this item (the same as the behavior of the user manually selecting this completion item)
  5. Repeat 2 to 5 until there is no such error, or there is no completely matching text
  6. Overwrite the text string in the memory into the file to be pasted

Originally posted by @imbant in #59905

@DanielRosenwasser DanielRosenwasser added Question An issue which isn't directly actionable in code Discussion Issues which may not have code impact labels Oct 10, 2024
@DanielRosenwasser
Copy link
Member Author

@navya9singh implemented #50187 at #57262, and I think she can give some details on how the current heuristics work.

@DanielRosenwasser
Copy link
Member Author

Also, are you asking in the context of Vue on Volar?

@imbant
Copy link

imbant commented Oct 11, 2024

Thanks for your help.
Yes I also post that in volar's discord, but I'm working on a different language server. I just wanted to ask someone who knows how to do this, so I use discord and GitHub

@navya9singh
Copy link
Member

@imbant you're idea looks good to me. That's mostly how we do it too. Just to give you some more details:

  1. You would want to have the string of the copied text and if you want, the location of where it was copied from, from the editor.
  2. To make it easy for us to see which symbols(in the copied text) need to be resolved, we add the text to the targeted file and update files for that project and check which symbols can be resolved and which cannot. This is done because it's not possible to get diagnostics only from a string of copied text.
  3. For the symbols that cannot be resolved, we add imports for them.
  4. Then the changes in the targeted file are reverted and tsserver returns the copied text, along with the imports to the editor.

Hope this help and please feel free to ask more questions if you'd like!

@imbant
Copy link

imbant commented Oct 12, 2024

Wow thank you very much for your support and help. I am glad that my idea is correct. This's very inspiring for my own language server.

Look forward to using import-on-paste in VS Code!

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Discussion Issues which may not have code impact Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants