Skip to content

Commit bfe24d2

Browse files
committed
Merge pull request #132 from tomjakubowski/move-module
Add rust-promote-module-into-dir
2 parents bc0df03 + 2f42da8 commit bfe24d2

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

rust-mode.el

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,28 @@ See `compilation-error-regexp-alist' for help on their format.")
14091409
(interactive)
14101410
(rust-playpen-region (point-min) (point-max)))
14111411

1412+
(defun rust-promote-module-into-dir ()
1413+
"Promote the module file visited by the current buffer into its own directory.
1414+
1415+
For example, if the current buffer is visiting the file `foo.rs',
1416+
then this function creates the directory `foo' and renames the
1417+
file to `foo/mod.rs'. The current buffer will be updated to
1418+
visit the new file."
1419+
(interactive)
1420+
(let ((filename (buffer-file-name)))
1421+
(if (not filename)
1422+
(message "Buffer is not visiting a file.")
1423+
(if (string-equal (file-name-nondirectory filename) "mod.rs")
1424+
(message "Won't promote a module file already named mod.rs.")
1425+
(let* ((basename (file-name-sans-extension
1426+
(file-name-nondirectory filename)))
1427+
(mod-dir (file-name-as-directory
1428+
(concat (file-name-directory filename) basename)))
1429+
(new-name (concat mod-dir "mod.rs")))
1430+
(mkdir mod-dir t)
1431+
(rename-file filename new-name 1)
1432+
(set-visited-file-name new-name))))))
1433+
14121434
(provide 'rust-mode)
14131435

14141436
;;; rust-mode.el ends here

0 commit comments

Comments
 (0)