2

Currently my team (non-Emacs users) indent java like so:

public DiskThresholdDecider(Settings settings,
                            NodeSettingsService nodeSettingsService,
                            ClusterInfoService infoService,
                            Client client) {
    String lowWatermark = settings.get(CLUSTER_ROUTING_ALLOCATION_LOW_DISK_WATERMARK,
            "85%");

There are two different indentation settings for the same c-style offset, for example (| denotes the cursor):

public DiskThresholdDecider(Settings settings,
                            |NodeSettingsService nodeSettingsService,
                            ClusterInfoService infoService,
                            Client client) {

C-c C-s (c-show-syntactic-information) returns (arglist-cont-nonempty ... ...)

Just a couple of lines down, on the method invocation, C-c C-s also returns (arglist-cont-nonempty ... ...) here:

    String lowWatermark = settings.get(CLUSTER_ROUTING_ALLOCATION_LOW_DISK_WATERMARK,
            |"85%");

In my configuration, I have this set up as:

(defconst intellij-java-style
  '((c-basic-offset . 4)
    (c-comment-only-line-offset . (0 . 0))
    (c-offsets-alist
     .
     ((inline-open . 0)
      (topmost-intro-cont    . +)
      (statement-block-intro . +)
      (knr-argdecl-intro     . +)
      (substatement-open     . +)
      (substatement-label    . +)
      (case-label            . +)
      (label                 . +)
      (statement-case-open   . +)
      (statement-cont        . ++)
      (arglist-intro         . 0)
      (arglist-cont-nonempty . ++) ;; <-- the actual setting
      (arglist-close         . --)
      (inexpr-class          . 0)
      (access-label          . 0)
      (inher-intro           . ++)
      (inher-cont            . ++)
      (brace-list-intro      . +)
      (func-decl-cont        . ++))))
  "Elasticsearch's Intellij Java Programming Style")

The problem is I need Emacs to treat class constructors as different indentation style than regular method invocations. This is so it will line up lines on the ( only for constructors.

Is there a function I can use to set for arglist-cont-nonempty that will indent this correctly or a way to do this?

2
  • Would it be sufficient to detect the arguments in a function declaration, as opposed to arguments of a constructor?
    – PythonNut
    Commented Oct 20, 2015 at 16:27
  • @PythonNut that would be perfect, in fact function declarations should indent the same way as constructor calls
    – Lee H
    Commented Oct 20, 2015 at 17:44

1 Answer 1

4
+50

Try the following:

(defun my/point-in-defun-declaration-p ()
  (let ((bod (save-excursion (c-beginning-of-defun)
                             (point))))
    (<= bod
        (point)
        (save-excursion (goto-char bod)
                        (re-search-forward "{")
                        (point)))))

(defun my/arglist-cont-nonempty-indentation (arg)
  (unless (my/point-in-defun-declaration-p) '++))

(push '(arglist-cont-nonempty 
        my/arglist-cont-nonempty-indentation 
        c-lineup-gcc-asm-reg 
        c-lineup-arglist) 
      c-offsets-alist)

I tested it, and it appears to be working.

2
  • This is great! I have modified it slightly to handle all the edge cases Java indentation unfortunately has. Full version here: gist.github.com/dakrone/0b4888de1ca142641b28
    – Lee H
    Commented Oct 21, 2015 at 3:09
  • @LeeH great to hear. Man, this is why I love LISP. :)
    – PythonNut
    Commented Oct 21, 2015 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.