all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#59894: [PATCH] Add dockerfile-ts-mode
@ 2022-12-08  2:11 Randy Taylor
  2022-12-08  4:10 ` Stefan Kangas
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Randy Taylor @ 2022-12-08  2:11 UTC (permalink / raw)
  To: 59894; +Cc: casouri


[-- Attachment #1.1: Type: text/plain, Size: 175 bytes --]

X-Debbugs-CC: casouri@gmail.com

Not adding to auto-mode-alist since the dockerfile-mode package exists.

Got a few more modes (hopefully) coming soon to a theatre near you...

[-- Attachment #1.2: Type: text/html, Size: 1070 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-dockerfile-ts-mode.patch --]
[-- Type: text/x-patch; name=0001-Add-dockerfile-ts-mode.patch, Size: 6923 bytes --]

From 7967f13f8a8cadf9fd931a752aec037c970d697a Mon Sep 17 00:00:00 2001
From: Randy Taylor <dev@rjt.dev>
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.
+
 \f
 * 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 <dev@rjt.dev>
+;; Maintainer : Randy Taylor <dev@rjt.dev>
+;; 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 <http://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-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


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* bug#59894: [PATCH] Add dockerfile-ts-mode
  2022-12-08  2:11 bug#59894: [PATCH] Add dockerfile-ts-mode Randy Taylor
@ 2022-12-08  4:10 ` Stefan Kangas
  2022-12-08  7:19 ` Eli Zaretskii
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: Stefan Kangas @ 2022-12-08  4:10 UTC (permalink / raw)
  To: Randy Taylor, 59894; +Cc: casouri

Randy Taylor <dev@rjt.dev> writes:

> Not adding to auto-mode-alist since the dockerfile-mode package exists.

Why not?  You can have more than one entry for the same type of files.

> Got a few more modes (hopefully) coming soon to a theatre near you...

Great!





^ permalink raw reply	[flat|nested] 9+ messages in thread

* bug#59894: [PATCH] Add dockerfile-ts-mode
  2022-12-08  2:11 bug#59894: [PATCH] Add dockerfile-ts-mode Randy Taylor
  2022-12-08  4:10 ` Stefan Kangas
@ 2022-12-08  7:19 ` Eli Zaretskii
  2022-12-08 10:17 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2022-12-09 21:27 ` Yuan Fu
  3 siblings, 0 replies; 9+ messages in thread
From: Eli Zaretskii @ 2022-12-08  7:19 UTC (permalink / raw)
  To: Randy Taylor; +Cc: casouri, 59894

> Cc: casouri@gmail.com
> Date: Thu, 08 Dec 2022 02:11:10 +0000
> From: Randy Taylor <dev@rjt.dev>
> 
> Not adding to auto-mode-alist since the dockerfile-mode package exists.

That mode is not in Emacs, so I see no reason not to add this new mode
to auto-mode-alist.

Thanks.





^ permalink raw reply	[flat|nested] 9+ messages in thread

* bug#59894: [PATCH] Add dockerfile-ts-mode
  2022-12-08  2:11 bug#59894: [PATCH] Add dockerfile-ts-mode Randy Taylor
  2022-12-08  4:10 ` Stefan Kangas
  2022-12-08  7:19 ` Eli Zaretskii
@ 2022-12-08 10:17 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2022-12-08 22:21   ` Randy Taylor
  2022-12-09 21:27 ` Yuan Fu
  3 siblings, 1 reply; 9+ messages in thread
From: Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2022-12-08 10:17 UTC (permalink / raw)
  To: Randy Taylor; +Cc: casouri, 59894

Randy Taylor <dev@rjt.dev> writes:

> X-Debbugs-CC: casouri@gmail.com
>
> Not adding to auto-mode-alist since the dockerfile-mode package exists.
>
> Got a few more modes (hopefully) coming soon to a theatre near you...
>

Nice, Looks good to me.  Could you maybe also fix the
admin/notes/tree-sitter/build-module/build.sh script so that we can get
the language installed easily?

Theo





^ permalink raw reply	[flat|nested] 9+ messages in thread

* bug#59894: [PATCH] Add dockerfile-ts-mode
  2022-12-08 10:17 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2022-12-08 22:21   ` Randy Taylor
  2022-12-09  9:52     ` Michael Albinus
  0 siblings, 1 reply; 9+ messages in thread
From: Randy Taylor @ 2022-12-08 22:21 UTC (permalink / raw)
  To: Theodor Thornhill; +Cc: casouri, Eli Zaretskii, 59894, Stefan Kangas

