Commit 69eb3457 authored by Dominik Honnef's avatar Dominik Honnef Committed by Brad Fitzpatrick

misc/emacs: Fix indentation for code following multiline function declarations

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
parent b4afe889
...@@ -233,6 +233,28 @@ STOP-AT-STRING is not true, over strings." ...@@ -233,6 +233,28 @@ STOP-AT-STRING is not true, over strings."
(puthash cur-line val go-dangling-cache)))) (puthash cur-line val go-dangling-cache))))
val)) 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) (defun go-goto-opening-parenthesis (&optional char)
(let ((start-nesting (go-paren-level))) (let ((start-nesting (go-paren-level)))
(while (and (not (bobp)) (while (and (not (bobp))
...@@ -245,6 +267,20 @@ STOP-AT-STRING is not true, over strings." ...@@ -245,6 +267,20 @@ STOP-AT-STRING is not true, over strings."
(go-goto-beginning-of-string-or-comment) (go-goto-beginning-of-string-or-comment)
(backward-char)))))) (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 () (defun go-indentation-at-point ()
(save-excursion (save-excursion
(let (start-nesting (outindent 0)) (let (start-nesting (outindent 0))
...@@ -258,7 +294,7 @@ STOP-AT-STRING is not true, over strings." ...@@ -258,7 +294,7 @@ STOP-AT-STRING is not true, over strings."
(go-goto-opening-parenthesis (char-after)) (go-goto-opening-parenthesis (char-after))
(if (go-previous-line-has-dangling-op-p) (if (go-previous-line-has-dangling-op-p)
(- (current-indentation) tab-width) (- (current-indentation) tab-width)
(current-indentation))) (go--indentation-for-opening-parenthesis)))
((progn (go--backward-irrelevant t) (looking-back go-dangling-operators-regexp)) ((progn (go--backward-irrelevant t) (looking-back go-dangling-operators-regexp))
;; only one nesting for all dangling operators in one operation ;; only one nesting for all dangling operators in one operation
(if (go-previous-line-has-dangling-op-p) (if (go-previous-line-has-dangling-op-p)
...@@ -269,7 +305,7 @@ STOP-AT-STRING is not true, over strings." ...@@ -269,7 +305,7 @@ STOP-AT-STRING is not true, over strings."
((progn (go-goto-opening-parenthesis) (< (go-paren-level) start-nesting)) ((progn (go-goto-opening-parenthesis) (< (go-paren-level) start-nesting))
(if (go-previous-line-has-dangling-op-p) (if (go-previous-line-has-dangling-op-p)
(current-indentation) (current-indentation)
(+ (current-indentation) tab-width))) (+ (go--indentation-for-opening-parenthesis) tab-width)))
(t (t
(current-indentation)))))) (current-indentation))))))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment