Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse indentation on Emacs

I'm an emacs user who just started working for a new company where eclipse is the standard. I've tried eclipse, but I want to also experiment with JDEE (I'm coming back to Java after a long hiatus). The major stumbling block so far is getting the indentation to match. Is there an easy way to do this, or am I going to need to dig deeply into the nuts and bolts of emacs indentation?

EDIT: sorry for the confusion in this question: I do not want to get Eclipse to mimic emacs, I want emacs to mimic Eclipse. I want to be able to use emacs to modify code without screwing up the indentation that the Eclipse users expect.

like image 495
user98166 Avatar asked Apr 30 '09 01:04

user98166


1 Answers

The main difference I found between Eclipse (and IntelliJ) and Emacs default java formatting is that Emacs lines up function arguments continued onto a new line with the previous arguments e.g. emacs does:

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

and Eclipse does:

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

The following added to you ~/.emacs file will make Emacs java-mode do the same:

;; 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"))))
like image 60
user563439 Avatar answered Sep 19 '22 06:09

user563439