[-- Attachment #1: Type: text/plain, Size: 346 bytes --]

Thanks for the feedback everyone.

New attached patched addressing the following:
- Added dockerfile-ts-mode to auto-mode-alist (perhaps a regex guru could squeeze it into one, I spent more time trying than I liked to no avail).
- Added dockerfile-ts-mode to eglot.
- Updated tree-sitter module build scripts to support building dockerfiles.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-dockerfile-ts-mode-Bug-59894.patch --]
[-- Type: text/x-patch; name=0001-Add-dockerfile-ts-mode-Bug-59894.patch, Size: 9845 bytes --]

From c843c2cca086752b66aceff9250c097117a48208 Mon Sep 17 00:00:00 2001
From: Randy Taylor <dev@rjt.dev>
Date: Wed, 7 Dec 2022 20:53:35 -0500
Subject: [PATCH] Add dockerfile-ts-mode (Bug#59894)

* admin/notes/tree-sitter/build-module/batch.sh: Add dockerfile support.
* admin/notes/tree-sitter/build-module/build.sh: Support different
namespaces and add dockerfile support.
* etc/NEWS: Mention it.
* lisp/progmodes/dockerfile-ts-mode.el: New major mode with
tree-sitter support.
* lisp/progmodes/eglot.el (eglot-server-programs): Add it.
---
 admin/notes/tree-sitter/build-module/batch.sh |   1 +
 admin/notes/tree-sitter/build-module/build.sh |   6 +-
 etc/NEWS                                      |   5 +
 lisp/progmodes/dockerfile-ts-mode.el          | 177 ++++++++++++++++++
 lisp/progmodes/eglot.el                       |   2 +-
 5 files changed, 189 insertions(+), 2 deletions(-)
 create mode 100644 lisp/progmodes/dockerfile-ts-mode.el

diff --git a/admin/notes/tree-sitter/build-module/batch.sh b/admin/notes/tree-sitter/build-module/batch.sh
index d45f37f4b6..6dce000caa 100755
--- a/admin/notes/tree-sitter/build-module/batch.sh
+++ b/admin/notes/tree-sitter/build-module/batch.sh
@@ -5,6 +5,7 @@ languages=
     'cpp'
     'css'
     'c-sharp'
+    'dockerfile'
     'go'
     'html'
     'javascript'
diff --git a/admin/notes/tree-sitter/build-module/build.sh b/admin/notes/tree-sitter/build-module/build.sh
index a71ccaa4ed..190ceb3c4d 100755
--- a/admin/notes/tree-sitter/build-module/build.sh
+++ b/admin/notes/tree-sitter/build-module/build.sh
@@ -14,11 +14,15 @@ topdir=
 
 ### Retrieve sources
 
+namespace="tree-sitter"
 repo="tree-sitter-${lang}"
 sourcedir="tree-sitter-${lang}/src"
 grammardir="tree-sitter-${lang}"
 
 case "${lang}" in
+    "dockerfile")
+        namespace="camdencheek"
+        ;;
     "typescript")
         sourcedir="tree-sitter-typescript/typescript/src"
         grammardir="tree-sitter-typescript/typescript"
@@ -30,7 +34,7 @@ grammardir=
         ;;
 esac
 
-git clone "https://github.com/tree-sitter/${repo}.git" \
+git clone "https://github.com/${namespace}/${repo}.git" \
     --depth 1 --quiet
 cp "${grammardir}"/grammar.js "${sourcedir}"
 # We have to go into the source directory to compile, because some
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.
+
 \f
 * 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..5214532ddb
