|
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 |
7 | 2 |
|
| 3 | +;;; Homepage: https://github.com/vmalloc/rust-auto-use.el |
8 | 4 |
|
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 |
25 | 6 |
|
| 7 | +;;; Commentary: |
| 8 | +;; Given a current symbol, this package allows developers to automatically insert Rust `use` statements for importing the symbol. |
26 | 9 |
|
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: |
33 | 11 |
|
| 12 | +(defvar rust-auto-use--symbol-cache) |
34 | 13 |
|
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")) |
37 | 15 |
|
| 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)))) |
38 | 20 |
|
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) |
45 | 22 |
|
46 | | -(defvar rust-auto-use-cache-filename "~/.emacs.d/.rust-auto-use-cache") |
47 | 23 |
|
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) |
54 | 36 | (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))) |
60 | 38 |
|
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." |
62 | 71 | (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)))) |
65 | 74 |
|
66 | | -(rust-auto-use/load-symbol-cache) |
| 75 | + |
| 76 | +(rust-auto-use--load-symbol-cache) |
67 | 77 |
|
68 | 78 | (provide 'rust-auto-use) |
| 79 | + |
| 80 | +;;; rust-auto-use.el ends here |
0 commit comments