summaryrefslogtreecommitdiff
path: root/rust-mode.el
diff options
context:
space:
mode:
authorS Pradeep Kumar <gohanpra@gmail.com>2013-12-07 12:44:12 +0900
committerS Pradeep Kumar <gohanpra@gmail.com>2013-12-09 14:36:56 +0900
commit5e34201c167f5d976b75c66434c0e1e57bd25804 (patch)
tree47031c618e9d8c1eb77722b8c0cd4db8140c2147 /rust-mode.el
parentd6b2c329a4951940f6c48dedb095de97790d66b9 (diff)
downloadrust-mode-5e34201c167f5d976b75c66434c0e1e57bd25804.tar.gz
Add defun motions for rust-mode.
Specifically, we can now use: + beginning-of-defun + end-of-defun + mark-defun where "defun" means a Rust item. + Add tests in rust-mode-tests.el + Fix indentation in rust-mode-tests.el + Add support for trait to Imenu
Diffstat (limited to 'rust-mode.el')
-rw-r--r--rust-mode.el44
1 files changed, 43 insertions, 1 deletions
diff --git a/rust-mode.el b/rust-mode.el
index 2a5f2d9..66cc3c3 100644
--- a/rust-mode.el
+++ b/rust-mode.el
@@ -324,7 +324,7 @@
;;; Imenu support
(defvar rust-imenu-generic-expression
(append (loop for item in
- '("enum" "struct" "type" "mod" "fn")
+ '("enum" "struct" "type" "mod" "fn" "trait")
collect `(nil ,(rust-re-item-def item) 1))
`(("Impl" ,(rust-re-item-def "impl") 1)))
"Value for `imenu-generic-expression' in Rust mode.
@@ -335,6 +335,46 @@ Imenu will show all the enums, structs, etc. at the same level.
Implementations will be shown under the `Impl` subheading.
Use idomenu (imenu with ido-mode) for best mileage.")
+;;; Defun Motions
+
+;;; Start of a Rust item
+(setq rust-top-item-beg-re
+ (concat "^\\s-*\\(?:priv\\|pub\\)?\\s-*"
+ (regexp-opt
+ '("enum" "struct" "type" "mod" "use" "fn" "static" "impl"
+ "extern" "impl" "static" "trait"
+ ))))
+
+(defun rust-beginning-of-defun (&optional arg)
+ "Move backward to the beginning of the current defun.
+
+With ARG, move backward multiple defuns. Negative ARG means
+move forward.
+
+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 "\\)\\b")
+ nil 'move (or arg 1)))
+
+(defun rust-end-of-defun ()
+ "Move forward to the next end of defun.
+
+With argument, do it that many times.
+Negative argument -N means move back to Nth preceding end of defun.
+
+Assume that this is called after beginning-of-defun. So point is
+at the beginning of the defun body.
+
+This is written mainly to be used as `end-of-defun-function' for Rust."
+ (interactive "p")
+ ;; Find the opening brace
+ (re-search-forward "[{]" nil t)
+ (goto-char (match-beginning 0))
+ ;; Go to the closing brace
+ (forward-sexp))
+
;; For compatibility with Emacs < 24, derive conditionally
(defalias 'rust-parent-mode
(if (fboundp 'prog-mode) 'prog-mode 'fundamental-mode))
@@ -374,6 +414,8 @@ Use idomenu (imenu with ido-mode) for best mileage.")
(set (make-local-variable 'comment-multi-line) t)
(set (make-local-variable 'comment-line-break-function) 'rust-comment-indent-new-line)
(set (make-local-variable 'imenu-generic-expression) rust-imenu-generic-expression)
+ (set (make-local-variable 'beginning-of-defun-function) 'rust-beginning-of-defun)
+ (set (make-local-variable 'end-of-defun-function) 'rust-end-of-defun)
)