Add purescript formatting support (#14984)

This commit is contained in:
Anupam | अनुपम 2021-08-20 09:18:29 +05:30 committed by GitHub
parent a99b527056
commit 9745c4e88a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 2 deletions

View File

@ -3130,6 +3130,7 @@ files (thanks to Daniel Nicolai)
- Enabled =purescript-decl-scan-mode= when loading =purescript-mode=
(thanks to Bjarke Vad Andersen)
- Allow adding node_modules/.bin path to exec-path (thanks to Anupam Jain)
- Added support for purescript formatters (thanks to Anupam Jain)
- Improved purescript keybinding discoverability & ergonomics.
(thanks to Jason Walker)
- Added =purescript-mode= to =spacemacs-indent-sensitive-modes=

View File

@ -9,6 +9,7 @@
- [[#features][Features:]]
- [[#install][Install]]
- [[#configuration][Configuration]]
- [[#formatting][Formatting]]
- [[#add-import-on-completion][Add import on completion]]
- [[#node-modules][Node Modules]]
- [[#error-popup][Error popup]]
@ -17,6 +18,7 @@
- [[#purescript][Purescript]]
- [[#imports][Imports]]
- [[#psc-ide][psc-ide]]
- [[#formatting-1][Formatting]]
- [[#repl][REPL]]
* Description
@ -39,6 +41,33 @@ binaries (psc-ide-server, psci,...) are on your path. Installation instructions
can be found [[https://github.com/purescript/documentation/blob/master/guides/Getting-Started.md][here]].
* Configuration
** Formatting
The following formatting tools are currently supported:
- =purs-tidy= (default): [https://github.com/natefaubion/purescript-tidy][Purs-tidy]]
You can choose the formatting tool by setting =purescript-fmt-tool=
variable in your =purescript= config section:
#+BEGIN_SRC elisp
(setq-default dotspacemacs-configuration-layers '(
(purescript :variables
purescript-fmt-tool 'purs-tidy)))
#+END_SRC
You can run the formatter manually with either `M-x spacemacs/purescript-format`
or with the shortcut `SPC m =`.
To enable automatic formatting of the buffer on save, enable =purescript-fmt-on-save=
variable in your =purescript= config section:
#+BEGIN_SRC elisp
(setq-default dotspacemacs-configuration-layers '(
(purescript :variables
purescript-fmt-on-save t)))
#+END_SRC
** Add import on completion
Set =purescript-add-import-on-completion= to nil to make =psc-ide= stop adding
imports on completion. Default value is =t=.
@ -113,6 +142,12 @@ intellisense config for LSP.
| ~SPC m h t~ | Show type at point |
| ~SPC m g g~ | Goto definition for identifier at point |
*** Formatting
| Key binding | Description |
|---------------+---------------------------------------------------------------------------|
| ~SPC m =~ | Format the buffer |
** REPL
[[https://github.com/ardumont/emacs-psci][psci]] provides a very basic REPL for purescript. The following key
bindings are available:

View File

@ -25,6 +25,13 @@
(spacemacs|define-jump-handlers purescript-mode)
(defvar purescript-fmt-on-save nil
"Run formatter on buffer save.")
(defvar purescript-fmt-tool 'purs-tidy
"The name of the tool to be used for Purescript source code formatting.
Possible values are 'purs-tidy (default).")
(defvar purescript-add-import-on-completion t
"If non-nil adds imports for completed identifiers")

View File

@ -37,3 +37,44 @@
(spacemacs|add-company-backends
:backends company-psc-ide-backend
:modes purescript-mode))))
(defun spacemacs/purescript-format ()
"Call formatting tool specified in `purescript-fmt-tool'."
(interactive)
(call-interactively
(pcase purescript-fmt-tool
('purs-tidy 'spacemacs/purescript-purs-tidy-format-buffer)
(_ (user-error
"%s isn't a valid purescript formatter. Possible values are 'purs-tidy"
purescript-fmt-tool)))))
(defun spacemacs/purescript-purs-tidy-format-buffer ()
"Format buffer with purs-tidy."
(interactive)
(if (executable-find "purs-tidy")
(let* ((extension (file-name-extension (or buffer-file-name "tmp.purs") t))
(tmpfile (make-temp-file "~fmt-tmp" nil extension))
(coding-system-for-read 'utf-8)
(coding-system-for-write 'utf-8)
(outputbuf (get-buffer-create "*~fmt-tmp.purs*")))
(unwind-protect
(progn
(with-current-buffer outputbuf (erase-buffer))
(write-region nil nil tmpfile)
(if (zerop (apply #'call-process-region nil nil "purs-tidy" nil
`(,outputbuf ,tmpfile) nil
`("format")))
(let ((p (point)))
(save-excursion
(with-current-buffer (current-buffer)
(replace-buffer-contents outputbuf)))
(goto-char p)
(message "formatted.")
(kill-buffer outputbuf))
(message "Formatting failed!")
(display-buffer outputbuf)))
(delete-file tmpfile)))
(error "purs-tidy not found. Run \"npm install -g purs-tidy\"")))
(defun spacemacs/purescript-fmt-before-save-hook ()
(add-hook 'before-save-hook 'spacemacs/purescript-format t t))

View File

@ -50,14 +50,16 @@
(add-hook 'purescript-mode-hook 'turn-on-purescript-indentation)
(add-hook 'purescript-mode-hook 'purescript-decl-scan-mode)
(add-hook 'purescript-mode-hook #'spacemacs//purescript-setup-backend)
(when purescript-fmt-on-save
(add-hook 'purescript-mode-hook 'spacemacs/purescript-fmt-before-save-hook))
(spacemacs/declare-prefix-for-mode 'purescript-mode "mg" "goto")
(spacemacs/declare-prefix-for-mode 'purescript-mode "mi" "imports")
(spacemacs/set-leader-keys-for-major-mode 'purescript-mode
"i=" 'purescript-mode-format-imports
"i`" 'purescript-navigate-imports-return
"ia" 'purescript-align-imports
"in" 'purescript-navigate-imports))))
"in" 'purescript-navigate-imports
"=" 'spacemacs/purescript-format))))
(defun purescript/init-psci ()
(use-package psci