branch: elpa/swift-mode commit 6d7093fb4a0b81686ffeff7e39f76027d83b7eb2 Author: taku0 <mxxouy6x3m_git...@tatapa.org> Commit: taku0 <mxxouy6x3m_git...@tatapa.org>
Improve performance of beginning/end-of-defun Improve performance of `which-function': https://github.com/swift-emacs/swift-mode/issues/180 --- swift-mode-beginning-of-defun.el | 6 +++--- swift-mode-indent.el | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/swift-mode-beginning-of-defun.el b/swift-mode-beginning-of-defun.el index 40a76a6845..079db0f7e7 100644 --- a/swift-mode-beginning-of-defun.el +++ b/swift-mode-beginning-of-defun.el @@ -151,7 +151,7 @@ The cursor must be at the beginning of a statement." ((member (swift-mode:token:text token) '("var" "let")) (when (swift-mode:class-like-member-p) token)) ((equal (swift-mode:token:text token) "case") - (swift-mode:backward-sexps-until '({)) + (swift-mode:backward-sexps-until-open-curly-brace) (swift-mode:beginning-of-statement) (let ((parent-token (swift-mode:find-defun-keyword-simple))) (when (equal (swift-mode:token:text parent-token) "enum") @@ -201,7 +201,7 @@ The cursor must be at the beginning of a statement." Also return t if the cursor is on a global declaration. Return nil otherwise." (or - (let ((parent (swift-mode:backward-sexps-until '({)))) + (let ((parent (swift-mode:backward-sexps-until-open-curly-brace))) (eq (swift-mode:token:type parent) 'outside-of-buffer)) (progn (swift-mode:beginning-of-statement) @@ -1391,7 +1391,7 @@ of ancestors." (if (bobp) nil (let ((name-token (swift-mode:current-defun-name-token))) - (swift-mode:backward-sexps-until '({)) + (swift-mode:backward-sexps-until-open-curly-brace) (if name-token (cons name-token (swift-mode:current-defun-name-token-list)) (swift-mode:current-defun-name-token-list))))) diff --git a/swift-mode-indent.el b/swift-mode-indent.el index 96c8015114..fb81534442 100644 --- a/swift-mode-indent.el +++ b/swift-mode-indent.el @@ -653,7 +653,7 @@ Also used for regexes." ;; After "in" for anonymous function parameters ((eq previous-type 'anonymous-function-parameter-in) (goto-char (swift-mode:token:start previous-token)) - (swift-mode:backward-sexps-until '({)) + (swift-mode:backward-sexps-until-open-curly-brace) (swift-mode:calculate-indent-after-open-curly-brace swift-mode:basic-offset)) @@ -1353,6 +1353,24 @@ is the symbol `any', it matches all tokens." (setq text (swift-mode:token:text parent))) parent)) +(defun swift-mode:backward-sexps-until-open-curly-brace () + "Backward sexps until an open curly brace appears. +Return the brace token. +When this function returns, the cursor is at the start of the token. + +If there is no open curly braces, return `outside-of-buffer' token. + +This is optimized version of (swift-mode:backward-sexps-until '({}))." + (let* ((parent-position (nth 1 (syntax-ppss)))) + (while (and parent-position + (and (goto-char parent-position) + (not (eq (char-after) ?{)))) + (setq parent-position (nth 1 (syntax-ppss)))) + (if (eq (char-after) ?{) + (save-excursion (swift-mode:forward-token)) + (goto-char (point-min)) + (swift-mode:backward-token)))) + (defun swift-mode:align-with-next-token (parent &optional offset) "Return indentation with the PARENT and OFFSET." (let ((parent-end (swift-mode:token:end parent)))