13

Is it possible to fold large blocks of code in IntellJ, select a section of code that encloses the folded section, and only copy the text that is visible?

For instance, if I have a JSON file like:

{
    "list1" : [
         1,
         2,
         3
     ],
    "list2" : [
         "a",
         "b",
         "c"
     ]
}

and I fold the lists so the IDE displays them as:

{
    "list1" : [...],
    "list2" : [...]
}

Is there a way to copy ONLY the text displayed above? Regular copy includes the code that is hidden from view.

aaroncarsonart
  • 1,054
  • 11
  • 27
  • Note: the use case for this is arbitrarily large files where I want to check the higher level design. I.E. thousands of lines in a file, with many various nested objects and arrays. (I realize in this simple case I can just type out the values) – aaroncarsonart Jul 26 '16 at 22:02

3 Answers3

3

Interestingly, when copying within IDEA, it keeps the folded state (See for instance the screenshot attached to IDEA-139523). This implies that the data is in the clipboard somehow, though perhaps it's only in an IDEA-specific format that other programs can't read.

The general feature request to do what you're asking appears to be in the JetBrains ticket tracking system as IDEA-126233. There's no indication in there of a workaround or when it may get worked on.

Unfortunately, your options seem to be limited here.

  1. You can use an external tool as suggested in another answer to do the formatting you need. Especially if it's limited to JSON formatting, you may be able to find some other standalone tool you can run in your development environment that meets your security and performance requirements.
  2. I recommend voting for that ticket in the JetBrains ticketing system. Might not accomplish a whole lot, but at least let them know that another person would find it useful. If you have further explanation or description that might be related that you think isn't described well enough in the existing ticket, you could add your comments to help flesh it out.
  3. Since the data appears to be there somewhere in the IDEA clipboard since it retains the folding when copying within the application, presumably it'd be possible to add this "Copy as displayed" feature within some sort of plugin. If you really need this and can't find an existing plugin that does it, you may want to see how hard it would be to write this plugin yourself.
  4. If you have a JetBrains support contract, you may want to contact them and see if they can help you further. While I doubt they can do a whole lot more than note your interest in the development of the ticket already in their system, it can't hurt to ask, and perhaps they're aware of some other workaround that would be useful to you.

Sorry for the rather unsatisfying answer, but I suspect it's the best one can have at the moment.

  • Cool, that's exactly what I needed to hear. If it is not available in IntelliJ, than that is the answer I need. I'm glad they're tracking it and it may be available in the future. For now there are work arounds such as other tools at least, even if IntelliJ does not directly support this. – aaroncarsonart Jul 28 '16 at 22:58
2

I don't know how to do it in jetbrains, but here is the way to do in vscode:

Step1: Open your code in vscode

Step2: Cmd + Shift + P (Mac) or Ctrl+Shift+P (Windows) to open Command Palette, search and select Fold All

Step3: Toolbar -> Selection -> Column Selection Mode

Step4: Column select all the code, copy and paste, done!

Shiming
  • 121
  • 6
1

For the use-case you presented, you don't need IntelliJ. Just open the file with another tool that knows how to fold your text correctly and copy it from there.

I did a short search for json online tools and found this one: https://jsonformatter.curiousconcept.com/

You can paste your json in the blue area, fold it how you like in the black area and copy it from the black area.

Notice you have a button to collapse all json nodes.

This is the coping result:

{  
   "list1":[  ],
   "list2":[  ]
}

enter image description here

In the next image you can see how the text in the black area looks like when you copy it:

enter image description here

If you insist you want to do it via IntelliJ there might be a plugin for your type of file that allows it or you can write a new IntelliJ plugin yourself.

from your

mgershen
  • 1,567
  • 16
  • 22
  • 5
    While this would work, it doesn't answer my question. Using an external tool like this may not be the best option if I am working on proprietary software. You never know what these external linters, formatters and diff checkers do with the data you give them. Also, if I copy/paste a 4000 line file, some websites may not be happy and crash. My question requested IntelliJ for example because it is best to not have to leave my super fancy IDE for what should be a simple task. I will check some plugins and update this question later if I find any. – aaroncarsonart Jul 27 '16 at 18:01
  • 2
    Also, the current (on 2021-2-23) web-design of this, unfortunately, the copy/paste (both web-button) and straight highlight/context-click/copy-as-plain-text both end up copying the full uncollapsed json. – Andrew Ward Feb 23 '21 at 19:42