From f0a74dfbaf87f65f6fe7f63fa63965a35fa30faa Mon Sep 17 00:00:00 2001
From: Dan Gudmundsson <dgud@erlang.org>
Date: Wed, 10 Jan 2018 12:59:20 +0100
Subject: emacs: Indent delimiter first in term elements correctly

---
 lib/tools/emacs/erlang.el             | 44 +++++++++++++++++++++--------------
 lib/tools/test/emacs_SUITE_data/funcs |  5 ++++
 lib/tools/test/emacs_SUITE_data/terms | 13 +++++++++--
 3 files changed, 42 insertions(+), 20 deletions(-)

(limited to 'lib/tools')

diff --git a/lib/tools/emacs/erlang.el b/lib/tools/emacs/erlang.el
index e29d3e0482..08ef83f848 100644
--- a/lib/tools/emacs/erlang.el
+++ b/lib/tools/emacs/erlang.el
@@ -2755,19 +2755,10 @@ Return nil if inside string, t if in a comment."
                          (nth 2 stack-top))))
                  ((= (following-char) ?,)
                   ;; a comma at the start of the line: line up with opening parenthesis.
-                  (nth 2 stack-top))
+                  (min (nth 2 stack-top)
+                       (erlang-indent-element stack-top indent-point token)))
                  (t
-                  (goto-char (nth 1 stack-top))
-                  (let ((base (cond ((erlang-record-or-function-args-p)
-                                     ;; Line ends with parenthesis.
-                                     (erlang-indent-parenthesis (nth 2 stack-top)))
-                                    (t
-                                     ;; Indent to the same column as the first
-                                     ;; argument.
-                                     (goto-char (1+ (nth 1 stack-top)))
-                                     (skip-chars-forward " \t")
-                                     (current-column)))))
-                    (erlang-indent-standard indent-point token base 't)))))
+                  (erlang-indent-element stack-top indent-point token))))
           ;;
           ((eq (car stack-top) '<<)
            ;; Element of binary (possible comprehension) expression,
@@ -2776,13 +2767,11 @@ Return nil if inside string, t if in a comment."
                   (+ 2 (nth 2 stack-top)))
                  ((looking-at "\\(>>\\)[^_a-zA-Z0-9]")
                   (nth 2 stack-top))
+                 ((= (following-char) ?,)
+                  (min (+ (nth 2 stack-top) 1)
+                       (- (erlang-indent-to-first-element stack-top 2) 1)))
                  (t
-                  (goto-char (nth 1 stack-top))
-                  ;; Indent to the same column as the first
-                  ;; argument.
-                  (goto-char (+ 2 (nth 1 stack-top)))
-                  (skip-chars-forward " \t")
-                  (current-column))))
+                  (erlang-indent-to-first-element stack-top 2))))
 
           ((memq (car stack-top) '(icr fun spec))
            ;; The default indentation is the column of the option
@@ -2917,6 +2906,22 @@ Return nil if inside string, t if in a comment."
                                 (current-column))) start-alternativ))))))
           )))
 
+(defun erlang-indent-to-first-element (stack-top extra)
+  ;; Indent to the same column as the first
+  ;; argument.  extra should be 1 for lists tuples or 2 for binaries
+  (goto-char (+ (nth 1 stack-top) extra))
+  (skip-chars-forward " \t")
+  (current-column))
+
+(defun erlang-indent-element (stack-top indent-point token)
+  (goto-char (nth 1 stack-top))
+  (let ((base (cond ((erlang-record-or-function-args-p)
+                     ;; Line ends with parenthesis.
+                     (erlang-indent-parenthesis (nth 2 stack-top)))
+                    (t
+                     (erlang-indent-to-first-element stack-top 1)))))
+    (erlang-indent-standard indent-point token base 't)))
+
 (defun erlang-indent-standard (indent-point token base inside-parenthesis)
   "Standard indent when in blocks or tuple or arguments.
    Look at last thing to see in what state we are, move relative to the base."
@@ -2942,6 +2947,9 @@ Return nil if inside string, t if in a comment."
                ;; Avoid treating comments a continued line.
                ((= (following-char) ?%)
                 base)
+               ((and (= (following-char) ?,) inside-parenthesis)
+                ;; a comma at the start of the line line up with parenthesis
+                (- base 1))
                ;; Continued line (e.g. line beginning
                ;; with an operator.)
                (t
diff --git a/lib/tools/test/emacs_SUITE_data/funcs b/lib/tools/test/emacs_SUITE_data/funcs
index b0c9716f0e..f2c9f44524 100644
--- a/lib/tools/test/emacs_SUITE_data/funcs
+++ b/lib/tools/test/emacs_SUITE_data/funcs
@@ -43,6 +43,11 @@ func4(A1
      ) ->
     ok.
 
+func5(
+  A41
+ ,A42) ->
+    ok.
+
 a_function_with_a_very_very_long_name() ->
     A00 = #record{
              field1=1,
diff --git a/lib/tools/test/emacs_SUITE_data/terms b/lib/tools/test/emacs_SUITE_data/terms
index 02b2d665fd..352364a73c 100644
--- a/lib/tools/test/emacs_SUITE_data/terms
+++ b/lib/tools/test/emacs_SUITE_data/terms
@@ -64,7 +64,11 @@ binary(3) ->
 binary(4) ->
     <<
       1:8
-      ,2:8
+     ,2:8
+    >>;
+binary(5) ->
+    << 1:8
+     , 2:8
     >>.
 
 record(1) ->
@@ -84,7 +88,12 @@ record(4) ->
     #record{
        a=1
       ,b=2
-      }.
+      };
+record(Record) ->
+    Record#record{
+      a=1
+     ,b=2
+     }.
 
 map(1) ->
     #{a=>1,
-- 
cgit v1.2.3