--- /dev/null
+++ b/lisp/progmodes/dockerfile-ts-mode.el
@@ -0,0 +1,177 @@
+;;; dockerfile-ts-mode.el --- tree-sitter support for Dockerfiles  -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2022 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.
+
+;; 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 <http://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-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
+(add-to-list 'auto-mode-alist
+             '("Dockerfile\\([.]\\(.*\\)\\)?$" . dockerfile-ts-mode))
+
+;;;###autoload
+(add-to-list 'auto-mode-alist
+             '("\\.[dD]ockerfile\\'" . dockerfile-ts-mode))
+
+;;;###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
diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index cafb99c6d8..808cfe46ea 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -236,7 +236,7 @@ eglot-server-programs
                                  . ,(eglot-alternatives '(("vscode-css-language-server" "--stdio")
                                                           ("css-languageserver" "--stdio"))))
                                 (html-mode . ,(eglot-alternatives '(("vscode-html-language-server" "--stdio") ("html-languageserver" "--stdio"))))
-                                (dockerfile-mode . ("docker-langserver" "--stdio"))
+                                ((dockerfile-mode dockerfile-ts-mode) . ("docker-langserver" "--stdio"))
                                 ((clojure-mode clojurescript-mode clojurec-mode)
                                  . ("clojure-lsp"))
                                 ((csharp-mode csharp-ts-mode)
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* bug#59894: [PATCH] Add dockerfile-ts-mode
  2022-12-08 22:21   ` Randy Taylor
@ 2022-12-09  9:52     ` Michael Albinus
  2022-12-09 20:51       ` Randy Taylor
  0 siblings, 1 reply; 9+ messages in thread
From: Michael Albinus @ 2022-12-09  9:52 UTC (permalink / raw)
  To: Randy Taylor
  Cc: casouri, Theodor Thornhill, Eli Zaretskii, 59894, Stefan Kangas

Randy Taylor <dev@rjt.dev> writes:

Hi Randy,

> New attached patched addressing the following:
> - Added dockerfile-ts-mode to auto-mode-alist (perhaps a regex guru could squeeze it into one, I spent more time trying than I liked to no avail).

(rx (| (: "Dockerfile" (? "." (* nonl))) (: "." (any "dD") "ockerfile")) eol)

Best regards, Michael.





^ permalink raw reply	[flat|nested] 9+ messages in thread

* bug#59894: [PATCH] Add dockerfile-ts-mode
  2022-12-09  9:52     ` Michael Albinus
@ 2022-12-09 20:51       ` Randy Taylor
  0 siblings, 0 replies; 9+ messages in thread
From: Randy Taylor @ 2022-12-09 20:51 UTC (permalink / raw)
  To: Michael Albinus
  Cc: casouri, Theodor Thornhill, Eli Zaretskii, 59894, Stefan Kangas

[-- Attachment #1: Type: text/plain, Size: 692 bytes --]

On Friday, December 9th, 2022 at 04:52, Michael Albinus <michael.albinus@gmx.de> wrote:
> 
> Randy Taylor dev@rjt.dev writes:
> 
> 
> Hi Randy,
> 
> > New attached patched addressing the following:
> > - Added dockerfile-ts-mode to auto-mode-alist (perhaps a regex guru could squeeze it into one, I spent more time trying than I liked to no avail).
> 
> 
> (rx (| (: "Dockerfile" (? "." (* nonl))) (: "." (any "dD") "ockerfile")) eol)
> 
> Best regards, Michael.

Thank you, Michael! It works perfectly - I should've thought about using rx.

I've attached a new patch with Michael's suggestion and also fixed the copyright notice per Stefan's recent changes to similar modes.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-dockerfile-ts-mode-Bug-59894.patch --]
[-- Type: text/x-patch; name=0001-Add-dockerfile-ts-mode-Bug-59894.patch, Size: 9853 bytes --]

From 1d1e5ecae795674c751572ec8c757c36e3592130 Mon Sep 17 00:00:00 2001
From: Randy Taylor <dev@rjt.dev>
Date: Wed, 7 Dec 2022 20:53:35 -0500
Subject: [PATCH] Add dockerfile-ts-mode (Bug#59894)

* admin/notes/tree-sitter/build-module/batch.sh: Add dockerfile support.
* admin/notes/tree-sitter/build-module/build.sh: Support different
namespaces and add dockerfile support.
* etc/NEWS: Mention it.
* lisp/progmodes/dockerfile-ts-mode.el: New major mode with
tree-sitter support.
* lisp/progmodes/eglot.el (eglot-server-programs): Add it.
---
 admin/notes/tree-sitter/build-module/batch.sh |   1 +
 admin/notes/tree-sitter/build-module/build.sh |   6 +-
 etc/NEWS                                      |   5 +
 lisp/progmodes/dockerfile-ts-mode.el          | 176 ++++++++++++++++++
 lisp/progmodes/eglot.el                       |   2 +-
 5 files changed, 188 insertions(+), 2 deletions(-)
 create mode 100644 lisp/progmodes/dockerfile-ts-mode.el

diff --git a/admin/notes/tree-sitter/build-module/batch.sh b/admin/notes/tree-sitter/build-module/batch.sh
index d45f37f4b6..6dce000caa 100755
--- a/admin/notes/tree-sitter/build-module/batch.sh
+++ b/admin/notes/tree-sitter/build-module/batch.sh
@@ -5,6 +5,7 @@ languages=
     'cpp'
     'css'
     'c-sharp'
+    'dockerfile'
     'go'
     'html'
     'javascript'
diff --git a/admin/notes/tree-sitter/build-module/build.sh b/admin/notes/tree-sitter/build-module/build.sh
index d562f1a784..cc31e3f6f0 100755
--- a/admin/notes/tree-sitter/build-module/build.sh
+++ b/admin/notes/tree-sitter/build-module/build.sh
@@ -14,11 +14,15 @@ topdir=
 
 ### Retrieve sources
 
+namespace="tree-sitter"
 repo="tree-sitter-${lang}"
 sourcedir="tree-sitter-${lang}/src"
 grammardir="tree-sitter-${lang}"
 
 case "${lang}" in
+    "dockerfile")
+        namespace="camdencheek"
+        ;;
     "typescript")
         sourcedir="tree-sitter-typescript/typescript/src"
         grammardir="tree-sitter-typescript/typescript"
@@ -30,7 +34,7 @@ grammardir=
         ;;
 esac
 
-git clone "https://github.com/tree-sitter/${repo}.git" \
+git clone "https://github.com/${namespace}/${repo}.git" \
     --depth 1 --quiet
 cp "${grammardir}"/grammar.js "${sourcedir}"
 # We have to go into the source directory to compile, because some
diff --git a/etc/NEWS b/etc/NEWS
index e4a19e2986..233ef3f572 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -3068,6 +3068,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.
+
 \f
 * 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..e08387ad96
--- /dev/null
+++ b/lisp/progmodes/dockerfile-ts-mode.el
@@ -0,0 +1,176 @@
+;;; dockerfile-ts-mode.el --- tree-sitter support for Dockerfiles  -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2022 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-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
+(add-to-list 'auto-mode-alist
+             `(,(rx (|
+                     (: "Dockerfile" (? "." (* nonl)))
+                     (: "." (any "dD") "ockerfile"))
+                    eol)
+               . dockerfile-ts-mode))
+
+;;;###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
diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index a53f62fc56..2ef022992e 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -236,7 +236,7 @@ eglot-server-programs
                                  . ,(eglot-alternatives '(("vscode-css-language-server" "--stdio")
                                                           ("css-languageserver" "--stdio"))))
                                 (html-mode . ,(eglot-alternatives '(("vscode-html-language-server" "--stdio") ("html-languageserver" "--stdio"))))
-                                (dockerfile-mode . ("docker-langserver" "--stdio"))
+                                ((dockerfile-mode dockerfile-ts-mode) . ("docker-langserver" "--stdio"))
                                 ((clojure-mode clojurescript-mode clojurec-mode)
                                  . ("clojure-lsp"))
                                 ((csharp-mode csharp-ts-mode)
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* bug#59894: [PATCH] Add dockerfile-ts-mode
  2022-12-08  2:11 bug#59894: [PATCH] Add dockerfile-ts-mode Randy Taylor
                   ` (2 preceding siblings ...)
  2022-12-08 10:17 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2022-12-09 21:27 ` Yuan Fu
  2022-12-10  3:24   ` Randy Taylor
  3 siblings, 1 reply; 9+ messages in thread
From: Yuan Fu @ 2022-12-09 21:27 UTC (permalink / raw)
  To: Randy Taylor
  Cc: Theodor Thornhill, Eli Zaretskii, Michael Albinus, stefankangas,
	59894


Randy Taylor <dev@rjt.dev> writes:

> On Friday, December 9th, 2022 at 04:52, Michael Albinus <michael.albinus@gmx.de> wrote:
>> 
>> Randy Taylor dev@rjt.dev writes:
>> 
>> 
>> Hi Randy,
>> 
>> > New attached patched addressing the following:
>> > - Added dockerfile-ts-mode to auto-mode-alist (perhaps a regex guru could squeeze it into one, I spent more time trying than I liked to no avail).
>> 
>> 
>> (rx (| (: "Dockerfile" (? "." (* nonl))) (: "." (any "dD") "ockerfile")) eol)
>> 
>> Best regards, Michael.
>
> Thank you, Michael! It works perfectly - I should've thought about using rx.
>
> I've attached a new patch with Michael's suggestion and also fixed the copyright notice per Stefan's recent changes to similar modes.

Thanks. I applied the patch :-)

Yuan





^ permalink raw reply	[flat|nested] 9+ messages in thread

* bug#59894: [PATCH] Add dockerfile-ts-mode
  2022-12-09 21:27 ` Yuan Fu
@ 2022-12-10  3:24   ` Randy Taylor
  0 siblings, 0 replies; 9+ messages in thread
From: Randy Taylor @ 2022-12-10  3:24 UTC (permalink / raw)
  To: Yuan Fu
  Cc: Michael Albinus, Eli Zaretskii, Theodor Thornhill, stefankangas,
	59894-done@debbugs.gnu.org

On Friday, December 9th, 2022 at 16:27, Yuan Fu <casouri@gmail.com> wrote:
> 
> 
> Thanks. I applied the patch :-)
> 
> Yuan
> 

Thanks! Closing.





^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-12-10  3:24 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-08  2:11 bug#59894: [PATCH] Add dockerfile-ts-mode Randy Taylor
2022-12-08  4:10 ` Stefan Kangas
2022-12-08  7:19 ` Eli Zaretskii
2022-12-08 10:17 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-12-08 22:21   ` Randy Taylor
2022-12-09  9:52     ` Michael Albinus
2022-12-09 20:51       ` Randy Taylor
2022-12-09 21:27 ` Yuan Fu
2022-12-10  3:24   ` Randy Taylor

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.