unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Jostein Kjønigsen" <jostein@secure.kjonigsen.net>
To: Stefan Kangas <stefankangas@gmail.com>
Cc: "Yuan Fu" <casouri@gmail.com>,
	"Jostein Kjønigsen" <jostein@kjonigsen.net>,
	"Ergus via Emacs development discussions." <emacs-devel@gnu.org>,
	"Theodor Thornhill" <theo@thornhill.no>
Subject: Re: [PATCH] New major-mode: bicep-ts-mode
Date: Fri, 5 Jan 2024 20:10:18 +0100	[thread overview]
Message-ID: <B0833DC8-21E7-48DF-9A15-9B7822ED0FA0@secure.kjonigsen.net> (raw)
In-Reply-To: <CADwFkmkhEX6t5xukUw9hNhdE7s1pD+bRc95ojtMA2bkh5or_bQ@mail.gmail.com>

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


> On 4 Jan 2024, at 05:52, Stefan Kangas <stefankangas@gmail.com> wrote:
> 
>> Looks good. Should we merge it now?
> 
> No objections from me as long as Jostein thinks it's ready, but:

I’m happy with it for now!

> - It should be called out in NEWS.

Done!

> - Don't forget updating `auto-mode-alist' if that makes sense.


I’ve added it in the major-mode itself. I assume this is the right place to add it?

Updated is a patch based on latest git master which combines all these changes, and also adds code to obtain and build the Bicep language-grammar alongside the other languages.

—
Kind Regards
Jostein Kjønigsen



[-- Attachment #2.1: Type: text/html, Size: 1243 bytes --]

[-- Attachment #2.2: 0001-Add-support-for-Bicep-files-via-tree-sitter-based-ma.patch --]
[-- Type: application/octet-stream, Size: 8571 bytes --]

From b7aa90ae365cb08984f2b2fc6fd2e8f45018b6fe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jostein=20Kj=C3=B8nigsen?= <jostein@kjonigsen.net>
Date: Fri, 5 Jan 2024 19:58:19 +0100
Subject: [PATCH] Add support for Bicep-files via tree-sitter based major-mode

* admin/notes/tree-sitter/build-module/batch.sh: Add bicep grammar to list.
* admin/notes/tree-sitter/build-module/build.sh: Add org for bicep grammar.
* etc/NEWS: Updated news.
* lisp/progmodes/bicep-ts-mode.el: New major-mode bicep-ts-mode.
---
 admin/notes/tree-sitter/build-module/batch.sh |   1 +
 admin/notes/tree-sitter/build-module/build.sh |   7 +-
 etc/NEWS                                      |   4 +
 lisp/progmodes/bicep-ts-mode.el               | 179 ++++++++++++++++++
 4 files changed, 189 insertions(+), 2 deletions(-)
 create mode 100644 lisp/progmodes/bicep-ts-mode.el

diff --git a/admin/notes/tree-sitter/build-module/batch.sh b/admin/notes/tree-sitter/build-module/batch.sh
index 9988d1eae4e..6ed2888d1d8 100755
--- a/admin/notes/tree-sitter/build-module/batch.sh
+++ b/admin/notes/tree-sitter/build-module/batch.sh
@@ -2,6 +2,7 @@
 
 languages=(
     'bash'
+    'bicep'
     'c'
     'cmake'
     'cpp'
diff --git a/admin/notes/tree-sitter/build-module/build.sh b/admin/notes/tree-sitter/build-module/build.sh
index 969187b7f92..46fe3d2d8db 100755
--- a/admin/notes/tree-sitter/build-module/build.sh
+++ b/admin/notes/tree-sitter/build-module/build.sh
@@ -25,12 +25,15 @@ sourcedir=
 grammardir="tree-sitter-${lang}"
 
 case "${lang}" in
-    "dockerfile")
-        org="camdencheek"
+    "bicep")
+        org="amaanq"
         ;;
     "cmake")
         org="uyha"
         ;;
+    "dockerfile")
+        org="camdencheek"
+        ;;
     "elixir")
         org="elixir-lang"
         ;;
