From 1d4a75f84865356a3cf6dbc1b0b32ec43496446e Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Wed, 2 Aug 2017 12:53:43 -0600 Subject: Make rust-beginning-of-defun ignore comments and strings Change rust-beginning-of-defun to keep searching when it stops in a comment or a string. Fixes #222. --- rust-mode.el | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'rust-mode.el') diff --git a/rust-mode.el b/rust-mode.el index 46231fd..33e6b28 100644 --- a/rust-mode.el +++ b/rust-mode.el @@ -1198,8 +1198,22 @@ This is written mainly to be used as `beginning-of-defun-function' for Rust. Don't move to the beginning of the line. `beginning-of-defun', which calls this, does that afterwards." (interactive "p") - (re-search-backward (concat "^\\(" rust-top-item-beg-re "\\)") - nil 'move (or arg 1))) + (let* ((arg (or arg 1)) + (magnitude (abs arg)) + (sign (if (< arg 0) -1 1))) + ;; If moving forward, don't find the defun we might currently be + ;; on. + (when (< sign 0) + (end-of-line)) + (catch 'done + (dotimes (_ magnitude) + ;; Search until we find a match that is not in a string or comment. + (while (if (re-search-backward (concat "^\\(" rust-top-item-beg-re "\\)") + nil 'move sign) + (rust-in-str-or-cmnt) + ;; Did not find it. + (throw 'done nil))))) + t)) (defun rust-end-of-defun () "Move forward to the next end of defun. -- cgit v1.2.3