From 7967f13f8a8cadf9fd931a752aec037c970d697a Mon Sep 17 00:00:00 2001 From: Randy Taylor Date: Wed, 7 Dec 2022 20:53:35 -0500 Subject: [PATCH] Add dockerfile-ts-mode * etc/NEWS: Mention it. * lisp/progmodes/dockerfile-ts-mode.el: New major mode with tree-sitter support. --- etc/NEWS | 5 + lisp/progmodes/dockerfile-ts-mode.el | 169 +++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 lisp/progmodes/dockerfile-ts-mode.el diff --git a/etc/NEWS b/etc/NEWS index a8e0ead4a4..4def95ca3f 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -3061,6 +3061,11 @@ A major mode based on the tree-sitter library for editing Bash shell scripts. It includes support for font-locking, indentation, Imenu, which-func, and navigation. +** New major mode 'dockerfile-ts-mode'. +A major mode based on the tree-sitter library for editing +Dockerfiles. It includes support for font-locking, indentation, Imenu, +and which-func. + * Incompatible Lisp Changes in Emacs 29.1 diff --git a/lisp/progmodes/dockerfile-ts-mode.el b/lisp/progmodes/dockerfile-ts-mode.el new file mode 100644 index 0000000000..fea0e76b8d --- /dev/null +++ b/lisp/progmodes/dockerfile-ts-mode.el @@ -0,0 +1,169 @@ +;;; dockerfile-ts-mode.el --- tree-sitter support for Dockerfiles -*- lexical-binding: t; -*- + +;; Copyright (C) 2022 Free Software Foundation, Inc. + +;; Author : Randy Taylor +;; Maintainer : Randy Taylor +;; Created : December 2022 +;; Keywords : dockerfile languages tree-sitter + +;; This file is part of GNU Emacs. + +;; This program 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. + +;; This program 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 this program. If not, see . + + +;;; 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-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))) + "Tree-sitter indent rules.") + +(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" . ,(dockerfile-ts-mode--imenu-1 stage-tree))))) + +(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 + (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) + + ;; 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))) + +(provide 'dockerfile-ts-mode) + +;;; dockerfile-ts-mode.el ends here -- 2.38.1