]> Cypherpunks repositories - gostls13.git/commitdiff
misc/emacs: Fix indentation for code following multiline function declarations
authorDominik Honnef <dominik.honnef@gmail.com>
Wed, 13 Mar 2013 04:37:18 +0000 (21:37 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 13 Mar 2013 04:37:18 +0000 (21:37 -0700)
Correctly indent the body of functions that have been declared
over multiple lines. See http://play.golang.org/p/MHMwNDbFyf for
an example.

Previously, the body of the function would be indented as deep as
the continuation line of the function declaration. Now it gets
indented as deep as the func keyword.

R=adonovan, cw, patrick.allen.higgins
CC=golang-dev
https://golang.org/cl/7628043

misc/emacs/go-mode.el

index 68ded4906f6fd249e2de9d5ebf17c11ee84e692f..b1dd0d5d9c6cb081100c830e5df99998103ab00f 100644 (file)
@@ -233,6 +233,28 @@ STOP-AT-STRING is not true, over strings."
               (puthash cur-line val go-dangling-cache))))
     val))
 
+(defun go--at-function-definition ()
+  "Return non-nil if point is on the opening curly brace of a
+function definition.
+
+We do this by first calling (beginning-of-defun), which will take
+us to the start of *some* function. We then look for the opening
+curly brace of that function and compare its position against the
+curly brace we are checking. If they match, we return non-nil."
+  (if (= (char-after) ?\{)
+      (save-excursion
+        (let ((old-point (point))
+              start-nesting)
+          (beginning-of-defun)
+          (when (looking-at "func ")
+            (setq start-nesting (go-paren-level))
+            (skip-chars-forward "^{")
+            (while (> (go-paren-level) start-nesting)
+              (forward-char)
+              (skip-chars-forward "^{") 0)
+            (if (and (= (go-paren-level) start-nesting) (= old-point (point)))
+                t))))))
+
 (defun go-goto-opening-parenthesis (&optional char)
   (let ((start-nesting (go-paren-level)))
     (while (and (not (bobp))
@@ -245,6 +267,20 @@ STOP-AT-STRING is not true, over strings."
               (go-goto-beginning-of-string-or-comment)
             (backward-char))))))
 
+(defun go--indentation-for-opening-parenthesis ()
+  "Return the semantic indentation for the current opening parenthesis.
+
+If point is on an opening curly brace and said curly brace
+belongs to a function declaration, the indentation of the func
+keyword will be returned. Otherwise the indentation of the
+current line will be returned."
+  (save-excursion
+    (if (go--at-function-definition)
+        (progn
+          (beginning-of-defun)
+          (current-indentation))
+      (current-indentation))))
+
 (defun go-indentation-at-point ()
   (save-excursion
     (let (start-nesting (outindent 0))
@@ -258,7 +294,7 @@ STOP-AT-STRING is not true, over strings."
         (go-goto-opening-parenthesis (char-after))
         (if (go-previous-line-has-dangling-op-p)
             (- (current-indentation) tab-width)
-          (current-indentation)))
+          (go--indentation-for-opening-parenthesis)))
        ((progn (go--backward-irrelevant t) (looking-back go-dangling-operators-regexp))
         ;; only one nesting for all dangling operators in one operation
         (if (go-previous-line-has-dangling-op-p)
@@ -269,7 +305,7 @@ STOP-AT-STRING is not true, over strings."
        ((progn (go-goto-opening-parenthesis) (< (go-paren-level) start-nesting))
         (if (go-previous-line-has-dangling-op-p)
             (current-indentation)
-          (+ (current-indentation) tab-width)))
+          (+ (go--indentation-for-opening-parenthesis) tab-width)))
        (t
         (current-indentation))))))