Skip to content

Commit

Permalink
provide alternative rustic mode that derives from rust-ts-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
brotzeit committed Dec 26, 2022
1 parent f4efdd8 commit 6f0748c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
emacs-version:
- 27.2
- 28.1
- snapshot

steps:
- uses: actions/checkout@v2

Expand Down
34 changes: 34 additions & 0 deletions rustic-rust-mode.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
;;; rustic-comint.el --- Cargo and comint facilities -*-lexical-binding: t-*-
;;; Commentary:

;; Deprecated code related to rust-mode

;;; Code:

(setq rust-load-optional-libraries nil)
(setq rust-before-save-hook #'rustic-before-save-hook)
(setq rust-after-save-hook #'rustic-after-save-hook)
(require 'rust-mode)

;;; Define aliases for removed rustic functions

(defvaralias 'rustic-indent-offset 'rust-indent-offset)
(defvaralias 'rustic-indent-method-chain 'rust-indent-method-chain)
(defvaralias 'rustic-indent-where-clause 'rust-indent-where-clause)
(defvaralias 'rustic-match-angle-brackets 'rust-match-angle-brackets)
(defvaralias 'rustic-indent-return-type-to-arguments 'rust-indent-return-type-to-arguments)
(defalias 'rustic-indent-line #'rust-mode-indent-line)
(defalias 'rustic-end-of-defun #'rust-end-of-defun)

;;;###autoload
(define-derived-mode rustic-mode rust-mode "Rustic"
"Major mode for Rust code.
\\{rustic-mode-map}"
:group 'rustic

(when (bound-and-true-p rustic-cargo-auto-add-missing-dependencies)
(add-hook 'lsp-after-diagnostics-hook 'rustic-cargo-add-missing-dependencies-hook nil t)))

(provide 'rustic-rust-mode)
;;; rustic-rust-mode.el ends here
22 changes: 22 additions & 0 deletions rustic-ts-mode.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
;;; rustic-ts-mode.el --- use native rust-ts-mode -*-lexical-binding: t-*-
;;; Commentary:

;; Derive from rust-ts-mode instead of rust-mode

;;; Code:

;;;###autoload
(define-derived-mode rustic-mode rust-ts-mode "Rustic"
"Major mode for Rust code.
\\{rustic-mode-map}"
:group 'rustic

(when (bound-and-true-p rustic-cargo-auto-add-missing-dependencies)
(add-hook 'lsp-after-diagnostics-hook 'rustic-cargo-add-missing-dependencies-hook nil t))

(add-hook 'before-save-hook 'rustic-before-save-hook nil t)
(add-hook 'after-save-hook 'rustic-after-save-hook nil t))

(provide 'rustic-ts-mode)
;;; rustic-ts-mode.el ends here
39 changes: 10 additions & 29 deletions rustic.el
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,13 @@

(require 'dash)

(setq rust-load-optional-libraries nil)
(setq rust-before-save-hook #'rustic-before-save-hook)
(setq rust-after-save-hook #'rustic-after-save-hook)
(require 'rust-mode)

;;; Customization

(defgroup rustic nil
"Support for Rust code."
:link '(url-link "https://www.rustic-lang.org/")
:group 'languages)

;;; Define aliases for removed rustic functions

(defvaralias 'rustic-indent-offset 'rust-indent-offset)
(defvaralias 'rustic-indent-method-chain 'rust-indent-method-chain)
(defvaralias 'rustic-indent-where-clause 'rust-indent-where-clause)
(defvaralias 'rustic-match-angle-brackets 'rust-match-angle-brackets)
(defvaralias 'rustic-indent-return-type-to-arguments 'rust-indent-return-type-to-arguments)
(defalias 'rustic-indent-line #'rust-mode-indent-line)
(defalias 'rustic-end-of-defun #'rust-end-of-defun)

;;; Workspace

(defvar-local rustic--buffer-workspace nil
Expand Down Expand Up @@ -111,6 +96,13 @@ this variable."
:type 'function
:group 'rustic)

(defcustom rustic-treesitter-derive t
"Whether rustic should derive from the new treesitter mode `rust-ts-mode'
instead of `rust-mode'. This option requires emacs29+."
:version "29.1"
:type 'boolean
:group 'rustic)

;;; Mode

(defvar rustic-mode-map
Expand Down Expand Up @@ -145,24 +137,13 @@ this variable."
map)
"Keymap for `rustic-mode'.")

;;;###autoload
(define-derived-mode rustic-mode rust-mode "Rustic"
"Major mode for Rust code.
\\{rustic-mode-map}"
:group 'rustic

(when (bound-and-true-p rustic-cargo-auto-add-missing-dependencies)
(add-hook 'lsp-after-diagnostics-hook 'rustic-cargo-add-missing-dependencies-hook nil t)))
(if (and (version<= "29.1" emacs-version) rustic-treesitter-derive)
(require 'rustic-ts-mode)
(require 'rustic-rust-mode))

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rustic-mode))

;; remove rust-mode from `auto-mode-alist'
(let ((mode '("\\.rs\\'" . rust-mode)))
(when (member mode auto-mode-alist)
(setq auto-mode-alist (remove mode auto-mode-alist))))

;;; _

(defun rustic-reload ()
Expand Down

0 comments on commit 6f0748c

Please sign in to comment.