Intellij IDEA Auto-Comments after each code block ( "}" character) - java

void main(){
runApp(MaterialApp(
home: Scaffold(
), //Scaffold
)); //MaterialApp
}
I didn't add comments, IDE did and I don't want to. How can I turn it off?

Settings -> Editor -> General -> Appearance, check box 'Show closing labels'.
Restart IDEA

Related

IntelliJ: foreach Live Template code formatting

In IntelliJ idea when I insert the foreach live template it will put newline after ':' so it will look like this:
for ( :
) {
}
I want to have the for statement on one line like this:
for ( : ) {
}
I tried to change my code formatting preferences, but could not figure out what setting influences this particular case.
So my question is how to set code style options to achieve the desired behavior?
Use the iter live template rather than the foreach. foreach is under the Android block, and the default style for that is what adds the newline.
Update:
As of at least 2018.1.1 (not sure when it was added), you can now type the <name of your collection>.for then tab and it will expand out into a foreach loop.
It's also brought in the same surrounding/expansion for stuff like <array>.stream then tab and probably a few others I'm not aware of.
Go to File -> Settings -> Editor -> Code Style -> Live Template.
At the right side open Android list and stay on foreach .
In the Options area uncheck Reformat according to style.
You can see how to do it in the IntelliJ IDEA settings foreach style
You can change the template for the enhanced for loop in IntelliJ by changing the setting in Live Templates.
Go to File -> Settings -> Editor -> Live Templates. In the right side, choose iterations -> "iter (Iterate Iterable | Array in J2SDK 5.0 syntax)". At the bottom you can see the template text and you can change it by introducing the newline where you want it. Change
for ($ELEMENT_TYPE$ $VAR$ : $ITERABLE_TYPE$) {
$END$
}
to
for ($ELEMENT_TYPE$ $VAR$ :
$ITERABLE_TYPE$) {
$END$
}
and apply your changes.
In the source code editor, choose Code -> Insert Live Template... -> iter, then IntelliJ will insert the code template as you've specified, with boxes around the variable names for changing them.
for (String arg :
args)
{
}

Eclipse Java Formatter - Newline before closing parentheses

Is it possible to configure the Eclipse Java Formatter to insert a newline before the closing parenthesis of expressions, method declarations, and method calls?
Notice the desired behavior on the lines marked with "XX" below.
=method declaration=
private static void fun1(
int param1,
String param2
){ // XX - notice this is on a new line
}
=method call=
fun1(
1,
"param2"
); // XX
=expression=
int a = (
(
(
1 + 2
)*23 // XX
) - 4 // XX
); // XX
The first Eclipse Integration Build allowing this is: Eclipse Neon 4.6 M7 Build id: I20160427-2000
To get closing parenthesis on new lines change the option
"Windows" -> "Preferences" -> "Java" -> "Code Style" -> "Formatter" -> "Edit..." -> "Parentheses" -> "Parentheses Positions" -> "Method declaration"
to
"Separate lines if not empty"
This will make the examples "method declaration" and "method call" work.
I'm not sure if the "expression" example can be made to work yet.
I was trying to figure this out as well and found that this may help:
Windows" -> "Preferences" -> "Java" -> "Code Style" -> "Formatter" -> "Edit..." -> "Parentheses"
Set "Method Declaration" (beginning of line) to "Separated lines if Wrapped"
Set "Method Invocation" (End of line) to "Separated lines if Wrapped"
Then got to "Line Wrapping" -> "Method Declarations"
Set "Parameters" to "Wrap where Necessary" and "Force Split, Even if line shorter than maximum line width."
Then got to "Line Wrapping" -> "Function Declarations"
Set "Arguments" to "Wrap where Necessary" and "Force Split, Even if line shorter than maximum line width."
Set "Object allocation arguments" to "Wrap where Necessary" and "Force Split, Even if line shorter than maximum line width."
Then got to "Line Wrapping" -> "Expressions"
Set "Binary Expressions" to "Wrap where Necessary", "Force Split, Even if line shorter than maximum line width.", and "Wrap Before Operator"
Hope this works for you. Take care.

Removing whitespace from an 'if else' block with eclipse formatter

I currently have Eclipse formatter set up to format an 'if-else' statement like so:
if(condition) {
return foo;
}
else{
return bar;
}
Note the space between the closing parenthesis of the condition, and the opening bracket of the true block - I'd like to remove this space.
In the formatter profile, under Whitespace -> Control statements -> 'if else', there is no option for 'after closing parenthesis'. In the Blocks section, there is an option for 'before opening brace', however this appears to only apply to the false block (and is turned off anyway).
I must be missing an option somewhere - how can I turn this whitespace off?
I am using Eclipse Mars 4.5.0.
You can do that in the Windows -> Preferences -> Java -> Code Style ->formatter -> Edit profile section
I think it does once you select the other option in drop down that you can see below
This is due to Eclipse Bug # 471145 (specific to Mars / 4.5.0), which was reported on 2015-06-26, fixed on 2015-08-01, and will be included in Eclipse 4.5.1 - which should be complete and ready for download by the end of September 2015 as part of the Mars coordinated service release.
Go to Windows > Preferences > Java > Code Style > Formatter > White Space and in Control statements > Blocks, unselect the "before opening brace" option :)

