unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* toml-ts-mode: first draft
@ 2022-12-11 13:28 Jostein Kjønigsen
  2022-12-11 17:09 ` Juri Linkov
  0 siblings, 1 reply; 36+ messages in thread
From: Jostein Kjønigsen @ 2022-12-11 13:28 UTC (permalink / raw)
  To: Ergus via Emacs development discussions., Yuan Fu,
	Theodor Thornhill, Eli Zaretskii


[-- Attachment #1.1: Type: text/html, Size: 1968 bytes --]

[-- Attachment #1.2: 0003-Introduce-support-for-TOML-config-format.patch --]
[-- Type: application/octet-stream, Size: 5735 bytes --]

From 0cf13cd41ad1a73e3fab3ce87699b2d27e1b5564 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jostein=20Kj=C3=B8nigsen?= <jostein@kjonigsen.net>
Date: Sun, 11 Dec 2022 13:05:29 +0100
Subject: [PATCH 3/3] Introduce support for TOML config-format

This commit introduces support for the semi-popular TOML
config-format[1] through a new major-mode: toml-ts-mode.

I've read through the full spec[2], and from what I can see this
major-mode should provide correct syntax-highligting for every sort of
config-declaration which adheres to the specification.

Besides that it also adds support for basic tree-sitter based
navigation.

For the time being toml-ts-mode does NOT support imenu navigation,
but this is clearly something which can be added later.

[1] https://toml.io/en/
[2] https://toml.io/en/v1.0.0
---
 admin/notes/tree-sitter/build-module/batch.sh |   1 +
 lisp/progmodes/toml-ts-mode.el                | 115 ++++++++++++++++++
 2 files changed, 116 insertions(+)
 create mode 100644 lisp/progmodes/toml-ts-mode.el

diff --git a/admin/notes/tree-sitter/build-module/batch.sh b/admin/notes/tree-sitter/build-module/batch.sh
index d45f37f4b64..73914125a8b 100755
--- a/admin/notes/tree-sitter/build-module/batch.sh
+++ b/admin/notes/tree-sitter/build-module/batch.sh
@@ -13,6 +13,7 @@ languages=
     'rust'
     'typescript'
     'tsx'
+    'toml'
 )
 
 for language in "${languages[@]}"
diff --git a/lisp/progmodes/toml-ts-mode.el b/lisp/progmodes/toml-ts-mode.el
new file mode 100644
index 00000000000..dcc2ba6e472
--- /dev/null
+++ b/lisp/progmodes/toml-ts-mode.el
@@ -0,0 +1,115 @@
+;;; toml-ts-mode.el --- tree-sitter support for TOML  -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2022 Free Software Foundation, Inc.
+
+;; Author     : Jostein Kjønigsen <jostein@kjonigsen.net>
+;; Maintainer : Jostein Kjønigsen <jostein@kjonigsen.net>
+;; Created    : December 2022
+;; Keywords   : toml 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-child-by-field-name "treesit.c")
+
+(defvar toml-ts-mode--font-lock-settings
+      (treesit-font-lock-rules
+       :language 'toml
+       :feature 'constant
+       '((boolean) @font-lock-constant-face)
+       :language 'toml
+       :feature 'delimiter
+       '((["="]) @font-lock-delimiter-face)
+       :language 'toml
+       :feature 'number
+       '((integer) @font-lock-number-face
+         (float) @font-lock-number-face
+         (local_date) @font-lock-number-face
+         (local_date_time) @font-lock-number-face
+         (local_time) @font-lock-number-face)
+       :language 'toml
+       :feature 'string
+       '((string) @font-lock-string-face)
+       :language 'toml
+       :feature 'escape-sequence
+       :override t
+       '((escape_sequence) @font-lock-escape-face)
+       :language 'toml
+       :feature 'pair
+       :override t ; Needed for overriding string face on keys.
+       '((comment) @font-lock-comment-face
+         (bare_key) @font-lock-property-face
+         (quoted_key) @font-lock-property-face
+         (table ("[" @font-lock-bracket-face
+                 (_) @font-lock-type-face
+                 "]" @font-lock-bracket-face))
+         (table_array_element ("[[" @font-lock-bracket-face
+                               (_) @font-lock-type-face
+                               "]]" @font-lock-bracket-face))
+         (table (quoted_key) @font-lock-type-face)
+         (table (dotted_key (quoted_key)) @font-lock-type-face))
+       :language 'toml
+       :feature 'error
+       :override t
+       '((ERROR) @font-lock-warning-face))
+      "Font-lock settings for TOML.")
+
+;;;###autoload
+(add-to-list 'auto-mode-alist '("\\.toml\\'" . toml-ts-mode))
+
+;;;###autoload
+(define-derived-mode toml-ts-mode prog-mode "TOML"
+  "Major mode for editing TOML, powered by tree-sitter."
+  :group 'toml-mode
+
+  (unless (treesit-ready-p 'toml)
+    (error "Tree-sitter for TOML isn't avilable"))
+
+  (treesit-parser-create 'toml)
+
+  ;; Comments
+  (setq-local comment-start "# ")
+  (setq-local commend-end "")
+
+  ;; Navigation.
+  (setq-local treesit-defun-type-regexp
+              (rx (or "table" "table_array_element")))
+
+  ;; Font-lock.
+  (setq-local treesit-font-lock-settings toml-ts-mode--font-lock-settings)
+  (setq-local treesit-font-lock-feature-list
+              '((constant number pair string)
+                (escape-sequence)
+                (delimiter error)))
+
+  ;; Imenu.
+  ;; (setq-local imenu-create-index-function #'toml-ts-mode--imenu)
+  ;; (setq-local which-func-functions nil) ;; Piggyback on imenu
+
+  (treesit-major-mode-setup))
+
+(provide 'toml-ts-mode)
+
+;;; toml-ts-mode.el ends here
-- 
2.37.2


[-- Attachment #1.3: Type: text/html, Size: 141 bytes --]

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

end of thread, other threads:[~2022-12-15 14:28 UTC | newest]

Thread overview: 36+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-11 13:28 toml-ts-mode: first draft Jostein Kjønigsen
2022-12-11 17:09 ` Juri Linkov
2022-12-11 17:23   ` Jostein Kjønigsen
2022-12-11 17:40   ` Eli Zaretskii
2022-12-11 18:19     ` Stefan Kangas
2022-12-11 18:23       ` Eli Zaretskii
2022-12-11 21:43         ` Stefan Kangas
2022-12-12  3:28           ` Eli Zaretskii
2022-12-12 17:04       ` Juri Linkov
2022-12-11 19:56     ` Jostein Kjønigsen
2022-12-11 20:07       ` Eli Zaretskii
2022-12-11 20:31         ` Jostein Kjønigsen
2022-12-11 20:38           ` Eli Zaretskii
2022-12-11 20:49             ` Jostein Kjønigsen
2022-12-11 23:01       ` Yuan Fu
2022-12-12 13:10         ` Jostein Kjønigsen
2022-12-12 13:53           ` Theodor Thornhill
2022-12-12 20:41         ` Jostein Kjønigsen
2022-12-12 21:17           ` Randy Taylor
2022-12-13 20:43             ` Jostein Kjønigsen
2022-12-13 22:37               ` Randy Taylor
2022-12-14  8:40                 ` Jostein Kjønigsen
2022-12-14 13:24                   ` Randy Taylor
2022-12-14 18:53                     ` toml-ts-mode (code-review done) Jostein Kjønigsen
2022-12-14 19:02                       ` Theodor Thornhill
2022-12-14 20:37                         ` Yuan Fu
2022-12-14 22:02                           ` Jostein Kjønigsen
2022-12-15  2:24                             ` Randy Taylor
2022-12-15 12:52                               ` Jostein Kjønigsen
2022-12-15 13:22                                 ` Theodor Thornhill
2022-12-15 13:45                                   ` Jostein Kjønigsen
2022-12-15 14:22                                     ` Eli Zaretskii
2022-12-15 14:28                                       ` Jostein Kjønigsen
2022-12-13 10:45         ` toml-ts-mode: first draft Rudolf Schlatte
2022-12-13 13:20           ` Eli Zaretskii
2022-12-13 14:22             ` Rudi Schlatte

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).