program story

Intellij IDEA에 대해 자동 형식 코드를 활성화하려면 어떻게해야합니까?

inputbox 2020. 8. 13. 08:19
반응형

Intellij IDEA에 대해 자동 형식 코드를 활성화하려면 어떻게해야합니까?


';'을 입력 한 후 Intellij IDEA에서 가능합니까? 또는 'Enter'를 눌러이 문자열의 형식을 지정 했습니까?

예를 들어 :

a+b=10;

후:

a + b = 10;

또는 가능한 옵션 : Code> Reformat Code ... ?

감사합니다!


IntelliJ의 자동 서식 지정 단축키

  • Windows의 경우 Ctrl+ Alt+ L.
  • 우분투 Ctrl+ Alt+ windows+ L.
  • Mac 용 Command+ Option+ L.

이 작업을 수행하는 두 가지 방법을 찾았습니다.

  1. 로 이동 Settings> Keymap.오른쪽 부분의 이동에서 Editor Actions> complete current statement.그것을 선택 추가 키보드 바로 가기를 클릭합니다. 누르기; 확인을 선택하십시오.

  2. 매크로를 사용하십시오. 이동

Edit> Macros> Start Macro Recording.

이제 세미콜론과 키보드 단축키를 눌러 코드 형식을 다시 지정하십시오 (다른 답변 또는 설정> 키맵에서 키보드 단축키를 찾을 수 있음).

재 포맷 후

Edit> Macros> Stop Macro Recording

이름 (자동 형식 또는 다른 이름)으로 매크로를 저장합니다. 그런 다음

Settings> Keymap> Macros> auto format (매크로 이름).

거기를 클릭하고 키보드 단축키 추가를 선택한 다음 세미콜론을 누르고 확인을 클릭하십시오. 이제 세미콜론을 누를 때마다 세미콜론을 작성하고 자동 포맷을 수행합니다.


Eclipse에는 파일을 저장할 때 자동으로 포맷하는 옵션이 있습니다. IntelliJ에는 이에 대한 옵션이 없지만 Ctrl + S (Mac의 경우 Cmd + S) 키에 대한 매크로를 구성하여 코드를 형식화하고 저장할 수 있습니다.

파일 저장시 Intellij 재 포맷


기본값은 Ctrl+ Alt+ L입니다. 모든 키보드 단축키를 구성 / 재정의 할 수있는 키 매핑 대화 상자가 있습니다.

파일-> 설정-> 키맵 탐색

여기에 이미지 설명 입력


파일-> 설정-> 키맵-> 현재 명령문 완성

거기에 ;키를 추가 했습니다. ';'를 입력 할 때 줄 끝에 자동 서식이 적용됩니다.

UPDATE

I realized that this will cause some problems in some situations. Use Ctrl+Shift+Enter instead. This key can be used in any position of cursor in a line. It will add ; at the end of the line. Also this shortcut have some other useful features.

like:

public void someMethod()

after shortcut:

public void someMethod() {
    // The cursor is here
}

so formatting on inserting ; is not necessary.


This can also be achieved by Ctrl+WindowsBtn+Alt+L. This will be important to some people,because in some Virtual Machines, Ctrl+Alt+L can log you out.


Ctrl+Shift+Enter combination (Complete Statement) also does the job and makes some more handy stuff with functions, cycles, etc.

JetBrains docs: https://www.jetbrains.com/help/idea/2016.3/completing-statements.html


In Mac it is Alt+Command+L(assuming you haven't changed any of your modifier keys or Intellij keyboard shortcuts from it's default state)


None of the solutions in Intellij is as elegant (or useful) as in Eclipse. What we need is feature request to the intellij so that we can add a hook (what actions to perform) when the IDE autosaves.

In Eclipse we can add "post-save" actions, such as organize imports and format the class. Yes you have to do a "save" or ctrl-s but the hook is very convenient.


Per the comment of Larson, this is now possible with the save-actions plugin.


Well that's not possible, but in intellij 13, how about adding a mouse gesture, something like single left mouse click to reformat the code? Or if you don't use the mouse much then add a very simple keyboard hotkey that you use all the time (possibly the "enter"? not sure if intellij would be happy with that to be honest)


The way I implemented automatical reformating like in Microsoft Visual Studio (It doesn't work perfect):

1. Edit > Macros > Start Macro Recording
2. Press continuously: Enter + Ctrl+Alt+I
3. Edit > Macros > Stop Macro Recording (Name it for example ReformatingByEnter)

Now we need to perform same actions but for Ctrl+Alt+L+;

4. Edit > Macros > Start Macro Recording
5. Press continuously: ; + Ctrl+Alt+I
6. Edit > Macros > Stop Macro Recording (Name it for example ReformatingBy;)

Now we need to assign HotKeys to these macros:

7. File > Settings > Keymap > press on a gear button > Duplicate...
8. Unfold a macro button (beneath) and by right clicking on two 
ours macros assign HotKeys for them: "Enter" and ";" correspondingly.

I know this question is quite old but there is actually a Save Actions plugin for IntelliJ that can be configured to format the code automatically.

참고URL : https://stackoverflow.com/questions/17879475/how-enable-auto-format-code-for-intellij-idea

반응형