What is the Eclipse equivalent of IntelliJ "Live templates"?

I mean stuff like typing "iter" and getting a "for" loop with a choice of what variable to iterate on , typing "soutv" to generate a "System.out.println" with the "variable=" already in ...
Thanks !
It is called Templates and it's found under,
Window → Preferences → Java → Editor → Templates
The "soutv" template does not exist ("sysout" does, and it's similar), but it's easy to add. I used this pattern:
System.out.println("variable=" + ${cursor}${});
For 'soutv' particularly, I found the following pattern worked well in Eclipse:
System.out.println("${var} = ${cursor}" + ${var});
As others have mentioned, you can add this template by navigating to Window > Preferences > Java > Editor > Templates and clicking New.
The equivalent of 'iter' seems to be 'for' in Eclipse.
Check under
Window -> Preferences -> Java -> Editor -> Templates
Reference:
Template Variables
They are called Templates.
Go to Preferences > Java > Editor > Templates to see a list of pre-defined templates.
For example, sysout is:
System.out.println(${word_selection}${});${cursor}
You can also create your own.

Eclipse formatter settings for the Builder pattern

I'm extremely frustrated with the Eclipse formatting rules for a series of qualified invocations (i.e., the Builder pattern style). For example, here is my preferred formatting for some code that creates a new Apache Commons CLI Options object:
Options options = new Options()
.addOption(OPTION_HELP_SHORT, OPTION_HELP, false, "print usage information")
.addOption(OPTION_VERSION_SHORT, OPTION_VERSION, false,
"print version and exit")
.addOption(OptionBuilder.withLongOpt(OPTION_PROPERTIES)
.hasArg()
.withArgName("FILE")
.withType(File.class)
.withDescription("specify a user properties file")
.create());
I.e., parameters are wrapped and indented if necessary and all qualified invocations except the first, unless necessary, are wrapped and indented if there is more than one. If a parameter list wraps inside a qualified invocation, the invocation should wrap first.
The default formatting in Eclipse ("Wrap only when necessary" for arguments and invocations) yields the following mess:
Options options = new Options().addOption(
OPTION_HELP_SHORT, OPTION_HELP, false, "print usage information")
.addOption(OPTION_VERSION_SHORT, OPTION_VERSION, false,
"print version and exit").addOption(
OptionBuilder.withLongOpt(OPTION_PROPERTIES).hasArg().withArgName(
"FILE").withType(File.class).withDescription(
"specify a user properties file").create());
Going into "Java Code Style -> Formatter -> Line Wrapping" and the line wrapping setting to "Wrap all elements, except first element if not necessary" for invocations yields:
Options options = new Options().addOption(
OPTION_HELP_SHORT, OPTION_HELP, false, "print usage information")
.addOption(OPTION_VERSION_SHORT, OPTION_VERSION, false,
"print version and exit")
.addOption(
OptionBuilder.withLongOpt(OPTION_PROPERTIES).hasArg().withArgName(
"FILE").withType(File.class).withDescription(
"specify a user properties file").create());
I don't like that the OptionBuilder expression isn't being wrapped, or that "FILE" gets wrapped without also wrapping withArgName.
Changing the indentation to "Indent on column" yields:
Options options = new Options().addOption(OPTION_HELP_SHORT, OPTION_HELP,
false, "print usage information")
.addOption(OPTION_VERSION_SHORT,
OPTION_VERSION, false,
"print version and exit")
.addOption(
OptionBuilder.withLongOpt(
OPTION_PROPERTIES)
.hasArg()
.withArgName("FILE")
.withType(File.class)
.withDescription(
"specify a user properties file")
.create());
The is breaking the lines where I'd prefer, but pushing things over much too far to the right.
Is there any way to convince Eclipse to apply my preferred formatting style or something closer to it than any of the above?
Turning off formatting with comments, or inserting line comments is too tedious.
The best way is described here:
... or you can select "Line Wrapping > Never join already wrapped
lines" globally. Then, you can break it manually and the formatter
will only format inside lines (or add additional line breaks if
necessary).
With this setting Eclipse formatter will stop ruining your builder statements.
Use comments:
Object o = foo() //
.bar() //
.toString();
Update for 2021. It is possible to change, navigate to: Code Style -> Formatter -> Line Wrapping -> Wrapping settings -> Function Calls -> Qualified invocations and change value to "Wrap all elements, except first element if not necessary"
In Eclipse 3.6 It's possible to turn off formatting for a region of code. See my answer to
How to turn off the Eclipse code formatter for certain sections of Java code?
AFAIK, this is known problem with Eclipse Formatter:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=59891
On the menu select Window -> Preferences when the window opens select Java -> Code Style -> Formatter and from there you can create your own format style to use by selecting the new or edit option. When editing a formatting profile a new window opens that gives you a lot of different options to use.

Categories