From: Dominik Honnef Date: Wed, 13 Mar 2013 04:37:18 +0000 (-0700) Subject: misc/emacs: Fix indentation for code following multiline function declarations X-Git-Tag: go1.1rc2~532 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=69eb3457279b1067e3c45e735d32916a41b87246;p=gostls13.git 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 --- diff --git a/misc/emacs/go-mode.el b/misc/emacs/go-mode.el index 68ded4906f..b1dd0d5d9c 100644 --- a/misc/emacs/go-mode.el +++ b/misc/emacs/go-mode.el @@ -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))))))