2016-11-12 12:14:55 +00:00
|
|
|
|
;;; funcs.el --- rust Layer functions File for Spacemacs
|
|
|
|
|
;;
|
2021-03-22 20:11:29 +00:00
|
|
|
|
;; Copyright (c) 2012-2021 Sylvain Benner & Contributors
|
2016-11-12 12:14:55 +00:00
|
|
|
|
;;
|
|
|
|
|
;; Author: NJBS <DoNotTrackMeUsingThis@gmail.com>
|
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
|
|
|
|
;;
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
;;
|
2021-03-24 03:31:44 +00:00
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
;;
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
;;
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
2016-11-12 12:14:55 +00:00
|
|
|
|
|
2019-09-30 01:30:53 +00:00
|
|
|
|
(defun spacemacs//rust-setup-backend ()
|
|
|
|
|
"Conditionally setup rust backend."
|
2021-03-18 04:59:48 +00:00
|
|
|
|
(pcase rust-backend
|
|
|
|
|
('racer (spacemacs//rust-setup-racer))
|
|
|
|
|
('lsp (spacemacs//rust-setup-lsp))))
|
2019-09-30 01:30:53 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//rust-setup-company ()
|
|
|
|
|
"Conditionally setup company based on backend."
|
2021-03-18 04:59:48 +00:00
|
|
|
|
(when (eq rust-backend 'racer)
|
|
|
|
|
(spacemacs//rust-setup-racer-company)))
|
2019-09-30 01:30:53 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//rust-setup-dap ()
|
|
|
|
|
"Conditionally setup elixir DAP integration."
|
|
|
|
|
;; currently DAP is only available using LSP
|
2021-03-18 04:59:48 +00:00
|
|
|
|
(when (eq rust-backend 'lsp)
|
|
|
|
|
(spacemacs//rust-setup-lsp-dap)))
|
2019-09-30 01:30:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; lsp
|
2019-12-28 15:17:51 +00:00
|
|
|
|
(defun spacemacs//lsp-layer-not-installed-message ()
|
|
|
|
|
(message (concat "`lsp' layer is not installed, "
|
|
|
|
|
"please add `lsp' layer to your dotfile.")))
|
2019-09-30 01:30:53 +00:00
|
|
|
|
|
2021-03-21 06:22:57 +00:00
|
|
|
|
(defun spacemacs/lsp-rust-switch-server ()
|
2021-03-15 19:17:03 +00:00
|
|
|
|
"Switch between rust-analyzer and rls."
|
|
|
|
|
(interactive)
|
|
|
|
|
(lsp-rust-switch-server)
|
|
|
|
|
(call-interactively 'lsp-workspace-restart))
|
|
|
|
|
|
2019-09-30 01:30:53 +00:00
|
|
|
|
(defun spacemacs//rust-setup-lsp ()
|
2021-03-21 06:22:57 +00:00
|
|
|
|
"Setup lsp backend."
|
2019-09-30 01:30:53 +00:00
|
|
|
|
(if (configuration-layer/layer-used-p 'lsp)
|
2020-04-02 16:41:28 +00:00
|
|
|
|
(progn
|
2021-06-06 20:25:00 +00:00
|
|
|
|
(lsp-deferred)
|
2020-02-10 21:48:43 +00:00
|
|
|
|
(spacemacs/declare-prefix-for-mode 'rust-mode "ms" "switch")
|
|
|
|
|
(spacemacs/set-leader-keys-for-major-mode 'rust-mode
|
2021-03-21 06:22:57 +00:00
|
|
|
|
"ss" 'spacemacs/lsp-rust-switch-server
|
|
|
|
|
"bR" 'spacemacs/lsp-rust-analyzer-reload-workspace))
|
2019-12-28 15:17:51 +00:00
|
|
|
|
(spacemacs//lsp-layer-not-installed-message)))
|
2019-09-30 01:30:53 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//rust-setup-lsp-dap ()
|
|
|
|
|
"Setup DAP integration."
|
|
|
|
|
(require 'dap-gdb-lldb))
|
2021-03-19 18:47:55 +00:00
|
|
|
|
|
2021-03-21 06:22:57 +00:00
|
|
|
|
(defun spacemacs/lsp-rust-analyzer-reload-workspace ()
|
|
|
|
|
"Reload workspaces to pick up changes in Cargo.toml.
|
|
|
|
|
Only applies to rust-analyzer, since rls automatically picks up changes already."
|
2021-03-19 18:47:55 +00:00
|
|
|
|
(interactive)
|
2021-03-21 06:22:57 +00:00
|
|
|
|
(if (member 'rust-analyzer (spacemacs//lsp-client-server-id))
|
2021-03-19 18:47:55 +00:00
|
|
|
|
(progn
|
|
|
|
|
(lsp-rust-analyzer-reload-workspace)
|
|
|
|
|
(message "Reloaded workspace"))
|
|
|
|
|
(message "RLS reloads automatically, and doesn't require an explicit reload")))
|
2021-03-21 06:22:57 +00:00
|
|
|
|
|
|
|
|
|
(when (configuration-layer/package-used-p 'cargo)
|
|
|
|
|
(defun spacemacs//cargo-maybe-reload ()
|
|
|
|
|
"Reload the workspace conditionally.
|
|
|
|
|
When one of the following is true, it won't reload:
|
|
|
|
|
- Backend is not rust-analyzer.
|
|
|
|
|
- `cargo-process-reload-on-modify' is nil."
|
|
|
|
|
(when (and cargo-process-reload-on-modify
|
|
|
|
|
(eq rust-backend 'lsp)
|
|
|
|
|
(member 'rust-analyzer (spacemacs//lsp-client-server-id)))
|
2021-06-06 20:25:00 +00:00
|
|
|
|
(lsp-rust-analyzer-reload-workspace)))
|
2021-03-21 06:22:57 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs/cargo-process-repeat ()
|
|
|
|
|
"Run last cargo process command, and conditionally reload the workspace."
|
|
|
|
|
(interactive)
|
|
|
|
|
(call-interactively 'cargo-process-repeat)
|
|
|
|
|
(when (member (car cargo-process-last-command)
|
|
|
|
|
'("Add" "Remove" "Upgrade"))
|
|
|
|
|
(spacemacs//cargo-maybe-reload)))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/cargo-process-add ()
|
|
|
|
|
"Run the cargo add command, and conditionally reload the workspace."
|
|
|
|
|
(interactive)
|
|
|
|
|
(call-interactively 'cargo-process-add)
|
|
|
|
|
(spacemacs//cargo-maybe-reload))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/cargo-process-rm()
|
|
|
|
|
"Run the cargo rm command, and conditionally reload the workspace."
|
|
|
|
|
(interactive)
|
|
|
|
|
(call-interactively 'cargo-process-rm)
|
|
|
|
|
(spacemacs//cargo-maybe-reload))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/cargo-process-upgrade()
|
|
|
|
|
"Run the cargo upgrade command, and conditionally reload the workspace."
|
|
|
|
|
(interactive)
|
|
|
|
|
(call-interactively 'cargo-process-upgrade)
|
|
|
|
|
(spacemacs//cargo-maybe-reload)))
|
2019-09-30 01:30:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; racer
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//rust-setup-racer ()
|
|
|
|
|
"Setup racer backend"
|
|
|
|
|
(progn
|
|
|
|
|
(racer-mode)))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//rust-setup-racer-company ()
|
|
|
|
|
"Setup racer auto-completion."
|
|
|
|
|
(spacemacs|add-company-backends
|
|
|
|
|
:backends company-capf
|
|
|
|
|
:modes rust-mode
|
|
|
|
|
:variables company-tooltip-align-annotations t))
|
|
|
|
|
|
2016-11-12 12:14:55 +00:00
|
|
|
|
(defun spacemacs/racer-describe ()
|
|
|
|
|
"Show a *Racer Help* buffer for the function or type at point.
|
|
|
|
|
If `help-window-select' is non-nil, also select the help window."
|
|
|
|
|
(interactive)
|
|
|
|
|
(let ((window (racer-describe)))
|
|
|
|
|
(when help-window-select
|
|
|
|
|
(select-window window))))
|
2019-09-30 01:30:53 +00:00
|
|
|
|
|
|
|
|
|
;; Misc
|
2016-11-16 18:33:01 +00:00
|
|
|
|
|
2019-12-23 05:31:34 +00:00
|
|
|
|
(defvar spacemacs//rust-quick-run-tmp-file nil
|
|
|
|
|
"Stores filename for the rust-quick-run function")
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//rust-quick-run-generate-tmp-file-name (input-file-name)
|
|
|
|
|
(concat temporary-file-directory
|
|
|
|
|
(file-name-nondirectory (buffer-file-name))
|
|
|
|
|
"-"
|
|
|
|
|
(md5 (buffer-file-name))))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//rust-quick-run-compilation-finish-function (buffer status)
|
2019-12-28 15:17:51 +00:00
|
|
|
|
(setq compilation-finish-functions
|
|
|
|
|
(delete 'spacemacs//rust-quick-run-compilation-finish-function
|
|
|
|
|
compilation-finish-functions))
|
2019-12-27 22:04:26 +00:00
|
|
|
|
(when (string-match "finished" status)
|
|
|
|
|
(newline)
|
|
|
|
|
(shell-command
|
|
|
|
|
(shell-quote-argument spacemacs//rust-quick-run-tmp-file) t)))
|
2019-12-23 05:31:34 +00:00
|
|
|
|
|
2016-11-16 18:33:01 +00:00
|
|
|
|
(defun spacemacs/rust-quick-run ()
|
|
|
|
|
"Quickly run a Rust file using rustc.
|
2019-12-28 15:17:51 +00:00
|
|
|
|
Meant for a quick-prototype flow only - use `spacemacs/open-junk-file' to open a
|
|
|
|
|
junk Rust file, type in some code and quickly run it. If you want to use
|
|
|
|
|
third-party crates, create a new project using `cargo-process-new' and run using
|
|
|
|
|
`cargo-process-run'."
|
2016-11-16 18:33:01 +00:00
|
|
|
|
(interactive)
|
2019-12-23 05:31:34 +00:00
|
|
|
|
(setq spacemacs//rust-quick-run-tmp-file
|
|
|
|
|
(spacemacs//rust-quick-run-generate-tmp-file-name(buffer-file-name)))
|
2019-12-28 15:17:51 +00:00
|
|
|
|
(add-to-list 'compilation-finish-functions
|
|
|
|
|
'spacemacs//rust-quick-run-compilation-finish-function)
|
2019-12-23 05:31:34 +00:00
|
|
|
|
(compile
|
|
|
|
|
(format "rustc -o %s %s"
|
|
|
|
|
(shell-quote-argument spacemacs//rust-quick-run-tmp-file)
|
|
|
|
|
(shell-quote-argument buffer-file-name))))
|