Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustfmt #1278

Merged
merged 2 commits into from
Jan 27, 2024
Merged

rustfmt #1278

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 13 additions & 29 deletions extensions/rust-mode/rust-mode.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
:rust-format-buffer))
(in-package :lem-rust-mode)

(defvar *rust-format-on-save* t)
(defvar *rust-format-buffer* "rustfmt")

(define-editor-variable rust-format-on-save t)

(defun tokens (boundary strings)
(let ((alternation
`(:alternation ,@(sort (copy-list strings) #'> :key #'length))))
Expand Down Expand Up @@ -61,21 +58,21 @@
,(ppcre:parse-string "([^\\t <>()-]+)")
:word-boundary)
:captures (vector nil
(make-tm-name 'syntax-keyword-attribute)
(make-tm-name 'syntax-function-name-attribute)))
(make-tm-name 'syntax-keyword-attribute)
(make-tm-name 'syntax-function-name-attribute)))
(make-tm-match `(:sequence
,(ppcre:parse-string "([^A-Z:\\t <>()-]+)")
"::"
:word-boundary)
:captures (vector nil
(make-tm-name 'syntax-variable-attribute)
nil))
(make-tm-name 'syntax-variable-attribute)
nil))
(make-tm-match `(:sequence
:word-boundary
,(ppcre:parse-string "([^\\t <>()-]+)!"))
:captures (vector nil
(make-tm-name 'syntax-variable-attribute)
nil))
(make-tm-name 'syntax-variable-attribute)
nil))
(make-tm-match
(tokens :word-boundary
'("u8" "i8"
Expand Down Expand Up @@ -134,18 +131,16 @@
(:name "Rust"
:keymap *rust-mode-keymap*
:syntax-table *rust-syntax-table*
:mode-hook *rust-mode-hook*)
:mode-hook *rust-mode-hook*
:formatter 'rust-format-buffer)
(setf (variable-value 'enable-syntax-highlight) t
(variable-value 'calc-indent-function) 'calc-indent
(variable-value 'indent-tabs-mode) nil
(variable-value 'beginning-of-defun-function) 'beginning-of-defun
(variable-value 'end-of-defun-function) 'end-of-defun
(variable-value 'line-comment) "//"
(variable-value 'insertion-line-comment) "// "
(variable-value 'tab-width :buffer) 4)
(add-hook (variable-value 'before-save-hook :buffer) 'rust-before-save-hook)
(add-hook (variable-value 'after-save-hook :buffer) 'rust-after-save-hook)
(setf (variable-value 'rust-format-on-save :buffer) *rust-format-on-save*))
(variable-value 'tab-width :buffer) 4))

(define-key *rust-mode-keymap* "C-c C-f" 'rust-format-buffer)
(define-key *rust-mode-keymap* "M-C-q" 'indent-exp)
Expand Down Expand Up @@ -304,23 +299,12 @@
(return-from calc-indent indent)))))
(calc-indent-region start point))))))

(define-command rust-format-buffer () ()
(defun rust-format-buffer (buffer)
(declare (ignore buffer))
(when (zerop (nth-value 2
(uiop:run-program (format nil "~A --version" *rust-format-buffer*)
:ignore-error-status t)))
(uiop:run-program (format nil "~A --version" *rust-format-buffer*)
:ignore-error-status t)))
(filter-buffer *rust-format-buffer*)
(message "Formatted buffer with rustfmt.")))

(defun rust-before-save-hook (buffer)
(declare (ignore buffer))
(when (variable-value 'rust-format-on-save :buffer)
(ignore-errors
(rust-format-buffer))))

(defun rust-after-save-hook (buffer)
(declare (ignore buffer))
(when (variable-value 'rust-format-on-save :buffer)

))

(define-file-type ("rs") rust-mode)
Loading