๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Environment·Tool/IntelliJ IDEA

IntelliJ์—์„œ Commit ์‹œ Reformat code ์˜ต์…˜๊ณผ Rearrange code ์˜ต์…˜์˜ ์ฐจ์ด

by Leica 2020. 3. 21.
๋ฐ˜์‘ํ˜•

IntelliJ์—์„œ Commit ์‹œ Reformat code ์˜ต์…˜๊ณผ Rearrange code ์˜ต์…˜์˜ ์ฐจ์ด

์ด์ œ๊นŒ์ง€ Git์— ์ปค๋ฐ‹์„ ํ•˜๋ฉด์„œ Reformat code์™€ Rearrange code ์˜ต์…˜์„ ํฌ๊ฒŒ ์˜์‹ํ•ด์„œ ์‚ฌ์šฉํ•œ ์ ์ด ์—†๋Š”๋ฐ ๋ฌธ๋“ ๋‘ ์˜ต์…˜์˜ ์ฐจ์ด๊ฐ€ ๊ถ๊ธˆํ•ด์ ธ์„œ ์ฐพ์•„๋ดค๋‹ค.

 

Commit changes - Reformat code์™€ Rearrange code

 

Reformat code ์˜ต์…˜

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class SampleRunner implements ApplicationRunner { private Logger logger = LoggerFactory.getLogger(SampleRunner.class);
        private ApplicationRunner applicationRunner;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.debug("===================");logger.debug("abcdefg");
        
        
                 logger.debug("===================");
                    }}

์œ„์™€ ๊ฐ™์ด ์ง€์ €๋ถ„ํ•˜๊ฒŒ ์ •๋ ฌ๋œ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž.

์œˆ๋„์šฐ ๊ธฐ์ค€์œผ๋กœ Ctrl + Alt + L ๋‹จ์ถ•ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด ์ฝ”๋“œ๊ฐ€ ๊น”๋”ํ•˜๊ฒŒ ์žฌ์ •๋ ฌ ๋œ๋‹ค.

 

@Component
public class SampleRunner implements ApplicationRunner {
    private Logger logger = LoggerFactory.getLogger(SampleRunner.class);
    private ApplicationRunner applicationRunner;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.debug("===================");
        logger.debug("abcdefg");


        logger.debug("===================");
    }
}

์ด๊ฒŒ Reformat code ์˜ต์…˜์„ ์‚ฌ์šฉํ–ˆ์„๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์ผ์ด๋‹ค.

๊ฐœ๋ฐœ์„ ํ•˜๋ฉด์„œ ๋“ค์—ฌ์“ฐ๊ธฐ๊ฐ™์€ ๊ฒƒ์„ ํฌ๊ฒŒ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š๊ณ  ๊ฐœ๋ฐœ์„ ํ•˜๋‹ค๊ฐ€ ํ•œ๋ฒˆ์”ฉ ์ฝ”๋“œ๋ฅผ ์ •๋ฆฌํ•˜๊ณ ์‹ถ์„๋•Œ ์‚ฌ์šฉํ•˜๊ธฐ ์ข‹์€ ๊ธฐ๋Šฅ์ด๋‹ค.

 

Rearrange code ์˜ต์…˜

Rearrange code ์˜ต์…˜์€ Reformat code ์˜ต์…˜๋ณด๋‹ค ๋” ์ƒ์„ธํ•œ ์ฝ”๋“œ ์žฌ์ •๋ ฌ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค๋ฉด ์„ค์ •๋œ ์‚ฌ์šฉ์ž ๊ทœ์น™์— ๋”ฐ๋ผ ๋ณ€์ˆ˜์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

 

@Component
public class SampleRunner implements ApplicationRunner {
    private Logger logger = LoggerFactory.getLogger(SampleRunner.class);
    private ApplicationRunner applicationRunner;
    private Boolean isExist;
}

์œ„์™€ ๊ฐ™์ด ๋ณ€์ˆ˜ ์„ธ ๊ฐœ๊ฐ€ ์„ ์–ธ๋˜์–ด ์žˆ๋‹ค.

์ด ๋ณ€์ˆ˜๋ฅผ Rearrange code ๊ธฐ๋Šฅ์„ ํ†ตํ•ด ์ด๋ฆ„ ์ˆœ์œผ๋กœ ์ž๋™ ์žฌ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๋จผ์ € ์‚ฌ์šฉ์ž ๊ทœ์น™์„ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค.

 

Settings - Editor - Code Style - Java - Arrangement๋ฅผ ํด๋ฆญํ•œ๋‹ค.

Matching rules์—์„œ field - private ์šฐ์ธก์˜ ์—ฐํ•„(์ˆ˜์ •) ์•„์ด์ฝ˜์„ ํด๋ฆญํ•œ๋‹ค.

 

์ˆ˜์ • ์ฐฝ์ด ์—ด๋ฆฌ๋Š”๋ฐ, Order์—์„œ order by name์„ ์„ ํƒํ•˜๊ณ  Apply ๋˜๋Š” OK๋ฅผ ํด๋ฆญํ•˜์—ฌ ์ ์šฉํ•œ๋‹ค.

 

Rearrange๋ฅผ ์ ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ํŒŒ์ผ์—์„œ ๋งˆ์šฐ์Šค ์šฐ์ธก๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๊ณ  Reformat Code๋ฅผ ํด๋ฆญํ•œ๋‹ค.

 

Rearrange entries์— ์ฒดํฌํ•˜๊ณ  OK๋ฅผ ํด๋ฆญํ•œ๋‹ค.

 

์„ค์ •ํ•œ ๋Œ€๋กœ ๋ณ€์ˆ˜๊ฐ€ ์ด๋ฆ„์ˆœ์œผ๋กœ ์ž๋™ ์ •๋ ฌ๋œ๋‹ค.

 

Commit ์‹œ Rearrange code์— ์ฒดํฌํ•˜๋ฉด ์ด๋Ÿฐ ์ผ๋“ค์ด ์ ์šฉ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

์ด๋ ‡๊ฒŒ Reformat code, Rearrange code ๋‘˜ ๋‹ค commit ์‹œ์—๋งŒ ์‚ฌ์šฉํ•˜๋Š” ์˜ต์…˜์ด ์•„๋‹ˆ๋ผ ๊ฐœ๋ฐœ ์ค‘ ๋ฒ”์šฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜์ด๋‹ค.

๋ณ€์ˆ˜ ์ˆœ์„œ์™€ ๊ฐ™์ด ์•„์ฃผ ์ƒ์„ธํ•œ ์žฌ์ •๋ ฌ์„ ํ•  ๊ฒƒ์ด ์•„๋‹ˆ๋ผ๋ฉด Reformat code ์˜ต์…˜๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

References

https://www.jetbrains.com/help/idea/reformat-and-rearrange-code.html

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€