I've never figured out how to make Intellij handle continuation indent for chained methods properly, and apparently today is the day it's annoyed me enough to consult you lovely people.
What I want is this:
makeAThing(
"with",
"params"
)
.setProperty("with some more params")
.start();
What I get is this:
makeAThing(
"with",
"params"
)
.setProperty("with some more params")
.start();
I get this in Java, Groovy, JavaScript and a bunch of other places. How can I persuade Intellij not to add continuation indent after a chained method call?
I just switched to intellij and also have found this rather annoying.
Only found two solutions:
forcing the coding style to have 0 for "continuation indent" which I'm starting to like anyway albeit not very canonical Java.
Turing off the formatter for blocks of code and press shift tab
Works for Java not sure for JS:
// #formatter:off
...
// #formatter:on
Related
An open-source project I contribute to uses banner style (also called Ratliff style). It looks like that:
// In C
for (i = 0; i < 10; i++) {
if (i % 2 == 0) {
doSomething(i);
}
else {
doSomethingElse(i);
}
}
Some IDE's like QtCreator have their own configurations for formatting, but others, like Visual Studio Code, require a .clang-format file.
I looked online for existing configurations, and couldn't find any. Then I tried to make one from scratch using this clang-format generator, but I couldn't manage to indent the braces right.
So, is it possible to create a clang-format file for Ratliff/Banner style, or is there some missing configuration that will force us to use some other generator?
This answer is not going to be the "answer" that you were looking for, but it is what I've discovered over the last two days of digging through the code for clang-format. I, too, have been looking for a way to auto-format a variant of banner style with clang-format.
clang-format seems to be missing some capabilities that would enable it to auto-format banner style.
For instance, you need to indent ending (right) braces for code blocks. clang-format does not even appear to track ending braces, but it does track starting (left) braces within its internals.
I started out my studying streak thinking I could understand the way clang-format implements its formatter system, so I could add the features needed to do it. Sadly it was way too complicated for me.
Perhaps someday we will be able to auto-format banner style with clang-format, but not today.
If someone out there knows this answer is inaccurate, please don't hesitate to correct me.
edit: Instead of giving you just a verification of what can't be done with clang-format, perhaps you might find http://astyle.sourceforge.net useful.
I want to keep manual indentation in some chained method calls using IntelliJ IDEA, so that the given, when, then are all not indented, but the formatter keeps shifting it back.
I type:
given
.spec(withAcceptsGzipHeader)
.authenticatedAsStandardUser()
.when
.get(resourceUnderTestPath)
.Then()
.statusCode(OK)
.header(HttpHeaders.CONTENT_ENCODING, "gzip")
.body("", hasKey(LICENCE_ID_OPTION))
and the formatter changes it to:
given
.spec(withAcceptsGzipHeader)
.authenticatedAsStandardUser()
.when
.get(resourceUnderTestPath)
.Then()
.statusCode(OK)
.header(HttpHeaders.CONTENT_ENCODING, "gzip")
.body("", hasKey(LICENCE_ID_OPTION))
I have Wrapping and Braces -> Chained method calls -> Align when muiltiline unchecked.
I know about #formatter:off/on but that's not quite a proper solution.
This post seems to ask for what I need. Surprised this hasn't been solved yet!
I'm using Intellij IDEA 14.1.4, formatting plain Java code.
IDEA does this:
super.getFoo()
.foo()
.getBar()
.bar();
I want it to do this:
super.getFoo().
foo().
getBar().
bar();
Anyone know how to tell it to do that?
There is not a setting to do this. But there is a feature request to add this: IDEA-123881 Code Formatting: method chain wrapping - Java configure position of dot operator I recommend you vote for it and watch it.
I only rarely use #synchronized, but as far as I can remember (meaning around Xcode 3.2 or something), it never suggested #synchronized when using the auto-completion, and still never does.
I do get suggestions when typing '#', like #autorelease, #encode, #selector and so forth.
Is there any known reason for this ? I wasn't able to find any related topic. It's been bugging me, because it gives me the feeling that this is not a valid method to handle concurrency in iOS.
#synchronized does not appear when using auto-completion because Apple has not mapped that keyword to any code sense implementation.
Per Apple's suggestion, I have filed a bug report.
However, I can offer an auto-complete solution.
You may create an #synchronized snippet that can be auto-completed or drag and dropped into your code.
Copy/paste the code below into one of your Xcode documents.
Select the new code and drag it to your snippets library.
Double-click the new snippet and click edit.
Enter #synchronized for the completion shortcut.
For more info: http://nshipster.com/xcode-snippets/
#synchronized(anObj) {
// Everything between the braces is protected by the #synchronized directive.
}
I have this line in my coffeescript file
if someVariable then no else yes
One of the most valuable feature in IDEA for me is code clean up. But it doesn't work as I expect here. After pressing Ctrl+Alt+L, IDEA reformats this line to ugly
if someVariable then {
no
} else yes
Which is not just bad style but error when compile from coffeescript to javascript.
Here is similar line which couldn't be formatted properly by IDEA.
return true if someVariable is 5
...becomes...
{
return true
} if someVariable is 5
Why is that?
It's a bug caused by the if() statement, Force braces: Always setting in JavaScript Wrapping and Braces.
Issue is submitted to YouTrack.
That looks like JavaScript code style. Try checking settings under File>Settings>Project Settings>Code Style.
If you can't manage to set the code style to a correct form, then you still have the option to customize it yourself..
Eventually take a look # http://www.jetbrains.com/webstorm/webhelp/coffeescript-support.html, you might find something interesting if you haven't gone over it all ready. Good luck!