Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The-one-only #60

Open
wants to merge 29 commits into
base: prettier-js-prettify-region
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7d5bdd8
Correct use of unwind-protect
juergenhoetzel Jun 12, 2017
13a5df4
Add Melpa badge
troglotit Jun 13, 2017
d10891e
Merge pull request #6 from troglotit/melpa-badge
rc-mz Jun 14, 2017
4bfc8b3
Merge pull request #5 from juergenhoetzel/fix-temp-file-leak
rc-mz Jun 14, 2017
3cdd851
Pass `bufferfile' on stdin and buffer-file-name as target
eqyiel Jul 3, 2017
22a3334
Merge pull request #9 from eqyiel/master
rc-mz Jul 3, 2017
2810a51
Fix support for non-js files
aaronjensen Jul 11, 2017
a663433
Fix type of defcustom prettier-js-args
jkseppan Jul 12, 2017
11ead61
Merge pull request #11 from jkseppan/prettier-js-args-type
rc-mz Jul 13, 2017
9cac36f
Merge pull request #10 from aaronjensen/fix-support-for-non-js
rc-mz Jul 13, 2017
c36c31b
Fix for Issue #13
baerrach Aug 12, 2017
c030992
Capitalize prettier-mode lighter
anmonteiro Aug 20, 2017
2542776
Merge pull request #15 from anmonteiro/patch-1
rc-mz Aug 23, 2017
56a228b
Merge pull request #14 from baerrach/patch-1
rc-mz Aug 23, 2017
d581775
Fixed typo
sunesimonsen Aug 27, 2017
6cc79cc
Merge pull request #16 from sunesimonsen/patch-1
rc-mz Aug 28, 2017
18c942a
Fix filename displayed in error buffer
aaronjensen Nov 25, 2017
0147a79
Apply patch without disturbing kill ring
jscheid Dec 17, 2017
8e44aad
update readme with prettier requirements
modulitos Dec 22, 2017
c664fb1
Merge pull request #23 from jscheid/18-preserve-kill-ring
rc-mz Jan 9, 2018
2931c3e
Merge pull request #24 from modulitos/update-readme
rc-mz Jan 9, 2018
0e8b95c
Merge pull request #22 from aaronjensen/fix-error-file-name
rc-mz Jan 9, 2018
e3aeff7
Fixed install typo
clayrisser Jan 22, 2018
c64e7b0
Merge pull request #26 from jamrizzi/patch-1
rc-mz Feb 15, 2018
c50f93a
Update README.md with add-node-modules-path
rc-mz Feb 15, 2018
fac9dd2
Update README.md
rc-mz Feb 15, 2018
30a1410
Update README.md with Windows Installation Notes
Mar 11, 2019
e9b73e8
Merge pull request #52 from tgroshon/patch-1
Mar 11, 2019
4159d36
The-one-only
The-one-only Sep 20, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
# Prettier-js for Emacs
[![MELPA](http://melpa.org/packages/prettier-js-badge.svg)](http://melpa.org/#/prettier-js)

prettier-js is a function that formats the current buffer using [prettier](https://github.com/prettier/prettier). The
package also exports a minor mode that applies `(prettier-js)` on save.

## Configuration

### Requirements

Ensure that the prettier program is installed:

```bash
which prettier
```

If prettier is not installed already, you can install prettier using `npm install -g prettier` or via your package manager.


### Basic configuration

First require the package:
Expand Down Expand Up @@ -51,6 +63,15 @@ And then hook to web-mode like this:
(enable-minor-mode
'("\\.jsx?\\'" . prettier-js-mode))))
```
## Installing on Windows

