Programming

Eclipse 포맷터가 모든 열거 형을 한 줄에 배치하는 것을 중지하는 방법

procodes 2020. 8. 15. 14:00
반응형

Eclipse 포맷터가 모든 열거 형을 한 줄에 배치하는 것을 중지하는 방법


다음과 같은 열거 형이 있습니다.

public static enum Command
{
login,
register,
logout,
newMessage
}

파일 형식을 지정할 때 출력은 다음과 같습니다.

public static enum Command 
{
login, register, logout, newMessage
}

@wjans의 대답은 일반 열거 형에서는 잘 작동했지만 인수가있는 열거 형에서는 작동하지 않았습니다. 그의 대답을 조금 확장하기 위해 Eclipse Juno에서 나에게 가장 합리적인 형식을 제공 한 설정은 다음과 같습니다.

  1. Window> Preferences> Java> Code Style>Formatter
  2. 딸깍 하는 소리 Edit
  3. Line Wrapping선택
  4. enum선언 트리 노드 선택
  5. 이제 괄호 안에 3/3이 표시되도록 설정 Line wrapping policy합니다 Wrap all elements, every element on a new line (...).
  6. Force split, even if line shorter than maximum line width (...)이제 괄호 안에 3/3이 표시 되도록 선택 취소 합니다.
  7. 트리 Constants노드 선택
  8. 검사 Force split, even if line shorter than maximum line width

이렇게하면 열거 형 트리 노드의 3 개의 하위 노드가 동일한 래핑 정책으로 설정되고 트리 노드를 제외하고 동일한 강제 분할 정책이 설정 Constants되므로 인수가있는 열거 형은 각 줄에 형식이 지정됩니다. 인수는 최대 선 너비를 초과하는 경우에만 줄 바꿈됩니다.

예 :

@wjans

enum Example {
    CANCELLED,
    RUNNING,
    WAITING,
    FINISHED
}

enum Example {
    GREEN(
        0,
        255,
        0),
    RED(
        255,
        0,
        0)
}

위에 설명 된 솔루션 :

enum Example {
    CANCELLED,
    RUNNING,
    WAITING,
    FINISHED
}

enum Example {
    GREEN(0, 255, 0),
    RED(255, 0, 0)
}

포맷터 기본 설정에서이를 지정할 수 있습니다.

  • 환경 설정 : Java-코드 스타일-포맷터
  • 편집을 클릭하십시오.
  • '줄 바꿈'탭을 선택합니다.
  • 왼쪽 상자에서 '열거 형'선언-> 상수를 선택합니다.
  • 줄 바꿈 정책을 '새 줄의 모든 요소, 모든 요소 래핑'으로 설정합니다.
  • '강제 분할 ...'을 확인하십시오.

약간 못 생겼지 만 회사 정책으로 인해 포맷터를 변경할 수없는 경우 줄 바꿈하지 않으려는 줄 끝에 주석을 추가 할 수 있습니다.

public static enum Command 
{
    login,//
    register,//
    logout,//
    newMessage//
};

It's not nice but you can turn the Eclipse formatter off for some sections of code...

// @formatter:off
public static enum Command {
    login,
    register,
    logout,
    newMessage
};
// @formatter:on

the option is in the Windows->Preferences->Java->Code Style->Formatter->Edit->Off/On Tags panel


You need to set the line wrapping policy under enum declaration for "Constants."

Set the wrapping policy to

  • Wrap all elements, every element on a new line

AND

  • Check the box that says "Force Split, even if line shorter than,,,,,

Just adding latest Eclipse 2018.9

  1. Window > Preferences > Java > Code Style > Formatter - Edit
  2. Expand Line Wrapping tree node.
  3. Expand Wrapping settings
  4. Expand 'enum' declaration
  5. Edit Constants and Constant arguments.

Constants need to be Wrap all elements, every element on a new line. Constant arguments need to be Wrap where necessary.

참고URL : https://stackoverflow.com/questions/6676305/how-to-stop-eclipse-formatter-from-placing-all-enums-on-one-line

반응형