;;; html-ts-mode.el --- tree-sitter support for HTML -*- lexical-binding: t; -*- ;; Copyright (C) 2023-2025 Free Software Foundation, Inc. ;; Author : Theodor Thornhill ;; Maintainer : Theodor Thornhill ;; 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 . ;;; Tree-sitter language versions ;; ;; html-ts-mode has been tested with the following grammars 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 has a good chance to work too. ;; 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") (add-to-list 'treesit-language-source-alist '(html "https://github.com/tree-sitter/tree-sitter-html" :commit "d9219ada6e1a2c8f0ab0304a8bd9ca4285ae0468") t) (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'.") (defvar html-ts-mode--treesit-things-settings `((html (sexp (not (or (and named ,(rx bos (or "document" "tag_name") eos)) (and anonymous ,(rx (or "" "