8000 Improve performance of beginning/end-of-defun · swift-emacs/swift-mode@6d7093f · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d7093f

Browse files
committed
Improve performance of beginning/end-of-defun
Improve performance of `which-function': #180
1 parent f4529ef commit 6d7093f

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

swift-mode-beginning-of-defun.el

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ The cursor must be at the beginning of a statement."
151151
((member (swift-mode:token:text token) '("var" "let"))
152152
(when (swift-mode:class-like-member-p) token))
153153
((equal (swift-mode:token:text token) "case")
154-
(swift-mode:backward-sexps-until '({))
154+
(swift-mode:backward-sexps-until-open-curly-brace)
155155
(swift-mode:beginning-of-statement)
156156
(let ((parent-token (swift-mode:find-defun-keyword-simple)))
157157
(when (equal (swift-mode:token:text parent-token) "enum")
@@ -201,7 +201,7 @@ The cursor must be at the beginning of a statement."
201201
Also return t if the cursor is on a global declaration.
202202
Return nil otherwise."
203203
(or
204-
(let ((parent (swift-mode:backward-sexps-until '({))))
204+
(let ((parent (swift-mode:backward-sexps-until-open-curly-brace)))
205205
(eq (swift-mode:token:type parent) 'outside-of-buffer))
206206
(progn
207207
(swift-mode:beginning-of-statement)
@@ -1391,7 +1391,7 @@ of ancestors."
13911391
(if (bobp)
13921392
nil
13931393
(let ((name-token (swift-mode:current-defun-name-token)))
1394-
(swift-mode:backward-sexps-until '({))
1394+
(swift-mode:backward-sexps-until-open-curly-brace)
13951395
(if name-token
13961396
(cons name-token (swift-mode:current-defun-name-token-list))
13971397
(swift-mode:current-defun-name-token-list)))))

swift-mode-indent.el

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ Also used for regexes."
653653
;; After "in" for anonymous function parameters
654654
((eq previous-type 'anonymous-function-parameter-in)
655655
(goto-char (swift-mode:token:start previous-token))
656-
(swift-mode:backward-sexps-until '({))
656+
(swift-mode:backward-sexps-until-open-curly-brace)
657657
(swift-mode:calculate-indent-after-open-curly-brace
658658
swift-mode:basic-offset))
659659

@@ -1353,6 +1353,24 @@ is the symbol `any', it matches all tokens."
13531353
(setq text (swift-mode:token:text parent)))
13541354
parent))
13551355

1356+
(defun swift-mode:backward-sexps-until-open-curly-brace ()
1357+
"Backward sexps until an open curly brace appears.
1358+
Return the brace token.
1359+
When this function returns, the cursor is at the start of the token.
1360+
1361+
If there is no open curly braces, return `outside-of-buffer' token.
1362+
1363+
This is optimized version of (swift-mode:backward-sexps-until '({}))."
1364+
(let* ((parent-position (nth 1 (syntax-ppss))))
1365+
(while (and parent-position
1366+
(and (goto-char parent-position)
1367+
(not (eq (char-after) ?{))))
1368+
(setq parent-position (nth 1 (syntax-ppss))))
1369+
(if (eq (char-after) ?{)
1370+
(save-excursion (swift-mode:forward-token))
1371+
(goto-char (point-min))
1372+
(swift-mode:backward-token))))
1373+
13561374
(defun swift-mode:align-with-next-token (parent &optional offset)
13571375
"Return indentation with the PARENT and OFFSET."
13581376
(let ((parent-end (swift-mode:token:end parent)))

0 commit comments

Comments
 (0)
0