【问题标题】:Why does Eclipse apply double-indent to some statements?为什么 Eclipse 对某些语句应用双缩进?
【发布时间】:2014-02-28 03:05:39
【问题描述】:

对于某些语句,Eclipse 格式化程序似乎应用了双缩进。如果您在首选项中打开格式化程序对话框,您有:

class Point {
public:
    Point(double x, double y) :
            x(x), y(y) {
    }
    /* etc. */

似乎没有办法告诉格式化程序去做:

class Point {
public:
    Point(double x, double y) :
        x(x), y(y) {
    }
    /* etc. */

相反。这是为什么?我该怎么办?

(我使用的是 Eclipse Kepler,但这不是一个新现象。)

【问题讨论】:

    标签: eclipse indentation code-formatting


    【解决方案1】:

    我相信这是因为未完成的行有 2 个缩进(并且插件开发人员认为构造函数 args 是前一行的一部分)。

    例如,如果你这样写:

    class Point extends GeometricStuff {
      ...
    }
    

    并且writeextends换行,相信会缩进两次:

    class Point
            extends GeometricStuff { // Note the double indentation here.
      ...
    }
    

    原因是当您按顺序编写代码时,您可以轻松识别给定行的结束位置(否则很容易被所有if-while-for 缩进弄乱):

    System.out.println("A");
    System.out.println("B name: "+ 
            b.name + ", " +
            b.age + " year(s) old");
    System.out.println("C");
    

    【讨论】:

    • 但我不想想要未完成的行有双缩进...单行有什么问题? :-( 无论如何,Eclipse 怎么不让我控制这个?
    • 不是Eclipse,是CDT插件不支持这个。 Java 格式化程序非常复杂,可以处理这些情况。这是个人喜好,但我觉得它很有用(尤其是如果您查看喜欢避免可选括号的队友的代码)。如果真的没有对此的支持,CDT 插件是一个开源插件,贡献总是非常受欢迎的 :-)
    • 我知道我很懒,但是如果您参与 CDT 开发工作,能否麻烦您向我介绍格式化程序的来源?
    • 我不知道CDT插件的内部结构,但是你可以从这里开始:eclipse.org/cdt/developers.php
    猜你喜欢
    • 2011-08-02
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多