Skip to content

Commit f9f4bc5

Browse files
committed
Fix beginning/end-of-defun
1 parent 848d088 commit f9f4bc5

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed

swift-mode-beginning-of-defun.el

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,46 @@
3939
(let (result
4040
pos)
4141
(if (<= 0 arg)
42-
(while (< 0 arg)
43-
(setq result (swift-mode:beginning-of-defun-1
44-
#'swift-mode:backward-token-or-list))
45-
(setq arg (1- arg)))
42+
(progn
43+
(setq pos (point))
44+
;; Special handling for the case that the cursor is between the
45+
;; beginning of the defun and the open curly brace of the defun.
46+
(when (< (save-excursion
47+
(swift-mode:beginning-of-statement)
48+
(point))
49+
(point))
50+
;; Searches forward { or end of a statement.
51+
(while (not
52+
(memq
53+
(swift-mode:token:type (swift-mode:forward-token-or-list))
54+
'({} implicit-\; \; } outside-of-buffer))))
55+
(when (eq (char-before) ?})
56+
(backward-list))
57+
;; Skips implicit ;
58+
(forward-comment (point-max))
59+
(if (swift-mode:is-point-before-body-of-defun)
60+
(progn
61+
(swift-mode:beginning-of-statement)
62+
(setq result t)
63+
(setq arg (1- arg)))
64+
(goto-char pos)))
65+
(while (< 0 arg)
66+
(setq result (swift-mode:beginning-of-defun-1
67+
#'swift-mode:backward-token-or-list))
68+
(setq arg (1- arg))))
4669
(while (< arg 0)
70+
;; If the cursor is on a defun, ensure the cursor is after the open
71+
;; curly brace of defun.
4772
(setq pos (point))
48-
4973
(swift-mode:beginning-of-statement)
50-
74+
;; swift-mode:beginning-of-statement may forward the cursor if the
75+
;; cursor is on a comment or whitespace. In that case, does not skip
76+
;; the defun.
5177
(when (<= (point) pos)
5278
(while (not
5379
(memq
5480
(swift-mode:token:type (swift-mode:forward-token-or-list))
55-
'({} outside-of-buffer)))))
81+
'({} } outside-of-buffer)))))
5682

5783
(setq result (swift-mode:beginning-of-defun-1
5884
(lambda ()
@@ -109,12 +135,23 @@
109135
Intended for internal use."
110136
(let ((parent (swift-mode:backward-sexps-until
111137
swift-mode:statement-parent-tokens)))
112-
(forward-comment (point-max))
113-
(swift-mode:goto-non-comment-bol)
114-
(when (< (point) (swift-mode:token:end parent))
115-
(goto-char (swift-mode:token:end parent)))
116-
(swift-mode:skip-whitespaces)))
117-
138+
(if (and
139+
(eq (swift-mode:token:type parent) 'implicit-\;)
140+
(save-excursion
141+
(goto-char (swift-mode:token:end parent))
142+
(eq
143+
(swift-mode:token:type (swift-mode:forward-token))
144+
'{)))
145+
(progn
146+
(forward-comment (- (point)))
147+
(swift-mode:beginning-of-statement))
148+
(goto-char (swift-mode:token:end parent))
149+
(setq parent (save-excursion (swift-mode:backward-token)))
150+
(forward-comment (point-max))
151+
(swift-mode:goto-non-comment-bol)
152+
(when (< (point) (swift-mode:token:end parent))
153+
(goto-char (swift-mode:token:end parent)))
154+
(swift-mode:skip-whitespaces))))
118155

119156
(defun swift-mode:end-of-defun (&optional arg)
120157
"Move forward to the end of a defun."

0 commit comments

Comments
 (0)