Prevent IntelliJ from removing newlines around single statement methods? - java

Sometimes IntelliJ will take single statement methods and put them on the same line, as seen here:
If I click either bracket it will turn back into:
This question isn't a debate for which of the two is better - I want to know how to turn this off. I combed through IntelliJ preferences and couldn't seem to find anything, and searching both here and Google didn't bring up what I wanted.

I think these options are responsible for this:

If you're talking about code reformatting putting the extra newlines into your code, then it's the "Simple methods in one line" option you want to check in Editor/Code Style/Scala/Wrapping and Braces:

Related

Eclipse: Replace multiple, different regexes simultaneously

I'm currently trying to replace an old (java) testing framework with a different, although similar, one. Therefore, as most occurences of old framework code are the same 90% of the time (in the sense of identic variable names, parameter types etc.), replacing them is pretty straightforward and rather repetitive.
Therefore, I wrote myself a few regex matches (which work just fine, they are not the focus of this question).
However, I have a rather large number of different test files, and I already at this point - just having started - have 6 different match/replacement-pairs that I would like to apply.
Now obviously, being a computer scientist, I would love to automate this, instead of going through every file, pressing Ctrl+F, pasting the matching regex, pasting the replacement regex, pressing the replace button, repeating this cycle 5 more time, and then moving to the next file.
So, let's say for the sake of simplicity, that these are my regexes:
//matches the existing framework
OldClass (.*?) = new OldClass("string");
//replacement regex:
NewClass $1 = new NewClass("string");
//example replacement:
OldClass foo = new OldClass("string");
//becomes:
NewClass foo = new NewClass("string");
So, If I want to replace several of these match/replace-pairs in lots of different files - can I use any built-in eclipse function, or is there an extension that provides this functionality?
Note that I'm aware that I could write a simply java program that just skims through all my source code and applies the regexes as desired.
I'd much rather avoid spending that time, though, and especially would also like to get a chance to apply them individually to each file, so I can re-run the tests afterwards and make sure nothing is broken - which will happen, as not all of the old framework code can be replaced automatically, due to too complex & specific cases. Since I'm also removing the old imports, though, this will break any still existing non-replaced code relying on now-no-longer-existing imports.
Eclipse should have a simple file search option with a "Replace.." button at the bottom. You can search as you would normally specifying the file endings that you'd like to search (probably in this case you'd want *.java). The replace button lets you replace each search result with a replacement using regular expressions.
Granted, this will change your source one replace at a time and that is awkward I know, but my recommendation is to perform small steps, minimizing time in which your code is broken. For instance if you move your class to a new location with a new name, just focus on renaming the class first (verifying that the code then works afterwards), and only then focus on changing its package.
Word to the wise, click Preview first!
Alternatively, consider using ctrl+shift+R to rename methods/variables/classes. Assuming the code is under a source folder, it will automatically rename everywhere it is used. Generally it is preferable to using regular expressions. But again, you can't perform multiple changes at the same time. Though this is probably for the best. Just make a backup of the project and organize the changes that need to be made before starting.
Good luck!

IntelliJ - Coding style

Sorry for the rather broad question, but how do I set IntelliJ to wrap long method calls like this:
The style by having methods aligned with each other on new lines.
This is what happens after building the project/reopening IntelliJ:
It works ok in my case, even if a line is bigger than the limit (after aplying reformatting):
This is the default behaviour (at least in Idea 15.0.1). If I were you, I would reset code styling settings (previously saved it) and start applying your changes one by one, until you find what's up.
FYI here's my settings that should work in your case as well:

Remove unnecessary braces

