keeping array constants and cases in order in source code

Consider some code like this:

  private static String[] AFFILIATE_DOMAINS = {
            "affiliates.art.com",
            "allposters.com",
            "alt.com",
            "bmtmicro.com",
            "bookbyte.com",
            "buddhagroove.com",
            "cc-dt.com",
            "ccbill.com",
            "cj.com",
            "clickbank.net",
            "clicksor.com",
            "clixgalore.com",
            "dir.webring.com",
            "disktrix.com",
            "dreamhost.com",
            "element5.com",
            "esellerate.net",
            "friendfinder.com",
            "gayfriendfinder.com",
            "gayuniverse.com",
            "greatcall.com",
            "hide-the-ip.com",
            "iconshock.com",
            "jzip.com",
            "linksynergy.com",
            "mycommerce.com",
            "myfonts.com",
            "outpersonals.com",
            "sextoyfun.com",
            "shop.oo-software.com",
            "sitepal.com",
            "spamnix.com",
            "thehubpeople.com",
            "weatherbuffs.com",
            "xara.com",
            "yourfonts.com"
         };

It would be nice if with a few keystrokes I could ensure this list is
sorted either alphabetically case-sensitive or case-insensitive in the
source code, with the commas adjusted.

It should also be possible to sort an array of ints, long, doubles
etc. all with the same click sequence, something you invoke by putting
your mouse on AFFILIATE_DOMAINS and right clicking or keying some
command sequence.

Similarly it should be possible to click on the keyword "switch" and
so long as there is no fallthru, sort the case clauses order by name
of the case enum name or by its numeric value so that all switches use
the same canonical order.

To do this now, I export  source to another editor, sort, and paste in
back.

--
Roedy Green Canadian Mind Products http://mindprod.com
Types of Garbage Collection:
()In Canada, the government sends men to your house every every week
  to take away your garbage. Hoarders are free to hang onto things
  they don't really need.
()In third world countries, it is up to you to take your own garbage away.
()Java's garbage collection system is analogous to a garbage removal
  system where every hour, workers scan your house for junk mail, the
  contents of waste baskets, carpet lint, toenail clippings and anything
  else they are absolutely sure you don't want to keep.
()C++'s system for disposing of unreferenced objects is similar to India's,
  with the strange feature that undiscarded garbage becomes invisible but
  still stinks.

4 comments
Comment actions Permalink

On Fri, 16 Nov 2012 11:09:31 -0800, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

>It should also be possible to sort an array of ints, long, doubles
>etc. all with the same click sequence, something you invoke by putting
>your mouse on AFFILIATE_DOMAINS and right clicking or keying some
>command sequence.

clicking on enum I could request the definitions of the enum constants
be sorted by name.

Code that is not kept in canonical order is much harder to proofread.

when multiple case labels on the same code, sort those alphabetically
first.
--
Roedy Green Canadian Mind Products http://mindprod.com
Types of Garbage Collection:
()In Canada, the government sends men to your house every every week
  to take away your garbage. Hoarders are free to hang onto things
  they don't really need.
()In third world countries, it is up to you to take your own garbage away.
()Java's garbage collection system is analogous to a garbage removal
  system where every hour, workers scan your house for junk mail, the
  contents of waste baskets, carpet lint, toenail clippings and anything
  else they are absolutely sure you don't want to keep.
()C++'s system for disposing of unreferenced objects is similar to India's,
  with the strange feature that undiscarded garbage becomes invisible but
  still stinks.

0
Comment actions Permalink

Either the SortSelection  [A-Z or Z-A] or Line Sorter [A-Z only] plug-in will do some of this for you. You would need to select all the contents of the array, but that is easily done via a couple of Ctrl+W commands. Then sort. Then reformat the selection (ctrl+alt+L). The one thing it won't handle is putting in a missing comma if the  last item gets moved up in the list.But even that can be mitigated by  simply having an ending comma in your array. The following is valid  syntax and creates an array of three elements:

static String[] array = new String[] {
            "a",
            "b",
            "c",
    };



So the sequence is:

  1. Place cursor in list of array elements
  2. Ctrl+W two or three times to select the elements
  3. Sort (either via menu or mapped hotkey)
  4. Ctrl+Alt+L to format
  5. F2 to go to next error to enter the missing comma (if necessary)
0
Comment actions Permalink

On Sat, 17 Nov 2012 06:48:54 MSK, Mark Vedder <no_reply@jetbrains.com>
wrote, quoted or indirectly quoted someone who said :

>Either the http://plugins.intellij.net/plugin/?idea&pluginId=7053   or http://plugins.intellij.net/plugin/?idea&pluginId=5919 plug-in will do some of this for you. You would need to select all the contents of the array, but that is easily done via a couple of CtrlW commands. Then sort. Then reformat the selection (ctrlalt+L). The one thing it won't handle is putting in a missing comma if the  last item gets moved up in the list.But even that can be mitigated by  simply having an ending comma in your array. The following is valid  syntax and creates an array of three elements:

Thanks. I had to drop the last letter of the URL
http://plugins.intellij.net/plugin/?idea&pluginId=7053

It sorts case sensitively.
You must have your columns perfectly aligned first. It does not trim.
As a corollary, when you select, you must include all the lead spaces
on the first line.
As you would expect, your strings must fit on a single line.
--
Roedy Green Canadian Mind Products http://mindprod.com
Types of Garbage Collection:
()In Canada, the government sends men to your house every every week
  to take away your garbage. Hoarders are free to hang onto things
  they don't really need.
()In third world countries, it is up to you to take your own garbage away.
()Java's garbage collection system is analogous to a garbage removal
  system where every hour, workers scan your house for junk mail, the
  contents of waste baskets, carpet lint, toenail clippings and anything
  else they are absolutely sure you don't want to keep.
()C++'s system for disposing of unreferenced objects is similar to India's,
  with the strange feature that undiscarded garbage becomes invisible but
  still stinks.

0
Comment actions Permalink

On Sat, 17 Nov 2012 06:48:54 MSK, Mark Vedder <no_reply@jetbrains.com>
wrote, quoted or indirectly quoted someone who said :

>Either the http://plugins.intellij.net/plugin/?idea&pluginId=7053   or http://plugins.intellij.net/plugin/?idea&pluginId=5919 plug-in will do some of this for you. You would need to select all the contents of the array, but that is easily done via a couple of CtrlW commands. Then sort. Then reformat the selection (ctrlalt+L). The one thing it won't handle is putting in a missing comma if the  last item gets moved up in the list.But even that can be mitigated by  simply having an ending comma in your array. The following is valid  syntax and creates an array of three elements:

We programmers are a very conservative lot.  We keeping thinking of
programs as strings of ascii text and think about source code
manipulations in those terms.

We are very gradually shifting to thinking of a source program as a
tree, where you do operations on nodes.  I have been pushing for this
since the 1970s. Some early attempts at scids burned programmers and
has slowed evolution.  see http://mindprod.com/project/scid.html


--
Roedy Green Canadian Mind Products http://mindprod.com
Types of Garbage Collection:
()In Canada, the government sends men to your house every every week
  to take away your garbage. Hoarders are free to hang onto things
  they don't really need.
()In third world countries, it is up to you to take your own garbage away.
()Java's garbage collection system is analogous to a garbage removal
  system where every hour, workers scan your house for junk mail, the
  contents of waste baskets, carpet lint, toenail clippings and anything
  else they are absolutely sure you don't want to keep.
()C++'s system for disposing of unreferenced objects is similar to India's,
  with the strange feature that undiscarded garbage becomes invisible but
  still stinks.

0

Please sign in to leave a comment.