summaryrefslogtreecommitdiff
path: root/.emacs.d/lisp/rust-utils.el
diff options
context:
space:
mode:
Diffstat (limited to '.emacs.d/lisp/rust-utils.el')
-rw-r--r--.emacs.d/lisp/rust-utils.el91
1 files changed, 91 insertions, 0 deletions
diff --git a/.emacs.d/lisp/rust-utils.el b/.emacs.d/lisp/rust-utils.el
new file mode 100644
index 0000000..41f1ba8
--- /dev/null
+++ b/.emacs.d/lisp/rust-utils.el
@@ -0,0 +1,91 @@
1;;; rust-utils.el --- Various Rust utilities -*- lexical-binding:t -*-
2;;; Commentary:
3
4;; This library implements various utilities for dealing with Rust
5;; code.
6
7;;; Code:
8
9(require 'thingatpt)
10
11(require 'rust-mode) ; for `rust-in-str' and `rust-looking-back-str'
12
13;;; Promote module
14
15(defun rust-promote-module-into-dir ()
16 "Promote the module file visited by the current buffer into its own directory.
17
18For example, if the current buffer is visiting the file `foo.rs',
19then this function creates the directory `foo' and renames the
20file to `foo/mod.rs'. The current buffer will be updated to
21visit the new file."
22 (interactive)
23 (let ((filename (buffer-file-name)))
24 (if (not filename)
25 (message "Buffer is not visiting a file.")
26 (if (string-equal (file-name-nondirectory filename) "mod.rs")
27 (message "Won't promote a module file already named mod.rs.")
28 (let* ((basename (file-name-sans-extension
29 (file-name-nondirectory filename)))
30 (mod-dir (file-name-as-directory
31 (concat (file-name-directory filename) basename)))
32 (new-name (concat mod-dir "mod.rs")))
33 (mkdir mod-dir t)
34 (rename-file filename new-name 1)
35 (set-visited-file-name new-name))))))
36
37;;; dbg! macro
38
39(defun rust-insert-dbg ()
40 "Insert the dbg! macro."
41 (cond ((region-active-p)
42 (when (< (mark) (point))
43 (exchange-point-and-mark))
44 (let ((old-point (point)))
45 (insert-parentheses)
46 (goto-char old-point)))
47 (t
48 (when (rust-in-str)
49 (up-list -1 t t))
50 (insert "(")
51 (forward-sexp)
52 (insert ")")
53 (backward-sexp)))
54 (insert "dbg!"))
55
56;;;###autoload
57(defun rust-dbg-wrap-or-unwrap ()
58 "Either remove or add the dbg! macro."
59 (interactive)
60 (save-excursion
61 (if (region-active-p)
62 (rust-insert-dbg)
63
64 (let ((beginning-of-symbol (ignore-errors (beginning-of-thing 'symbol))))
65 (when beginning-of-symbol
66 (goto-char beginning-of-symbol)))
67
68 (let ((dbg-point (save-excursion
69 (or (and (looking-at-p "dbg!") (+ 4 (point)))
70 (ignore-errors
71 (while (not (rust-looking-back-str "dbg!"))
72 (backward-up-list))
73 (point))))))
74 (cond (dbg-point
75 (goto-char dbg-point)
76 (delete-char -4)
77 (delete-pair))
78 (t (rust-insert-dbg)))))))
79
80(defun rust-toggle-mutability ()
81 "Toggles the mutability of the variable defined on the current line"
82 (interactive)
83 (save-excursion
84 (back-to-indentation)
85 (forward-word)
86 (if (string= " mut" (buffer-substring (point) (+ (point) 4)))
87 (delete-region (point) (+ (point) 4))
88 (insert " mut"))))
89;;; _
90(provide 'rust-utils)
91;;; rust-utils.el ends here