Is there's a way in NetBeans to remove unnecessary braces in one-line statements?
I would like to convert something like this:
if (something) {
doSomething();
}
to
if (something) doSomething();
I do this manually all the time, but sometimes I have to format the code for some other reasons using alt+shift+f command and netbeans adds those braces again.
If is not possible, Is there a way to tell the formatter not to add those braces when I press the alt+shift+f command? I know Eclipse do this, and I would like to know if Netbeans can.
I have read this and this answers, and apparently it can't be done, but both are php oriented; this apply to java too?
I'm using Netbeans 8.0. If you don't have the exact same version, hopefully you'll find that the following steps are similar enough to make it work for you.
Go to Tools --> Options.
Click on the top Editor button.
Go to Formatting tab.
Make sure the Language combo box has Java selected.
Modify the value of the Category combo box to Braces.
Under Braces Generation, change the value for if: to Eliminate.
After you apply these changes, formatting the code will correctly remove the braces for single line statements, and leave them for multi line statements.
You can do the same for other keywords like for or while if you want.
EDIT
Since a few heated comments were made on the question itself about the validity of such a request, I'd like to make a comment of my own on the topic of the practice of coding if-else statements without braces.
I personally always use braces, even if it means typing a little more, and wasting a little more screen space. As already mentioned, it is so much safer to do so. The clarity to the code it provides far outweighs any cosmetic benefits.
That said, I respect that it is still a matter of personal preference, especially if you are the only one maintaining the code. Also, if you have the good habit of auto-formatting your code, then that will minimize any risks associated with misinterpreting the blocks of codes without braces, because the auto-formatter will indent the code appropriately.
But if you work in a team environment, I think it would be very considerate on your part to consider leaving the braces in.
Possible workaround:
You can use the formatter on just certain sections of the code. Highlight just the function you want format.

How to compare two identical methods in eclipse?

I'm doing android app from a book, for some reason the exact method source code I wrote myself does not work as expected and I am trying to debug it.
I have two exact chunks of code, my method and the sample method.
How to compare them in eclipse?
Select both files by clicking the first, then while holding CTRL click on the second.
Now both of them got selected.
Now click one of them (doesn't matter which one) with the right mouse button.
From the appearing context menu choose:
Compare
Each other
Now you can do a text compare.
Did I get right that neither of the sample method nor your method do what they should?
Then there are two possibilities why the code won't do what it should:
The book is obsolet
You made something wrong
either way, google for your specific problem, maybe someone else has encountered it as well and already solved it.
for your Question: already answered in another comment
use beyond compare, it's a great tool for comparing classes, and methods! download beyond compare

Is there an easy way to print a graphical menu to console in Java?

So this is for an assignment (which I've already completed), I just left this part out because it seemed like a pain in the ass while I was working on the pattern logic.
The assignment asks that you print this menu graphic to help the user decide which pattern to pick. Is there an easy way to do it or do I just need to get a ton of printf/println statements in there? Seems like a very awkward thing to code. Here's an example:
EDIT: This is just for the graphical menu. I know I have to use loops for the actual patterns (which I've already done). The assignment is essentially finished, just missing this menu. I wasn't sure how best to print out this graphic horizontally without awkwardly formatting it by hand.
Text UI has been implemented so many times. Instead of spending some time trying to develop yet another text-based selection/navigation components try to use available solutions:
Fully featured text UI -- Lanterna
Shell-like approach -- JLine
There is another interesting answer, which provides two other, but not so good options.
I think it depends on whether you're going to be assessed on it.
My engineering head tells me that if you're not going to be assessed on it, getting the formatting correct is going to be fiddly and I would just reduce it down to 5 or so println()s, the implementation of which is nothing more than typing.
There's nothing to be ashamed of in such circumstances by choosing what appears to be a trivial exercise. Of course if part 2 of your assessment asks you to extend this to 6 lines (or similar) then a more extensible solution would be appropriate.
I note (following your edit) that you have pattern methods to generate the above. In that case you may wish to modify their inputs/outputs appropriately to facilitate the above. e.g. perhaps they could take in an array of 'n' lines, and append the pattern to those lines, line by line. You'd also need some justification method to pad those lines for the next pattern generator.

Categories