I would like to copy code to the clipboard from a ...
# intellij
t
I would like to copy code to the clipboard from a web app and then paste it into IntelliJ. Would it be possible to add the import information to the code somehow so IntelliJ automatically adds the imports as well?
g
I see this settings, at least in the last Ultimate I believe it's the default 🤔
thank you color 1
t
I'll check this out. I'm using Kotlin, so I'll have to see if this setting works with it as well.
g
I'm using it too nod
thank you color 1
a
Unambiguous imports on the fly
would work when single class by short name is available. Otherwise, IDEA won't know where to take fully qualified names.
w
How does it know the imports when copying the code from IJ to IJ? Like if I copy the same code between files, IJ seems to remember the imports. Is the imports data carried somehow in the clipboard metadata, or it's done in the IDE internally? Or it doesn't work like that and I'm imagining things? 😄
t
Yes, this is why I asked. 🙂 In IntelliJ it works well when moving code around. Would be nice to have the same when I copy sample code from the web. Of course, I do have fully qualified names in the web samples as well, so it would be possible to build clipboard data the correct way if there is one.
a
When it's inside IDE, we remember the FQNs in "metadata". If it's from web, then there is no place to store that additional data
t
My framework uses actual, runnable code to provide the samples, so the qualified names are actually checked by the compiler during compilation.
a
if you copy fragment with FQNames, then they would be preserved/optimized to imports
thank you color 1
t
Hm, I'll give that a try, should work if there are no .* imports.
w
we remember the FQNs in "metadata".
my question is, is the metadata stored in the IDE or in the clipboard data? I suspect it's in the IDE
a
IDE provides this data to clipboard, so another IDE instance can grab it as well. Not sure you were asking about those dirty implementation details though
w
Yep that's what I was asking, I'm saying that if the web app provided the same information to clipboard (basically copied the implementation details), an IJ instance would be able to paste code with imports as well
a
It's overcomplicated to simulate IDE here, just my opinion of cause
kodee sad 1
a
I've looked into this before, because I think it would be a clever solution. Like @Anna Kozlova says, IntelliJ uses DataFlavor to encode extra metadata to the clipboard. It's quite hard to view this extra metadata. • The metadata doesn't seem to be available when pasting into websites like https://evercoder.github.io/clipboard-inspector/. I can see it when I use this util: https://github.com/sindresorhus/Pasteboard-Viewer • The encoded data is saved using Java Serialisation, so again, it's hard to see (see screenshot 2). • iirc IJ doesn't literally encode the required imports into the metadata, it just encodes a reference to the original file of the copied code. https://github.com/JetBrains/intellij-community/blob/70bceadfce1481b7b582b6c95f2bc7d8e3417675/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinCopyPasteReferenceProcessor.kt#L114C13-L114C20 So, also I think it would be hard to reimplement this directly.
👌 1
🙌 1
I think an easier solution would be for IntelliJ (or Kotlin plugin) to automatically merge imports of a pasted code to the top of the file. So, if you copied:
Copy code
import java.nio.file.Path

class Foo {
  fun bar(file: Path) {
    println(file)
  }
}
Currently, if you paste it at the end of an existing file, the code will be invalid because the imports need to be at the top. But IJ could just automatically merge the pasted imports with the existing imports. I made a similar request for kts scripts, because it would help with Gradle scripts https://youtrack.jetbrains.com/issue/KTIJ-30687/K1-IDE-Automatically-move-import-statement-to-the-top-of-the-file-on-paste-in-scripts
2
Another idea: update KotlinCopyPasteReferenceProcessor.kt to also support custom import metadata (i.e. just a list of imports) that can be manually specified by the web app.
t
Hm, seems indeed complicated to use the internal format. I guess I'll just copy the imports manually for now.