Skip to content

Commit

Permalink
Merge branch 'main' into babel
Browse files Browse the repository at this point in the history
  • Loading branch information
CeleritasCelery committed Jul 8, 2024
2 parents f16f5ed + f16cb6c commit 97ae8a6
Show file tree
Hide file tree
Showing 20 changed files with 82 additions and 64 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.org
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
- Make ~rustic-cargo-test~ rembmer it's universal arguments.
- ~rustic-recompile~ will remember any universal arguments that is
passed to it.
- Fix ~rustic-cargo-upgrade~ in the presence of universal arguments.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ FTR #174 #179 #236

The colors that are displayed in compilation buffers come from cargo
and are translated by xterm-color. You can change these colors by
modifying `rustic-ansi-faces`.
modifying `xterm-color-names` and `xterm-color-names-bright`.

`rustic-compilation-mode` doesn't use the default faces of
compile.el. If you want to change these colors you can use something
Expand Down
8 changes: 4 additions & 4 deletions rustic-babel.el
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ should be wrapped in which case we will disable rustfmt."
(toolchain-kw-or-string (format "+%s" toolchain-kw-or-string))
(rustic-babel-default-toolchain (format "+%s" rustic-babel-default-toolchain))
(t nil)))
(params (remove nil (list "cargo" toolchain "build" "--quiet")))
(params (remove nil (list (rustic-cargo-bin) toolchain "build" "--quiet")))
(inhibit-read-only t))
(rustic-compilation-setup-buffer err-buff dir 'rustic-compilation-mode)
(when rustic-babel-display-compilation-buffer
Expand Down Expand Up @@ -124,7 +124,7 @@ execution with rustfmt."

;; run project
(let* ((err-buff (get-buffer-create rustic-babel-compilation-buffer-name))
(params (remove nil (list "cargo" toolchain "run" "--quiet")))
(params (remove nil (list (rustic-cargo-bin) toolchain "run" "--quiet")))
(inhibit-read-only t))
(rustic-make-process
:name rustic-babel-process-name
Expand Down Expand Up @@ -216,7 +216,7 @@ after successful compilation."
Return full path if EXPAND is t."
(let* ((default-directory rustic-org-babel-temporary-directory)
(dir (make-temp-file-internal "cargo" 0 "" nil)))
(shell-command-to-string (format "cargo new %s --bin --quiet" dir))
(shell-command-to-string (format "%s new %s --bin --quiet" (rustic-cargo-bin) dir))
(if expand
(concat (expand-file-name dir) "/")
dir)))
Expand Down Expand Up @@ -407,7 +407,7 @@ at least one time in this emacs session before this command can be used."
(default-directory rustic-org-babel-temporary-directory)
(body (org-element-property :value (org-element-at-point)))
(project (rustic-babel-project))
(params (list "cargo" "clippy")))
(params (list (rustic-cargo-bin) "clippy")))
(let* ((dir (setq rustic-babel-dir (expand-file-name project)))
(main (expand-file-name "main.rs" (concat dir "/src")))
(default-directory dir))
Expand Down
39 changes: 26 additions & 13 deletions rustic-cargo.el
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ If ARG is not nil, use value as argument and store it in
(defun rustic-cargo-test-rerun ()
"Run 'cargo test' with `rustic-test-arguments'."
(interactive)
(rustic-cargo-test-run rustic-test-arguments))
(let ((default-directory (or rustic-compilation-directory default-directory)))
(rustic-cargo-test-run rustic-test-arguments)))

;;;###autoload
(defun rustic-cargo-current-test ()
Expand Down Expand Up @@ -362,7 +363,7 @@ Execute process in PATH."

(defun rustic-cargo-install-crate-p (crate)
"Ask whether to install crate CRATE."
(let ((cmd (format "cargo install cargo-%s" crate)))
(let ((cmd (format "%s install cargo-%s" (rustic-cargo-bin) crate)))
(when (yes-or-no-p (format "Cargo-%s missing. Install ? " crate))
(async-shell-command cmd (rustic-cargo-bin) "cargo-error"))))

Expand Down Expand Up @@ -521,12 +522,22 @@ The CRATE-LINE is a single line from the `rustic-cargo-oudated-buffer-name'"
(let (upgrade)
(dolist (crate crates)
(setq upgrade (concat upgrade (format "-p %s@%s " (rustic-crate-name crate) (rustic-crate-version crate)))))
(let ((output (shell-command-to-string (format "cargo upgrade --offline %s" upgrade))))
(let ((output (shell-command-to-string (format "%s upgrade --offline %s" (rustic-cargo-bin) upgrade))))
(if (string-match "error: no such subcommand:" output)
(rustic-cargo-install-crate-p "edit")
(rustic-cargo-reload-outdated)))))

;;; New project
(defun rustic--split-path (project-path)
"Split PROJECT-PATH into two parts: the longest prefix of directories that
exist, and the rest. Return a cons cell of the two parts."
(let ((components (file-name-split project-path))
(existing-dir ""))
(while (and (not (null components))
(file-directory-p (file-name-concat existing-dir (car components))))
(setq existing-dir (file-name-concat existing-dir (car components)))
(setq components (cdr components)))
(cons existing-dir (apply 'file-name-concat (cons "" components)))))

(defun rustic-create-project (project-path is-new &optional bin)
"Run either 'cargo new' if IS-NEW is non-nil, or 'cargo init' otherwise.
Expand All @@ -546,18 +557,20 @@ BIN is not nil, create a binary application, otherwise a library."
"/src/main.rs"
"/src/lib.rs")))))))
(proc (format "rustic-cargo-%s-process" cmd))
(buf (format "*cargo-%s*" cmd)))
(buf (format "*cargo-%s*" cmd))
(dir-pair (rustic--split-path project-path))
(default-directory (car dir-pair)))
(make-process :name proc
:buffer buf
:command (list (rustic-cargo-bin) cmd bin project-path)
:command (list (rustic-cargo-bin) cmd bin (cdr dir-pair))
:sentinel new-sentinel
:file-handler t)))

;;;###autoload
(defun rustic-cargo-new (project-path &optional bin)
"Run 'cargo new' to start a new package in the path specified by PROJECT-PATH.
If BIN is not nil, create a binary application, otherwise a library."
(interactive "DProject path: ")
(interactive "GProject path: ")
(rustic-create-project project-path t bin))

;;;###autoload
Expand Down Expand Up @@ -619,7 +632,8 @@ When calling this function from `rustic-popup-mode', always use the value of
(defun rustic-cargo-run-rerun ()
"Run 'cargo run' with `rustic-run-arguments'."
(interactive)
(rustic-cargo-run-command rustic-run-arguments))
(let ((default-directory (or rustic-compilation-directory default-directory)))
(rustic-cargo-run-command rustic-run-arguments)))

(defun rustic--get-run-arguments ()
"Helper utility for getting arguments related to 'examples' directory."
Expand Down Expand Up @@ -746,11 +760,10 @@ The documentation is built if necessary."
"Add crate to Cargo.toml using 'cargo add'.
If running with prefix command `C-u', read whole command from minibuffer."
(interactive "P")
(let* ((command (if arg
(read-from-minibuffer "Cargo add command: "
(rustic-cargo-bin) " add ")
(concat (rustic-cargo-bin) " add "
(read-from-minibuffer "Crate: ")))))
(let* ((base (concat (rustic-cargo-bin) " add "))
(command (if arg
(read-from-minibuffer "Cargo add command: " base)
(concat base (read-from-minibuffer "Crate: ")))))
(rustic-run-cargo-command command)))

(defun rustic-cargo-add-missing-dependencies (&optional arg)
Expand Down Expand Up @@ -862,7 +875,7 @@ If running with prefix command `C-u', read whole command from minibuffer."
(interactive "P")
(let* ((command (if arg
(read-from-minibuffer "Cargo upgrade command: "
(rustic-cargo-bin) " upgrade ")
(format "%s upgrade " (rustic-cargo-bin)))
(concat (rustic-cargo-bin) " upgrade"))))
(rustic-run-cargo-command command)))

Expand Down
3 changes: 2 additions & 1 deletion rustic-comint.el
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ Read the full command from the minibuffer when ARG is non-nil or
when called with a prefix command \\[universal-argument]."
(interactive "P")
(let* ((command (if arg
(read-from-minibuffer "Cargo run command: " "cargo run -- ")
(read-from-minibuffer "Cargo run command: "
(concat (rustic-cargo-bin) " run -- "))
(concat (rustic-cargo-bin) " run "
(setq rustic-run-arguments
(read-from-minibuffer
Expand Down
31 changes: 11 additions & 20 deletions rustic-compile.el
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

;; Unlike compile.el, rustic makes use of a non dumb terminal in order to receive
;; all ANSI control sequences, which get translated by xterm-color.
;; This file also adds a derived compilation mode. Error matching regexes from
;; This file also adds a derived compilation mode. Error matching regexes from
;; compile.el are removed.

;;; Code:
Expand Down Expand Up @@ -115,18 +115,6 @@
"Override `compilation-column-face' for rust compilation."
:group 'rustic-compilation)

(defcustom rustic-ansi-faces ["black"
"red3"
"green3"
"yellow3"
"blue2"
"magenta3"
"cyan3"
"white"]
"Term ansi faces."
:type '(vector string string string string string string string string)
:group 'rustic-compilation)

;;; Compilation-mode

(defvar rustic-compilation-mode-map
Expand Down Expand Up @@ -184,9 +172,6 @@ Error matching regexes from compile.el are removed."
(setq-local compilation-column-face 'rustic-compilation-column)
(setq-local compilation-line-face 'rustic-compilation-line)

(setq-local xterm-color-names-bright rustic-ansi-faces)
(setq-local xterm-color-names rustic-ansi-faces)

(setq-local compilation-error-regexp-alist-alist nil)
(add-to-list 'compilation-error-regexp-alist-alist
(cons 'rustic-error rustic-compilation-error))
Expand All @@ -209,6 +194,9 @@ Error matching regexes from compile.el are removed."

;;; Compilation Process

(defvar-local rustic-compilation-directory nil
"original directory for rust compilation process.")

(defvar rustic-compilation-process-name "rustic-compilation-process"
"Process name for rust compilation processes.")

Expand Down Expand Up @@ -251,6 +239,7 @@ Set environment variables for rust process."
(erase-buffer)
(setq default-directory dir)
(funcall mode)
(setq-local rustic-compilation-directory dir)
(unless no-mode-line
(setq mode-line-process
'((:propertize ":%s" face compilation-mode-line-run)
Expand Down Expand Up @@ -297,6 +286,7 @@ ARGS is a plist that affects how the process is run.
(unless (plist-get args :no-display)
(funcall rustic-compile-display-method buf))
(with-current-buffer buf
(setq compilation--start-time (float-time))
(let ((inhibit-read-only t))
(insert (format "%s \n" (s-join " " command))))
(rustic-make-process :name process
Expand Down Expand Up @@ -460,7 +450,7 @@ Buffers are formatted after saving if turned on by `rustic-format-trigger'."
"Provide possibility use `compile-goto-error' on line numbers in compilation buffers.
This hook checks if there's a line number at the beginning of the
current line in an error section."
(-if-let* ((rustic-p (eq major-mode 'rustic-compilation-mode))
(-if-let* ((rustic-p (derived-mode-p 'rustic-compilation-mode))
(default-directory rustic-compilation-workspace)
(line-contents (buffer-substring-no-properties
(line-beginning-position)
Expand Down Expand Up @@ -550,9 +540,10 @@ buffer."
(defun rustic-compile (&optional arg)
"Compile rust project.
If `compilation-read-command' is non-nil or if called with prefix
argument ARG then read the command in the minibuffer. Otherwise
use `rustic-compile-command'.
If the variable `compilation-read-command' is non-nil or if
`rustic-compile` is called with prefix argument ARG then read the
command in the minibuffer. Otherwise use
`rustic-compile-command'.
In either store the used command in `compilation-arguments'."
(interactive "P")
Expand Down
6 changes: 3 additions & 3 deletions rustic-doc.el
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,11 @@ If NOCONFIRM is non-nil, install all dependencies without prompting user."
(when (and (or missing-fd missing-makedocs missing-rg)
(or noconfirm (y-or-n-p "Missing some dependencies for rustic doc, install them? ")))
(when missing-fd
(rustic-doc--start-process "install-fd" "cargo" nil "install" "fd-find"))
(rustic-doc--start-process "install-fd" (rustic-cargo-bin) nil "install" "fd-find"))
(when missing-rg
(rustic-doc--start-process "install-rg" "cargo" nil "install" "ripgrep"))
(rustic-doc--start-process "install-rg" (rustic-cargo-bin) nil "install" "ripgrep"))
(when missing-makedocs
(rustic-doc--start-process "install-makedocs" "cargo" nil
(rustic-doc--start-process "install-makedocs" (rustic-cargo-bin) nil
"install" "cargo-makedocs"))))))

;;;###autoload
Expand Down
4 changes: 1 addition & 3 deletions rustic-lsp.el
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ with `lsp-rust-switch-server'."
;;; eglot support

(defvar eglot-ignored-server-capabilites)
(defvar eglot-ignored-server-capabilites)
(defvar eglot-server-programs)
(defvar eglot-server-programs)
(declare-function eglot-ensure "eglot" ())

Expand All @@ -125,7 +123,7 @@ with `lsp-rust-switch-server'."
(when (symbolp (car mode))
(eq (car mode) 'rust-mode)))
eglot-server-programs)))))
(add-to-list 'eglot-server-programs `(rustic-mode . (eglot-rust-analyzer . ,rustic-analyzer-command)))))
(add-to-list 'eglot-server-programs `((rustic-mode :language-id "rust") . (eglot-rust-analyzer . ,rustic-analyzer-command)))))

(with-eval-after-load 'eglot
(defclass eglot-rust-analyzer (eglot-lsp-server) ()
Expand Down
4 changes: 2 additions & 2 deletions rustic-popup.el
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ If directory is not in a rust project call `read-directory-name'."
(c (intern (concat "rustic-cargo-" command))))
(if (commandp c)
(call-interactively c)
(call-interactively 'rustic-compile (concat "cargo " command)))))
(call-interactively 'rustic-compile (concat (rustic-cargo-bin) " " command)))))
(when rustic-kill-buffer-and-window
(kill-buffer-and-window)))

Expand Down Expand Up @@ -220,7 +220,7 @@ corresponding line."

(defun rustic-popup-help-flags (command)
"Get flags of COMMAND."
(let ((string (shell-command-to-string (format "cargo %s -h" command)))
(let ((string (shell-command-to-string (format "%s %s -h" (rustic-cargo-bin) command)))
flags)
(dolist (s (split-string string "\n"))
(when (and (not (string-match "^\s+\-h" s))
Expand Down
5 changes: 3 additions & 2 deletions rustic-rustfmt.el
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,15 @@ and it's `cdr' is a list of arguments."
(command (or (plist-get args :command)
(rustic-compute-rustfmt-args)))
(command (if (listp command) command (list command)))
(cur-buf (current-buffer)))
(cur-buf (current-buffer))
(rustfmt (rustic-rustfmt-bin)))
(setq rustic-save-pos (set-marker (make-marker) (point) (current-buffer)))
(rustic-compilation-setup-buffer err-buf dir 'rustic-format-mode t)
(--each files
(unless (file-exists-p it)
(error (format "File %s does not exist." it))))
(with-current-buffer err-buf
(let* ((c `(,(rustic-rustfmt-bin)
(let* ((c `(,rustfmt
,@(split-string rustic-rustfmt-args)
,@command "--" ,@files))
(proc (rustic-make-process :name rustic-format-process-name
Expand Down
9 changes: 4 additions & 5 deletions rustic-spellcheck.el
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"Buffer name for spellcheck buffers.")

(defvar rustic-spellcheck-arguments ""
"Holds arguments for 'cargo spellcheck', similar to `compilation-arguments`.")
"Holds arguments for `cargo spellcheck', similar to `compilation-arguments`.")

(defvar rustic-cargo-spellcheck-mode-map
(let ((map (make-sparse-keymap)))
Expand Down Expand Up @@ -90,7 +90,7 @@ Error matching regexes from compile.el are removed."

;;;###autoload
(defun rustic-cargo-spellcheck (&optional arg)
"Run 'cargo spellcheck'.
"Run `cargo spellcheck'.
If ARG is not nil, use value as argument and store it in
`rustic-spellcheck-arguments'. When calling this function from
Expand All @@ -104,18 +104,17 @@ If ARG is not nil, use value as argument and store it in
(t ""))))

(defun rustic-cargo-spellcheck-command (&optional spellcheck-args)
"Start compilation process for 'cargo spellcheck' with optional SPELLCHECK-ARGS."
"Start compilation process for `cargo spellcheck' with optional SPELLCHECK-ARGS."
(let* ((command (list (rustic-cargo-bin) "spellcheck"))
(c (append command (split-string (if spellcheck-args spellcheck-args ""))))
(spellcheck-command (string-join c " "))
(buf rustic-spellcheck-buffer-name)
(proc rustic-spellcheck-process-name)
(mode 'rustic-cargo-spellcheck-mode))
(rustic-spellcheck-compilation c (list :buffer buf :process proc :mode mode))))

;;;###autoload
(defun rustic-cargo-spellcheck-rerun ()
"Run 'cargo spellcheck' with `rustic-spellcheck-arguments'."
"Run `cargo spellcheck' with `rustic-spellcheck-arguments'."
(interactive)
(rustic-cargo-spellcheck-command rustic-spellcheck-arguments))

Expand Down
4 changes: 3 additions & 1 deletion test/rustic-babel-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
;; Before editing, eval (load-file "test-helper.el")

(require 'ert)
(require 'test-helper)
(load (expand-file-name "test-helper.el"
(file-name-directory
(or load-file-name buffer-file-name))))

(setq org-confirm-babel-evaluate nil)

Expand Down
4 changes: 3 additions & 1 deletion test/rustic-cargo-test.el
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
;; -*- lexical-binding: t -*-
;; Before editing, eval (load-file "test-helper.el")
(require 'rustic)
(require 'test-helper)
(load (expand-file-name "test-helper.el"
(file-name-directory
(or load-file-name buffer-file-name))))

(ert-deftest rustic-test-cargo-test ()
(let* ((string "#[test]
Expand Down
4 changes: 3 additions & 1 deletion test/rustic-clippy-test.el
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
;; -*- lexical-binding: t -*-

(require 'rustic)
(require 'test-helper)
(load (expand-file-name "test-helper.el"
(file-name-directory
(or load-file-name buffer-file-name))))

(ert-deftest rustic-test-trigger-and-fix-format-on-compile ()
(ignore-errors (kill-buffer (get-buffer rustic-compilation-buffer-name)))
Expand Down
Loading

0 comments on commit 97ae8a6

Please sign in to comment.