Eclipse code formatter multiline function call closing bracket indention - eclipse

Currently my eclipse formatter formats a multiline function call like this:
someObject.doSomething(
some().long().chain().of().methods()
);
But what I want is for eclipse to align the closing bracket with the method call:
someObject.doSomething(
some().long().chain().of().methods()
);
I have tried playing around with new line and wrapping rules in the code formatter but haven't been able to achieve this. What would be the solution?
After some time of digging I found a similar question which has an accepted answer but seems not to answer the same question:
Can the Eclipse formatter be configured to indent multiple lines between parenthesis properly?
The author of this question also states:
Edit: I found the settings for "Line Wrapping" -> "Default indentation
for wrapped lines" and "Default indentation for array initializes" and
have set them to "1" instead of "0". That is better for the array
initializers, but still doesn't indent closing parethesis to match the
opening parenthesis the way that I want it to:

The latest proposal on this does not take into account the closing );, but the first expression.
See Eclipse 4.23 (Q1 2022):
Method invocation wrapping indentation
It turns out it's not obvious how to indent a wrapped method invocation when the preceding expression itself is complex enough to also be wrapped into multiple lines.
Should the indentation be added to the existing indentation at the end of the expression, or just reset and assume that only the indentation of expression's first line matters?
Previously only the former behavior was available, now there's a setting to choose the latter.
The checkbox called Indent from the base expression's first line is located in the Line Wrapping > Wrapping settings > Function calls section, right under the Qualified invocations setting.

Related

How can we configure our VSCode to format C code as per our custom coding standard?

How can we configure our VSCode to format C code as per our custom coding standard?
Formatting rules like -
Initial indentation is 4 spaces, next indentation is a tab, and one
more level of indentation is tab+4 spaces, another level is 2 tabs
and goes on.
And the opening braces to be on the same line as the if or for
statement?
How the indentation of the statement in the next line should be. Whether the statement inside if
statement should be indented or just be in the same column alignment as the if statement.
And so many other coding style formats.
When I was using Eclipse, I was able to create my custom profile and make all the necessary changes inside it like that -

How to specify indentations on multiline parameter lists in IntelliJ Scala?

This has been bothering me for a while, but I can't seem to figure out how to change this formatting. Let's take a case class as an example:
I prefer two tabs after a line-continuation; however, IntelliJ seems to force this style:
This behavior seems to be controlled by Preferences -> Editor -> Code Style -> Scala -> Other -> Alternate indentation for constructor args and parameter declarations, which specifies a minimum of 0 spaces, and that simply brings the arg list inline with the opening parentheses. This isn't a big deal by itself, but whenever I copy/paste blocks of code, it reformats everything and I have to go back and shift-tab ad nauseam. Is there a style field that I'm missing somewhere?
There's an Intellij only solution:
Under Wrapping and Braces, disable Method declaration parameters > Use normal indent for parameters.
Then, under Other, enable Alternate indentation for constructor args and parameter declarations and set to the number of spaces you want to indent from the declaration level (in your case 4).
You want "Align when multiline" checked in the code style settings
You can also look into scalari-form plugin. It gives you much more and IDE independent. You are particuliary interested in alignArguments=true and if I remember correctly defaults should make the indentation as you want.
*Note, that it formats code after some sbt task, for instance sbt test, not when you press Ctrl+Alt+L or similiar in IntelliJ

Netbeans generated code settings

I was wondering where can I change the way Netbeans generates code. For example, I want the opening bracket to be on a new line.
I've been looking in the [Tools] -> [Options] menu but I didn't find anything.
Solution 1 should solve your problem. For any auto-generated code formatting that Solution 1 won't solve, use Solution 2. Beware the "Code Templates" tab though, it has been relatively buggy for me in the past.
Important Note: Attempting to modify the "Code Templates" for the placement of braces won't work correctly because it's overridden by the preferences in Solution 1. Any changes to formatting wanted should first be attempted with Solution 1, as the "Formatting" tab in Netbeans takes precedence over the "Code Templates" tab.
Solution 1:
Go to [Tools] -> [Options] -> [Editor] -> [Formatting]. Choose the language you are using, then, for the Category drop down box, select Braces. Directly under that you'll see Braces Placement. You can modify the placement of the braces for a class declaration, method declaration, or "other". Options are:
Same Line - places bracket on same line as class, method, or "other".
New Line - places bracket on a new line under the class, method, or "other".
New Line Half Indented - same as "New Line" with a half indention added.
New Line Indented - same as "New Line" with a full indention added.
Solution 2:
Go to [Tools] -> [Options] -> [Editor] -> [Code Templates]. Choose the language you are using, then modify the templates according to what formatting you'd prefer.
For example, if Java is selected, I can scroll to the "pm" abbreviation and see how Netbeans generates private methods. Here is a incomplete but helpful abbreviation-to-meaning list for a few common Java abbreviations you might want to change.
m - Method
p - Private
f - Final
s - Static
Therefore, if you want to change the formatting for an auto-generated method that is listed as "private", you can change the formatting for the abbreviation "pm" in the "Expanded Text" area, which shows the auto-generation rules/formatting for a private method.
Note: Combinations of these letters can sometimes mean something different, for example "pr" is expanded to "private", but "prm" is listed as a protected method, not a private method.

Force "same line" on curly braces in Eclipse formatter

In my java code formatter, in my profile I have (actually it's the eclipse's default) all choices in the "braces" tab set to "same line". Nevertheless when I have a piece of code like this:
interface TestI
{
}
and I invoke the formatter (key binding, context menu, whatever) all I get is
interface TestI
{
}
it doesn't move the opening braces to the same line, the same applies to methods, control statements etc. then when I manually change it to
interface TestI {
}
it then formats it to:
interface TestI {
}
So it generally respects the format I did manually but only formats the spaces between the braces and other elements.
Other formatting options in my formatter work as as specified. I'm using the latest Eclipse Indigo release 3.7.1, I had the same with 3.7, and my friend tried it with a 3.5 release, so it makes me think that this is on purpose, but then again why is there this "braces" tab at all if I can't reformat the code using this rule ...
I hope I'm missing something and that you will make me fell stupid in a moment and the problem will go away, because I have a gazillion of classes to cleanup and I don't want to do it by hand .. :)
UPDATE: when I set the rule not to "same line" but "next line" then formatting works as expected, it forces the rule regardless of how I've written the code, as soon as I change it back to "same line" the the formatter doesn't force this rule.
Found it... it was caused by:
<setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="false"/>
which in the Formatter setting GUI is under "Line Wrapping/Never join already wrapped lines"
yes now I remember setting it, but I never thought that this would override the braces setting ...
a nice-to-have feature of the formatter would be to have warnings if you set options that are no-ops because of other options that you've set override them.
For those wondering how to make this apply to array initializers, the settings are
New Lines > After opening brace of array initializer
New Lines > Before closing brace of array initializer

Help with Eclipse line wrapping

I'm trying to get something like:
someObject.firstFunctionCall().secondFunctionCall().thirdFunctionCall();
to look like:
someObject.firstFunctionCall()
.secondFunctionCall()
.thirdFunctionCall()
I played around with the formatting editor and tried searching to no avail. I just can't think of the name for multiple function calls in one statement. I can do it myself but then it reverts to the top example every time I run the formatter.
You'll have to format it yourself.
To configure the formatter to not rewrap already wrapped lines, you can select this option in the "Line Wrapping" section of the formatter:
Never join already wrapped lines
Though it will change the indentation of the two lines.