IntelliJ doesn't automatically break line - intellij-idea

I'm having issues with IntelliJ automatic Reformat Code on one specific line of code:
catch (MyException1 | MyException2 | MyException3 | MyException4 | MyException5 | MyException6 | MyException7 | MyException8 e) {
I have set in Editor -> Code Style Hard wrap at 140
In Editor -> Code Style -> Java -> Wrapping and Braces everything is either Chop down if Long or Wrap if long
Now, the issue is that running Code -> Reformat Code doesn't break this line into 2 automatically (it should as it exceeds 140 characters). And what's worse, if I break it manually, then Reformat Code will bring it back to one line. As I have the Line Length inspection on, IntelliJ complains about this line.
This is the only line in the whole project where IntelliJ has this problem, all other expressions containing + or || etc. are correctly wrapped.

Wrapping or chopping down multi-catch types is not supported on IntelliJ IDEA 2021.3 and below. IntelliJ IDEA 2022.1 will support this. See https://youtrack.jetbrains.com/issue/IDEA-178941

Related

How to remove line breaks on code formatting in IntelliJ?

How to make intellij remove line breaks from this
long orderId =
orderInserter
.executeAndReturnKey(values)
.longValue();
to become this:
long orderId = orderInserter.executeAndReturnKey(values).longValue();
Ok, I just found the answer. it's: File -> Settings -> Editor -> Code Style -> Java -> Wraping and Braces Uncheck Line Breaks.

Change 88 characters limit for Black plugin in PyCharm

I am using Black inside PyCharm to format my Python code.
I am using the Black-Pycharm plugin, unfortunately, selecting code and applying Black on it (Code > Reformat Code (BLACK)) cuts all my lines at 88 characters (the default limit for Black).
I want to change this limit to cut the lines at 80 characters. I tried two different ways:
Changing the Black exe path in the "Black plugin settings" from ~/.local/bin/black to ~/.local/bin/black -l80, but applying Black with PyCharm outputs this error: BlackPycharm: Cannot run program "/home/BCT/.local/bin/black -l80": error=2, File or folder not found
Using Black as an 'External Tool' in Pycharm (as described here), and specifying the line length in the arguments text box. This successfully applies Black on my file with the desired character limit, but:
It automatically saves/replaces my file with the new formatted file, I can't undo the change.
I can't apply Black on a portion of code only.
Do you know ways to use Black with:
The ability to specify a desired line length
The ability to reformat only a portion of code
at the same time ?
EDIT: Apparently PyCharm cannot use Black for only a portion of code...
I also had the same problem with adjusting the linelength for the black 'external tool'.
1- follow this link to install and set black as an external tool: https://black.readthedocs.io/en/stable/editor_integration.html#pycharm-intellij-idea
2- within the "PyCharm/IntelliJ IDEA" section, in "Arguments":
replace "$FilePath$" with "$FilePath$" -l 120
Note: the '-l 120' should be outside the quotes, and replace 120 by whatever line length you need.
Cheers!
Maher.
I use Black as an external tool within PyCharm, but I am able to specify line length by adding a pyproject.toml (The PEP/ more info) file to my root project directory. I don't have to pass anything as an argument. Maybe it's able to solve your problems. It looks the following way:
# NOTE: you have to use single-quoted strings in TOML for regular expressions.
# It's the equivalent of r-strings in Python. Multiline strings are treated as
# verbose regular expressions by Black. Use [ ] to denote a significant space
# character.
[tool.black]
line-length = 79
target-version = ['py37', 'py38']
include = '\.pyi?$'
exclude = '''
/(
\.eggs
| \.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
# The following are specific to Black, you probably don't want those.
| blib2to3
| tests/data
| profiling
)/
'''
#Nadros solution is correct but it should be noted that -l 120 has to be added to the FileWatcher that was created as well if you are using it to format files on save.

Intellij IDEA 2018.1 - Disable simple comment folding

Using IntelliJ 2018.1 I noticed that on certain files multi-line simple comments are always folded:
// My multi
// line
// comment
becomes
//...
How do I turn it off? I need to always see my comments.
Settings | Editor | General | Code Folding | [ ] End of line comments sequence

How to change line width in IntelliJ (from 120 character)

I was wondering how I can change the line length in IntelliJ.
Since I use a pretty high resolution, I get that line that shows 120 characters straight through the middle of the screen. Can I change it from 120 to, say, 250?
IntelliJ IDEA 2018
File > Settings... > Editor > Code Style > Hard wrap at
IntelliJ IDEA 2016 & 2017
File > Settings... > Editor > Code Style > Right margin (columns):
You can alter the "Right margin" attribute in the preferences, which can be found via
File | Settings | Project Settings | Code Style - General
Right Margin (columns) In this text box, specify the number of columns
to be used to display pages in the editor.
Source: Jetbrains
It seems like Jetbrains made some renaming and moved settings around so the accepted answer is no longer 100% valid anymore.
Intellij 2018.3:
hard wrap - idea will automatically wrap the line as you type, this is not what the OP was asking for
visual guide - just a vertical line indicating a characters limit, default is 120
If you just want to change the visual guide from the default 120 to lets say 80 in my example:
Also you can change the color or the visual guide by clicking on the Foreground:
Lastly, you can also set the visual guide for all file types (unless specified) here:
It may be useful to notice that very good answers given above may not be enough. It is because of one more tick is required here:
Be aware that need to change both location:
File > Settings... > Editor > Code Style > "Hard Wrap at"
and
File > Settings... > Editor > Code Style > (your language) > Wrapping and Braces > Hard wrap at
I didn't understand why my this didn't work but I found out that this setting is now also under the programming language itself at:
'Editor' | 'Code Style' | < your language > | 'Wrapping and Braces' | 'Right margin (columns)'
Open .editorconfig in the root of your project (or create one if you don't have any) and add:
max_line_length = 80
The IDE should pick it up immediately (works in PhpStorm, developed by the same company). As a bonus, it can be committed to GIT and will stay consistent across different editors you may be using in the future. More info on .editorconfig.

Force code formatter in IntelliJ to join lines

I'm trying to force IntelliJ to reformat the code the way Eclipse does it.
When I set a line width to e.g. 120 chars I would like IDE to join line that were broken up to multiple lines (e.g. because the line width was set to 80 chars).
I want to go from here:
int a = 1
+ 2;
To here:
int a = 1 + 2
Is it possible in IntelliJ? I'm looking for the appropriate setting in the formatter but cannot find it. Could someone tell me where I can find it?
This works for me in intellij 8 if i go to:
File -> Settings -> Code Style -> Wrapping -> Binary operations -> set to: 'Wrap if long' or 'Do not wrap'
Then CTRL-ALT-L to reformat the code
Menu -> Edit -> Join Lines (Ctrl+Shift+J)
If your code already contains inappropriate line breaks and you want the formatter to remove them during the reformat,
File -> Settings -> Code Style -> Wrapping and Braces ->
Keep When Formatting -> Line Breaks -> Untick
(at least on IntelliJ 15 based Editors)
For Intellij 2016.2.x I had to deselect this option in the Settings:
Then go back to your code and press CTRL-ALT-L