Skip to content

Commit 3eb6d2f

Browse files
tarsiusbrotzeit
authored andcommitted
Create rust-utils.el from existing code
1 parent 649f492 commit 3eb6d2f

File tree

3 files changed

+85
-67
lines changed

3 files changed

+85
-67
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ELS += rust-cargo.el
1414
ELS += rust-compile.el
1515
ELS += rust-playpen.el
1616
ELS += rust-rustfmt.el
17+
ELS += rust-utils.el
1718
ELCS = $(ELS:.el=.elc)
1819

1920
DEPS =

rust-mode.el

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
(eval-when-compile (require 'rx))
1919

20-
(require 'thingatpt)
21-
2220
(defvar electric-pair-inhibit-predicate)
2321
(defvar electric-pair-skip-self)
2422
(defvar electric-indent-chars)
@@ -1551,71 +1549,6 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
15511549
;; There is no opening brace, so consider the whole buffer to be one "defun"
15521550
(goto-char (point-max))))
15531551

1554-
;;; Secondary Commands
1555-
1556-
(defun rust-promote-module-into-dir ()
1557-
"Promote the module file visited by the current buffer into its own directory.
1558-
1559-
For example, if the current buffer is visiting the file `foo.rs',
1560-
then this function creates the directory `foo' and renames the
1561-
file to `foo/mod.rs'. The current buffer will be updated to
1562-
visit the new file."
1563-
(interactive)
1564-
(let ((filename (buffer-file-name)))
1565-
(if (not filename)
1566-
(message "Buffer is not visiting a file.")
1567-
(if (string-equal (file-name-nondirectory filename) "mod.rs")
1568-
(message "Won't promote a module file already named mod.rs.")
1569-
(let* ((basename (file-name-sans-extension
1570-
(file-name-nondirectory filename)))
1571-
(mod-dir (file-name-as-directory
1572-
(concat (file-name-directory filename) basename)))
1573-
(new-name (concat mod-dir "mod.rs")))
1574-
(mkdir mod-dir t)
1575-
(rename-file filename new-name 1)
1576-
(set-visited-file-name new-name))))))
1577-
1578-
(defun rust-insert-dbg ()
1579-
"Insert the dbg! macro."
1580-
(cond ((region-active-p)
1581-
(when (< (mark) (point))
1582-
(exchange-point-and-mark))
1583-
(let ((old-point (point)))
1584-
(insert-parentheses)
1585-
(goto-char old-point)))
1586-
(t
1587-
(when (rust-in-str)
1588-
(up-list -1 t t))
1589-
(insert "(")
1590-
(forward-sexp)
1591-
(insert ")")
1592-
(backward-sexp)))
1593-
(insert "dbg!"))
1594-
1595-
;;;###autoload
1596-
(defun rust-dbg-wrap-or-unwrap ()
1597-
"Either remove or add the dbg! macro."
1598-
(interactive)
1599-
(save-excursion
1600-
(if (region-active-p)
1601-
(rust-insert-dbg)
1602-
1603-
(let ((beginning-of-symbol (ignore-errors (beginning-of-thing 'symbol))))
1604-
(when beginning-of-symbol
1605-
(goto-char beginning-of-symbol)))
1606-
1607-
(let ((dbg-point (save-excursion
1608-
(or (and (looking-at-p "dbg!") (+ 4 (point)))
1609-
(ignore-errors
1610-
(while (not (rust-looking-back-str "dbg!"))
1611-
(backward-up-list))
1612-
(point))))))
1613-
(cond (dbg-point
1614-
(goto-char dbg-point)
1615-
(delete-char -4)
1616-
(delete-pair))
1617-
(t (rust-insert-dbg)))))))
1618-
16191552
;;; _
16201553

16211554
(defun rust-mode-reload ()
@@ -1625,4 +1558,6 @@ visit the new file."
16251558
(rust-mode))
16261559

16271560
(provide 'rust-mode)
1561+
(require 'rust-utils)
1562+
16281563
;;; rust-mode.el ends here

rust-utils.el

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
18+
For example, if the current buffer is visiting the file `foo.rs',
19+
then this function creates the directory `foo' and renames the
20+
file to `foo/mod.rs'. The current buffer will be updated to
21+
visit 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+
;;; _
81+
(provide 'rust-utils)
82+
;;; rust-utils.el ends here

0 commit comments

Comments
 (0)