我的 Eclipse 发生了一些我以前不记得发生过的奇怪的事情。基本上,如果我有一个很长的语句并将其分成两行,那么之后的所有内容都会比应有的缩进一个制表符。这是一个例子:
正确的缩进:
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) {
}
}
使用 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) {
}
}
现在,如果我将该枚举保留在一行上并自动缩进,那么效果就很好。我刚刚买了一台新笔记本电脑,并在上面安装了新的 Eclipse 副本,并且没有更改任何设置,因此这就是默认自动缩进的工作原理。但我记得在我的旧笔记本电脑上,如果我将一条语句分成两行,那么之后的所有内容仍然会正确对齐?
(也在这篇文章的开头我放了“嘿伙计们”,但看起来 StackOverflow 自动删除了它?我尝试编辑问题并重新插入它,但发布后它仍然被删除。我尝试放“嘿,”但这也被删除了。难道不相信问候吗??)
最佳答案
我能够重现该问题。
枚举的结束大括号似乎让 Eclipse 感到困惑。如果您将其放在单独的行上,则缩进开始正常工作:
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) {
}
}
您还可以尝试格式化代码(Ctrl + Shift + F),然后更正缩进(Ctrl + A 和 Ctrl + I)。当您格式化代码时,您会注意到 Eclipse 还将结束大括号放在下一行,而不是放在 Enum 常量主体旁边。
关于java - Eclipse 自动缩进错误地缩进文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11818041/