emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/go-mode 5c3a797 129/495: Handle indentation for multi-line


From: ELPA Syncer
Subject: [nongnu] elpa/go-mode 5c3a797 129/495: Handle indentation for multi-line function declarations correctly
Date: Sat, 7 Aug 2021 09:04:58 -0400 (EDT)

branch: elpa/go-mode
commit 5c3a797f993efbb31a96aad40eb28f64d8faa73d
Author: Dominik Honnef <dominikh@fork-bomb.org>
Commit: Dominik Honnef <dominikh@fork-bomb.org>

    Handle indentation for multi-line function declarations correctly
    
    Closes gh-15
---
 go-mode.el | 40 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)

diff --git a/go-mode.el b/go-mode.el
index 2fe519e..bf57475 100644
--- a/go-mode.el
+++ b/go-mode.el
@@ -237,6 +237,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))
@@ -249,6 +271,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)
@@ -262,7 +298,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)
@@ -273,7 +309,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))))))
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]