Add some kdl stuff

This commit is contained in:
Alexander Rosenberg 2024-04-24 21:01:50 -07:00
parent ade60d10a1
commit 5bd69822d3
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730
2 changed files with 141 additions and 1 deletions

139
elisp/kdl-ts-mode.el Normal file
View File

@ -0,0 +1,139 @@
;;; kdl-ts-mode.el --- Tree-sitter support for KDL -*- lexical-binding: t; -*-
;; Copyright (C) 2024 dataphract
;; Homepage : https://github.com/dataphract/kdl-ts-mode
;; Version : 0.1
;; Author : dataphract <dataphract@gmail.com>
;; Maintainer : dataphract <dataphract@gmail.com>
;; Created : January 2024
;; Keywords : kdl languages tree-sitter
;; Package-Requires: ((emacs "29.1"))
;;; Commentary:
;; This package provides a major mode for editing KDL files, powered by tree-sitter.
;;; Code:
(require 'treesit)
(declare-function treesit-parser-create "treesit.c")
(defcustom kdl-ts-mode-indent-offset 2
"Number of spaces for each indentation step in `kdl-ts-mode'."
:version "29.1"
:type 'integer
:safe 'integerp
:group 'kdl)
(defvar kdl-ts-mode--syntax-table
(let ((table (make-syntax-table)))
(modify-syntax-entry ?= ".")
(modify-syntax-entry ?/ ". 124")
(modify-syntax-entry ?* ". 23b")
(modify-syntax-entry ?\n ">")
table)
"Syntax table for `kdl-ts-mode'.")
(defvar kdl-ts-mode--indent-rules
'((kdl
((parent-is "source_file") column-0 0)
((node-is "}") (and parent parent-bol) 0)
((parent-is "node_children") parent-bol kdl-ts-mode-indent-offset)))
"Tree-sitter indent rules for `kdl-ts-mode'.")
;; Syntax highlighting
(defvar kdl-ts-mode--font-lock-settings
(treesit-font-lock-rules
:language 'kdl
:feature 'bracket
'((["(" ")" "{" "}"]) @font-lock-bracket-face)
:language 'kdl
:feature 'comment
'((single_line_comment) @font-lock-comment-face
(multi_line_comment) @font-lock-comment-face)
:language 'kdl
:feature 'constant
'("null" @font-lock-constant-face
(boolean) @font-lock-constant-face)
:language 'kdl
:feature 'number
'((number) @font-lock-number-face)
:language 'kdl
:feature 'type
'((type) @font-lock-type-face)
:language 'kdl
:feature 'string
:override t
'((string) @font-lock-string-face)
:language 'kdl
:feature 'escape-sequence
:override t
'((escape) @font-lock-escape-face)
:language 'kdl
:feature 'node
:override t
'((node (identifier) @font-lock-function-call-face))
:language 'kdl
:feature 'property
:override t
'((prop (identifier) @font-lock-property-use-face))
:language 'kdl
:feature 'error
:override t
'((ERROR) @font-lock-warning-face)
:language 'kdl
:feature 'comment
:override t
'((node (node_comment)) @font-lock-comment-face
(node (node_field (node_field_comment)) @font-lock-comment-face)
(node_children (node_children_comment)) @font-lock-comment-face))
"Tree-sitter font-lock settings for `kdl-ts-mode'.")
;;;###autoload
(define-derived-mode kdl-ts-mode prog-mode "KDL"
"Major mode for editing KDL, powered by tree-sitter."
:group 'kdl
:syntax-table kdl-ts-mode--syntax-table
(when (treesit-ready-p 'kdl)
(treesit-parser-create 'kdl)
(setq-local comment-start "//")
(setq-local comment-end "")
(setq-local indent-tabs-mode nil
treesit-simple-indent-rules kdl-ts-mode--indent-rules)
(setq-local treesit-font-lock-settings kdl-ts-mode--font-lock-settings)
(setq-local treesit-font-lock-feature-list
'((comment)
(string type)
(constant escape-sequence number node property)
(bracket
;delimiter
error
;misc-punctuation
)))
(treesit-major-mode-setup)))
(if (treesit-ready-p 'kdl)
(add-to-list 'auto-mode-alist '("\\.kdl\\'" . kdl-ts-mode)))
(provide 'kdl-ts-mode)
;;; kdl-ts-mode.el ends here

View File

@ -131,7 +131,8 @@
(javascript "https://github.com/tree-sitter/tree-sitter-javascript") (javascript "https://github.com/tree-sitter/tree-sitter-javascript")
(bash "https://github.com/tree-sitter/tree-sitter-bash") (bash "https://github.com/tree-sitter/tree-sitter-bash")
(cmake "https://github.com/uyha/tree-sitter-cmake") (cmake "https://github.com/uyha/tree-sitter-cmake")
(blueprint "https://github.com/huanie/tree-sitter-blueprint"))) (blueprint "https://github.com/huanie/tree-sitter-blueprint")
(kdl "https://github.com/tree-sitter-grammars/tree-sitter-kdl")))
;; Tree sitter major mode conversions ;; Tree sitter major mode conversions
(setq major-mode-remap-alist (setq major-mode-remap-alist
'((c-mode . c-ts-mode) '((c-mode . c-ts-mode)