Skip to content

Commit

Permalink
Merge pull request #61 from kenranunderscore/add-mode-specific-commands
Browse files Browse the repository at this point in the history
Integrate with `read-extended-command-predicate`
  • Loading branch information
purcell authored Dec 4, 2024
2 parents 8962e1d + dce9c3f commit f2cb594
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
31 changes: 31 additions & 0 deletions reformatter-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,37 @@
(reformatter-tests-shfmt-in-place-buffer)
(should (equal "[ foo ] && echo yes\n" (buffer-string)))))

;; Formatting commands tagged for specific modes: `command-modes' checks which
;; modes they're defined to be interactively usable in, but it's only available
;; in Emacs 28 and newer.
(when (fboundp 'command-modes)
(reformatter-define reformatter-tests-shfmt-no-interactive-modes
:program "shfmt")

(ert-deftest reformatter-tests-no-interactive-modes ()
(should (not (command-modes 'reformatter-tests-shfmt-no-interactive-modes-buffer)))
(should (not (command-modes 'reformatter-tests-shfmt-no-interactive-modes-region))))

(reformatter-define reformatter-tests-shfmt-single-interactive-mode
:program "shfmt"
:interactive-modes (sh-mode))

(ert-deftest reformatter-tests-single-interactive-mode ()
(should (equal (command-modes 'reformatter-tests-shfmt-single-interactive-mode-buffer)
'(sh-mode)))
(should (equal (command-modes 'reformatter-tests-shfmt-single-interactive-mode-region)
'(sh-mode))))

(reformatter-define reformatter-tests-shfmt-multiple-interactive-modes
:program "shfmt"
:interactive-modes (sh-mode haskell-mode))

(ert-deftest reformatter-tests-multiple-interactive-modes ()
(should (equal (command-modes 'reformatter-tests-shfmt-multiple-interactive-modes-buffer)
'(sh-mode haskell-mode)))
(should (equal (command-modes 'reformatter-tests-shfmt-multiple-interactive-modes-region)
'(sh-mode haskell-mode)))))


(provide 'reformatter-tests)
;;; reformatter-tests.el ends here
17 changes: 13 additions & 4 deletions reformatter.el
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ WORKING-DIRECTORY see the documentation of the `reformatter-define' macro."
(delete-file stdout-file))))

;;;###autoload
(cl-defmacro reformatter-define (name &key program args (mode t) (stdin t) (stdout t) input-file lighter keymap group (exit-code-success-p 'zerop) working-directory)
(cl-defmacro reformatter-define (name &key program args (mode t) (stdin t) (stdout t) input-file lighter keymap group (exit-code-success-p 'zerop) working-directory interactive-modes)
"Define a reformatter command with NAME.
When called, the reformatter will use PROGRAM and any ARGS to
Expand Down Expand Up @@ -241,7 +241,16 @@ WORKING-DIRECTORY
Directory where your reformatter program is started. If provided, this
should be a form that evaluates to a string at runtime. Default is the
value of `default-directory' in the buffer."
value of `default-directory' in the buffer.
INTERACTIVE-MODES
If provided, this is a list of mode names (as unquoted
symbols). The created commands for formatting regions and
buffers are then tagged for interactive use in these modes,
making them compatible with some built-in predicate functions
for `read-extended-command-predicate', like
`command-completion-default-include-p'."
(declare (indent defun))
(cl-assert (symbolp name))
(cl-assert (functionp exit-code-success-p))
Expand Down Expand Up @@ -282,7 +291,7 @@ might use:
"Reformats the region from BEG to END.
When called interactively, or with prefix argument
DISPLAY-ERRORS, shows a buffer if the formatting fails."
(interactive "rp")
(interactive "rp" ,@interactive-modes)
(let ((input-file ,(if input-file
input-file
`(reformatter--make-temp-file ',name))))
Expand All @@ -300,7 +309,7 @@ DISPLAY-ERRORS, shows a buffer if the formatting fails."
"Reformats the current buffer.
When called interactively, or with prefix argument
DISPLAY-ERRORS, shows a buffer if the formatting fails."
(interactive "p")
(interactive "p" ,@interactive-modes)
(message "Formatting buffer")
(,region-fn-name (point-min) (point-max) display-errors))

Expand Down

0 comments on commit f2cb594

Please sign in to comment.