This package requires the `diff` tool which is already included on Unix platforms. The simplest way to install `diff` on Windows is to use [Chocolatey](https://chocolatey.org/). The steps are as follows:

1. Follow the Chocolatey install instructions: https://chocolatey.org/install
2. Open an Admin Powershell session
3. Install the `diff` program: `choco install diffutils`

You should now be able to open Emacs and successfully use this package.

## Customization

Expand All @@ -63,4 +84,15 @@ M-x customize-group prettier-js
* `prettier-js-command` is the prettier command
* `prettier-js-args` are the args passed to the prettier command
* `prettier-js-show-errors` customizes where to display the error output (buffer, echo or nil)
* `prettier-js-width-mode` customizes the width when formatting ubffer contents (window, fill or nil)
* `prettier-js-width-mode` customizes the width when formatting buffer contents (window, fill or nil)

## Using node_modules/.bin/prettier

If you want to use your project's prettier version you can rely on https://github.com/codesuki/add-node-modules-path

```elisp
(eval-after-load 'web-mode
'(progn
(add-hook 'web-mode-hook #'add-node-modules-path)
(add-hook 'web-mode-hook #'prettier-js-mode)))
```
1 change: 1 addition & 0 deletions The-one-only
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

33 changes: 17 additions & 16 deletions prettier-js.el
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

(defcustom prettier-js-args '()
"List of args to send to prettier command."
:type 'list
:type '(repeat string)
:group 'prettier-js)

(defcustom prettier-js-show-errors 'buffer
Expand Down Expand Up @@ -122,13 +122,14 @@ a `before-save-hook'."
(with-current-buffer target-buffer
(prettier-js--goto-line (- from line-offset))
(setq line-offset (+ line-offset len))
(kill-whole-line len)
(setq kill-ring (cdr kill-ring))))
(let ((beg (point)))
(forward-line len)
(delete-region (point) beg))))
(t
(error "Invalid rcs patch or internal error in prettier-js--apply-rcs-patch")))))))))

(defun prettier-js--process-errors (filename tmpfile errorfile errbuf)
"Process errors for FILENAME, using a TMPFILE an ERRORFILE and display the output in ERRBUF."
(defun prettier-js--process-errors (filename errorfile errbuf)
"Process errors for FILENAME, using an ERRORFILE and display the output in ERRBUF."
(with-current-buffer errbuf
(if (eq prettier-js-show-errors 'echo)
(progn
Expand All @@ -138,7 +139,7 @@ a `before-save-hook'."
;; Convert the prettier stderr to something understood by the compilation mode.
(goto-char (point-min))
(insert "prettier errors:\n")
(while (search-forward-regexp (regexp-quote tmpfile) nil t)
(while (search-forward-regexp "^stdin" nil t)
(replace-match (file-name-nondirectory filename)))
(compilation-mode)
(display-buffer errbuf))))
Expand Down Expand Up @@ -182,27 +183,27 @@ a `before-save-hook'."
(with-current-buffer patchbuf
(erase-buffer))
(if (zerop (apply 'call-process
prettier-js-command nil (list (list :file outputfile) errorfile)
nil (append (append prettier-js-args width-args) (list bufferfile))))
prettier-js-command bufferfile (list (list :file outputfile) errorfile)
nil (append prettier-js-args width-args (list "--stdin" "--stdin-filepath" buffer-file-name))))
(progn
(call-process-region (point-min) (point-max) "diff" nil patchbuf nil "-n" "-"
(call-process-region (point-min) (point-max) "diff" nil patchbuf nil "-n" "--strip-trailing-cr" "-"
outputfile)
(prettier-js--apply-rcs-patch patchbuf)
(message "Applied prettier with args `%s'" prettier-js-args)
(if errbuf (prettier-js--kill-error-buffer errbuf)))
(message "Could not apply prettier")
(if errbuf
(prettier-js--process-errors (buffer-file-name) bufferfile errorfile errbuf))
)))
(kill-buffer patchbuf)
(delete-file errorfile)
(delete-file bufferfile)
(delete-file outputfile)))
(prettier-js--process-errors (buffer-file-name) errorfile errbuf))
))
(kill-buffer patchbuf)
(delete-file errorfile)
(delete-file bufferfile)
(delete-file outputfile))))

;;;###autoload
(define-minor-mode prettier-js-mode
"Runs prettier on file save when this mode is turned on"
:lighter " prettier"
:lighter " Prettier"
:global nil
(if prettier-js-mode
(add-hook 'before-save-hook 'prettier-js nil 'local)
Expand Down