diff --git a/README.md b/README.md index 2864f6f..d263fcb 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 @@ -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))) +``` diff --git a/The-one-only b/The-one-only new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/The-one-only @@ -0,0 +1 @@ + diff --git a/prettier-js.el b/prettier-js.el index b1dbce8..08d90b8 100644 --- a/prettier-js.el +++ b/prettier-js.el @@ -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 @@ -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 @@ -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)))) @@ -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)