Is there a way to convert multiple variable declarations to one per line?
Asked Answered
M

5

13

In Eclipse, is there an automatic way to convert this:

int myX, myY, myZ;

... to this...

int myX;
int myY;
int myZ;

I thought I could do this from the 'Clean Up' and 'Members Sort Order' preferences, but it does not break up the multiple declarations into one per line. Is there a way to do it?

Edit: To clarify, this is in regards to member variables.

Mongeau answered 12/3, 2014 at 18:2 Comment(8)
i dont think format will do this for you, tho you might be able to create a formatting rule (you can do that right?)Holytide
Do you insist on an Eclipse answer?Life
@IraBaxter I'm most interested in a way to do it via Eclipse, but if another IDE is the only way, I'd still like to hear it.Mongeau
How many times would you have to do this? i.e. how many variables are you doing this to?Faxun
What is your purpose of having a tool that accomplishes this? (i.e. why do you want variable declarations on separate lines?)Marchellemarcher
@Ryan One-per-line abides by Java conventions (see the answer below that states Checkstyle will flag multiple declarations). It's considered to have less potential for confusion.Mongeau
@Faxun Thousands across hundreds of class files.Mongeau
@whiskeyspider Unfortunately I cannot be of much help as I've never used Eclipse, but I am with you in spirit as you stare down this great task.Faxun
V
7

While I don't know if a single function which does this, you can obtain similar results with a few keystrokes:

  1. Highlight the line which contains the multiple variable definitions, and use the Find/Replace function (keystroke: Ctrl-F) to replace the comma-space sequence to a semicolon, space and the type of the variable. Make sure the scope is set to "selected lines" as in the following screenshot:

    Find replace dialog

  2. Now you'll still have a line with all the variable declarations in a single line. This can be fixed by highlighting the same line and the Source > Format function (keystroke: Shift-Ctrl-F).

    Source format menu

  3. All done, now you have your variables with their respective type, one per line:

    All done

Vitiligo answered 14/7, 2014 at 22:18 Comment(5)
This method is useless in anything larger then the most trivial of files.Channel
I'd make the find and replace step a regex that looks for comma plus optional whitespace.Licastro
I think @E-Riz answer would ultimately be best solution, but this worked as a fast, dead-simple fix for this issue. Thanks.Mongeau
@whiskeyspider: I think you'll find it pretty hard to use this method for "Thousands across hundreds of class files." :)Myriammyriameter
@Myriammyriameter It's not a solution that will work automatically across all of the classes, but on a class-by-class basis -- since I already had a formatter ready -- it did save time.Mongeau
P
6

There is no built-in Clean Up or Formatter option to do that (which surprises me). However, writing a plugin to do it would not be terribly difficult. For reference, you'd start with this extension point. The class org.eclipse.jdt.internal.ui.fix.VariableDeclarationCleanUp looks like a potential one to study as an example and guide.

Prerogative answered 12/7, 2014 at 18:24 Comment(0)
U
3

Eclipse does not provide this formatting feature out of the box. You can use Checkstyle plugin to check for this and many more coding standards.

In Checkstyle configuration file, use module MultipleVariableDeclarations like this,

<module name="Checker">
    <module name="TreeWalker">
        <property name="cacheFile" value="target/cachefile"/>
        <property name="tabWidth" value="4"/>
        ...
        <module name="MultipleVariableDeclarations"/>
        ...
    </module>
</module>

If you are OK with command line tool, then astyle and GC GreatCode are two good code formatters.

GC GreatCode is given as C/C++ code formatter but it will format your code in this scenario well.

In GC the option to split variable declaration is -code_split_decl_style

Original code:

String fileName, a, b, c;

With -code_split_decl_style-1 option:

String  fileName,
        a,
        b,
        c;

With -code_split_decl_style-2 option:

String fileName;
String a;
String b;
String c;

You can play around with different command line formatting tools and configurations for various languages in one GUI tool using UniversalIndentGUI.


Checkstyle: http://checkstyle.sourceforge.net/

Astyle: astyle.sourceforge.net/

GC GreateCode: http://sourceforge.net/projects/gcgreatcode/

UniversalIndentGUI: universalindent.sourceforge.net/

Underrate answered 17/7, 2014 at 10:51 Comment(0)
M
2

Eclipse doesn't seem to provide a function for this, and I haven't found a plugin that does this. But assuming that you have access to awk, you might find this little awk script helpful:

{
  if (match($0,/([a-z|A-Z|0-9|_]+)\s+(([a-z|A-Z|0-9|_]+)\s*,\s*)+([a-z|A-Z|0-9|_]+);/)) {
    match($0,/(\s*[a-z|A-Z|0-9|_]+)\s+([^;]*)/,components);
    split(components[2],vars,",");
    for(i in vars) {
      gsub (/'[[:cntrl:]]|[[:space:]]|\x20/,"",vars[i])
      print components[1],vars[i]";"
    }
  }
  else {
   print $0;
  }
}

It will do the required job. Assuming you save it to the file script.awk you would call it like this

awk -f script.awk target_file.java

NOTE: you must direct the output to an alternate file or your source will be overwritten (emptied)

So if you want to process a number of files at one go you should do it like this (assuming a bash shell):

for f in *.java; do awk -f awk1.awk $f > $f.pr; done

For each java file this will create a new file with the ending .java.pr, and then you just move those over into files with a .java ending.

================== EDIT =================

Just want to add that there exist plug-ins for Eclipse that support bash/awk scripts over a selected region. See here:

http://eclipseexeditor.sourceforge.net/ (probably to old for newer versions of Eclipse)

and here also:

Bash script plugin for Eclipse?.

So, my script coupled with one of those plug-ins would presumably add the requested feature to Eclipse.

Myriammyriameter answered 17/7, 2014 at 17:40 Comment(0)
P
0

There is no out of box such feature in Eclipse that I know of.

I assume you are a java programmer and hence your best bet would be to write a simple program that will parse out source code for such pattern and convert it to multi-line as desired. Then run clean up on top of it to have it properly formatted as required.

Powerful answered 14/7, 2014 at 21:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.