all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [PATCH] New major-mode: bicep-ts-mode
@ 2023-12-21 14:32 Jostein Kjønigsen
  2023-12-22 10:42 ` Stefan Kangas
  2024-01-07 18:04 ` Daniel Mendler via Emacs development discussions.
  0 siblings, 2 replies; 21+ messages in thread
From: Jostein Kjønigsen @ 2023-12-21 14:32 UTC (permalink / raw)
  To: Ergus via Emacs development discussions.; +Cc: Yuan Fu, Theodor Thornhill

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

Hey everyone.

QUICK PREFACE

Currently if one works with Microsoft-oriented solutions (C#, .NET, TypeScript, Azure, etc) one often also has to work with Bicep. Bicep is a programming language used to describe Infrastructure-as-Code used for deploying services in Microsoft Azure cloud.

Unfortunately the only editor currently available with useful Bicep-support is Microsoft VSCode. Working with Bicep in Emacs currently is not really feasible.

The Bicep language itself is a type-safe language with an associated open-source toolchain, which in turn gets compiled to JSON-formatted Azure Resource Manager (ARM) templates, which can actually provision resources in Azure.

With appropriate editor-support, working with Bicep is superior to working with the untyped JSON for ARM template purposes. As such, adding support for Bicep to Emacs, would mean most Azure-developers can still stay 100% within Emacs to do their job :)

ABOUT THE PATCH

The attached patch tries to rectify the situation by making introducing tree-sitter based major-mode to handle important aspects of Bicep development:

* provide syntax highligting
* provide indentation
* provide imenu-support

The implementation itself depends on tree-sitter and the tree-sitter grammar developed by Amanda Qureshi (unaffiliated with this effort):
https://github.com/amaanq/tree-sitter-bicep

This grammar compiles and installs cleanly with the default settings found in M-x treesit-install-language-grammar given the repo URL only.

The implementation is fairly basic, but as far as I can see it works well and greatly improve the user-experience for Bicep-development.

NOT yet provided:

* adding Bicep.LangServer LSP-configuration to eglot (although I’ve tested that locally and it seems to work seamlessly once you have the Bicep language-server resolved).

The latter is essential into bringing Emacs to feature-parity with VSCode, but its probably best to land this major-mode, before committing to how that major-mode should be wired up inside Emacs :)


Any opinions?


For more information about Bicep, Microsoft has documentation online: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/overview?tabs=bicep
Bicep toolchain souce: https://github.com/Azure/bicep


—
Kind Regards
Jostein Kjønigsen



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

[-- Attachment #2.2: bicep-ts-model.el --]
[-- Type: application/octet-stream, Size: 5800 bytes --]

;;; bicep-ts-mode.el --- tree-sitter support for Bicep  -*- lexical-binding: t; -*-

;; Copyright (C) 2024-2024 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)
     ((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)
     (type_declaration
      (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

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

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

end of thread, other threads:[~2024-01-15 19:20 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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.