Skip to content

Commit

Permalink
rust; autoload
Browse files Browse the repository at this point in the history
  • Loading branch information
semenInRussia committed Jun 20, 2024
1 parent dc9db1a commit e410e39
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 85 deletions.
96 changes: 11 additions & 85 deletions lisp/languages/my-rust.el
Original file line number Diff line number Diff line change
Expand Up @@ -11,97 +11,23 @@
;; My configuration for rust

;;; Code:

(require 'my-leaf)
(require 'dash)
(require 's)
(require 'just)
(require 'my-lib)
(require 'f)

(declare-function embrace-add-pair-regexp "embrace.el")
(declare-function embrace-add-pair "embrace.el")

(autoload 'my-rust-find-Cargo.toml-in-directory "my-rust.el")


(defcustom my-rust-maybe-pub-words
'(async fn mod struct enum type trait)
"List of the symbols indicating words which can be public in Rust."
:type '(repeat symbol)
:group 'my)

(leaf rust-mode
:ensure t
:defun my-lsp-ensure
;; some functions are define inside `my-rust-editing'
:bind (:rust-mode-map
("C-c C-m" . 'rust-toggle-mutability)
("C-c M-p" . 'my-rust-toggle-pub)
("C-c C-t" . 'my-rust-visit-Cargo.toml)
("C-c C-m" . 'rust-toggle-mutability))
:config ;nofmt
(add-hook 'rust-mode-hook #'my-lsp-ensure)

(defun my-rust-toggle-pub ()
"Toggle public/private scope of the current rust function/imple/struct."
(interactive)
(let ((line-start (pos-bol)))
(save-excursion
(end-of-line)
(or
;; try search a keyword in the current line
(--first
(search-backward-regexp
(s-concat (symbol-name it) " ")
line-start t)
my-rust-maybe-pub-words)
;; otherwise try search a keyword in the current buffer
(--first
(search-backward-regexp
(s-concat (symbol-name it) " ")
nil t)
my-rust-maybe-pub-words))
(if (looking-back "pub *" nil)
(just-delete-word -1)
(insert "pub ")))
(repeat-at-last-keystroke)))

(defun my-rust-find-Cargo.toml-in-directory (&optional dir)
"Find closest Cargo.toml in the DIR and return path to it."
(interactive)
(setq dir (or dir default-directory))
(let ((cargo.toml (f-join dir "Cargo.toml")))
(if (f-exists-p cargo.toml)
cargo.toml
(my-rust-find-Cargo.toml-in-directory (f-parent dir)))))

(defun my-rust-visit-Cargo.toml ()
"Visit Cargo.toml file of current rust crate."
(interactive)
(find-file (my-rust-find-Cargo.toml-in-directory)))

(leaf embrace
:after embrace
:hook (rust-mode-hook . my-rust-embrace-hook)
:config
(defun my-rust-embrace-hook ()
"Add parens to `embrace' parens for `rust-mode'."
(interactive)
(embrace-add-pair ?v "Vec<" ">")
(embrace-add-pair ?d "dbg!(" ")")
(embrace-add-pair ?b "Box<" ">")
(embrace-add-pair ?o "Option<" ">")
(embrace-add-pair ?r "Result<" ">")
(embrace-add-pair-regexp ?p
"print\\(ln\\)?!(\".*?\"," ")"
(lambda ()
(interactive)
(cons
(concat
"println!("
(read-string
"Template to format string: "
"\"{}\"")
", ")
");"))))))
("C-c C-t" . 'my-rust-visit-Cargo.toml))
:config
(add-hook 'rust-mode-hook #'my-lsp-ensure))

(autoload 'my-rust-embrace-hook "my-rust-editing")
(leaf embrace
:after embrace
:hook (rust-mode-hook . my-rust-embrace-hook))

(provide 'my-rust)
;;; my-rust.el ends here
80 changes: 80 additions & 0 deletions lisp/local-projects/my-rust-editing.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
;;; my-rust-editing.el --- SOme functions useful for editing Rust file -*- lexical-binding: t -*-
;; semenInRussia 2024

;;; Commentary:
;;; Code:
(require 'dash)
(require 'just)
(require 's)
(require 'my-lib)

(declare-function embrace-add-pair-regexp "embrace.el")
(declare-function embrace-add-pair "embrace.el")

(defcustom my-rust-maybe-pub-words
'(async fn mod struct enum type trait)
"List of the symbols indicating words which can be public in Rust."
:type '(repeat symbol)
:group 'my)

(defun my-rust-toggle-pub ()
"Toggle public/private scope of the current rust function/imple/struct."
(interactive)
(let ((line-start (pos-bol)))
(save-excursion
(end-of-line)
(or
;; try search a keyword in the current line
(--first
(search-backward-regexp
(s-concat (symbol-name it) " ")
line-start t)
my-rust-maybe-pub-words)
;; otherwise try search a keyword in the current buffer
(--first
(search-backward-regexp
(s-concat (symbol-name it) " ")
nil t)
my-rust-maybe-pub-words))
(if (looking-back "pub *" nil)
(just-delete-word -1)
(insert "pub ")))
(repeat-at-last-keystroke)))

(defun my-rust-find-Cargo.toml-in-directory (&optional dir)
"Find closest Cargo.toml in the DIR and return path to it."
(interactive)
(setq dir (or dir default-directory))
(let ((cargo.toml (f-join dir "Cargo.toml")))
(if (f-exists-p cargo.toml)
cargo.toml
(my-rust-find-Cargo.toml-in-directory (f-parent dir)))))

(defun my-rust-visit-Cargo.toml ()
"Visit Cargo.toml file of current rust crate."
(interactive)
(find-file (my-rust-find-Cargo.toml-in-directory)))

(defun my-rust-embrace-hook ()
"Add parens to `embrace' parens for `rust-mode'."
(interactive)
(embrace-add-pair ?v "Vec<" ">")
(embrace-add-pair ?d "dbg!(" ")")
(embrace-add-pair ?b "Box<" ">")
(embrace-add-pair ?o "Option<" ">")
(embrace-add-pair ?r "Result<" ">")
(embrace-add-pair-regexp ?p
"print\\(ln\\)?!(\".*?\"," ")"
(lambda ()
(interactive)
(cons
(concat
"println!("
(read-string
"Template to format string: "
"\"{}\"")
", ")
");"))))

(provide 'my-rust-editing)
;;; my-rust-editing.el ends here

0 comments on commit e410e39

Please sign in to comment.