I have the Eclipse formatter configured to allow me to do one liners as much as possible. However sometimes I want related commands on the same line such as
temp.add; temp.flush(); ⇦Problem
Also I configured the formatter to allow my if statements to look like this
if(true) return;
But if I have multiple commands it will push it down to the next line but I want it to format like this
if(true) {n++; return;} ⇦Problem
It formats like this
if(true) {
n++;
return;
}
Is there a setting to retain these multi line commands but still format the spacing and everything? It gets to the point that I purposely use ternary operators as much as possible because I hate doing one logical step on multiple lines. If there is no way to configure this or a plugin to fix this I think I am going to just turn the formatter off. I know I can just use ctrl+shift+f but I enjoy it formatting every time I run my program.
You can use off/on tags to avoid formatting in certain regions of editor. if you are using eclipse version 3.6 onwards.
This post explains how to do this How to turn off the Eclipse code formatter for certain sections of Java code?
Related
I am using prettier with VSCode, How can I configure it to format my code like this :
function test()
{
if()
{
MYCODE GOES HERE;
}
}
I want the { and } on new lines, and an empty line after { and before }.
Currently, it moves the curly brackets to same lines if condition or function name, and also remove the empty lines after/before { and }.
Prettier is considered an " opinionated " formatter, which means it doesn't let you choose things like that. If you want more control over the formatting, you can use a different formatter.
The built-in VS code formatter allows you to do what you're looking for, just search the settings for " function new line " and similar options.
There are of course many other formatting extensions available in the VS code marketplace as well. Whichever you choose, you will have to select it has your default formatter in your VS code settings.
As mentioned in this answer, VS Code's formatter itself works quite well, but if you want this to be part of workflow, then using ESLint might make things easier. There's a rule called brace-style.
You can then run eslint ./path/to/your/file --fix to format your code, or eslint . --fix to format code in the entire project directory.
Disclaimer: I use ESLint for code formatting most of the time and it works for me. I actually use it to find & fix problems too so it's like killing two birds with one stone, but note that ESLint is more about finding problems in the code and fixing them, so using ESLint just for code formatting might not be the best idea.
I have code that was formatted based on a wrap of 120 chars. I changed the setting to wrap it at 200 chars in IntelliJ CE. Running a reformat doesn't re-wrap the code as per 200 char limit. I am having to manually move the split lines up to a single line.
Example of existing code :
final MyOwnFactoryClass myOwnFactoryClass = new
MyOwnFactoryClass(myStringVar1 , myStringVar2,
myStringVar3);
How I want it to look
final MyOwnFactoryClass myOwnFactoryClass = new MyOwnFactoryClass(myStringVar1 , myStringVar2, myStringVar3);
Is there an easier way of doing this either in IntelliJ or Eclipse?
For those wondering why I need this, I have new code that I need to read through and it is extremely tough reading and interpreting in the 1st format. And no the company has no code standards so I won't be violating anything.
Would changes to plugins or settings be of use?
I found a way just by experimenting :) .
Intellij --> File --> Settings (Preferences for MacOS) --> Code Style --> Java --> Wrapping and Braces --> check all the align when multiline option in for all the blocks you want like for(), if(), while() etc. --> Apply --> run code format. and it should do the needful! :)
Aside from working with the automatic formatting, you can use the "Join Lines" action.
With the cursor on the first part of the line, select the action, either by name (using Cmd-Shift-A or double-shift) or with the keyboard shortcut Ctrl-Shift-J.
I'd like for my lines, especially within comments, to be automatically managed so they don't get too long.
I remember once I had a configuration for vim which automatically moved the word I was typing to the next line once I reached 72 characters. It wasn't smart enough to rearrange the paragraph if I edit it, but it was a start.
Is there something that can manage these for me? I have a tendency to write really long comments in my code, and it helps to make them look neat by having consistent width, but it's always a pain to do this because oftentimes editing a sentence requires editing the entire rest of the paragraph.
I have just recently discovered the Ctrl+Shift+F feature. It is amazing and superior to Ctrl+I which is what I was using up till now, but I noticed that it does not do anything to clean up my comments.
Update: The answers are correct when working with Java in Eclipse. It seems like I have to wait for the CDT to incorporate this feature.
In "Windows -> Preferences", go to "Java -> Code style -> Formatter" to customize the formatter (called when you click Ctrl+Shift+F). In the tab "comment", you can set the maximum line width for comments (it can be different then the line width for code).
Tip: in the preferences, "Java -> Editor -> Save actions", you can make Eclipse to automatically format your file when you save it, so your code is always correctly indented !
The automatic formatting of Eclipse great no question.
If your comments are reformatted depends on what comment type and how you already have inserted line breaks.
Writing for example one very long line comment starting with // will be broken down by the formatter into multiple lines.
However you later edit the formatted lines - e.g. delete parts of it the formatter will leave them as they are. Only over-long lines will be changed.
Just in difference to block comments like this: /* comment */
Those comments will always be re-formatted in case the line is too short or too long.
If you want to format your header comment, you have to check Enable header comment formatting - that was the trick for me.
Obviously, to use this, you must create new formatter profile.
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.
how is it done? cant find the option?
why is it anyways, that so many people format their code that way?
i really dont like it...
int
foo(int); //dont like
int foo(int); //like
By default, Eclipse shouldn't be reformatting your code unless you have it do so through the Format command.
However, if you're working with other people's codes and want to format it to your liking it is possible to have Eclipse apply formatting.
You can adjust how this works by locating the Code Style section under the language you are using under the Preferences menu.
Start with the Window->Preferences menu. I like to type "code" into the search box to limit the options. Then follow the steps in the image ** You can format almost everything.
I can't answer why some people would like the return value on a line by itself; I've never met anyone that did.