2018-05-14 04:46:45 +00:00
|
|
|
#+TITLE: JSON layer
|
|
|
|
|
2019-05-07 08:21:07 +00:00
|
|
|
#+TAGS: dsl|layer|markup|programming
|
2019-05-05 17:26:40 +00:00
|
|
|
|
2018-05-14 04:46:45 +00:00
|
|
|
[[file:img/json.png]]
|
|
|
|
|
2019-05-07 20:05:06 +00:00
|
|
|
* Table of Contents :TOC_5_gh:noexport:
|
2018-05-14 04:46:45 +00:00
|
|
|
- [[#description][Description]]
|
|
|
|
- [[#features][Features:]]
|
|
|
|
- [[#install][Install]]
|
2020-06-23 20:19:51 +00:00
|
|
|
- [[#configuration][Configuration]]
|
2018-05-18 05:15:03 +00:00
|
|
|
- [[#web-beautify][web-beautify]]
|
2018-06-19 06:25:33 +00:00
|
|
|
- [[#prettier][prettier]]
|
|
|
|
- [[#choosing-a-formatter][Choosing a formatter]]
|
2019-09-05 19:09:25 +00:00
|
|
|
- [[#formatting-on-save][Formatting on save]]
|
2020-06-23 20:19:51 +00:00
|
|
|
- [[#lsp][LSP]]
|
2018-05-14 04:46:45 +00:00
|
|
|
- [[#usage][Usage]]
|
|
|
|
- [[#reformat][Reformat]]
|
|
|
|
- [[#display-navigable-hierarchy][Display navigable hierarchy]]
|
2018-12-05 03:03:03 +00:00
|
|
|
- [[#key-bindings][Key bindings]]
|
2018-05-14 04:46:45 +00:00
|
|
|
- [[#json-hierarchy][JSON hierarchy]]
|
|
|
|
|
|
|
|
* Description
|
|
|
|
This layer adds support for JSON files with [[https://github.com/joshwnj/json-mode][json-mode]]
|
|
|
|
|
|
|
|
** Features:
|
|
|
|
- Syntax highlighting
|
|
|
|
- Auto-completion
|
|
|
|
- Get the path to a JSON value with [[https://github.com/Sterlingg/json-snatcher][json-snatcher]]
|
|
|
|
- Navigate JSON hierarchy with [[https://github.com/DamienCassou/json-navigator][json-nagivator]]
|
2018-11-07 06:30:42 +00:00
|
|
|
- Formatting with [[https://github.com/yasuyk/web-beautify][web-beautify]] or [[https://github.com/prettier/prettier][prettier]]
|
2018-05-14 04:46:45 +00:00
|
|
|
|
|
|
|
* Install
|
|
|
|
To use this configuration layer, add it to your =~/.spacemacs=. You will need to
|
|
|
|
add =json= to the existing =dotspacemacs-configuration-layers= list in this file.
|
|
|
|
|
2020-06-23 20:19:51 +00:00
|
|
|
* Configuration
|
2018-05-14 04:46:45 +00:00
|
|
|
To define the default indentation set the variable =js-indent-level=.
|
|
|
|
|
2018-05-18 05:15:03 +00:00
|
|
|
** web-beautify
|
2018-05-18 06:04:45 +00:00
|
|
|
See [[file:../../+tools/web-beautify/README.org][web-beautify layer]] documentation.
|
2018-05-18 05:15:03 +00:00
|
|
|
|
2018-06-19 06:25:33 +00:00
|
|
|
** prettier
|
|
|
|
See [[file:../../+tools/prettier/README.org][prettier layer]] documentation.
|
|
|
|
|
|
|
|
** Choosing a formatter
|
|
|
|
To choose a formatter, set the layer variable =json-fmt-tool=:
|
|
|
|
|
|
|
|
#+BEGIN_SRC elisp
|
2018-09-19 03:54:47 +00:00
|
|
|
(json :variables json-fmt-tool 'web-beautify)
|
2018-06-19 06:25:33 +00:00
|
|
|
#+END_SRC
|
|
|
|
|
2020-06-23 20:19:51 +00:00
|
|
|
The formatter can be chosen on a per project basis using directory local variables
|
2018-06-19 06:25:33 +00:00
|
|
|
(files named =.dir-locals.el= at the root of a project), an example to use the
|
|
|
|
=prettier= formatter:
|
|
|
|
|
|
|
|
#+BEGIN_SRC elisp
|
2018-09-19 03:54:47 +00:00
|
|
|
;;; Directory Local Variables
|
|
|
|
;;; For more information see (info "(emacs) Directory Variables")
|
2018-06-19 06:25:33 +00:00
|
|
|
|
2018-09-19 03:54:47 +00:00
|
|
|
((json-mode (json-fmt-tool . prettier)))
|
2018-06-19 06:25:33 +00:00
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
*Note:* you can easily add a directory local variable with ~SPC f v d~.
|
|
|
|
|
2019-09-05 03:44:13 +00:00
|
|
|
** Formatting on save
|
|
|
|
To enable using the selected formatter on save, set the layer variable =json-fmt-on-save=:
|
|
|
|
|
|
|
|
#+BEGIN_SRC elisp
|
|
|
|
(json :variables json-fmt-on-save t)
|
|
|
|
#+END_SRC
|
|
|
|
|
2020-06-23 20:19:51 +00:00
|
|
|
** LSP
|
|
|
|
To enable LSP, install the lsp server via npm,
|
|
|
|
then set the layer variable =json-backend= to ='lsp= like shown below:
|
|
|
|
|
|
|
|
#+BEGIN_SRC emacs-lisp
|
|
|
|
(json :variables json-backend 'lsp)
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
Alternatively you can also keep the variable on nil, then lsp will be used if lsp
|
|
|
|
layer is loaded.
|
|
|
|
|
|
|
|
Installing the lsp server dependency can be done like this:
|
|
|
|
|
|
|
|
#+BEGIN_SRC sh
|
|
|
|
npm install -g vscode-json-languageserver
|
|
|
|
#+END_SRC
|
|
|
|
|
2018-05-14 04:46:45 +00:00
|
|
|
* Usage
|
|
|
|
** Reformat
|
2020-06-23 20:19:51 +00:00
|
|
|
~SPC m = =~ will reformat the whole buffer or the active region. Use numerical
|
2018-05-14 04:46:45 +00:00
|
|
|
prefix argument to specify a different indentation than =js-indent-level=.
|
|
|
|
Use the universal prefix argument to print decoded strings, for instance:
|
|
|
|
|
2018-09-19 03:54:47 +00:00
|
|
|
#+BEGIN_SRC json
|
|
|
|
{"name":"foo\"bar","nick":"foo \u00e4 bar","description":"<pre>\nbaz\n</pre>","home":"/home/foobar"}
|
2018-05-14 04:46:45 +00:00
|
|
|
|
2018-09-19 03:54:47 +00:00
|
|
|
Will be reformatted:
|
2018-05-14 04:46:45 +00:00
|
|
|
|
2018-09-19 03:54:47 +00:00
|
|
|
{
|
|
|
|
"name": "foo\"bar",
|
|
|
|
"nick": "foo ä bar",
|
|
|
|
"description": "<pre>
|
|
|
|
baz
|
|
|
|
</pre>",
|
|
|
|
"home": "/home/foobar"
|
|
|
|
}
|
|
|
|
#+END_SRC
|
2018-05-14 04:46:45 +00:00
|
|
|
|
|
|
|
** Display navigable hierarchy
|
2020-06-23 20:19:51 +00:00
|
|
|
~SPC m T h~ toggle the display of a hierarchy for the whole JSON document or the active
|
|
|
|
region. Use the universal prefix argument ~SPC u SPC m T h~ to create the
|
2018-05-14 04:46:45 +00:00
|
|
|
hierarchy for the JSON *after* the point.
|
|
|
|
|
2018-12-05 03:03:03 +00:00
|
|
|
* Key bindings
|
2018-05-14 04:46:45 +00:00
|
|
|
|
2020-06-23 20:19:51 +00:00
|
|
|
| Key binding | Description |
|
|
|
|
|-------------+------------------------------------------------|
|
|
|
|
| ~SPC m = =~ | Reformat thing under point |
|
|
|
|
| ~SPC m h p~ | Print the path to the json element under point |
|
|
|
|
| ~SPC m T h~ | Toggle graphical JSON hierarchy |
|
2018-05-14 04:46:45 +00:00
|
|
|
|
|
|
|
** JSON hierarchy
|
|
|
|
|
2018-12-05 03:03:03 +00:00
|
|
|
| Key binding | Description |
|
2018-05-14 04:46:45 +00:00
|
|
|
|-------------+----------------------|
|
|
|
|
| ~RET~ | Expand/Collapse node |
|
|
|
|
| ~TAB~ | Select next node |
|
|
|
|
| ~S-TAB~ | Select previous node |
|