AutoFormatting Lines in IntelliJ - java

I was using the formatter in IntelliJ and the right margin was 100
Because of this, when I auto format my code: IntelliJ automatically split long statements which was intended.
return Arrays.stream(values()).filter(myEnum -> myEnum.getValue() == enumValue).findFirst()
.orElse(SomeEnum.CUSTOM);
Now, I changed right margin to 160 and when i auto format it again it does not rollback the splitted statements into one statement such like following:
return Arrays.stream(values()).filter(myEnum -> myEnum.getValue() == enumValue).findFirst().orElse(SomeEnum.CUSTOM);
There are lots of codes in project and i do not want to do this process manually.
Are there any suggestion?

Well, I found solution.
Settings -> Editor -> Code Style -> Java -> Wrapping and Braces -> Keep when reformatting
Uncheck the Line breaks checkbox and reformat the code again.
It worked for my case.

Related

IntelliJ: Copy & paste a text as comment well formatted

I would like to know if there is a way on IntelliJ (v.2019.3) to copy a text, i.e. the following:
Screening a transaction should return ‘REJECTED’ if the email id of
transaction is in blacklist and transaction was made within last 30
days. In all other cases, it should return ‘ACCEPT’.
and to be paste as comment well formatted instead to have a single infinite line as:
could you try to go to Settings/Editor/Java/Wrapping and Braces/Ensure right margin is not exceeded

IntelliJ: add formatter:off/on comments around selected block

In IntelliJ IDEA (for Java code at least), it's poissble to instruct the code-formatter to ignore lines with these (see https://stackoverflow.com/a/19492318/117750):
// #formatter:off
...
// #formatter:on
What I'd like to do is automate adding these around a code block. Workflow I want:
Select a block of code.
Invoke an action (with a shortcut or a menu item etc. or with the Cmd-Shit-A).
This command needs to
add // #formatter:off on a new line before the first selected line, at the correct indentation.
add // #formatter:on on a new line after the last selected line, at the correct indentation.
From what I am reading, it is not possible to do with a Macro. Is it?
If not, do I need to write a plugin to do this? I am happy to write one, can someone give me brief high-level get-started steps on:
how to approach this action in a plugin
and pointers to get-started with plugins
You can get (close to) what you want using a Live Template. Go to Editor | Live Templates in the settings and add a new template (e.g. under the surround group) with the following text:
// #formatter:off
$SELECTION$
// #formatter:on
You can use the defined live template by selecting some text in the editor and invoking Code | Surround with Live Template... (Ctrl/Cmd+Alt+J) and selecting the live template you have created.

Stop Eclipse from cutting code lines on clean up

In Eclipse I want (for example) that code like this
public Foo bar() {
}
gets formatted to this
public Foo bar()
{
}
via the clean up function.
But to do that I have to check "Format source code" in the clean up profile.
But that also formats code like this
alert.setHeaderText("blablablablablablablablablablablablablablablabla");
to this
alert.setHeaderText(
"blablablablablablablablablablablablablablablabla");
which I absolutely do not want. Is there any possible way to stop Eclipse from cutting lines like that?
Go to Window->Preferences->Java->Code Style->Formatter. Create new formatter. Click on edit and then pick tab Line Wrapping and set Line Wrapping policy to Do not wrap.
For more clarification refer the below Link :-
http://eclipsesource.com/blogs/2013/07/09/invisible-chaos-mastering-white-spaces-in-eclipse/
You can configure the style to which code is formatted. Under
Preferences: Java -> CodeStyle -> Formatter
Then look for "Line wrapping".

How to do a specific completion action without adding some unnecessary comment ?

I have the following warning
The serializable class myClassD4 does not declare a static final
serialVersionUID field of type long
From this interesting discussion https://stackoverflow.com/a/285809/813853 I know what to do to handle this warning.
BUT my problem is kind of different, it is related to Eclipse parametrization. When I get this warning, I do right click. And choose completion action :
Add generated serial version id
the problem is that this add a comment block , something like /* ** */. That I don't want. Please, how to get rid of that ? I have looked on the configuration of Eclipse but not found it YET.
Found it :)
Window -> Preferences -> Java -> Code Style -> Code Templates -> Comments -> Fields
All I have to do is to edit the default string.
The only way that I know is to turn off comment-generation for ALL fields (=instance vairables). This is how to do it:
Windows -> Preferences -> Java -> Code Style -> Code Template .
Open Comments tab, click on Fields, click on Edit button and remove the text

IntelliJ Idea: code completion for creating method like in Eclipse

I am using Eclipse for about 5 years. Now I'm begging with IntelliJ Idea 13. I can not get used to code completion :-(
How can I create new public method?
In Eclipse i press:
pub CTRL+SPACE ENTER int CTRL+SPACE test ENTER
and get
public int test() {
}
how can I do it in Idea?
IF this scenario is important to you,
You can use IntelliJ's Live Templates
There are no built-in templates for that. You can create your own, like other answers advise. But in general, just typing the method with autopopup completion seems to require almost the same number of key presses:
pub < autopopup appears with "public" selected, hit ENTER > int test( < CTRL+SHIFT+ENTER >
You can add a 'Live Template' in IntelliJ. Go to Preferences -> Live Templates -> Select your language (if Java is not there, you can select Other), select the "+" symbol and add a new template. e.g. Abbreviation = test, and put your method in the Template text.
IntelliJ will display a message 'No applicaable contexts yet'. Define' just below the box. If you click on 'Define' you can select 'Java' in the list that shows up.
In your editor, type the template abbreviation and press the tab key.

Categories

Resources