Skip to content

Commit a0eea19

Browse files
Broaden documentation (#95)
* Update readme to suggest hooking into a mode that comes with Emacs * Document that this package works with more than just JS * Document available commands * Simplify check for code block It's not really important that point be specifically inside the code block. Being "at" the code block is fine and makes it a bit easier to satisfy the condition.
1 parent c581e12 commit a0eea19

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

Eask

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
(package "prettier-js"
44
"0.1.0"
5-
"Minor mode to format JS code on file save")
5+
"Minor mode to format code on file save")
66

77
(website-url "https://github.com/prettier/prettier-emacs")
88
(keywords "convenience" "wp" "edit" "js")

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
This Emacs package provides a function, `prettier-js-prettify`, which formats the current buffer using [Prettier](https://github.com/prettier/prettier). It also exports a minor mode, `prettier-js-mode`, which calls `prettier-js-prettify` on save.
66

7+
Despite the "-js" in its name, this package actually supports more than just JavaScript. It works with any language that Prettier supports! [List here.](https://prettier.io/docs/)
8+
79
## Configuration
810

911
### Requirements
@@ -63,10 +65,10 @@ Or use `use-package` (available in Emacs 29.1 and above):
6365
(use-package prettier-js)
6466
```
6567

66-
Then you can hook into your favorite JavaScript mode:
68+
Then you can hook into any modes where you want to format with Prettier:
6769

6870
```elisp
69-
(add-hook 'js2-mode-hook 'prettier-js-mode)
71+
(add-hook 'js-mode-hook 'prettier-js-mode)
7072
(add-hook 'web-mode-hook 'prettier-js-mode)
7173
```
7274

@@ -88,8 +90,7 @@ Say that you only want to use Prettier with certain projects. Instead of configu
8890
(if (locate-dominating-file default-directory ".prettierrc")
8991
(prettier-js-mode +1)))
9092
91-
(add-hook 'typescript-mode-hook 'maybe-use-prettier)
92-
(add-hook 'js2-mode-hook 'maybe-use-prettier)
93+
(add-hook 'js-mode-hook 'maybe-use-prettier)
9394
```
9495

9596
Alternatively, say that you want to use Prettier everywhere by default, except for when editing files in certain directories. Use `(add-hook 'some-mode-hook 'prettier-js-mode)`, and in directories where you want Prettier disabled, add a `.dir-locals.el` file with the following contents:
@@ -140,6 +141,12 @@ If you want to mostly eliminate the overhead of running the `prettier` command o
140141

141142
Note that this may come at the expense of a bit more complexity in terms of configuring/managing the daemon.
142143

144+
## Available commands
145+
146+
* `M-x prettier-js-prettify` formats the current buffer. In `org-mode`, it formats all the code blocks in the buffer.
147+
* `M-x prettier-js-prettify-region` formats the current region
148+
* `M-x prettier-js-prettify-code-block` formats the code block at point (in `org-mode`)
149+
143150
## Customization
144151

145152
This package is customizable via Emacs' easy customization interface:

prettier-js-test.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@
421421

422422
;; Verify that the appropriate error is signaled with the correct message
423423
(let ((err (should-error (prettier-js-prettify-code-block) :type 'user-error)))
424-
(should (string= "Not inside a source code block" (cadr err))))))
424+
(should (string= "No source code block at point" (cadr err))))))
425425

426426
(ert-deftest prettier-js-test-prettify-code-blocks ()
427427
"Test that prettier-js-prettify-code-blocks formats all code blocks in an org file."

prettier-js.el

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
;;; prettier-js.el --- Minor mode to format JS code on file save -*- lexical-binding: t; -*-
1+
;;; prettier-js.el --- Minor mode to format code on file save -*- lexical-binding: t; -*-
22

33
;; Copyright (c) 2014 The go-mode Authors. All rights reserved.
44
;; Portions Copyright (c) 2015-present, Facebook, Inc. All rights reserved.
@@ -39,7 +39,7 @@
3939
;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.)
4040

4141
;;; Commentary:
42-
;; Formats your JavaScript code using 'prettier' on file save.
42+
;; Formats your code using 'prettier' on file save.
4343

4444
;;; Code:
4545

@@ -49,7 +49,7 @@
4949
(declare-function org-element-type "org-element-ast")
5050

5151
(defgroup prettier-js nil
52-
"Minor mode to format JS code on file save"
52+
"Minor mode to format code on file save"
5353
:group 'languages
5454
:prefix "prettier-js"
5555
:link '(url-link :tag "Repository" "https://github.com/prettier/prettier"))
@@ -429,15 +429,8 @@ Signal an error if not within a code block."
429429
(unless (derived-mode-p 'org-mode)
430430
(user-error "Not in org-mode"))
431431
(let ((element (org-element-at-point)))
432-
;; Like (org-in-src-block-p t):
433-
(unless (and (eq (org-element-type element) 'src-block)
434-
(not (or (<= (line-beginning-position)
435-
(org-element-property :post-affiliated element))
436-
(>= (line-end-position)
437-
(org-with-point-at (org-element-property :end element)
438-
(skip-chars-backward " \t\n\r")
439-
(point))))))
440-
(user-error "Not inside a source code block"))
432+
(unless (eq (org-element-type element) 'src-block)
433+
(user-error "No source code block at point"))
441434
(prettier-js--format-code-block element)))
442435

443436
(defun prettier-js--format-code-block (element)

0 commit comments

Comments
 (0)