emacs/lisp/textmodes/yaml-ts-mode.el

229 lines
7.3 KiB
EmacsLisp
Raw Normal View History

;;; yaml-ts-mode.el --- tree-sitter support for YAML -*- lexical-binding: t; -*-
;; Copyright (C) 2022-2025 Free Software Foundation, Inc.
;; Author : Randy Taylor <dev@rjt.dev>
;; Maintainer : Randy Taylor <dev@rjt.dev>
;; Created : December 2022
;; Keywords : yaml languages tree-sitter
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; 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.
;; GNU Emacs is distributed in the hope that it will be useful,
;; 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
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'treesit)
(declare-function treesit-parser-create "treesit.c")
(declare-function treesit-node-start "treesit.c")
(declare-function treesit-node-end "treesit.c")
(declare-function treesit-node-type "treesit.c")
(declare-function treesit-node-child-by-field-name "treesit.c")
Lock tree-sitter language grammars to verified versions. * admin/notes/tree-sitter/build-module/build.sh: Update org for toml/yaml. * admin/tree-sitter/treesit-admin.el (treesit-admin--builtin-language-sources): Add verified versions. * lisp/progmodes/c-ts-mode.el: Append language source to treesit-language-source-alist. (c-ts-mode, c++-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/cmake-ts-mode.el: Append language source to treesit-language-source-alist. (cmake-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/csharp-mode.el: Append language source to treesit-language-source-alist. (csharp-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/dockerfile-ts-mode.el: Append language source to treesit-language-source-alist. (dockerfile-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/elixir-ts-mode.el: Append language source to treesit-language-source-alist. (elixir-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/go-ts-mode.el: Append language source to treesit-language-source-alist. (go-ts-mode, go-mod-ts-mode, go-work-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/heex-ts-mode.el: Append language source to treesit-language-source-alist. (heex-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/java-ts-mode.el: Append language source to treesit-language-source-alist. (java-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/js.el: Append language source to treesit-language-source-alist. (js-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/json-ts-mode.el: Append language source to treesit-language-source-alist. (json-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/lua-ts-mode.el: Append language source to treesit-language-source-alist. (lua-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/php-ts-mode.el (php-ts-mode--language-source-alist): Update versions from comments in ts-modes. Append to treesit-language-source-alist. (php-ts-mode-install-parsers): Use treesit-language-source-alist directly. (php-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/ruby-ts-mode.el: Append language source to treesit-language-source-alist. (ruby-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/rust-ts-mode.el: Append language source to treesit-language-source-alist. (rust-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/sh-script.el: Append language source to treesit-language-source-alist. (bash-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/typescript-ts-mode.el: Append language source to treesit-language-source-alist. (typescript-ts-mode, tsx-ts-mode): Use treesit-ensure-installed. * lisp/textmodes/css-mode.el: Append language source to treesit-language-source-alist. (css-ts-mode): Use treesit-ensure-installed. * lisp/textmodes/html-ts-mode.el: Append language source to treesit-language-source-alist. (html-ts-mode): Use treesit-ensure-installed. * lisp/textmodes/markdown-ts-mode.el: Append language source to treesit-language-source-alist. (markdown-ts-mode): Use treesit-ensure-installed. * lisp/textmodes/mhtml-ts-mode.el (mhtml-ts-mode--language-source-alist): Append to treesit-language-source-alist. (mhtml-ts-mode-install-parsers): Use treesit-language-source-alist directly. (mhtml-ts-mode): Use treesit-ensure-installed. * lisp/textmodes/toml-ts-mode.el: Append language source to treesit-language-source-alist. (toml-ts-mode): Use treesit-ensure-installed. * lisp/textmodes/yaml-ts-mode.el: Append language source to treesit-language-source-alist. (yaml-ts-mode): Use treesit-ensure-installed. * test/infra/Dockerfile.emba: Add verified versions to treesit-language-source-alist.
2025-04-18 19:22:50 +03:00
(add-to-list
'treesit-language-source-alist
'(yaml "https://github.com/tree-sitter-grammars/tree-sitter-yaml" "v0.7.0")
t)
(defvar yaml-ts-mode--syntax-table
(let ((table (make-syntax-table)))
(modify-syntax-entry ?# "<" table)
(modify-syntax-entry ?\n ">" table)
(modify-syntax-entry ?& "." table)
(modify-syntax-entry ?* "." table)
(modify-syntax-entry ?\( "." table)
(modify-syntax-entry ?\) "." table)
(modify-syntax-entry ?\' "\"" table)
table)
"Syntax table for `yaml-ts-mode'.")
(defvar yaml-ts-mode--font-lock-settings
(treesit-font-lock-rules
:language 'yaml
:feature 'bracket
'((["[" "]" "{" "}"]) @font-lock-bracket-face)
:language 'yaml
:feature 'comment
'((comment) @font-lock-comment-face)
:language 'yaml
:feature 'constant
'([(boolean_scalar)
(null_scalar)
(reserved_directive)
(tag_directive)
(yaml_directive)] @font-lock-constant-face)
:language 'yaml
:feature 'delimiter
'((["," ":" "-" ">" "?" "|"]) @font-lock-delimiter-face)
:language 'yaml
:feature 'misc-punctuation
'((["---" "..." "&" "*"]) @font-lock-misc-punctuation-face)
:language 'yaml
:feature 'number
'([(float_scalar) (integer_scalar)] @font-lock-number-face)
:language 'yaml
:feature 'type
'([(alias_name) (anchor_name) (tag)] @font-lock-type-face)
:language 'yaml
:feature 'string
:override t
'([(block_scalar)
(double_quote_scalar)
(single_quote_scalar)
(string_scalar)] @font-lock-string-face)
:language 'yaml
:feature 'escape-sequence
:override t
'((escape_sequence) @font-lock-escape-face)
:language 'yaml
:feature 'property
:override t
'((block_mapping_pair
key: (flow_node (plain_scalar (string_scalar) @font-lock-property-use-face)))
(block_mapping_pair
key: (flow_node
[(double_quote_scalar) (single_quote_scalar)] @font-lock-property-use-face))
(flow_mapping
(_ key: (flow_node (plain_scalar (string_scalar) @font-lock-property-use-face))))
(flow_mapping
(_ key:
(flow_node
[(double_quote_scalar) (single_quote_scalar)] @font-lock-property-use-face)))
(flow_sequence
(_ key: (flow_node (plain_scalar (string_scalar) @font-lock-property-use-face))))
(flow_sequence
(_ key:
(flow_node
[(double_quote_scalar) (single_quote_scalar)] @font-lock-property-use-face))))
:language 'yaml
:feature 'error
:override t
'((ERROR) @font-lock-warning-face))
"Tree-sitter font-lock settings for `yaml-ts-mode'.")
(defun yaml-ts-mode--fill-paragraph (&optional justify)
"Fill paragraph.
Behaves like `fill-paragraph', but respects block node
boundaries. JUSTIFY is passed to `fill-paragraph'."
(interactive "*P")
(save-restriction
(widen)
(let ((node (treesit-node-at (point))))
(pcase (treesit-node-type node)
("block_scalar"
(let* ((start (treesit-node-start node))
(end (treesit-node-end node))
(start-marker (point-marker))
(fill-paragraph-function nil))
(save-excursion
(goto-char start)
(forward-line)
(move-marker start-marker (point))
(narrow-to-region (point) end))
(fill-region start-marker end justify)))
("comment"
(fill-comment-paragraph justify))))
t))
Improve treesit settings for {json,html,toml,yaml}-ts-mode (bug#73404) * lisp/progmodes/json-ts-mode.el (json-ts-mode): Add 'list' thing to 'treesit-thing-settings'. (json-ts-mode): Disable outlines. * lisp/textmodes/html-ts-mode.el (html-ts-mode--defun-name): Get a grandchild 'tag_name' from 'element' that was already defined by 'treesit-defun-type-regexp'. (html-ts-mode--outline-predicate): New function. (html-ts-mode): Add "comment" to the 'list' thing in 'treesit-thing-settings'. Use "tag_name" and "attribute" in 'sentence' to conform to sentence navigating arguments in other ts-modes. Remove unnecessary heading from 'treesit-simple-imenu-settings' and use "element" supported by 'html-ts-mode--defun-name'. Set 'treesit-outline-predicate' to 'html-ts-mode--outline-predicate'. * lisp/textmodes/toml-ts-mode.el (toml-ts-mode): Add 'treesit-thing-settings'. * lisp/textmodes/yaml-ts-mode.el (yaml-ts-mode--defun-name) (yaml-ts-mode--outline-predicate): New functions. (yaml-ts-mode): Set 'treesit-defun-type-regexp', 'treesit-defun-name-function', 'treesit-defun-tactic'. Add 'sentence' to 'treesit-thing-settings'. Set 'treesit-simple-imenu-settings' and 'treesit-outline-predicate'. Use 'kill-local-variable' for 'forward-sexp-function' and 'show-paren-data-function' instead of resetting their value. * lisp/treesit.el (treesit-outline-search): Check for the thing before the end of the line to support such case when the thing fits on the current line and ends before the end of the line such as e.g. '<h1>...</h1>' in html-ts-mode. (treesit-hs-find-next-block, treesit-hs-inside-comment-p): Use anchors for "\\`comment\\'" (bug#75609).
2025-02-09 19:54:02 +02:00
(defun yaml-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."
(when (equal (treesit-node-type node) "block_mapping_pair")
(treesit-node-text (treesit-node-child-by-field-name
node "key")
t)))
(defun yaml-ts-mode--outline-predicate (node)
"Limit outlines to top-level mappings."
(let ((regexp (rx (or "block_mapping_pair" "block_sequence_item"))))
(when (string-match-p regexp (treesit-node-type node))
(not (treesit-node-top-level node regexp)))))
Improve treesit settings for {json,html,toml,yaml}-ts-mode (bug#73404) * lisp/progmodes/json-ts-mode.el (json-ts-mode): Add 'list' thing to 'treesit-thing-settings'. (json-ts-mode): Disable outlines. * lisp/textmodes/html-ts-mode.el (html-ts-mode--defun-name): Get a grandchild 'tag_name' from 'element' that was already defined by 'treesit-defun-type-regexp'. (html-ts-mode--outline-predicate): New function. (html-ts-mode): Add "comment" to the 'list' thing in 'treesit-thing-settings'. Use "tag_name" and "attribute" in 'sentence' to conform to sentence navigating arguments in other ts-modes. Remove unnecessary heading from 'treesit-simple-imenu-settings' and use "element" supported by 'html-ts-mode--defun-name'. Set 'treesit-outline-predicate' to 'html-ts-mode--outline-predicate'. * lisp/textmodes/toml-ts-mode.el (toml-ts-mode): Add 'treesit-thing-settings'. * lisp/textmodes/yaml-ts-mode.el (yaml-ts-mode--defun-name) (yaml-ts-mode--outline-predicate): New functions. (yaml-ts-mode): Set 'treesit-defun-type-regexp', 'treesit-defun-name-function', 'treesit-defun-tactic'. Add 'sentence' to 'treesit-thing-settings'. Set 'treesit-simple-imenu-settings' and 'treesit-outline-predicate'. Use 'kill-local-variable' for 'forward-sexp-function' and 'show-paren-data-function' instead of resetting their value. * lisp/treesit.el (treesit-outline-search): Check for the thing before the end of the line to support such case when the thing fits on the current line and ends before the end of the line such as e.g. '<h1>...</h1>' in html-ts-mode. (treesit-hs-find-next-block, treesit-hs-inside-comment-p): Use anchors for "\\`comment\\'" (bug#75609).
2025-02-09 19:54:02 +02:00
;;;###autoload
(define-derived-mode yaml-ts-mode text-mode "YAML"
"Major mode for editing YAML, powered by tree-sitter."
:group 'yaml
:syntax-table yaml-ts-mode--syntax-table
Lock tree-sitter language grammars to verified versions. * admin/notes/tree-sitter/build-module/build.sh: Update org for toml/yaml. * admin/tree-sitter/treesit-admin.el (treesit-admin--builtin-language-sources): Add verified versions. * lisp/progmodes/c-ts-mode.el: Append language source to treesit-language-source-alist. (c-ts-mode, c++-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/cmake-ts-mode.el: Append language source to treesit-language-source-alist. (cmake-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/csharp-mode.el: Append language source to treesit-language-source-alist. (csharp-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/dockerfile-ts-mode.el: Append language source to treesit-language-source-alist. (dockerfile-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/elixir-ts-mode.el: Append language source to treesit-language-source-alist. (elixir-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/go-ts-mode.el: Append language source to treesit-language-source-alist. (go-ts-mode, go-mod-ts-mode, go-work-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/heex-ts-mode.el: Append language source to treesit-language-source-alist. (heex-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/java-ts-mode.el: Append language source to treesit-language-source-alist. (java-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/js.el: Append language source to treesit-language-source-alist. (js-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/json-ts-mode.el: Append language source to treesit-language-source-alist. (json-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/lua-ts-mode.el: Append language source to treesit-language-source-alist. (lua-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/php-ts-mode.el (php-ts-mode--language-source-alist): Update versions from comments in ts-modes. Append to treesit-language-source-alist. (php-ts-mode-install-parsers): Use treesit-language-source-alist directly. (php-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/ruby-ts-mode.el: Append language source to treesit-language-source-alist. (ruby-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/rust-ts-mode.el: Append language source to treesit-language-source-alist. (rust-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/sh-script.el: Append language source to treesit-language-source-alist. (bash-ts-mode): Use treesit-ensure-installed. * lisp/progmodes/typescript-ts-mode.el: Append language source to treesit-language-source-alist. (typescript-ts-mode, tsx-ts-mode): Use treesit-ensure-installed. * lisp/textmodes/css-mode.el: Append language source to treesit-language-source-alist. (css-ts-mode): Use treesit-ensure-installed. * lisp/textmodes/html-ts-mode.el: Append language source to treesit-language-source-alist. (html-ts-mode): Use treesit-ensure-installed. * lisp/textmodes/markdown-ts-mode.el: Append language source to treesit-language-source-alist. (markdown-ts-mode): Use treesit-ensure-installed. * lisp/textmodes/mhtml-ts-mode.el (mhtml-ts-mode--language-source-alist): Append to treesit-language-source-alist. (mhtml-ts-mode-install-parsers): Use treesit-language-source-alist directly. (mhtml-ts-mode): Use treesit-ensure-installed. * lisp/textmodes/toml-ts-mode.el: Append language source to treesit-language-source-alist. (toml-ts-mode): Use treesit-ensure-installed. * lisp/textmodes/yaml-ts-mode.el: Append language source to treesit-language-source-alist. (yaml-ts-mode): Use treesit-ensure-installed. * test/infra/Dockerfile.emba: Add verified versions to treesit-language-source-alist.
2025-04-18 19:22:50 +03:00
(when (treesit-ensure-installed 'yaml)
(setq treesit-primary-parser (treesit-parser-create 'yaml))
;; Comments.
(setq-local comment-start "# ")
(setq-local comment-end "")
(setq-local comment-start-skip "#+\\s-*")
;; Indentation.
(setq-local indent-tabs-mode nil)
;; Font-lock.
(setq-local treesit-font-lock-settings yaml-ts-mode--font-lock-settings)
(setq-local treesit-font-lock-feature-list
'((comment)
(string type)
(constant escape-sequence number property)
(bracket delimiter error misc-punctuation)))
(setq-local fill-paragraph-function #'yaml-ts-mode--fill-paragraph)
;; Navigation.
Improve treesit settings for {json,html,toml,yaml}-ts-mode (bug#73404) * lisp/progmodes/json-ts-mode.el (json-ts-mode): Add 'list' thing to 'treesit-thing-settings'. (json-ts-mode): Disable outlines. * lisp/textmodes/html-ts-mode.el (html-ts-mode--defun-name): Get a grandchild 'tag_name' from 'element' that was already defined by 'treesit-defun-type-regexp'. (html-ts-mode--outline-predicate): New function. (html-ts-mode): Add "comment" to the 'list' thing in 'treesit-thing-settings'. Use "tag_name" and "attribute" in 'sentence' to conform to sentence navigating arguments in other ts-modes. Remove unnecessary heading from 'treesit-simple-imenu-settings' and use "element" supported by 'html-ts-mode--defun-name'. Set 'treesit-outline-predicate' to 'html-ts-mode--outline-predicate'. * lisp/textmodes/toml-ts-mode.el (toml-ts-mode): Add 'treesit-thing-settings'. * lisp/textmodes/yaml-ts-mode.el (yaml-ts-mode--defun-name) (yaml-ts-mode--outline-predicate): New functions. (yaml-ts-mode): Set 'treesit-defun-type-regexp', 'treesit-defun-name-function', 'treesit-defun-tactic'. Add 'sentence' to 'treesit-thing-settings'. Set 'treesit-simple-imenu-settings' and 'treesit-outline-predicate'. Use 'kill-local-variable' for 'forward-sexp-function' and 'show-paren-data-function' instead of resetting their value. * lisp/treesit.el (treesit-outline-search): Check for the thing before the end of the line to support such case when the thing fits on the current line and ends before the end of the line such as e.g. '<h1>...</h1>' in html-ts-mode. (treesit-hs-find-next-block, treesit-hs-inside-comment-p): Use anchors for "\\`comment\\'" (bug#75609).
2025-02-09 19:54:02 +02:00
(setq-local treesit-defun-type-regexp "block_mapping_pair")
(setq-local treesit-defun-name-function #'yaml-ts-mode--defun-name)
(setq-local treesit-defun-tactic 'top-level)
(setq-local treesit-thing-settings
`((yaml
Improve treesit settings for {json,html,toml,yaml}-ts-mode (bug#73404) * lisp/progmodes/json-ts-mode.el (json-ts-mode): Add 'list' thing to 'treesit-thing-settings'. (json-ts-mode): Disable outlines. * lisp/textmodes/html-ts-mode.el (html-ts-mode--defun-name): Get a grandchild 'tag_name' from 'element' that was already defined by 'treesit-defun-type-regexp'. (html-ts-mode--outline-predicate): New function. (html-ts-mode): Add "comment" to the 'list' thing in 'treesit-thing-settings'. Use "tag_name" and "attribute" in 'sentence' to conform to sentence navigating arguments in other ts-modes. Remove unnecessary heading from 'treesit-simple-imenu-settings' and use "element" supported by 'html-ts-mode--defun-name'. Set 'treesit-outline-predicate' to 'html-ts-mode--outline-predicate'. * lisp/textmodes/toml-ts-mode.el (toml-ts-mode): Add 'treesit-thing-settings'. * lisp/textmodes/yaml-ts-mode.el (yaml-ts-mode--defun-name) (yaml-ts-mode--outline-predicate): New functions. (yaml-ts-mode): Set 'treesit-defun-type-regexp', 'treesit-defun-name-function', 'treesit-defun-tactic'. Add 'sentence' to 'treesit-thing-settings'. Set 'treesit-simple-imenu-settings' and 'treesit-outline-predicate'. Use 'kill-local-variable' for 'forward-sexp-function' and 'show-paren-data-function' instead of resetting their value. * lisp/treesit.el (treesit-outline-search): Check for the thing before the end of the line to support such case when the thing fits on the current line and ends before the end of the line such as e.g. '<h1>...</h1>' in html-ts-mode. (treesit-hs-find-next-block, treesit-hs-inside-comment-p): Use anchors for "\\`comment\\'" (bug#75609).
2025-02-09 19:54:02 +02:00
(list ,(rx (or "block_mapping_pair" "flow_sequence")))
(sentence ,"block_mapping_pair"))))
;; Imenu.
(setq-local treesit-simple-imenu-settings
'((nil "\\`block_mapping_pair\\'" nil nil)))
;; Outline minor mode.
(setq-local treesit-outline-predicate #'yaml-ts-mode--outline-predicate)
(treesit-major-mode-setup)
;; Use the `list' thing defined above to navigate only lists
;; with `C-M-n', `C-M-p', `C-M-u', `C-M-d', but not sexps
;; with `C-M-f', `C-M-b' neither adapt to 'show-paren-mode'
;; that is problematic in languages without explicit
;; opening/closing nodes.
Improve treesit settings for {json,html,toml,yaml}-ts-mode (bug#73404) * lisp/progmodes/json-ts-mode.el (json-ts-mode): Add 'list' thing to 'treesit-thing-settings'. (json-ts-mode): Disable outlines. * lisp/textmodes/html-ts-mode.el (html-ts-mode--defun-name): Get a grandchild 'tag_name' from 'element' that was already defined by 'treesit-defun-type-regexp'. (html-ts-mode--outline-predicate): New function. (html-ts-mode): Add "comment" to the 'list' thing in 'treesit-thing-settings'. Use "tag_name" and "attribute" in 'sentence' to conform to sentence navigating arguments in other ts-modes. Remove unnecessary heading from 'treesit-simple-imenu-settings' and use "element" supported by 'html-ts-mode--defun-name'. Set 'treesit-outline-predicate' to 'html-ts-mode--outline-predicate'. * lisp/textmodes/toml-ts-mode.el (toml-ts-mode): Add 'treesit-thing-settings'. * lisp/textmodes/yaml-ts-mode.el (yaml-ts-mode--defun-name) (yaml-ts-mode--outline-predicate): New functions. (yaml-ts-mode): Set 'treesit-defun-type-regexp', 'treesit-defun-name-function', 'treesit-defun-tactic'. Add 'sentence' to 'treesit-thing-settings'. Set 'treesit-simple-imenu-settings' and 'treesit-outline-predicate'. Use 'kill-local-variable' for 'forward-sexp-function' and 'show-paren-data-function' instead of resetting their value. * lisp/treesit.el (treesit-outline-search): Check for the thing before the end of the line to support such case when the thing fits on the current line and ends before the end of the line such as e.g. '<h1>...</h1>' in html-ts-mode. (treesit-hs-find-next-block, treesit-hs-inside-comment-p): Use anchors for "\\`comment\\'" (bug#75609).
2025-02-09 19:54:02 +02:00
(kill-local-variable 'forward-sexp-function)
(kill-local-variable 'show-paren-data-function)))
(derived-mode-add-parents 'yaml-ts-mode '(yaml-mode))
(if (treesit-ready-p 'yaml)
(add-to-list 'auto-mode-alist '("\\.ya?ml\\'" . yaml-ts-mode)))
(provide 'yaml-ts-mode)
;;; yaml-ts-mode.el ends here