unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob 42fa7482a87c356092ebf525cad45a1ffe0aa037 5876 bytes (raw)
name: lisp/progmodes/dockerfile-ts-mode.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
 
;;; dockerfile-ts-mode.el --- tree-sitter support for Dockerfiles  -*- lexical-binding: t; -*-

;; Copyright (C) 2022-2024 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-node-child "treesit.c")
(declare-function treesit-node-child-by-field-name "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--stage-name (node)
  "Return the stage name of NODE.
Return nil if there is no name or if NODE is not a stage node."
  (pcase (treesit-node-type node)
    ("from_instruction"
     (treesit-node-text
      (or (treesit-node-child-by-field-name node "as")
          (treesit-node-child node 1))
      t))))

;;;###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)
    (setq treesit-primary-parser (treesit-parser-create 'dockerfile))

    ;; Comments.
    (setq-local comment-start "# ")
    (setq-local comment-end "")
    (setq-local comment-start-skip (rx "#" (* (syntax whitespace))))

    ;; Imenu.
    (setq-local treesit-simple-imenu-settings
                `(("Stage" "\\`from_instruction\\'" nil dockerfile-ts-mode--stage-name)))
    (setq-local which-func-functions nil)

    ;; Indent.
    (setq-local treesit-simple-indent-rules
                dockerfile-ts-mode--indent-rules)

    ;; Navigation
    (setq-local treesit-thing-settings
                `((dockerfile
                   (sentence "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)))

(derived-mode-add-parents 'dockerfile-ts-mode '(dockerfile-mode))

(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

debug log:

solving 42fa7482a87 ...
found 42fa7482a87 in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).