Eclipse 3.7 formatting static in class - java

In Eclipse 3.7 is code:
public class AppResources
{
private static Image titleIcon;
static
{
AppResources.titleIcon = getIconImage();
}
public static Image getTitleIcon()
{
return AppResources.titleIcon;
}
}
Where in Formatter is possible to say that before static should be empty line?

There is no separate formatting setting for the static block. For formatting purposes it appears to be treated as any other member variable (as opposed to a function or some other construct).
There is another way to end up with the formatting you want though. If you set the formatting option Blank Lines->Existing Blank Lines->Number of empty lines to preserve to be 1, then you can manually add a blank line before the static block and the formatter will not remove it.

As other answers point out, this is not possible to do for static blocks through the Eclipse formatter. One other way to accomplish this would be through search/replace in File Search in Eclipse:
From:
(^\s*\n)*(^\s*static\s*\{)
To:
\n\2
with Regular Expression enabled

Related

Complete Current Statement behaves differently in Scala in IntelliJ

I recently upgraded my IDEA and now using IntelliJ IDEA 2022.2 Ultimate Edition.
I found the Complete Current Statement in Scala code behaves differently as in Java code, which is very annoying.
For example in Java code:
public static void main(String[] args) {
String foo = "bar"
}
Press Complete Current Statement shortcut(shift+cmd+enter for me) anywhere in line #2, will add a ; at the end of the line, and an auto-indent will be applied too:
public static void main(String[] args) {
String foo = "bar";
}
Then press Complete Current Statement again will bring you to a new line when there is nothing more to adjust.
public static void main(String[] args) {
String foo = "bar";
}
In previous version of IntelliJ, I roughly remember the behavior is same for Scala code.
But in this version of IntelliJ, when I try to do samething in Scala code, for example:
def foo (): Unit = {
throw new RuntimeException
}
When I press Complete Current Statement in line #2, nothing happens.
Could anyone please help me checkout why or how should I config to align with Java code's behavior? Thank you very much!
You don't need to use that in Scala because semicolons are optional, and almost never used. Actually, your Scala code sample is already what you would call a "complete statement".
For formatting what I do and recommend is having set File -> Settings -> Tools -> Actions on save and check Reformat code and optionally Optimize imports, and it will do both whenever you save your source file using Ctrl + S. I believe it's Cmd + S on your Mac.
This uses the default Intellij Formatter for Scala. Scala also has it's own Formatter called Scalafmt with customizable setups more control of formatting different Scala features based on your preferences. This is located at Settings -> Editor -> Code Style -> Scala.
If for some reason you would still like to use your shortcut key, then the only thing the Complete current statement can do to your Scala code is auto-indenting the current line, which for some reason it doesn't so it seems to be a bug on Intellij's side. But what you can do is replace the Auto-Indent Lines shortcut key to use your Complete current statement shortcut key instead and get the same behavior.

Why does IntelliJ not show the "System.out.println()" shortcut when I type "sout"?

When i type sout in IntelliJ, it doesn't show the System.out.println() shortcut, and instead shows WSDLOutput, WSDLOutputImpl, JSWBlend_SRC_Outpeer, and LSOutput.
This live template works in Java files inside a method (not in the root of the class where there can be no executable code except the static initializer blocks).
So, make sure you are inside a method and also check Java class in inside the sources root.
Sample class to try:
public class Main {
public static void main(String[] args) {
sout <- press Tab here
}
}
You can expand it using the Tab key.
Go to Preferences -> Live Templates -> Java. Check that you have the sout live template. If you don't, add a new live template under the user section with the abbreviation sout and the text System.out.println().

How can I suppress the addition of a blank new line after the class declaration in IntelliJ? (Code Style)

I'm having the problem that IntelliJ Idea keeps adding a blank new line after the class statement. A class like the one below
public class A {
private String name;
}
gets formatted into this one below.
public class A {
private String name;
}
The following diff show the blank new line between the class statement and the first field declaration.
My code style settings on Wrapping and Braces tab are as follows:
I was not able to find a setting that suppresses this additional new line. Can anyone help me on this?
In settings: Editor|Code Style|Java on the 'Blank Lines' tab you probably have 'After class header' set to 1. Change this value to 0 and the unwanted blank line won't be added by the formatter.

How to add a new line at the beginning of constructor body in Eclipse formatter?

I'm using eclipse Neon, but this could be present in previous versions too.
The formatter allows to add a blank line at the beginning of a method body, but it does not allow to do the same for constructors. This snippet is taken directly from the preview code of the formatter editor:
public Example(LinkedList list) {
fList = list;
counter = 0;
}
public void push(Pair p) {
fList.add(p);
++counter;
}
I want the constructor to look the same as the method. I could not find a way to do that in the formatter settings.
(This is not a question about the right way to format code - these are closed as opinion based. This is a question about configuring a tool in an IDE. Please keep the discussion relevant.)

How to move String block of text xTab spaces to the right in Java?

If I have got a whole block of text as a String, which contains several new-lines.
For example: System.out.println(myString) would give:
<http-request>
<param1>value</param>
<param2>value</param>
</http-request>
And what I want to do is space the whole block three tab spaces to the right...
What is the best way to get this done?
Not tried in code but sounds like the replace(regex, string) should work.
Just go for replace("\n", "\n\t\t\t") -> one \t per tab space.
(replace from String's standardlib)
Since java 12 you can use String.indent() method. For example
public static void main(String[] args) {
System.out.println("""
<http-request>
<param1>value</param>
<param2>value</param>
</http-request>
""".indent(8));
}
will produce output like below
<http-request>
<param1>value</param>
<param2>value</param>
</http-request>

Categories