Search code examples
javaeclipseindentationauto-indent

Eclipse auto-indent incorrectly indents file


Something weird is happening to my Eclipse that I never remember happening before. Basically if I've got a long statement and split it onto two lines, then everything after that gets indented a tab further than it should be. Here's an example:

Correct indentation:

public static class Shape {

    enum Tetrominoes { NoShape, ZShape, SShape, LineShape, TShape, 
        SquareShape, LShape, MirroredLShape };

    private Tetrominoes pieceShape;
    private int coords[][];
    private int[][][] coordsTable;

    public Shape() {

        coords = new int[4][2];
        setShape(Tetrominoes.NoShape);
    }

    public void setShape(Tetrominoes shape) {

    }

}

What it looks like with Ctrl+A, Ctrl+I:

public static class Shape {

    enum Tetrominoes { NoShape, ZShape, SShape, LineShape, TShape, 
        SquareShape, LShape, MirroredLShape };

        private Tetrominoes pieceShape;
        private int coords[][];
        private int[][][] coordsTable;

        public Shape() {

            coords = new int[4][2];
            setShape(Tetrominoes.NoShape);
        }

        public void setShape(Tetrominoes shape) {

        }

}

Now if I keep that enum on one line and auto-indent it, then it works out just fine. I just got a new laptop and put a fresh copy of Eclipse on it and didn't change any settings, so this is how the default auto-indent works. But I remember on my old laptop if I would split a statement onto two lines then everything else after that would still be properly aligned?

(Also at the start of this post I put "Hey guys," but it looks like StackOverflow automatically removed it? I tried editing the question and reinserting it but it still got removed once posting. I tried putting "Hey," but that got removed too. Does SO not believe in greetings??)


Solution

  • I was able to reproduce the issue.

    It seems that the ending brace for the enum is confusing Eclipse. If you put that on a separate line the indentation starts working fine :

    public static class Shape {
    
        enum Tetrominoes { NoShape, ZShape, SShape, LineShape, TShape, 
            SquareShape, LShape, MirroredLShape 
        };
    
        private Tetrominoes pieceShape;
        private int coords[][];
        private int[][][] coordsTable;
    
        public Shape() {
    
            coords = new int[4][2];
            setShape(Tetrominoes.NoShape);
        }
    
        public void setShape(Tetrominoes shape) {
    
        }
    
    }
    

    You can also attempt to format the code (Ctrl + Shift + F) followed by correcting indentation (Ctrl + A and Ctrl + I ). When you format the code, you'll notice that Eclipse also puts the ending brace on the next line instead of alongside the Enum constant body.