Skip to content

Commit 48e5470

Browse files
committed
checkdoc madness
1 parent 9ffe5dd commit 48e5470

File tree

1 file changed

+64
-52
lines changed

1 file changed

+64
-52
lines changed

rust-auto-use.el

Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,80 @@
1-
(defun rust-auto-use ()
2-
(interactive)
3-
(save-excursion
4-
(rust-auto-use/insert-use-line (rust-auto-use/deduce-use-line))))
5-
6-
(global-set-key (kbd "C-<f11>") 'rust-auto-use)
1+
;;; rust-auto-use.el --- Automatically inserts Rust `use` lines
72

3+
;;; Homepage: https://github.com/vmalloc/rust-auto-use.el
84

9-
(defun rust-auto-use/insert-use-line (use-line)
10-
(save-excursion
11-
(beginning-of-buffer)
12-
(if (not (search-forward-regexp "^use" nil t))
13-
(progn
14-
(while (or (looking-at "/")
15-
(looking-at "extern"))
16-
(forward-line)
17-
(beginning-of-line))
18-
(newline)
19-
(forward-line -1)))
20-
21-
22-
(end-of-line)
23-
(newline)
24-
(insert use-line)))
5+
;;; Version: 0.1.0
256

7+
;;; Commentary:
8+
;; Given a current symbol, this package allows developers to automatically insert Rust `use` statements for importing the symbol.
269

27-
(defun rust-auto-use/deduce-use-line ()
28-
(let ((symbol (symbol-at-point)))
29-
(let ((cached-result (gethash symbol rust-auto-use/symbol-cache)))
30-
(if cached-result
31-
cached-result
32-
(rust-auto-use/save-result symbol (rust-auto-use/parse-import-string-from-symbol symbol))))))
10+
;;; Code:
3311

12+
(defvar rust-auto-use--symbol-cache)
3413

35-
(defun rust-auto-use/parse-import-string-from-symbol (symbol)
36-
(format "use %s::%s;" (read-string (format "import %s from? " symbol)) symbol))
14+
(defvar rust-auto-use-cache-filename (concat user-emacs-directory "e.rust-auto-use-cache"))
3715

16+
(defun rust-auto-use ()
17+
"Attempts to insert a required `use` statement for the symbol at point."
18+
(interactive)
19+
(save-excursion (rust-auto-use--insert-use-line (rust-auto-use--deduce-use-line))))
3820

39-
(defun rust-auto-use/save-result (symbol result)
40-
(progn
41-
(puthash symbol result rust-auto-use/symbol-cache)
42-
(rust-auto-use/dump-symbol-cache))
43-
result
44-
)
21+
(global-set-key (kbd "C-<f11>") 'rust-auto-use)
4522

46-
(defvar rust-auto-use-cache-filename "~/.emacs.d/.rust-auto-use-cache")
4723

48-
(defun rust-auto-use/dump-symbol-cache ()
49-
(with-temp-file rust-auto-use-cache-filename
50-
(emacs-lisp-mode)
51-
(insert ";; this file was automatically generated by rust-auto-use.el")
52-
(newline)
53-
(insert "(setq rust-auto-use/symbol-cache (make-hash-table :test 'equal))")
24+
(defun rust-auto-use--insert-use-line (use-line)
25+
"Insert the actual use line USE-LINE into the code."
26+
(save-excursion
27+
(goto-char (point-min))
28+
(if (not (search-forward-regexp "^use" nil t))
29+
(progn (while (or (looking-at "/")
30+
(looking-at "extern"))
31+
(forward-line)
32+
(beginning-of-line))
33+
(newline)
34+
(forward-line -1)))
35+
(end-of-line)
5436
(newline)
55-
(maphash (lambda (key value)
56-
(insert (format "(puthash \"%s\" \"%s\" rust-auto-use/symbol-cache)" key value))
57-
(newline))
58-
rust-auto-use/symbol-cache)
59-
rust-auto-use/symbol-cache))
37+
(insert use-line)))
6038

61-
(defun rust-auto-use/load-symbol-cache ()
39+
(defun rust-auto-use--deduce-use-line ()
40+
"Attempt to deduce which line to insert based on the cached values. If none is found, prompt the user to enter the module to use."
41+
(let ((symbol (symbol-at-point)))
42+
(let ((cached-result (gethash symbol rust-auto-use--symbol-cache)))
43+
(if cached-result cached-result (rust-auto-use--cache-result symbol (format "use %s::%s;"
44+
(read-string (format
45+
"import
46+
%s from? " symbol)) symbol))))))
47+
48+
49+
50+
(defun rust-auto-use--cache-result (symbol result)
51+
"Cache a use-line RESULT for a given symbol SYMBOL for future use."
52+
(progn (puthash symbol result rust-auto-use--symbol-cache)
53+
(rust-auto-use--dump-symbol-cache))
54+
result)
55+
56+
57+
(defun rust-auto-use--dump-symbol-cache ()
58+
"Save the symbol cache to file."
59+
(with-temp-file rust-auto-use-cache-filename (emacs-lisp-mode)
60+
(insert ";; this file was automatically generated by rust-auto-use.el")
61+
(newline)
62+
(insert "(setq rust-auto-use--symbol-cache (make-hash-table :test 'equal))")
63+
(newline)
64+
(maphash (lambda (key value)
65+
(insert (format "(puthash \"%s\" \"%s\" rust-auto-use--symbol-cache)"
66+
key value))
67+
(newline)) rust-auto-use--symbol-cache) rust-auto-use--symbol-cache))
68+
69+
(defun rust-auto-use--load-symbol-cache ()
70+
"Load the symbol cache from a file."
6271
(load rust-auto-use-cache-filename t)
63-
(if (not (boundp 'rust-auto-use/symbol-cache))
64-
(setq rust-auto-use/symbol-cache (make-hash-table :test 'equal))))
72+
(if (not (boundp 'rust-auto-use--symbol-cache))
73+
(setq rust-auto-use--symbol-cache (make-hash-table :test 'equal))))
6574

66-
(rust-auto-use/load-symbol-cache)
75+
76+
(rust-auto-use--load-symbol-cache)
6777

6878
(provide 'rust-auto-use)
79+
80+
;;; rust-auto-use.el ends here

0 commit comments

Comments
 (0)