IntelliJ IDEA empty constructor/method code style - intellij-idea

How to tweak InlliJ IDEA 14 code style for Java, to make it keep closing brace of empty constructor/method right after opening one.
E. g. :
class A {
private A() {}
public void b() {}
}

Go to Settings/Code style/Java/Wrapping and Braces and select these options:
Keep when reformatting
Simple blocks in one line
Simple methods in one line
Simple classes in one line
That will keep code like this untouched while reformatting your code:
if(true) {}
public void foo() {}
public class Bar {}
I tested this on IntelliJ 13.1.5, but hopefully it will work the same way on 14 too.

Isn't this the default behaviour of IntelliJ 14? At least in my version it is. As soon as I open curly bracket it gets closed automatically on the same line.

Related

How to find where a symbol comes from?

Imagine you have multiple wildcard imports in a source file. Or a class/interface that extends multiple interfaces of a deep hierarchy. You've just encountered a symbol and would like to know which expression in the file is responsible for bringing it into scope. Is there any way to highlight such expression in IntelliJ IDEA?
For example,
import a.b.c.* /* doStuff comes from here */
import x.y.z.*
class MyClass
extends BaseClass /* doOtherStuff comes from here */ {
MyClass() {
doStuff() // when the cursor is here, highlight 'import a.b.c.*'
doOtherStuff() // when the cursor here, highlight the 'extends BaseClass'
}
}
PS: I know multiple ad-hoc ways of figuring this out, but am looking for a shortcut, "single click" solution.

How to decapitalize an argument in a Live Template in IntelliJ

In my project, I have many serialization classes and, to make the classes more readable, I'm standardizing them, with an argument which has the same name of the $class$, that is $argName$:
#immutable
class $class$_Serialize extends Serialize<$class$> {
final $class$ $argName$;
$class$_Serialize(this.$argName$);
#override
Map<String, Object> run() => $serialize$
}
In the Live Template above, the annoyance is that I have to retype $argName$ instead of the template simply decapitalizing $class$. How would I tie $argName$ to the decapitalization of $class$ in IntelliJ?
I've already tried to mess around with editing the variables and adding the decaptialize() function in the expression column, but so far haven't had much success. It was something like this:
You are using the wrong syntax for the decapitalize function. It should look like this:
decapitalize(class)
Note that there are no $ signs around the class in the function argument.

Use extension function from different context in Kotlin

Here is an example of what I'd like to achieve:
open class A {
open fun Int.foo() {
print("foo")
}
}
object B: A() {
val number = 5;
override fun Int.foo() {
print("overriden foo");
// I want to call the A.(Int.foo())
}
}
B.number.foo(); //outputs: "foooverriden foo"
First of all, does anything like this exist? Can I somehow assume number to be in the context of class A in its override method? How would I write this?
The more I think about it the more it twists my mind. Of course, you cannot call number.super.foo() because super for number is kotlin.Number. You cannot cast it to A because Int has nothing to do with A. The only way I can think about solving this to somehow import the extension function itself and rename it with as, but I cannot do that here since it is inside a class, so I cannot just import it. Any suggestions?
My use case for this is that I have a class where I manipulate some data, then in special cases, I want to manipulate it differently, but fall back to the original code as the last option. I could use normal functions instead of extension functions of course, but in my case, it comes natural to use extension functions, so I wanted to see if this could be achieved somehow.
It looks like this is impossible so far, I'm afraid.
There's an open issue for this on JetBrains' issue-tracking system: KT-11488.  There's a Kotlin work-around there, though that needs tweaks to the class designs.
(Also discussed on the JetBrains discussion board.  That mentions another workaround requiring a Java class.)
override fun Int.foo() {
print("overriden foo")
with (A()) {
foo()
}
}
Of course this is a bit of a hack and will get worse if A has some state which foo() depends on, which you'll then need to set manually.

Intellij disable new line before annotated field for Kotlin

IntelliJ autoformat keeps adding a blank lines between class field declarations having annotations.
Do you know how to disable it?
This happens for kotlin files since does not work like that for java files.
Example, between each var intellij adds a blank line each time I execute autoformat.
class Resign : UnitTest() {
#InjectMocks
private lateinit var resignService: ResignServiceImpl
#Mock
private lateinit var actionAccess: ActionAccess
#Mock
private lateinit var userReportConverter: UserReportConverter
....
}
The problem was fixed in KT-37891. The option to disable this behavior should be available in the next plugin version.
You may also consider voting for KT-32185: 'More code style options for minimum blank lines'
Go to Settings > Editor > Code style > Kotlin > Wrapping and Braces.
Change Property annotations to "Do not wrap" or "Wrap if long".
No answer properly points out the exact name of the configuration. Under the Kotlin code style, it can be found on the "Blank Lines" tab, with the name "Before declaration with comment or annotation".
For me, this defaulted to 1. Setting it to 0 let's me decide myself whether I want a new line or not.

Is there any way to change IntelliJ Kotlin coding style to catch extra white space after a function or at the end of the file?

Is there any way to change the formatting of files within IntelliJ using Kotlin formatting from code that looks like this:
class Class {
class InnerClass {
}
}
EOF
To something like this:
class Class {
class InnerClass {
}
}
EOF
I would love to be able to have my code reformatting catch this kind of thing but I can't seem to find it in the settings or even mentioned anywhere online. If you go into settings->editor->Kotlin->Blank Lines you do find similar settings but not quite what I'm looking for. The similar settings I found were here: