Function: json-ts-mode

json-ts-mode is an autoloaded, interactive and byte-compiled function defined in json-ts-mode.el.gz.

Signature

(json-ts-mode)

Documentation

Major mode for editing JSON, powered by tree-sitter.

In addition to any hooks its parent mode prog-mode might have run, this mode runs the hook json-ts-mode-hook, as the final or penultimate step during initialization.

Probably introduced at or before Emacs version 29.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/json-ts-mode.el.gz
;;;###autoload
(define-derived-mode json-ts-mode prog-mode "JSON"
  "Major mode for editing JSON, powered by tree-sitter."
  :group 'json
  :syntax-table json-ts-mode--syntax-table

  (unless (treesit-ensure-installed 'json)
    (error "Tree-sitter for JSON isn't available"))

  (setq treesit-primary-parser (treesit-parser-create 'json))

  ;; Comments.
  (setq-local comment-start "// ")
  (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
  (setq-local comment-end "")

  ;; Electric
  (setq-local electric-indent-chars
              (append "{}():;," electric-indent-chars))

  ;; Indent.
  (setq-local treesit-simple-indent-rules json-ts--indent-rules)

  ;; Navigation.
  (setq-local treesit-defun-type-regexp
              (rx (or "pair" "object")))
  (setq-local treesit-defun-name-function #'json-ts-mode--defun-name)

  (setq-local treesit-thing-settings
              `((json
                 (list ,(rx (or "object" "array")))
                 (sentence "pair"))))

  ;; Font-lock.
  (setq-local treesit-font-lock-settings json-ts-mode--font-lock-settings)
  (setq-local treesit-font-lock-feature-list
              '((comment constant number pair string)
                (escape-sequence)
                (bracket delimiter error)))

  ;; Imenu.
  (setq-local treesit-simple-imenu-settings
              '((nil "\\`pair\\'" nil nil)))

  (treesit-major-mode-setup)

  ;; Disable outlines since they are created for 'pair' from
  ;; 'treesit-simple-imenu-settings' almost on every line:
  (kill-local-variable 'outline-search-function)
  (kill-local-variable 'outline-level))