Create new json layer extracted from javascript layer

* add some goodies like `json-reformat` and `json-navigator`
* make the javascript layer dependent on the json layer
This commit is contained in:
syl20bnr 2018-05-14 00:46:45 -04:00
parent ac4b2686b4
commit e481894fcb
7 changed files with 167 additions and 28 deletions

View File

@ -21,7 +21,6 @@
- [[#formatting-web-beautify][Formatting (web-beautify)]]
- [[#documentation-js-doc][Documentation (js-doc)]]
- [[#auto-complete-and-documentation-tern][Auto-complete and documentation (tern)]]
- [[#json][JSON]]
- [[#repl-skewer-mode][REPL (skewer-mode)]]
* Description
@ -33,7 +32,6 @@ This layer adds support for the JavaScript language using [[https://github.com/m
- Refactoring: done using [[https://github.com/magnars/js2-refactor.el][js2-refactor]].
- Auto-completion and documentation
- Formatting with [[https://github.com/yasuyk/web-beautify][web-beautify]]
- Get the path to a JSON value with [[https://github.com/Sterlingg/json-snatcher][json-snatcher]]
- REPL available via [[https://github.com/skeeto/skewer-mode][skewer-mode]] and [[https://github.com/pandeiro/livid-mode][livid-mode]]
* Install
@ -240,12 +238,6 @@ You can check more [[https://github.com/mooz/js-doc/][here]]
| ~SPC m h t~ | find the type of the thing under the cursor |
| ~SPC m r r V~ | rename variable under the cursor using tern |
** JSON
| Key Binding | Description |
|-------------+------------------------------------|
| ~SPC m h p~ | Get the path of the value at point |
** REPL (skewer-mode)
| Key Binding | Description |

View File

@ -9,4 +9,4 @@
;;
;;; License: GPLv3
(configuration-layer/declare-layers '(node))
(configuration-layer/declare-layers '(node json))

View File

@ -24,8 +24,6 @@
js-doc
js2-mode
js2-refactor
json-mode
json-snatcher
livid-mode
(lsp-javascript-typescript
:requires lsp-mode
@ -36,10 +34,8 @@
web-beautify))
(defun javascript/post-init-add-node-modules-path ()
(spacemacs/add-to-hooks #'add-node-modules-path
'(css-mode-hook
js2-mode-hook
json-mode-hook)))
(spacemacs/add-to-hooks #'add-node-modules-path '(css-mode-hook
js2-mode-hook)))
(defun javascript/post-init-counsel-gtags ()
(spacemacs/counsel-gtags-define-keys-for-mode 'js2-mode))
@ -55,8 +51,7 @@
:defer t))
(defun javascript/post-init-flycheck ()
(dolist (mode '(js2-mode json-mode))
(spacemacs/enable-flycheck mode)))
(spacemacs/enable-flycheck 'js2-mode))
(defun javascript/post-init-ggtags ()
(add-hook 'js2-mode-local-vars-hook #'spacemacs/ggtags-mode-enable))
@ -161,17 +156,6 @@
"xmj" 'js2r-move-line-down
"xmk" 'js2r-move-line-up))))
(defun javascript/init-json-mode ()
(use-package json-mode
:defer t))
(defun javascript/init-json-snatcher ()
(use-package json-snatcher
:defer t
:config
(spacemacs/set-leader-keys-for-major-mode 'json-mode
"hp" 'jsons-print-path)))
(defun javascript/init-livid-mode ()
(use-package livid-mode
:defer t

View File

@ -0,0 +1,74 @@
#+TITLE: JSON layer
[[file:img/json.png]]
* Table of Contents :TOC_4_gh:noexport:
- [[#description][Description]]
- [[#features][Features:]]
- [[#install][Install]]
- [[#configuration][Configuration]]
- [[#usage][Usage]]
- [[#reformat][Reformat]]
- [[#display-navigable-hierarchy][Display navigable hierarchy]]
- [[#key-bindings][Key Bindings]]
- [[#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]]
* 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=.
* 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:
#+BEGIN_EXAMPLE json
{"name":"foo\"bar","nick":"foo \u00e4 bar","description":"<pre>\nbaz\n</pre>","home":"/home/foobar"}
Will be reformated:
{
"name": "foo\"bar",
"nick": "foo ä bar",
"description": "<pre>
baz
</pre>",
"home": "/home/foobar"
}
#+END_EXAMPLE
** Display navigable hierarchy
~SPC m h h~ displays the hierarchy for the whole JSON document or the active
region. Use the universal prefix argument ~SPC u SPC m h h~ to create the
hierarchy for the JSON *after* the point.
* Key Bindings
| Key Binding | Description |
|-------------+---------------------------------------------|
| ~SPC m =~ | DWIM Reformat JSON |
| ~SPC m h p~ | Get the path of the value at point |
| ~SPC m h h~ | DWIM navigate JSON hierarchy |
| ~SPC m h H~ | Navigate JSON hierarchy of the whole buffer |
** JSON hierarchy
| Key Binding | Description |
|-------------+----------------------|
| ~RET~ | Expand/Collapse node |
| ~TAB~ | Select next node |
| ~S-TAB~ | Select previous node |

View File

@ -0,0 +1,34 @@
;;; funcs.el --- JSON Layer functions File for Spacemacs
;;
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
;;
;; Author: Muneeb Shaikh <muneeb@reversehack.in>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defun spacemacs/json-navigator-dwim (arg &optional start end)
"Display the JSON hierarchy of the whole buffer or the active region.
If ARG is a universal prefix argument then display the hierarchy after point."
(interactive "P\nr")
(if arg
(json-navigator-navigate-after-point)
(if (equal start end)
(save-excursion (json-navigator-navigate-region (point-min) (point-max)))
(json-navigator-navigate-region start end))))
(defun spacemacs/json-reformat-dwim (arg &optional start end)
"Reformat the whole buffer of the active region.
If ARG is non-nil (universal prefix argument) then try to decode the strings.
If ARG is a numerical prefix argument then specify the indendation level."
(interactive "P\nr")
(let ((json-reformat:indent-width js-indent-level)
(json-reformat:pretty-string? nil))
(cond
((numberp arg) (setq json-reformat:indent-width arg))
(arg (setq json-reformat:pretty-string? t)))
(if (equal start end)
(save-excursion (json-reformat-region (point-min) (point-max)))
(json-reformat-region start end))))

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

View File

@ -0,0 +1,55 @@
;;; packages.el --- JSON Layer packages File for Spacemacs
;;
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(setq json-packages
'(
add-node-modules-path
flycheck
json-mode
json-navigator
json-reformat
json-snatcher
))
(defun json/post-init-add-node-modules-path ()
(add-hook json-mode-hook #'add-node-modules-path))
(defun json/post-init-flycheck ()
(spacemacs/enable-flycheck 'json-mode))
(defun json/init-json-mode ()
(use-package json-mode
:defer t))
(defun json/init-json-navigator ()
(use-package json-navigator
:defer t
:init
(progn
(evilified-state-evilify-map tabulated-list-mode-map
:mode special-mode)
(spacemacs/set-leader-keys-for-major-mode 'json-mode
"hh" 'spacemacs/json-navigator-dwim))))
(defun json/init-json-reformat ()
(use-package json-reformat
:defer t
:init
(spacemacs/set-leader-keys-for-major-mode 'json-mode
"=" 'spacemacs/json-reformat-dwim)))
(defun json/init-json-snatcher ()
(use-package json-snatcher
:defer t
:config
(spacemacs/set-leader-keys-for-major-mode 'json-mode
"hp" 'jsons-print-path)))