1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-29 07:58:28 +00:00
emacs/lisp/progmodes/dockerfile-ts-mode.el
Eli Zaretskii 970f94a2dd Merge from origin/emacs-29
ede3535051 ; Fix last change
8ec786349e Fix apostrophe handling in rust-ts-mode and go-ts-mode (B...
0eba9cf651 * test/infra/Dockerfile.emba (emacs-base): Install also g...
4897c98b6c Fix 'python-util-clone-local-variables'
6b2c8dc905 Revert "Enhance Python font-lock to support multilines"
348e4504c6 Fix typo in calc.texi
03663b8798 Update to Transient v0.4.1
dc7acb1aaf Avoid errors in 'delete-forward-char' deleting static com...
2f94f6de9d Make VS-15 and VS-16 compositions work correctly
753f8aa1f1 Fix project-name for vc-aware backend in non-file buffers
17c7915ab9 ; Fix 'package-install-upgrade-built-in' check for packag...
e252ce26ea Add type_predicate 'is' as keyword in typescript-ts-mode ...
0a354d6578 Fix infloop in info-look.el
83b22139e4 Fix several todo-mode.el item editing bugs (bug#63811)
ed4cd3eddf dockerfile-ts-mode: Prevent empty categories in imenu (Bu...
2e20e318da Brush up doc strings and terminology in plstore.el
372bc1278c Add internal documentation on plstore.el
23a14e7b90 Add compact_constructor_declaration font-locking to java-...
500abc4dc3 * lisp/tmm.el (tmm-completion-delete-prompt): Add more ch...
afc1f32935 Allow to disable the DWIMish behavior of 'x' in package menu
08104c0150 Allow dired to invoke secondary browser
a3063f0bc8 Add a binding for enriched-toggle-markup
d8ba28fa39 Fix order of tmm-menubar when 'tmm-mid-prompt' is nil

# Conflicts:
#	etc/NEWS
2023-06-03 05:50:58 -04:00

201 lines
6.6 KiB
EmacsLisp

;;; dockerfile-ts-mode.el --- tree-sitter support for Dockerfiles -*- lexical-binding: t; -*-
;; Copyright (C) 2022-2023 Free Software Foundation, Inc.
;; Author : Randy Taylor <dev@rjt.dev>
;; Maintainer : Randy Taylor <dev@rjt.dev>
;; Created : December 2022
;; Keywords : dockerfile 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)
(eval-when-compile (require 'rx))
(declare-function treesit-parser-create "treesit.c")
(declare-function treesit-induce-sparse-tree "treesit.c")
(declare-function treesit-node-child "treesit.c")
(declare-function treesit-node-child-by-field-name "treesit.c")
(declare-function treesit-node-start "treesit.c")
(declare-function treesit-node-type "treesit.c")
(defvar dockerfile-ts-mode--syntax-table
(let ((table (make-syntax-table)))
(modify-syntax-entry ?# "<" table)
(modify-syntax-entry ?\n ">" table)
table)
"Syntax table for `dockerfile-ts-mode'.")
(defvar dockerfile-ts-mode--indent-rules
`((dockerfile
((parent-is "copy_instruction") (nth-sibling 1) 0)
((parent-is "env_instruction") (nth-sibling 1) 0)
((parent-is "expose_instruction") (nth-sibling 1) 0)
((parent-is "label_instruction") (nth-sibling 1) 0)
((parent-is "shell_command") first-sibling 0)
((parent-is "string_array") first-sibling 1)
((dockerfile-ts-mode--line-continuation-p) dockerfile-ts-mode--line-continuation-anchor 0)))
"Tree-sitter indent rules.")
(defun dockerfile-ts-mode--line-continuation-p ()
"Return t if the current node is a line continuation node."
(lambda (node _ _ &rest _)
(string= (treesit-node-type node) "\n")))
(defun dockerfile-ts-mode--line-continuation-anchor (_ _ &rest _)
"This anchor is used to align any nodes that are part of a line
continuation to the previous entry."
(save-excursion
(forward-line -1)
(let ((prev-node (treesit-node-at (point))))
(if (string= (treesit-node-type prev-node) "\\\n")
(back-to-indentation)
(forward-word)
(forward-char))
(+ 1 (- (point) (pos-bol))))))
(defvar dockerfile-ts-mode--keywords
'("ADD" "ARG" "AS" "CMD" "COPY" "CROSS_BUILD" "ENTRYPOINT" "ENV"
"EXPOSE" "FROM" "HEALTHCHECK" "LABEL" "MAINTAINER" "ONBUILD" "RUN"
"SHELL" "STOPSIGNAL" "USER" "VOLUME" "WORKDIR")
"Dockerfile keywords for tree-sitter font-locking.")
(defvar dockerfile-ts-mode--font-lock-settings
(treesit-font-lock-rules
:language 'dockerfile
:feature 'bracket
'((["[" "]"]) @font-lock-bracket-face)
:language 'dockerfile
:feature 'comment
'((comment) @font-lock-comment-face)
:language 'dockerfile
:feature 'delimiter
'(([","]) @font-lock-delimiter-face)
:language 'dockerfile
:feature 'image-spec
'((image_spec) @font-lock-constant-face)
:language 'dockerfile
:feature 'keyword
`([,@dockerfile-ts-mode--keywords] @font-lock-keyword-face)
:language 'dockerfile
:feature 'number
'((expose_port) @font-lock-number-face)
:language 'dockerfile
:feature 'operator
'((["="]) @font-lock-operator-face)
:language 'dockerfile
:feature 'string
'((double_quoted_string) @font-lock-string-face)
:language 'dockerfile
:feature 'error
:override t
'((ERROR) @font-lock-warning-face))
"Tree-sitter font-lock settings.")
(defun dockerfile-ts-mode--imenu ()
"Return Imenu alist for the current buffer."
(let* ((node (treesit-buffer-root-node))
(stage-tree (treesit-induce-sparse-tree
node "from_instruction"
nil 1000))
(stage-index (dockerfile-ts-mode--imenu-1 stage-tree)))
(when stage-index `(("Stage" . ,stage-index)))))
(defun dockerfile-ts-mode--imenu-1 (node)
"Helper for `dockerfile-ts-mode--imenu'.
Find string representation for NODE and set marker, then recurse
the subtrees."
(let* ((ts-node (car node))
(children (cdr node))
(subtrees (mapcan #'dockerfile-ts-mode--imenu-1
children))
(name (when ts-node
(pcase (treesit-node-type ts-node)
("from_instruction"
(treesit-node-text
(or (treesit-node-child-by-field-name ts-node "as")
(treesit-node-child ts-node 1)) t)))))
(marker (when ts-node
(set-marker (make-marker)
(treesit-node-start ts-node)))))
(cond
((or (null ts-node) (null name)) subtrees)
(subtrees
`((,name ,(cons name marker) ,@subtrees)))
(t
`((,name . ,marker))))))
;;;###autoload
(define-derived-mode dockerfile-ts-mode prog-mode "Dockerfile"
"Major mode for editing Dockerfiles, powered by tree-sitter."
:group 'dockerfile
:syntax-table dockerfile-ts-mode--syntax-table
(when (treesit-ready-p 'dockerfile)
(treesit-parser-create 'dockerfile)
;; Comments.
(setq-local comment-start "# ")
(setq-local comment-end "")
(setq-local comment-start-skip (rx "#" (* (syntax whitespace))))
;; Imenu.
(setq-local imenu-create-index-function
#'dockerfile-ts-mode--imenu)
(setq-local which-func-functions nil)
;; Indent.
(setq-local treesit-simple-indent-rules
dockerfile-ts-mode--indent-rules)
;; Navigation
(setq-local treesit-sentence-type-regexp
"instruction")
;; Font-lock.
(setq-local treesit-font-lock-settings
dockerfile-ts-mode--font-lock-settings)
(setq-local treesit-font-lock-feature-list
'((comment)
(keyword string)
(image-spec number)
(bracket delimiter error operator)))
(treesit-major-mode-setup)))
(if (treesit-ready-p 'dockerfile)
(add-to-list 'auto-mode-alist
;; NOTE: We can't use `rx' here, as it breaks bootstrap.
'("\\(?:Dockerfile\\(?:\\..*\\)?\\|\\.[Dd]ockerfile\\)\\'"
. dockerfile-ts-mode)))
(provide 'dockerfile-ts-mode)
;;; dockerfile-ts-mode.el ends here