This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
spacemacs/layers/+lang/json
2021-06-06 22:26:31 +02:00
..
img
config.el json: refactor 2021-04-04 12:48:21 +02:00
funcs.el Defer loading of lsp servers up to when the buffer is visible 2021-06-06 22:26:31 +02:00
layers.el Apply GPLv3 terms explicitly to all elisp files 2021-03-25 22:59:32 -04:00
packages.el [json] Fix "void variable" in init-json-navigator 2021-05-12 05:13:29 +00:00
README.org [json] Add lsp support 2020-06-23 22:58:26 +02:00

JSON layer

/TakeV/spacemacs/media/commit/80386e8414f74498314c6ded7d4f34b9e5f24068/layers/+lang/json/img/json.png

Description

This layer adds support for JSON files with json-mode

Features:

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.

Configuration

To define the default indentation set the variable js-indent-level.

web-beautify

See web-beautify layer documentation.

prettier

See prettier layer documentation.

Choosing a formatter

To choose a formatter, set the layer variable json-fmt-tool:

  (json :variables json-fmt-tool 'web-beautify)

The formatter can be chosen on a per project basis using directory local variables (files named .dir-locals.el at the root of a project), an example to use the prettier formatter:

  ;;; Directory Local Variables
  ;;; For more information see (info "(emacs) Directory Variables")

  ((json-mode (json-fmt-tool . prettier)))

Note: you can easily add a directory local variable with SPC f v d.

Formatting on save

To enable using the selected formatter on save, set the layer variable json-fmt-on-save:

  (json :variables json-fmt-on-save t)

LSP

To enable LSP, install the lsp server via npm, then set the layer variable json-backend to 'lsp like shown below:

  (json :variables json-backend 'lsp)

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:

  npm install -g vscode-json-languageserver

Usage

Reformat

SPC m = = will reformat the whole buffer or the active region. Use numerical prefix argument to specify a different indentation than js-indent-level. Use the universal prefix argument to print decoded strings, for instance:

  {"name":"foo\"bar","nick":"foo \u00e4 bar","description":"<pre>\nbaz\n</pre>","home":"/home/foobar"}

  Will be reformatted:

  {
    "name": "foo\"bar",
    "nick": "foo ä bar",
    "description": "<pre>
    baz
    </pre>",
    "home": "/home/foobar"
  }

Display navigable hierarchy

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 hierarchy for the JSON after the point.

Key bindings

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

JSON hierarchy

Key binding Description
RET Expand/Collapse node
TAB Select next node
S-TAB Select previous node