diff --git a/etc/NEWS b/etc/NEWS
index 7bbfbf9512d..7790d887a93 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1244,6 +1244,10 @@ A major mode based on the tree-sitter library for editing Elixir files.
 *** New major mode 'lua-ts-mode'.
 A major mode based on the tree-sitter library for editing Lua files.
 
+---
+*** New major mode 'bicep-ts-mode'.
+A major mode based on the tree-sitter library for editing Bicep files.
+
 ** Minibuffer and Completions
 
 +++
diff --git a/lisp/progmodes/bicep-ts-mode.el b/lisp/progmodes/bicep-ts-mode.el
new file mode 100644
index 00000000000..fa42da5e270
--- /dev/null
+++ b/lisp/progmodes/bicep-ts-mode.el
@@ -0,0 +1,179 @@
+;;; bicep-ts-mode.el --- tree-sitter support for Bicep  -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2023-2023 Free Software Foundation, Inc.
+
+;; Author     : Jostein Kjønigsen <jostein@kjonigsen.net>
+;; Maintainer : Jostein Kjønigsen <jostein@kjonigsen.net>
+;; Created    : December 2023
+;; Keywords   : bicep 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)
+
+(declare-function treesit-parser-create "treesit.c")
+(declare-function treesit-induce-sparse-tree "treesit.c")
+(declare-function treesit-node-start "treesit.c")
+(declare-function treesit-node-type "treesit.c")
+(declare-function treesit-node-child "treesit.c")
+(declare-function treesit-node-child-by-field-name "treesit.c")
+
+(defcustom bicep-ts-mode-indent-offset 2
+  "Number of spaces for each indentation step in `bicep-ts-mode'."
+  :version "29.1"
+  :type 'natnum
+  :safe 'natnump
+  :group 'bicep)
+
+(defvar bicep-ts-mode--syntax-table
+  (let ((table (make-syntax-table)))
+    (modify-syntax-entry ?=  "."   table)
+    (modify-syntax-entry ?\' "\""  table)
+    (modify-syntax-entry ?\n "> b" table)
+    table)
+  "Syntax table for `bicep-ts-mode'.")
+
+(defvar bicep-ts-mode--indent-rules
+  `((bicep
+     ((node-is "}") parent-bol 0)
+     ((node-is "]") parent-bol 0)
+     ((parent-is "array") parent-bol bicep-ts-mode-indent-offset)
+     ((parent-is "object") parent-bol bicep-ts-mode-indent-offset)
+     ((parent-is "for_statement") parent-bol bicep-ts-mode-indent-offset))))
+
+(defvar bicep-ts-mode--keywords
+  '("var" "param" "resource"
+    "module" "type" "metadata"
+    "targetScope" "output"
+    "for" "in")
+  "Bicep keywords for tree-sitter font-locking.")
+
+(defvar bicep-ts-mode--font-lock-settings
+  (treesit-font-lock-rules
+   :language 'bicep
+   :feature 'comment
+   '((comment) @font-lock-comment-face)
+
+   :language 'bicep
+   :feature 'delimiter
+   '(("=") @font-lock-delimiter-face)
+
+   :language 'bicep
+   :feature 'keyword
+   `([,@bicep-ts-mode--keywords] @font-lock-keyword-face)
+
+   :language 'bicep
+   :feature 'definition
+   '((parameter_declaration
+      (identifier) @font-lock-variable-name-face
+      (type) @font-lock-type-face)
+     (variable_declaration
+      (identifier) @font-lock-variable-name-face)
+     (resource_declaration
+      (identifier) @font-lock-variable-name-face)
+     (module_declaration
+      (identifier) @font-lock-variable-name-face)
+     (type_declaration
+      (identifier) @font-lock-type-face)
+     ((builtin_type) @font-lock-type-face)
+     (output_declaration
+      (identifier) @font-lock-variable-name-face)
+     (output_declaration
+      (type) @font-lock-type-face))
+
+   :language 'bicep
+   :feature 'number
+   '((number)
+     @font-lock-number-face)
+
+   :language 'bicep
+   :feature 'string
+   '((string_content) @font-lock-string-face)
+
+   :language 'bicep
+   :feature 'boolean
+   '((boolean) @font-lock-constant-face)
+
+   :language 'bicep
+   :feature 'functions
+   '((call_expression
+      function: (identifier) @font-lock-function-name-face))
+
+   :language 'bicep
+   :feature 'error
+   :override t
+   '((ERROR) @font-lock-warning-face))
+  "Font-lock settings for BICEP.")
+
+(defun bicep-ts-mode--defun-name (node)
+  "Return the defun name of NODE.
+Return nil if there is no name or if NODE is not a defun node."
+  (treesit-node-text
+   (treesit-node-child node 1)
+   t))
+
+;;;###autoload
+(define-derived-mode bicep-ts-mode prog-mode "Bicep"
+  "Major mode for editing BICEP, powered by tree-sitter."
+  :group 'bicep-mode
+  :syntax-table bicep-ts-mode--syntax-table
+
+  (when (treesit-ready-p 'bicep)
+    (treesit-parser-create 'bicep)
+
+    ;; Comments
+    (setq-local comment-start "# ")
+    (setq-local comment-end "")
+
+    ;; Indent.
+    (setq-local treesit-simple-indent-rules bicep-ts-mode--indent-rules)
+
+    ;; Navigation.
+    (setq-local treesit-defun-type-regexp
+                (rx (or "module_declaration" "type_declaration" "variable_declaration"
+                        "parameter_declaration" "resource_declaration" "output_declaration")))
+    (setq-local treesit-defun-name-function #'bicep-ts-mode--defun-name)
+
+    ;; Font-lock.
+    (setq-local treesit-font-lock-settings bicep-ts-mode--font-lock-settings)
+    (setq-local treesit-font-lock-feature-list
+                '((comment delimiter keyword)
+                  (definition number string boolean)
+                  (functions)
+                  (error)))
+
+    ;; Imenu.
+    (setq-local treesit-simple-imenu-settings
+                '(("Modules" "\\`module_declaration\\'" nil nil)
+                  ("Types" "\\`type_declaration\\'" nil nil)
+                  ("Variables" "\\`variable_declaration\\'" nil nil)
+                  ("Parameters" "\\`parameter_declaration\\'" nil nil)
+                  ("Resources" "\\`resource_declaration\\'" nil nil)
+                  ("Outputs" "\\`output_declaration\\'" nil nil)))
+
+    (treesit-major-mode-setup)))
+
+(if (treesit-ready-p 'bicep)
+    (add-to-list 'auto-mode-alist '("\\.bicep\\'" . bicep-ts-mode)))
+
+(provide 'bicep-ts-mode)
+
+;;; bicep-ts-mode.el ends here
-- 
2.43.0


[-- Attachment #2.3: Type: text/html, Size: 234 bytes --]

  reply	other threads:[~2024-01-05 19:10 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-21 14:32 [PATCH] New major-mode: bicep-ts-mode Jostein Kjønigsen
2023-12-22 10:42 ` Stefan Kangas
2023-12-22 11:55   ` Jostein Kjønigsen
2023-12-24 14:32     ` Stefan Kangas
2024-01-02  7:52       ` Jostein Kjønigsen
2024-01-03  7:16         ` Yuan Fu
2024-01-04  4:52           ` Stefan Kangas
2024-01-05 19:10             ` Jostein Kjønigsen [this message]
2024-01-07 18:04 ` Daniel Mendler via Emacs development discussions.
2024-01-07 20:26   ` Stefan Kangas
2024-01-07 23:25     ` Stefan Monnier
2024-01-08 11:30       ` Jostein Kjønigsen
2024-01-08 19:23         ` Stefan Kangas
2024-01-09  0:33           ` Stefan Monnier
2024-01-09 19:12             ` Jostein Kjønigsen
2024-01-09 19:16               ` Stefan Kangas
2024-01-14  4:43                 ` Stefan Monnier
2024-01-15  9:56                   ` Jostein Kjønigsen
2024-01-15 17:35                   ` Philip Kaludercic
2024-01-15 19:20                     ` Stefan Monnier
2024-01-09 19:24               ` Philip Kaludercic

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=B0833DC8-21E7-48DF-9A15-9B7822ED0FA0@secure.kjonigsen.net \
    --to=jostein@secure.kjonigsen.net \
    --cc=casouri@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=jostein@kjonigsen.net \
    --cc=stefankangas@gmail.com \
    --cc=theo@thornhill.no \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).