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

171 lines
5.7 KiB
EmacsLisp
Raw Normal View History

;;; html-ts-mode.el --- tree-sitter support for HTML -*- lexical-binding: t; -*-
;; Copyright (C) 2023-2025 Free Software Foundation, Inc.
;; Author : Theodor Thornhill <theo@thornhill.no>
;; Maintainer : Theodor Thornhill <theo@thornhill.no>
;; Created : January 2023
;; Keywords : html 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/>.
;;; Tree-sitter language versions
;;
;; html-ts-mode is known to work with the following languages and version:
;; - tree-sitter-html: v0.23.2-1-gd9219ad
;;
;; We try our best to make builtin modes work with latest grammar
;; versions, so a more recent grammar version has a good chance to work.
;; Send us a bug report if it doesn't.
;;; Commentary:
;;
;;; Code:
(require 'treesit)
(require 'sgml-mode)
(declare-function treesit-parser-create "treesit.c")
(declare-function treesit-node-type "treesit.c")
(declare-function treesit-search-subtree "treesit.c")
(defcustom html-ts-mode-indent-offset 2
"Number of spaces for each indentation step in `html-ts-mode'."
:version "29.1"
:type 'integer
:safe 'integerp
:group 'html)
(defvar html-ts-mode--indent-rules
`((html
((parent-is "fragment") column-0 0)
((node-is "/>") parent-bol 0)
((node-is ">") parent-bol 0)
((node-is "end_tag") parent-bol 0)
((parent-is "comment") prev-adaptive-prefix 0)
((parent-is "element") parent-bol html-ts-mode-indent-offset)
((parent-is "script_element") parent-bol html-ts-mode-indent-offset)
((parent-is "style_element") parent-bol html-ts-mode-indent-offset)
((parent-is "start_tag") parent-bol html-ts-mode-indent-offset)
((parent-is "self_closing_tag") parent-bol html-ts-mode-indent-offset)))
"Tree-sitter indent rules.")
(defvar html-ts-mode--font-lock-settings
(treesit-font-lock-rules
:language 'html
:override t
:feature 'comment
`((comment) @font-lock-comment-face)
:language 'html
:override t
:feature 'keyword
`("doctype" @font-lock-keyword-face)
:language 'html
:override t
:feature 'definition
`((tag_name) @font-lock-function-name-face)
:language 'html
:override t
:feature 'string
`((quoted_attribute_value) @font-lock-string-face)
:language 'html
:override t
:feature 'property
`((attribute_name) @font-lock-variable-name-face))
"Tree-sitter font-lock settings for `html-ts-mode'.")
(defun html-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."
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
(when (string-match-p "element" (treesit-node-type node))
(treesit-node-text
(treesit-search-subtree node "\\`tag_name\\'" nil nil 2)
t)))
(defun html-ts-mode--outline-predicate (node)
"Limit outlines to a few most meaningful elements."
(let ((name (html-ts-mode--defun-name node)))
(and name (string-match-p
(rx bos (or "html" "head" "script" "style"
"body" (and "h" (any "1-6"))
"ol" "ul" "table")
eos)
name))))
;;;###autoload
(define-derived-mode html-ts-mode html-mode "HTML"
"Major mode for editing Html, powered by tree-sitter."
:group 'html
(unless (treesit-ready-p 'html)
(error "Tree-sitter for HTML isn't available"))
(setq treesit-primary-parser (treesit-parser-create 'html))
;; Indent.
(setq-local treesit-simple-indent-rules html-ts-mode--indent-rules)
;; Navigation.
(setq-local treesit-defun-type-regexp "element")
(setq-local treesit-defun-name-function #'html-ts-mode--defun-name)
(setq-local treesit-thing-settings
`((html
(sexp ,(regexp-opt '("element"
"text"
"attribute"
"value")))
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
;; Also match script_element and style_element
"element"
;; HTML comments have the element syntax
"comment")))
(sentence ,(rx (and bos (or "tag_name" "attribute") eos)))
(text ,(regexp-opt '("comment" "text"))))))
;; Font-lock.
(setq-local treesit-font-lock-settings html-ts-mode--font-lock-settings)
(setq-local treesit-font-lock-feature-list
'((comment keyword definition)
(property string)
() ()))
;; Imenu.
(setq-local treesit-simple-imenu-settings
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
'((nil "element" nil nil)))
;; Outline minor mode.
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-outline-predicate #'html-ts-mode--outline-predicate)
;; `html-ts-mode' inherits from `html-mode' that sets
;; regexp-based outline variables. So need to restore
;; the default values of outline variables to be able
;; to use `treesit-outline-predicate' above.
(kill-local-variable 'outline-regexp)
(kill-local-variable 'outline-heading-end-regexp)
(kill-local-variable 'outline-level)
(treesit-major-mode-setup))
(derived-mode-add-parents 'html-ts-mode '(html-mode))
(if (treesit-ready-p 'html)
(add-to-list 'auto-mode-alist '("\\.html\\'" . html-ts-mode)))
(provide 'html-ts-mode)
;;; html-ts-mode.el ends here