-
Notifications
You must be signed in to change notification settings - Fork 12
/
rustic-interaction.el
71 lines (60 loc) · 2.23 KB
/
rustic-interaction.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
;;; rustic-interaction.el --- Common interactive functions -*-lexical-binding: t-*-
;;; Code:
(require 'newcomment)
(require 'rustic)
;;; Miscellaneous
(defun rustic-docstring-dwim ()
"Use `comment-dwim' to make a docstring."
(interactive)
(let ((comment-start "/// "))
(call-interactively 'comment-dwim)))
;;;###autoload
(defun rustic-open-dependency-file ()
"Open the 'Cargo.toml' file at the project root if the current buffer is
visiting a project."
(interactive)
(let ((workspace (rustic-buffer-crate t)))
(if workspace
(find-file (concat workspace "/Cargo.toml"))
(message "The current buffer is not inside a rust project!"))))
;;; Defun Motions
;; TODO: check if we should use `rustic-func-item-beg-re' or
;; `rust-top-item-beg-re' here. It seems there was a reason
;; that we added an extra regexp for funcs.
(defvar rustic-func-item-beg-re
(concat "^"
"\\(?:\\s-*"
(regexp-opt '("priv" "pub")) "\\s-*"
"\\(?:(\\s-*\\(?:in\\s-+\\)?" (regexp-opt '("crate" "self" "super")) "\\s-*)\\)?"
"\\)?"
"\\(?:\\s-*" (regexp-opt '("async" "const")) "\\s-+\\)?"
"\\(?:\\s-*unsafe\\s-+\\)?"
"\\(?:\\s-*extern\\(?:\\s-+\"C\"\\)?\\s-+\\)?"
"\\s-*fn\\s-+")
"Start of a rust function.")
(defun rustic-beginning-of-function ()
"Move to beginning of rust function at point."
(rustic-beginning-of-defun 1))
;; this function only differs from the rust-mode version in applying
;; `rustic-func-item-beg-re'
(defun rustic-beginning-of-defun (&optional arg)
(interactive "p")
(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 "^\\(" rustic-func-item-beg-re "\\)")
nil 'move sign)
(rust-in-str-or-cmnt)
;; Did not find it.
(throw 'done nil)))))
t))
;;; _
(provide 'rustic-interaction)
;;; rustic-interaction.el ends here