2022-11-12 14:33:14 -08:00
|
|
|
;;; json-ts-mode.el --- tree-sitter support for JSON -*- lexical-binding: t; -*-
|
2022-11-10 17:15:49 +01:00
|
|
|
|
2023-01-01 05:31:12 -05:00
|
|
|
;; Copyright (C) 2022-2023 Free Software Foundation, Inc.
|
2022-11-10 17:15:49 +01:00
|
|
|
|
|
|
|
;; Author : Theodor Thornhill <theo@thornhill.no>
|
|
|
|
;; Maintainer : Theodor Thornhill <theo@thornhill.no>
|
|
|
|
;; Created : November 2022
|
|
|
|
;; Keywords : json languages tree-sitter
|
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
2022-12-08 23:56:24 +01:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2022-11-10 17:15:49 +01:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
2022-12-08 23:56:24 +01:00
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2022-11-10 17:15:49 +01:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2022-12-08 23:56:24 +01:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2022-11-10 17:15:49 +01:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'treesit)
|
|
|
|
(require 'rx)
|
|
|
|
|
2022-11-21 19:08:25 +02:00
|
|
|
(declare-function treesit-parser-create "treesit.c")
|
|
|
|
(declare-function treesit-induce-sparse-tree "treesit.c")
|
|
|
|
(declare-function treesit-node-start "treesit.c")
|
2022-12-28 16:41:58 +02:00
|
|
|
(declare-function treesit-node-type "treesit.c")
|
2022-11-21 19:08:25 +02:00
|
|
|
(declare-function treesit-node-child-by-field-name "treesit.c")
|
|
|
|
|
|
|
|
|
2022-11-10 17:15:49 +01:00
|
|
|
(defcustom json-ts-mode-indent-offset 2
|
|
|
|
"Number of spaces for each indentation step in `json-ts-mode'."
|
2022-11-14 08:03:11 +01:00
|
|
|
:version "29.1"
|
2022-11-10 17:15:49 +01:00
|
|
|
:type 'integer
|
|
|
|
:safe 'integerp
|
|
|
|
:group 'json)
|
|
|
|
|
|
|
|
(defvar json-ts-mode--syntax-table
|
|
|
|
(let ((table (make-syntax-table)))
|
|
|
|
(modify-syntax-entry ?_ "_" table)
|
|
|
|
(modify-syntax-entry ?\\ "\\" table)
|
|
|
|
(modify-syntax-entry ?+ "." table)
|
|
|
|
(modify-syntax-entry ?- "." table)
|
|
|
|
(modify-syntax-entry ?= "." table)
|
|
|
|
(modify-syntax-entry ?% "." table)
|
|
|
|
(modify-syntax-entry ?< "." table)
|
|
|
|
(modify-syntax-entry ?> "." table)
|
|
|
|
(modify-syntax-entry ?& "." table)
|
|
|
|
(modify-syntax-entry ?| "." table)
|
2022-12-05 13:37:58 +01:00
|
|
|
(modify-syntax-entry ?\' "\"" table)
|
2022-11-10 17:15:49 +01:00
|
|
|
(modify-syntax-entry ?\240 "." table)
|
2022-12-05 13:37:58 +01:00
|
|
|
(modify-syntax-entry ?/ ". 124b" table)
|
|
|
|
(modify-syntax-entry ?* ". 23" table)
|
|
|
|
(modify-syntax-entry ?\n "> b" table)
|
|
|
|
(modify-syntax-entry ?\^m "> b" table)
|
2022-11-10 17:15:49 +01:00
|
|
|
table)
|
|
|
|
"Syntax table for `json-ts-mode'.")
|
|
|
|
|
|
|
|
|
|
|
|
(defvar json-ts--indent-rules
|
|
|
|
`((json
|
|
|
|
((node-is "}") parent-bol 0)
|
|
|
|
((node-is ")") parent-bol 0)
|
|
|
|
((node-is "]") parent-bol 0)
|
2022-12-16 16:01:41 +01:00
|
|
|
((parent-is "object") parent-bol json-ts-mode-indent-offset)
|
|
|
|
((parent-is "array") parent-bol json-ts-mode-indent-offset))))
|
2022-11-10 17:15:49 +01:00
|
|
|
|
|
|
|
(defvar json-ts-mode--font-lock-settings
|
|
|
|
(treesit-font-lock-rules
|
2022-12-09 22:36:03 +01:00
|
|
|
:language 'json
|
|
|
|
:feature 'comment
|
|
|
|
'((comment) @font-lock-comment-face)
|
2022-11-10 17:15:49 +01:00
|
|
|
:language 'json
|
Utilize new font-lock faces for more tree-sitter modes (Bug#59397)
* lisp/progmodes/java-ts-mode.el (java-ts-mode--font-lock-settings):
Use font-lock-number-face.
(java-ts-mode): Alphabetize features.
* lisp/progmodes/js.el (js--treesit-operators): Define operators.
(js--treesit-font-lock-settings): Use bracket, delimiter,
escape-sequence, property, number, and operator font-lock faces.
(js-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/json-ts-mode.el (json-ts-mode--font-lock-settings):
Use bracket, delimiter, escape-sequence, and number faces. Remove
unused features.
(json-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/sh-script.el (sh-mode--treesit-settings): Use
bracket, delimiter, number, misc-punctuation, and operator font-lock
faces.
(sh-mode--treesit-operators): Remove ; and ;; from list.
(bash-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/ts-mode.el (ts-mode--operators): Define operators.
(ts-mode--font-lock-settings): Use escape-sequence, number, and
operator font-lock faces.
(ts-mode): Add them to the feature list and alphabetize.
2022-11-19 22:30:13 -05:00
|
|
|
:feature 'bracket
|
|
|
|
'((["[" "]" "{" "}"]) @font-lock-bracket-face)
|
2022-11-14 08:03:11 +01:00
|
|
|
:language 'json
|
Utilize new font-lock faces for more tree-sitter modes (Bug#59397)
* lisp/progmodes/java-ts-mode.el (java-ts-mode--font-lock-settings):
Use font-lock-number-face.
(java-ts-mode): Alphabetize features.
* lisp/progmodes/js.el (js--treesit-operators): Define operators.
(js--treesit-font-lock-settings): Use bracket, delimiter,
escape-sequence, property, number, and operator font-lock faces.
(js-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/json-ts-mode.el (json-ts-mode--font-lock-settings):
Use bracket, delimiter, escape-sequence, and number faces. Remove
unused features.
(json-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/sh-script.el (sh-mode--treesit-settings): Use
bracket, delimiter, number, misc-punctuation, and operator font-lock
faces.
(sh-mode--treesit-operators): Remove ; and ;; from list.
(bash-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/ts-mode.el (ts-mode--operators): Define operators.
(ts-mode--font-lock-settings): Use escape-sequence, number, and
operator font-lock faces.
(ts-mode): Add them to the feature list and alphabetize.
2022-11-19 22:30:13 -05:00
|
|
|
:feature 'constant
|
|
|
|
'([(null) (true) (false)] @font-lock-constant-face)
|
|
|
|
:language 'json
|
|
|
|
:feature 'delimiter
|
|
|
|
'((["," ":"]) @font-lock-delimiter-face)
|
2022-11-14 08:03:11 +01:00
|
|
|
:language 'json
|
|
|
|
:feature 'number
|
Utilize new font-lock faces for more tree-sitter modes (Bug#59397)
* lisp/progmodes/java-ts-mode.el (java-ts-mode--font-lock-settings):
Use font-lock-number-face.
(java-ts-mode): Alphabetize features.
* lisp/progmodes/js.el (js--treesit-operators): Define operators.
(js--treesit-font-lock-settings): Use bracket, delimiter,
escape-sequence, property, number, and operator font-lock faces.
(js-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/json-ts-mode.el (json-ts-mode--font-lock-settings):
Use bracket, delimiter, escape-sequence, and number faces. Remove
unused features.
(json-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/sh-script.el (sh-mode--treesit-settings): Use
bracket, delimiter, number, misc-punctuation, and operator font-lock
faces.
(sh-mode--treesit-operators): Remove ; and ;; from list.
(bash-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/ts-mode.el (ts-mode--operators): Define operators.
(ts-mode--font-lock-settings): Use escape-sequence, number, and
operator font-lock faces.
(ts-mode): Add them to the feature list and alphabetize.
2022-11-19 22:30:13 -05:00
|
|
|
'((number) @font-lock-number-face)
|
2022-11-14 08:03:11 +01:00
|
|
|
:language 'json
|
Utilize new font-lock faces for more tree-sitter modes (Bug#59397)
* lisp/progmodes/java-ts-mode.el (java-ts-mode--font-lock-settings):
Use font-lock-number-face.
(java-ts-mode): Alphabetize features.
* lisp/progmodes/js.el (js--treesit-operators): Define operators.
(js--treesit-font-lock-settings): Use bracket, delimiter,
escape-sequence, property, number, and operator font-lock faces.
(js-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/json-ts-mode.el (json-ts-mode--font-lock-settings):
Use bracket, delimiter, escape-sequence, and number faces. Remove
unused features.
(json-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/sh-script.el (sh-mode--treesit-settings): Use
bracket, delimiter, number, misc-punctuation, and operator font-lock
faces.
(sh-mode--treesit-operators): Remove ; and ;; from list.
(bash-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/ts-mode.el (ts-mode--operators): Define operators.
(ts-mode--font-lock-settings): Use escape-sequence, number, and
operator font-lock faces.
(ts-mode): Add them to the feature list and alphabetize.
2022-11-19 22:30:13 -05:00
|
|
|
:feature 'string
|
|
|
|
'((string) @font-lock-string-face)
|
|
|
|
:language 'json
|
|
|
|
:feature 'escape-sequence
|
2022-11-14 08:03:11 +01:00
|
|
|
:override t
|
Utilize new font-lock faces for more tree-sitter modes (Bug#59397)
* lisp/progmodes/java-ts-mode.el (java-ts-mode--font-lock-settings):
Use font-lock-number-face.
(java-ts-mode): Alphabetize features.
* lisp/progmodes/js.el (js--treesit-operators): Define operators.
(js--treesit-font-lock-settings): Use bracket, delimiter,
escape-sequence, property, number, and operator font-lock faces.
(js-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/json-ts-mode.el (json-ts-mode--font-lock-settings):
Use bracket, delimiter, escape-sequence, and number faces. Remove
unused features.
(json-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/sh-script.el (sh-mode--treesit-settings): Use
bracket, delimiter, number, misc-punctuation, and operator font-lock
faces.
(sh-mode--treesit-operators): Remove ; and ;; from list.
(bash-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/ts-mode.el (ts-mode--operators): Define operators.
(ts-mode--font-lock-settings): Use escape-sequence, number, and
operator font-lock faces.
(ts-mode): Add them to the feature list and alphabetize.
2022-11-19 22:30:13 -05:00
|
|
|
'((escape_sequence) @font-lock-escape-face)
|
2022-11-14 08:03:11 +01:00
|
|
|
:language 'json
|
2022-12-05 15:59:41 +01:00
|
|
|
:feature 'pair
|
|
|
|
:override t ; Needed for overriding string face on keys.
|
2023-02-28 04:07:55 +02:00
|
|
|
'((pair key: (_) @font-lock-property-use-face))
|
2022-12-05 15:59:41 +01:00
|
|
|
:language 'json
|
Utilize new font-lock faces for more tree-sitter modes (Bug#59397)
* lisp/progmodes/java-ts-mode.el (java-ts-mode--font-lock-settings):
Use font-lock-number-face.
(java-ts-mode): Alphabetize features.
* lisp/progmodes/js.el (js--treesit-operators): Define operators.
(js--treesit-font-lock-settings): Use bracket, delimiter,
escape-sequence, property, number, and operator font-lock faces.
(js-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/json-ts-mode.el (json-ts-mode--font-lock-settings):
Use bracket, delimiter, escape-sequence, and number faces. Remove
unused features.
(json-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/sh-script.el (sh-mode--treesit-settings): Use
bracket, delimiter, number, misc-punctuation, and operator font-lock
faces.
(sh-mode--treesit-operators): Remove ; and ;; from list.
(bash-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/ts-mode.el (ts-mode--operators): Define operators.
(ts-mode--font-lock-settings): Use escape-sequence, number, and
operator font-lock faces.
(ts-mode): Add them to the feature list and alphabetize.
2022-11-19 22:30:13 -05:00
|
|
|
:feature 'error
|
2022-11-14 08:03:11 +01:00
|
|
|
:override t
|
Utilize new font-lock faces for more tree-sitter modes (Bug#59397)
* lisp/progmodes/java-ts-mode.el (java-ts-mode--font-lock-settings):
Use font-lock-number-face.
(java-ts-mode): Alphabetize features.
* lisp/progmodes/js.el (js--treesit-operators): Define operators.
(js--treesit-font-lock-settings): Use bracket, delimiter,
escape-sequence, property, number, and operator font-lock faces.
(js-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/json-ts-mode.el (json-ts-mode--font-lock-settings):
Use bracket, delimiter, escape-sequence, and number faces. Remove
unused features.
(json-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/sh-script.el (sh-mode--treesit-settings): Use
bracket, delimiter, number, misc-punctuation, and operator font-lock
faces.
(sh-mode--treesit-operators): Remove ; and ;; from list.
(bash-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/ts-mode.el (ts-mode--operators): Define operators.
(ts-mode--font-lock-settings): Use escape-sequence, number, and
operator font-lock faces.
(ts-mode): Add them to the feature list and alphabetize.
2022-11-19 22:30:13 -05:00
|
|
|
'((ERROR) @font-lock-warning-face))
|
2022-11-10 17:15:49 +01:00
|
|
|
"Font-lock settings for JSON.")
|
|
|
|
|
2022-12-24 18:24:01 -08:00
|
|
|
(defun json-ts-mode--defun-name (node)
|
|
|
|
"Return the defun name of NODE.
|
|
|
|
Return nil if there is no name or if NODE is not a defun node."
|
|
|
|
(pcase (treesit-node-type node)
|
|
|
|
((or "pair" "object")
|
2022-12-27 20:57:12 -08:00
|
|
|
(string-trim (treesit-node-text
|
|
|
|
(treesit-node-child-by-field-name
|
|
|
|
node "key")
|
|
|
|
t)
|
|
|
|
"\"" "\""))))
|
2022-11-10 17:15:49 +01:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(define-derived-mode json-ts-mode prog-mode "JSON"
|
2022-11-12 14:33:14 -08:00
|
|
|
"Major mode for editing JSON, powered by tree-sitter."
|
2022-11-10 17:15:49 +01:00
|
|
|
:group 'json
|
|
|
|
:syntax-table json-ts-mode--syntax-table
|
|
|
|
|
2022-11-19 18:58:12 -08:00
|
|
|
(unless (treesit-ready-p 'json)
|
2022-11-12 14:33:14 -08:00
|
|
|
(error "Tree-sitter for JSON isn't available"))
|
2022-11-10 17:15:49 +01:00
|
|
|
|
|
|
|
(treesit-parser-create 'json)
|
|
|
|
|
|
|
|
;; Comments.
|
|
|
|
(setq-local comment-start "// ")
|
|
|
|
(setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
|
|
|
|
(setq-local comment-end "")
|
|
|
|
|
2022-11-14 08:03:11 +01:00
|
|
|
;; Electric
|
|
|
|
(setq-local electric-indent-chars
|
|
|
|
(append "{}():;," electric-indent-chars))
|
|
|
|
|
2022-11-10 17:15:49 +01:00
|
|
|
;; Indent.
|
|
|
|
(setq-local treesit-simple-indent-rules json-ts--indent-rules)
|
|
|
|
|
|
|
|
;; Navigation.
|
|
|
|
(setq-local treesit-defun-type-regexp
|
|
|
|
(rx (or "pair" "object")))
|
2022-12-24 18:24:01 -08:00
|
|
|
(setq-local treesit-defun-name-function #'json-ts-mode--defun-name)
|
2022-11-10 17:15:49 +01:00
|
|
|
|
2023-09-01 17:14:44 -07:00
|
|
|
(setq-local treesit-thing-settings
|
|
|
|
`((json
|
|
|
|
(sentence "pair"))))
|
2023-01-16 14:48:56 +01:00
|
|
|
|
2022-11-10 17:15:49 +01:00
|
|
|
;; Font-lock.
|
|
|
|
(setq-local treesit-font-lock-settings json-ts-mode--font-lock-settings)
|
|
|
|
(setq-local treesit-font-lock-feature-list
|
2022-12-09 22:36:03 +01:00
|
|
|
'((comment constant number pair string)
|
Utilize new font-lock faces for more tree-sitter modes (Bug#59397)
* lisp/progmodes/java-ts-mode.el (java-ts-mode--font-lock-settings):
Use font-lock-number-face.
(java-ts-mode): Alphabetize features.
* lisp/progmodes/js.el (js--treesit-operators): Define operators.
(js--treesit-font-lock-settings): Use bracket, delimiter,
escape-sequence, property, number, and operator font-lock faces.
(js-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/json-ts-mode.el (json-ts-mode--font-lock-settings):
Use bracket, delimiter, escape-sequence, and number faces. Remove
unused features.
(json-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/sh-script.el (sh-mode--treesit-settings): Use
bracket, delimiter, number, misc-punctuation, and operator font-lock
faces.
(sh-mode--treesit-operators): Remove ; and ;; from list.
(bash-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/ts-mode.el (ts-mode--operators): Define operators.
(ts-mode--font-lock-settings): Use escape-sequence, number, and
operator font-lock faces.
(ts-mode): Add them to the feature list and alphabetize.
2022-11-19 22:30:13 -05:00
|
|
|
(escape-sequence)
|
|
|
|
(bracket delimiter error)))
|
2022-11-10 17:15:49 +01:00
|
|
|
|
|
|
|
;; Imenu.
|
2022-12-27 20:57:12 -08:00
|
|
|
(setq-local treesit-simple-imenu-settings
|
|
|
|
'((nil "\\`pair\\'" nil nil)))
|
2022-11-10 17:15:49 +01:00
|
|
|
|
|
|
|
(treesit-major-mode-setup))
|
|
|
|
|
2023-01-20 10:28:26 +02:00
|
|
|
(if (treesit-ready-p 'json)
|
|
|
|
(add-to-list 'auto-mode-alist
|
|
|
|
'("\\.json\\'" . json-ts-mode)))
|
|
|
|
|
2022-11-10 17:15:49 +01:00
|
|
|
(provide 'json-ts-mode)
|
|
|
|
|
|
|
|
;;; json-ts-mode.el ends here
|