Skip to content

Commit 1970f1d

Browse files
committed
Fix `beginning-of-defun' inside class methods
1 parent 359efc0 commit 1970f1d

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

swift-mode-beginning-of-defun.el

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,33 @@ The cursor must be at the beginning of a statement."
167167
(let ((token (swift-mode:forward-token-or-list))
168168
(defun-keywords
169169
'("import" "typealias" "associatedtype"
170-
"enum" "struct" "class" "protocol" "extension"
170+
"enum" "struct" "protocol" "extension"
171171
"func" "init" "deinit" "subscript" "get" "set" "willSet" "didSet"
172172
"prefix" "postfix" "infix" "precedencegroup"
173173
"var" "let"
174174
"case"))
175175
(stop-tokens '(\; implicit-\; {} } \) \]
176-
anonymous-function-parameter-in outside-of-buffer)))
176+
anonymous-function-parameter-in outside-of-buffer))
177+
(class-token nil))
177178
(while (not (or
178179
(memq (swift-mode:token:type token) stop-tokens)
179180
(member (swift-mode:token:text token) defun-keywords)))
181+
;; "class" token may be either a class declaration keyword or a modifier:
182+
;;
183+
;; // Nested class named "final"
184+
;; class Foo { class final {} }
185+
;;
186+
;; // Nonoverridable class method named "foo"
187+
;; class Foo { class final func foo() {} }
188+
;;
189+
;; Keeps scanning and returns the token if there are no other
190+
;; `defun-keywords'.
191+
(when (equal (swift-mode:token:text token) "class")
192+
(setq class-token token))
180193
(setq token (swift-mode:forward-token-or-list)))
181-
(when (member (swift-mode:token:text token) defun-keywords)
182-
token)))
194+
(if (member (swift-mode:token:text token) defun-keywords)
195+
token
196+
class-token)))
183197

184198
(defun swift-mode:class-like-member-p ()
185199
"Return t if the cursor is on a member of a class-like declaration.

test/swift-files/beginning-of-defun/beginning-of-defun.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ let
5858
}/*}*/
5959
}/*}*/
6060

61-
/*{*/func foo() {
61+
/*{*/class func foo() {
6262
let x = foo()
6363
let
6464
y

0 commit comments

Comments
 (0)