【问题标题】:Eclipse formatter: how do I align the code in my methodEclipse 格式化程序:如何在我的方法中对齐代码
【发布时间】:2012-12-13 22:35:59
【问题描述】:

ctrl+shift+F 适用于 public class 中的内容。 但是格式在方法内部不起作用,请参阅 = 符号未对齐。为什么?以及如何让它发挥作用?

public class myClass extends ActivityInstrumentationTestCase2 {
    public static boolean       myVar                            = true;
    private static final String TARGET_PACKAGE_ID                = "com.xxxx.test";
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.xxxx.test";
    private static Class        launcherActivityClass;

...
...
private String getOnScreeninfoByType() {
        ArrayList<TextView> textViews = new ArrayList<TextView>();
        ArrayList<Button> listButtons = new ArrayList<Button>();
        ArrayList<ToggleButton> listToggleButtons = new ArrayList<ToggleButton>();
        ArrayList<EditText> listEditTexts = new ArrayList<EditText>();
        ArrayList<CheckBox> listCheckBoxes = new ArrayList<CheckBox>();
        ArrayList<RadioButton> listRadioButtons = new ArrayList<RadioButton>();
        ArrayList<ImageButton> listImageButtons = new ArrayList<ImageButton>();
        ArrayList<ImageView> listImageViews = new ArrayList<ImageView>();
        ArrayList<ProgressBar> listProgressBars = new ArrayList<ProgressBar>();
...

    }

谢谢

【问题讨论】:

    标签: java eclipse formatter


    【解决方案1】:

    Preferences &gt; Java &gt; Code Style &gt; Formatter &gt; Edit &gt; Indentation 中有一个选项Align Fields in Columns。如果选中此选项,类中的字段将如您所见对齐。但是,我找不到任何选项Align Local Variables 或类似的东西。似乎没有在列中对齐局部变量的选项。

    【讨论】:

    • 这里一样,找不到任何可以在方法中对齐的东西。
    【解决方案2】:

    它们没有对齐,因为它们不应该对齐!
    类中的特殊对齐可能是由创建该项目的人设置的特殊非标准格式规则引起的。 在 Eclipse 中的 Code Formatter(或类似)下查看项目设置

    【讨论】:

    • 为什么不应该对齐?我的意思是,代码会更好更容易阅读吗?
    • 不,不是。在嵌套循环中,这将导致行长度大于 100。这也是不可能的。代码比变量声明复杂
    • 我的代码中只有几个嵌套循环,而且由于一些程序员有 22'+ 显示超过 100 的行长度并不是真正的问题。
    • @dit 即使没有对齐,您也很容易超过 120 的限制。所以对齐局部变量只是个坏主意。
    • 300 的限制远远超出了行业标准,这个限制是谁规定的?你自己?那么 300 的值也是一个坏主意。你有没有想过为什么报纸有多个栏目而电子书没有?一旦你发现,你就会知道为什么 300 不是一个好主意。如果你在一家公司工作,价值 300 应该有人让他的警钟响起。考虑重构你的代码,也许它太嵌套了。
    猜你喜欢
    • 2021-05-31
    • 1970-01-01
    • 2012-11-24
    • 2012-11-23
    • 1970-01-01
    • 2015-03-29
    • 2012-11-03
    • 1970-01-01
    • 2023-01-20
    相关资源
    最近更新 更多