;;; 401-rust.el --- handle rust code ;;; Commentary: ;; using rust-mode with rust-analyzer via lsp ;;; Code: ;; load rustic -> IDE like for rust ;; it superseeds rust-mode ;; reference: https://robert.kra.hn/posts/2021-02-07_rust-with-emacs/ (use-package rustic :ensure t :bind (:map rustic-mode-map ("M-j" . lsp-ui-imenu) ("M-?" . lsp-find-references) ("C-c C-c l" . flycheck-list-errors) ("C-c C-c a" . lsp-execute-code-action) ("C-c C-c r" . lsp-rename) ("C-c C-c q" . lsp-workspace-restart) ("C-c C-c Q" . lsp-workspace-shutdown) ("C-c C-c s" . lsp-rust-analyzer-status)) :custom (global-linum-mode) (rustic-lsp-server 'rust-analyzer) (rustic-lsp-format t) (setq rustic-format-on-save t) ;;(linum-relative-mode 0) ;;(linum-relative-toggle) ;;(hs-minor-mode t) ;;(lsp-origami-try-enable) ;;(linum-relative-mode 1) ;;(setq-default indent-tabs-mode nil) :config ;; recommended: use spaces instead of tabs (add-hook 'rust-mode-hook (lambda () (setq indent-tabs-mode nil))) ;; enable code folding und line numbering rust buffers (add-hook 'prog-mode-hook ;;'lsp-origami-mode 'hs-minor-mode ;;'linum-relative-mode 'tmp/rustic-mode-hook) ;; (remove-hook 'rustic-mode-hook 'flycheck-mode) ;; (setq lsp-eldoc-hook nil) ;; (setq lsp-enable-symbol-highlighting nil) ;; (setq lsp-signature-auto-activate nil) (defun tmp/rustic-mode-hook () ;; so that run C-c C-c C-r works without having to confirm, but don't try to ;; save rust buffers that are not file visiting. Once ;; https://github.com/brotzeit/rustic/issues/253 has been resolved this should ;; no longer be necessary. (when buffer-file-name (setq-local buffer-save-without-query t))) ;; use the lsp package ;; https://emacs-lsp.github.io/lsp-mode (use-package lsp-mode :ensure :commands lsp :custom ;; what to use when checking on-save. "check" is default, I prefer clippy (lsp-rust-analyzer-cargo-watch-command "clippy") (lsp-eldoc-render-all t) (lsp-idle-delay 0.6) (lsp-rust-analyzer-server-display-inlay-hints t) :config (add-hook 'lsp-mode-hook 'lsp-ui-mode)) (use-package lsp-ivy :ensure t) (use-package lsp-ui :ensure t :commands lsp-ui-mode :custom (lsp-ui-peek-always-show t) (lsp-ui-sideline-show-hover t) (lsp-ui-doc-enable nil)) ;; use the company package ;; a completion framework for emacs (use-package company :ensure :custom (company-idle-delay 2.5) ;; how long to wait until popup ;; (company-begin-commands nil) ;; uncomment to disable popup :bind (:map company-active-map ("C-n". company-select-next) ("C-p". company-select-previous) ("M-<". company-select-first) ("M->". company-select-last)) (:map company-mode-map ("". tab-indent-or-complete) ("TAB". tab-indent-or-complete))) ;; use the yasnippet (use-package yasnippet :ensure t ;; YASnippet is a template system for Emacs. ;; It allows you to type an abbreviation and automatically expand it into function templates ;; use YASnippet as a non-global minor mode :config (yas-reload-all)) ;; company-capf.el from load-path ;;(use-package linum-relative ;; :config ;; (setq linum-relative-backend 'display-line-numbers-mode)) ) ;; end use-package rustic (defun company-yasnippet-or-completion () (interactive) (or (do-yas-expand) (company-complete-common))) (defun check-expansion () (save-excursion (if (looking-at "\\_>") t (backward-char 1) (if (looking-at "\\.") t (backward-char 1) (if (looking-at "::") t nil))))) (defun do-yas-expand () (let ((yas/fallback-behavior 'return-nil)) (yas/expand))) (defun tab-indent-or-complete () (interactive) (if (minibufferp) (minibuffer-complete) (if (or (not yas/minor-mode) (null (do-yas-expand))) (if (check-expansion) (company-complete-common) (indent-for-tab-command))))) (provide '401-rust) ;; Local Variables: ;; no-byte-compile: t ;; End ;;; 401-rust.el ends here