branch: elpa/swift-mode
commit 1970f1d2358885b837100bc1135ab116776e13cf
Author: taku0 <mxxouy6x3m_git...@tatapa.org>
Commit: taku0 <mxxouy6x3m_git...@tatapa.org>

    Fix `beginning-of-defun' inside class methods
---
 swift-mode-beginning-of-defun.el                   | 22 ++++++++++++++++++----
 .../beginning-of-defun/beginning-of-defun.swift    |  2 +-
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/swift-mode-beginning-of-defun.el b/swift-mode-beginning-of-defun.el
index 0bba2b5..daaf643 100644
--- a/swift-mode-beginning-of-defun.el
+++ b/swift-mode-beginning-of-defun.el
@@ -167,19 +167,33 @@ The cursor must be at the beginning of a statement."
   (let ((token (swift-mode:forward-token-or-list))
         (defun-keywords
           '("import" "typealias" "associatedtype"
-            "enum" "struct" "class" "protocol" "extension"
+            "enum" "struct" "protocol" "extension"
             "func" "init" "deinit" "subscript" "get" "set" "willSet" "didSet"
             "prefix" "postfix" "infix" "precedencegroup"
             "var" "let"
             "case"))
         (stop-tokens '(\; implicit-\; {} } \) \]
-                       anonymous-function-parameter-in outside-of-buffer)))
+                       anonymous-function-parameter-in outside-of-buffer))
+        (class-token nil))
     (while (not (or
                  (memq (swift-mode:token:type token) stop-tokens)
                  (member (swift-mode:token:text token) defun-keywords)))
+      ;; "class" token may be either a class declaration keyword or a modifier:
+      ;;
+      ;; // Nested class named "final"
+      ;; class Foo { class final {} }
+      ;;
+      ;; // Nonoverridable class method named "foo"
+      ;; class Foo { class final func foo() {} }
+      ;;
+      ;; Keeps scanning and returns the token if there are no other
+      ;; `defun-keywords'.
+      (when (equal (swift-mode:token:text token) "class")
+        (setq class-token token))
       (setq token (swift-mode:forward-token-or-list)))
-    (when (member (swift-mode:token:text token) defun-keywords)
-      token)))
+    (if (member (swift-mode:token:text token) defun-keywords)
+        token
+      class-token)))
 
 (defun swift-mode:class-like-member-p ()
   "Return t if the cursor is on a member of a class-like declaration.
diff --git a/test/swift-files/beginning-of-defun/beginning-of-defun.swift 
b/test/swift-files/beginning-of-defun/beginning-of-defun.swift
index e4e1dfb..b8b4d2d 100644
--- a/test/swift-files/beginning-of-defun/beginning-of-defun.swift
+++ b/test/swift-files/beginning-of-defun/beginning-of-defun.swift
@@ -58,7 +58,7 @@ let
         }/*}*/
     }/*}*/
 
-    /*{*/func foo() {
+    /*{*/class func foo() {
         let x = foo()
         let
           y

Reply via email to