]> git.eshelyaron.com Git - emacs.git/commitdiff
Add yaml-ts-mode (Bug#60105)
authorRandy Taylor <dev@rjt.dev>
Tue, 13 Dec 2022 14:41:01 +0000 (09:41 -0500)
committerYuan Fu <casouri@gmail.com>
Fri, 16 Dec 2022 22:55:02 +0000 (14:55 -0800)
* admin/notes/tree-sitter/build-module/batch.sh:
* admin/notes/tree-sitter/build-module/build.sh: Add yaml support.
* etc/NEWS: Mention it.
* lisp/textmodes/yaml-ts-mode.el: New major mode with
tree-sitter support.
* lisp/progmodes/eglot.el (eglot-server-programs): Add it.

admin/notes/tree-sitter/build-module/batch.sh
admin/notes/tree-sitter/build-module/build.sh
etc/NEWS
lisp/progmodes/eglot.el
lisp/textmodes/yaml-ts-mode.el [new file with mode: 0644]

index c50b9df37ed25f489f38b065c957365dcc2a6b18..58272c7454948482e213d66877446dc6cfbaa5c6 100755 (executable)
@@ -18,6 +18,7 @@ languages=(
     'toml'
     'tsx'
     'typescript'
+    'yaml'
 )
 
 for language in "${languages[@]}"
index b6c83ea9b999c674b2003581b864ea1283e6c845..f0962940287314f8ce40e9edd1b3f150fdcefcf0 100755 (executable)
@@ -40,6 +40,9 @@ case "${lang}" in
         sourcedir="tree-sitter-typescript/tsx/src"
         grammardir="tree-sitter-typescript/tsx"
         ;;
+    "yaml")
+        org="ikatyang"
+        ;;
 esac
 
 git clone "https://github.com/${org}/${repo}.git" \
index 157fe98c9839de39b559c58ff75ad275ac5070a6..6d28bef409dd2c151a4159af7402e7b3fff4aea3 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -3094,6 +3094,11 @@ the Go language.  It is auto-enabled for files with the ".go" extension.
 A major mode based on the tree-sitter library for editing "go.mod"
 files.  It is auto-enabled for files which are named "go.mod".
 
+*** New major mode 'yaml-ts-mode'.
+A major mode based on the tree-sitter library for editing files
+written in YAML.  It is auto-enabled for files with the ".yaml" or
+".yml" extensions.
+
 \f
 * Incompatible Lisp Changes in Emacs 29.1
 
index 20534cfa1ce97074cab2986490a98951342939af..5b9277d73f647950e4c0c9df2c7247c73a5d30a6 100644 (file)
@@ -225,7 +225,7 @@ chosen (interactively or automatically)."
                                 ((tex-mode context-mode texinfo-mode bibtex-mode)
                                  . ,(eglot-alternatives '("digestif" "texlab")))
                                 (erlang-mode . ("erlang_ls" "--transport" "stdio"))
-                                (yaml-mode . ("yaml-language-server" "--stdio"))
+                                ((yaml-ts-mode yaml-mode) . ("yaml-language-server" "--stdio"))
                                 (nix-mode . ,(eglot-alternatives '("nil" "rnix-lsp")))
                                 (gdscript-mode . ("localhost" 6008))
                                 ((fortran-mode f90-mode) . ("fortls"))
diff --git a/lisp/textmodes/yaml-ts-mode.el b/lisp/textmodes/yaml-ts-mode.el
new file mode 100644 (file)
index 0000000..6ef6dab
--- /dev/null
@@ -0,0 +1,151 @@
+;;; yaml-ts-mode.el --- tree-sitter support for YAML  -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2022 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")
+
+(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-face)))
+     (block_mapping_pair
+      key: (flow_node
+            [(double_quote_scalar) (single_quote_scalar)] @font-lock-property-face))
+     (flow_mapping
+      (_ key: (flow_node (plain_scalar (string_scalar) @font-lock-property-face))))
+     (flow_mapping
+      (_ key:
+         (flow_node
+          [(double_quote_scalar) (single_quote_scalar)] @font-lock-property-face)))
+     (flow_sequence
+      (_ key: (flow_node (plain_scalar (string_scalar) @font-lock-property-face))))
+     (flow_sequence
+      (_ key:
+         (flow_node
+          [(double_quote_scalar) (single_quote_scalar)] @font-lock-property-face))))
+
+   :language 'yaml
+   :feature 'error
+   :override t
+   '((ERROR) @font-lock-warning-face))
+  "Tree-sitter font-lock settings for `yaml-ts-mode'.")
+
+;;;###autoload
+(add-to-list 'auto-mode-alist '("\\.ya?ml\\'" . yaml-ts-mode))
+
+;;;###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
+
+  (when (treesit-ready-p 'yaml)
+    (treesit-parser-create 'yaml)
+
+    ;; Comments.
+    (setq-local comment-start "# ")
+    (setq-local comment-end "")
+
+    ;; 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)))
+
+    (treesit-major-mode-setup)))
+
+(provide 'yaml-ts-mode)
+
+;;; yaml-ts-mode.el ends here