ChatGPT解决这个技术问题 Extra ChatGPT

Is there an Eclipse line-width marker?

I have a specific project where I need to wrap every code line at 65 characters. I have set up the eclipse Java code formatter properly for this. But what I really want is a vertical line to be drawn in the editor showing where the max line width while I am typing, not just when I run the formmater. I know this feature is available in some capacity because it is displayed in the code formatter property page.

I don't see any option in eclipse to turn this on and I didn't see any plug-ins that do it on Eclipse Plugin Central

In case you are looking for a solution for the Eclipse Neon bug, check bugs.eclipse.org/bugs/show_bug.cgi?id=468307#c11.

J
Jon Skeet

Look in Windows / Preferences (at least on Windows - IIRC it moves around for different operating systems) then:

General -> Editors -> Text Editors -> Show Print Margin

Tick this and it should show the line.

As a quick way of finding this, use the search filter in the top and filter on "margin".

Notes from the comments - unverified by me, but I have no reason to doubt them:

It has changed somehow in 2016: For details see [here] (https://bugs.eclipse.org/bugs/show_bug.cgi?id=495490#c2) You have to set it in the formatter: From menu [Window]-->[Preferences], select [Java]-->[Code Style]-->[Formatter], and then edit your formatter profile. In the tab page [Line wrapping], you can find a setting named "Maximum line width". Change this setting, and the print margin in Java source editor will be changed too.


You should also change the line length for code formatting: stackoverflow.com/a/3697302/311744
Because these can have different column values, remember to set the 'Maximum line width' to the same value as 'Print margin column'
Neon (4.6) does not have this setting :(
It has changed somehow in 2016: For details see here: bugs.eclipse.org/bugs/show_bug.cgi?id=495490#c2 You have to set it in the formatter: From menu [Window]-->[Preferences], select [Java]-->[Code Style]-->[Formatter], and then edit your formatter profile. In the tab page [Line wrapping], you can find a setting named "Maximum line width". Change this setting, and the print margin in Java source editor will be changed too.
Please update the answer as per Uwe Günther 's suggestion. It was necessary for me to get the line to appear at a distance > 80. Eclipse version is 2018-09
J
James Graham

https://i.stack.imgur.com/P9TPM.png


This answer repeats the answer above, with a screen shot.
@AlexeyIvanov True but this answer adds to the original. The screenshot is a nice addition in general but especially because the highlighting shows how to change the color of the margin. I would have found this myself eventually because the print margin in the Luna dark theme was blinding me but this answer made my life easier which is always a good thing in an SO answer.
Is there a way to have eclipse show warning when a line is longer then the margin?
Neon (4.6) does not have this setting :(
G
Gabriel Staples

@Jon Skeet's answer is incomplete.

(1/2) First, do what he said:

Window --> Preferences --> General --> Editors --> Text Editors --> check the box for Show Print Margin

Ticking this box will show the vertical line.

https://i.stack.imgur.com/2JXhX.png

As a quick way of finding this, use the search filter in the top and filter on "margin".

However, this only shows the line, but under most situations the "Print margin column" value there is flat-out ignored.

To set the column number for where the line should be, do what @John Percival Hackworth mentions here:

(2/2) Go to:

Window --> Preferences --> C/C++ [or whatever language you are using] --> Code Style --> Formatter --> click Edit --> under the Line Wrapping tab set the value you desire for Maximum line width.

https://i.stack.imgur.com/JPET7.png

Side note:

Use Alt + Shift + Y to toggle soft line wrapping on and off. It will soft wrap (ie: no carriage return) at the end of the screen, however, not at the column you set above.

How do you enforce hard line wrapping at the column you set above (ie: that adds a carriage return)? I don't know yet. If you figure it out let me know. In Sublime Text 3 (a much better editor but with a much worse indexer/function definition finder :() it's Alt + Q.

Update: I think it may be possible with the "CppStyle" plugin, which uses clang-format, by using Ctrl + Shift + F to apply the auto-format, but I don't know the exact instructions to make it work yet.

Related:

Set tab width: Changing editor tab width in eclipse 3.5