Skip to content

Import-on-paste implementation question #60194

Closed as not planned
Closed as not planned

Description

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 (@imbant) in #59905

Activity

  1. DanielRosenwasser commented on Oct 10, 2024

    @DanielRosenwasser
    MemberAuthor

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

  2. DanielRosenwasser commented on Oct 10, 2024

    @DanielRosenwasser
    MemberAuthor

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

  3. imbant commented on Oct 11, 2024

    @imbant

    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

  4. navya9singh commented on Oct 11, 2024

    @navya9singh
    Member

    imbant (@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!

  5. imbant commented on Oct 12, 2024

    @imbant

    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!

  6. typescript-bot commented on Oct 15, 2024

    @typescript-bot
    Contributor

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

  7. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

DiscussionIssues which may not have code impactQuestionAn issue which isn't directly actionable in code

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions