From 895308aecba6d1082f1fc29cb2c3b7f6f83feae1 Mon Sep 17 00:00:00 2001 From: Tommi Komulainen Date: Sat, 22 Sep 2018 22:08:44 +0200 Subject: [PATCH] json: tell prettier content is to be parsed as json In json-mode tell prettier the content is always to be parsed as json. Fixes formatting in orgmode `#+BEGIN_SRC json` blocks as prettier otherwise expects content as javascript. Fixes: #11343 --- layers/+lang/json/funcs.el | 5 +++++ layers/+lang/json/packages.el | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/layers/+lang/json/funcs.el b/layers/+lang/json/funcs.el index f45d3ce97..e5a3f67ad 100644 --- a/layers/+lang/json/funcs.el +++ b/layers/+lang/json/funcs.el @@ -32,3 +32,8 @@ If ARG is a numerical prefix argument then specify the indentation level." (if (equal start end) (save-excursion (json-reformat-region (point-min) (point-max))) (json-reformat-region start end)))) + +(defun spacemacs/json-setup-prettier () + "Tell prettier the content is to be parsed as JSON regardless of any file +extensions." + (setq-local prettier-js-args '("--parser=json"))) diff --git a/layers/+lang/json/packages.el b/layers/+lang/json/packages.el index b54ac25c7..fcd6227a5 100644 --- a/layers/+lang/json/packages.el +++ b/layers/+lang/json/packages.el @@ -57,8 +57,9 @@ "hp" 'jsons-print-path))) (defun json/pre-init-prettier-js () - (if (eq json-fmt-tool 'prettier) - (add-to-list 'spacemacs--prettier-modes 'json-mode))) + (when (eq json-fmt-tool 'prettier) + (add-to-list 'spacemacs--prettier-modes 'json-mode) + (add-hook 'json-mode-hook #'spacemacs/json-setup-prettier))) (defun json/pre-init-web-beautify () (if (eq json-fmt-tool 'web-beautify)