How to stop reformatting of intelliJ after save - intellij-idea

This is a simple question that I can't find an answer. I'm using IntelliJ for Java practice.
I have blocks of code that are just a single line, followed by an end brace, like this,
public void example(){
System.out.println("Some string");
}
Edit: After saving and closing the app, then reopen the project, IntelliJ reformats this as
public void example(){ System.out.println("Some string"); }
How do I stop IntelliJ from doing this? It becomes a hassle when I need to add more things into the block, I need to click it to expand the block. Thank you!

It is not reformat, it is a folding feature. Please disable the Settings (Preferences on macOS) | Editor | General | Code Folding | One-line methods option.

Since this is not IntelliJ's standard behaviour, I assume you might have Save Actions plugin installed.
If this is the case, check its options (Settings -> Save Actions -> Formatting actions).

Related

Rider automatically places attributes on the same line as the property [duplicate]

I prefer my code to be more compact but JetBrains Rider (or any IntelliJ based editor) automatically formats it like this:
void Start()
{
}
How can I change it to the following by default?
void Start() {
}
You should not configure this thing manually in settings, just allow Rider to "learn" from your code.
Format code block as you like
Select desired code block and invoke Reformat and cleanup... -> Detect code style settings... from alt+enter menu:
Observe changes which Rider detects:
Save settings for all solutions on this machine, or just for the specific (current) solution. There are other options also:
Now your IDE is configured to format code exactly you want :)
P.S. This thing works in JetBrains Rider and R# tools, do not know about other IDEs, probably there you have to find the same settings manually.

Exclude unit from IntelliJ clean-up code option (the fix for type inference of explicit exceptions in java 8)

I would like to exclude a specific line (or minimum sized unit) from IntelliJ's clean-up code option. We need to workaround this problem with type inference of exceptions which was fixed in Java 9 but we are stuck in Java 8.
The workaround being cleaned by IntelliJ is given by Oracle in the link:
- return g(f("Hi", MyException.class));
+ return g(this.<String, MyException>f("Hi", MyException.class));
This fix is cleaned up by intellij if you tick clean-up code.
We can push with 'clean-up code' unticked but it has many contributors so someone will eventually tick and undo the fix.
You can define formatter markers: Preferences | Editor | Code Style | Enable formatter markers.
Check Example of using formatting markers
Solved, critically in the code as we do not push changes to our individual IntelliJ configurations.
Alt + Enter on the greyed out generic (the warning)
highlight Remove all type arguments and press right arrow
Suppress for ...
For 'method' an annotation #SuppressWarnings will appear - this is my personal preference.

Intellij adding extra line in new blocks

When I'm writing code in Intellij, I often create new blocks by typing these types of sequences:
if (test) {
Typing that open curly brace causes Intellij to automatically insert a closing brace and move the cursor between the two braces:
if (test) {|}
At this point, I simply press Enter and get a nicely formatted block with the cursor right where it should be:
if (test) {
|
}
However, today Intellij is adding an extra line to the block, like this:
if (test) {
|
}
I've looked all over the project and IDE settings, but I'm not sure what to change. FYI, it's happening in various file types (.java, .js, .css) across different types of projects (Spring/Java, Node/Express). Anyone know how to fix this little annoyance?
Just got this error myself. Weirdly enough, for me it turned out to be the AWS Toolkit plugin.
If you have the same problem, it is most likely something with one of your other plugins if not the one mentioned above. I went through tons of IDE settings, and believe me, I wouldn't wish that upon anyone ;)
As of 5MAR2015 the solution is to disable the Gauge plugin. Credit for this goes to #KaPaHgaIII

Allman-Style in Intelliji

I was wondering how I might configure Intelliji IDEA to automatically make my code look like:
public void Something()
{
// something
}
rather than:
public void Something() {
// something
}
I believe it is called the "Allman" style. Intelliji comes out of box using the "K&R" (I think?) style, however.
go to project settings. configure code style formatting.
To do Allman for Java (newbie-ish version) go to File/Settings/Editor/Code Style/Java. Next select the Wrapping and Braces tab and expand Braces placement. Now you must click 'End of line' for all three choices and select 'Next line' for each in the box that appears. Then click apply and OK.
I know this question is already answered, but to save people from actually modifying the settings.
Here is the AllmanIndent.xml download the .zip file.
After you download and extract the zip do the following steps:
From IntelliJ go to File->Settings->Editor->Code-Style and click the gear icon by the "Scheme" and then click on "Import Scheme -> IntelliJ code style XML" and then choose the extracted .xml file.
After this import, you can press Ctrl+Alt+L to auto-indent code.

Disable reformatting code when saving files

I made a small change to an old java file in IntelliJ and when I save the file the IDE automatically reformats all the code. Normally this would be preferable, however it seems almost every line has unnecessary white space that gets cleaned up. Now it appears the entire file was modified even though I only made a small fix. This will make looking at the significant changes in VCS more difficult.
Is there a way to disable code reformatting when a file gets saved? So far I haven't had any luck finding the setting in the Project Settings dialog. I'm using IntelliJ IDEA 9.0.2
Alternatively I could perform the reformat, commit the changes, and then make my modification but I feel that this will end up happening a lot and I don't want to impose my formatting preferences on code owned by other groups.
Settings | Editor | General > Other | Strip trailing spaces on Save -> None.
Updated for IntelliJ Idea 2021+
Settings | Editor | General > On Save | Remove trailing spaces on: <...> -> None.
For newer versions (at least WebStorm 2018), Ctrl + S is bound to a save Macro that has a reformat code action followed by save.
You can remove this reformat with Ctrl + Shift + A search for Macro then modify the save one.
Edit | Macros | Edit Macros | save | - on the Action: Reformat...
You can add these comments to make it temporarily disable formatting on the file, or a specific function:
// #formatter:off
If you want one section to remain, you can also add this:
// #formatter:on
IntelliJ Documentation Link
If #formatter:off is not working, you can enable it in preferences:
Today in 2020 IDEA is silently formatting HTML before commit, which might break your web pages appearance. To disable that unselect this checkbox:
Preferences -> Tools -> Actions on save -> uncheck "Reformat Code"
Settings > Tools > Actions on save > first line with checkbox "Reformat code"
change "Whole file" to "Changed lines"
As it advertise only the modified lines will be affected by formatting on save.