lassik / emacs-format-all-the-code

Auto-format source code in many languages with one command
https://melpa.org/#/format-all
MIT License
613 stars 106 forks source link

Change indentation in .json files to 4 #204

Closed EmilyGraceSeville7cf closed 2 years ago

EmilyGraceSeville7cf commented 2 years ago

Emacs version: 28.1 ~/.emacs:

;;; .emacs --- My custom .emacs config.
;;; Commentary:
;;; Installs different packages for code linting and formatting to enable user feel Emacs like an IDE.
;;; Code:

(require 'package)
(require 'lsp-mode)

(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)

(defvar emacs-themes '(soft-stone-theme) "Themes installed as packages.")
(defvar emacs-linters '(flycheck json-mode yaml-mode markdown-mode) "Code linters installed as packages.")
(defvar emacs-language-servers '(lsp-mode lsp-pyright lsp-java) "Language servers installed as packages.")
(defvar emacs-formatters '(format-all) "Code formatters installed as packages.")
(defvar emacs-other '(company) "Other packages installed as packages.")

(defvar emacs-packages (append emacs-themes emacs-linters emacs-language-servers emacs-formatters emacs-other))
(dolist (package emacs-packages)
  (package-install package))

(setq emacs-language-servers '(bash-ls csharp-ls))
; (dolist (server emacs-language-servers)
;  (lsp-install-server nil server)) ; Reinstallation occures even when UPDATE? is nil

(defvar bookmark-save-flag 1)
(load-theme 'soft-stone t)
(setq-default tab-width 4)
(setq-default display-line-numbers 'relative)
(add-hook 'prog-mode-hook #'flycheck-mode)
(add-hook 'prog-mode-hook #'company-mode)
(add-hook 'prog-mode-hook #'format-all-mode)
(defvar emacs-hooks '(shell-mode-hook python-mode-hook java-mode-hook csharp-mode-hook c++-mode-hook))
(dolist (hook emacs-hooks)
  (add-hook hook #'lsp))
(defvar format-all-formatters '(("JSON" prettier)
                ("YAML" prettier)
                ("Shell" shfmt)
                ("Python" black)
                ("Java" clang-format)
                ("C#" clang-format)))

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(package-selected-packages
   '(lsp-java csharp-mode company lsp-pyright lsp-mode format-all yaml-mode json-mode markdown-mode flycheck soft-stone-theme)))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )

(provide '.emacs)
;;; .emacs ends here

Steps to reproduce

  1. Type the following json file:
    {
        "a": 1
    }
  2. Press C-x C-s and obtain decreased indention level

Here is how it looks in a real life config:

image

Pay attention to the last line with a comma in my JSON file: it's indented as expected when other lines have smaller indentation level.

lassik commented 2 years ago