【问题标题】:Eclipse indentation on EmacsEmacs 上的 Eclipse 缩进
【发布时间】:2009-04-30 01:45:43
【问题描述】:

我是一名 emacs 用户,刚开始为一家以 eclipse 为标准的新公司工作。我已经尝试过 eclipse,但我也想尝试 JDEE(我在长时间的中断后回到 Java)。到目前为止,主要的绊脚石是让缩进匹配。有没有一种简单的方法可以做到这一点,还是我需要深入研究 emacs 缩进的具体细节?

编辑:对于这个问题的混淆感到抱歉:我不想让 Eclipse 模仿 emacs,我想让 emacs 模仿 Eclipse。我希望能够使用 emacs 来修改代码,而不会破坏 Eclipse 用户所期望的缩进。

【问题讨论】:

    标签: java eclipse emacs


    【解决方案1】:

    我发现 Eclipse(和 IntelliJ)和 Emacs 默认 java 格式之间的主要区别是 Emacs 将函数参数与先前的参数(例如emacs 可以:

    BigLongJavaStuff.doFoobarToQuux("argument 1",
                                    "argument 2");
    

    Eclipse 会这样做:

    BigLongJavaStuff.doFoobarToQuux("argument 1",
            "argument 2");
    

    以下添加到你的 ~/.emacs 文件将使 Emacs java-mode 做同样的事情:

    ;; eclipse-java-style is the same as the "java" style (copied from
    ;; cc-styles.el) with the addition of (arglist-cont-nonempty . ++) to
    ;; c-offsets-alist to make it more like default Eclipse formatting -- function
    ;; arguments starting on a new line are indented by 8 characters
    ;; (++ = 2 x normal offset) rather than lined up with the arguments on the
    ;; previous line
    (defconst eclipse-java-style
      '((c-basic-offset . 4)
        (c-comment-only-line-offset . (0 . 0))
        ;; the following preserves Javadoc starter lines
        (c-offsets-alist . ((inline-open . 0)
                            (topmost-intro-cont    . +)
                            (statement-block-intro . +)
                            (knr-argdecl-intro     . 5)
                            (substatement-open     . +)
                            (substatement-label    . +)
                            (label                 . +)
                            (statement-case-open   . +)
                            (statement-cont        . +)
                            (arglist-intro  . c-lineup-arglist-intro-after-paren)
                            (arglist-close  . c-lineup-arglist)
                            (access-label   . 0)
                            (inher-cont     . c-lineup-java-inher)
                            (func-decl-cont . c-lineup-java-throws)
                            (arglist-cont-nonempty . ++)
                            )))
      "Eclipse Java Programming Style")
    (c-add-style "ECLIPSE" eclipse-java-style)
    (customize-set-variable 'c-default-style (quote ((java-mode . "eclipse") (awk-mode . "awk") (other . "gnu"))))
    

    【讨论】:

    • 相关帖子stackoverflow.com/questions/5556558/… 涉及非常基本的制表符和空格问题,它解决了前 90%。据我所知,上面发布的答案试图解决剩余 10% 的问题。
    • 我觉得这很有帮助!我必须对其进行一些更改以获得我想要的行为——我希望能够在方法调用本身之后的行上启动参数列表。在 (arglist-cont-nonempty . +) 之后添加 (arglist-intro . +) (arglist-cont . +)。 (我也把 ++ 改成了 +)
    【解决方案2】:

    需要在java模式下自定义缩进。看看hereherehere

    【讨论】:

    • 是的,由于 Eclipse 缩进的一些特殊性(或者至少是他们使用的缩进样式),这是我试图避免的那种基本要素。我希望有人已经编写了一个 Eclipse 风格的程序,我可以直接加入。感谢您的参考。
    • 好吧,如果你完成了 - 你能发布 alist 吗?甚至可能在这里?
    • 请参阅stackoverflow.com/questions/5556558/… 以获得良好、简单的开始。正如 Arkadiy 所说,如果有人指出这一点,请发布!
    【解决方案3】:

    老实说:我不确定您所说的使 Eclipse 的缩进匹配 Emacs 的确切含义。但是你可以在这里修改缩进: Windows->Preferences-General->Editors

    单击“文本编辑器”选项,您将看到选项卡宽度属性。

    还有一点需要注意的是,Eclipse 内置了 Emacs 样式的键绑定: Windows->Preferences->General->Keys

    在“方案”下,有一个 Emacs 选项。

    注意:您还可以在此处修改 Java 代码的格式: Windows->Preferences->Java->Code Style->Formatter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多