如何配置Eclipse代码格式化工具在赋值运算符处换行
Configure Eclipse Formatter to Wrap Long Assignment Lines Over 120 Characters
Got it, let's get this sorted out for you! Here's a step-by-step guide to tweak Eclipse's code formatter so it wraps those lengthy assignment lines exactly how you want:
- Open the formatter settings: Go to
Window → Preferences → Java → Code Style → Formatter - Create an editable profile: Since you can't modify the default built-in profiles directly, click the
Newbutton, give your custom profile a name (like "Custom 120 Line Width"), and hitOKto duplicate the default settings. - Navigate to Line Wrapping rules: Switch to the
Line Wrappingtab in the formatter editor. - Target assignment expressions: In the left-hand
Wrapping settingspane, expandExpressionsand selectAssignments. - Set up the wrapping policy:
- Change the
Line wrapping policyto Wrap when necessary — this tells Eclipse to only split the line once it exceeds your 120-character limit. - Under
Settings for wrapped lines, adjust the indentation to your preference. Most folks useIndent by one levelso the wrapped part aligns neatly under the start of the line, orIndent on columnif you want it to line up with the assignment operator (=). - Double-check that your
Maximum line width(found in theGeneral settingssection of theLine Wrappingtab) is still set to 120 to ensure the trigger works correctly.
- Change the
- Optional: Tame long generics: If you also want the generic type arguments to wrap when they're too long, expand
Expressions → Generic type argumentsand apply the sameWrap when necessarypolicy there.
Once you save these settings (click Apply then OK), reformat your code with Ctrl+Shift+F. Your example line will now look something like this:
private Map<Instruction, LocalRegisterAssignmentInformation> instructionRegisterMap = new IdentityHashMap<Instruction, LocalRegisterAssignmentInformation>();
Or if you enabled generic wrapping:
private Map<Instruction, LocalRegisterAssignmentInformation> instructionRegisterMap = new IdentityHashMap<>( Instruction, LocalRegisterAssignmentInformation );
A quick heads-up: If some lines still don't wrap as expected, check for inline formatter comments (// @formatter:off) that might be overriding the global settings, or verify that your custom profile is selected as the active formatter.
内容的提问来源于stack exchange,提问作者Rajat Khanna
相关产品推荐
相关产品推荐

