-
Notifications
You must be signed in to change notification settings - Fork 0
/
piglet-mode.el
214 lines (162 loc) · 6.94 KB
/
piglet-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
;;; piglet-mode.el --- Major mode for Piglet LISP -*- lexical-binding: t -*-
;; Author: Arne Brasseur <[email protected]>
;; Filename: piglet-mode.el
;;; Commentary:
;; Major mode and interactive development commands for Piglet. Based on
;; Tree-sitter, so you will need Emacs 29. Work in progress.
;;; Code:
(require 'treesit)
(require 'rainbow-delimiters)
(require 'aggressive-indent)
(require 'smartparens)
(when (not (fboundp 'treesit-language-available-p))
(read-key "WARNING: Emacs was not compiled with tree-sitter support (--with-tree-sitter), make sure libtree-sitter0 is installed before building."))
(add-to-list
'treesit-language-source-alist
'(piglet "https://github.com/piglet-lang/tree-sitter-piglet.git"))
(defvar piglet-ts--indent-rules
'((piglet
((parent-is "source") first-sibling 0)
((parent-is "list") first-sibling 2)
((parent-is "vector") first-sibling 1)
((parent-is "dict") first-sibling 1))))
(setq piglet-mode--font-lock-settings
(treesit-font-lock-rules
:feature 'brackets
:language 'piglet
'((_ (_ (_ (_ (_ (_ (_ (_ ["(" ")" "[" "]" "{" "}"] @rainbow-delimiters-depth-8-face)))))))))
:feature 'brackets
:language 'piglet
'((_ (_ (_ (_ (_ (_ (_ ["(" ")" "[" "]" "{" "}"] @rainbow-delimiters-depth-7-face))))))))
:feature 'brackets
:language 'piglet
'((_ (_ (_ (_ (_ (_ ["(" ")" "[" "]" "{" "}"] @rainbow-delimiters-depth-6-face)))))))
:feature 'brackets
:language 'piglet
'((_ (_ (_ (_ (_ ["(" ")" "[" "]" "{" "}"] @rainbow-delimiters-depth-5-face))))))
:feature 'brackets
:language 'piglet
'((_ (_ (_ (_ ["(" ")" "[" "]" "{" "}"] @rainbow-delimiters-depth-4-face)))))
:feature 'brackets
:language 'piglet
'((_ (_ (_ ["(" ")" "[" "]" "{" "}"] @rainbow-delimiters-depth-3-face))))
:feature 'brackets
:language 'piglet
'((_ (_ ["(" ")" "[" "]" "{" "}"] @rainbow-delimiters-depth-2-face)))
:feature 'identifiers
:language 'piglet
'((list :anchor (symbol) @font-lock-function-call-face))
:feature 'identifiers
:language 'piglet
'((symbol) @default)
:feature 'identifiers
:language 'piglet
'((keyword) @font-lock-constant-face)
:feature 'identifiers
:language 'piglet
'((prefix_name) @font-lock-constant-face)
;; :feature 'built-ins
;; :language 'piglet
;; '((symbol) @font-lock-keyword-face
;; (:equal @font-lock-keyword-face "defn"))
;; :feature 'identifiers
;; :language 'piglet
;; '((qname) @font-lock-constant-face)
:feature 'numbers
:language 'piglet
'((number) @font-lock-number-face)
:feature 'strings
:language 'piglet
'((string) @font-lock-string-face)
:feature 'comments
:language 'piglet
'((comment) @font-lock-comment-face)
))
;; (treesit-query-capture 'piglet
;; '((symbol) @font-lock-keyword-face
;; (:equal @font-lock-keyword-face "defn"))
;; )
(setq piglet-mode--font-features '((brackets identifiers strings numbers built-ins comments)))
(defun piglet-setup-font-lock ()
(interactive)
(setq-local treesit-font-lock-settings piglet-mode--font-lock-settings)
(setq-local treesit-font-lock-feature-list piglet-mode--font-features)
(treesit-major-mode-setup))
(sp-local-pair 'piglet-mode "'" nil :actions nil)
(sp-local-pair 'piglet-mode "`" nil :actions nil)
(setq piglet--find-package-name-query
(treesit-query-compile
'piglet
"(source
(dict
(prefix_name) @prefix . (symbol) @sym)
(#equal @prefix \":pkg:name\"))"))
(setq piglet--find-module-name-query
(treesit-query-compile
'piglet
"(source .
(list
(symbol) @list-head . (symbol) @name)
(#equal @list-head \"module\"))"))
(defun piglet--package-name (file)
(let* ((pkg-root (locate-dominating-file file "package.pig")))
(if (not pkg-root)
(concat "file://" (directory-file-name (file-name-directory file)))
(let ((pkg-pig (expand-file-name "package.pig" pkg-root)))
(with-temp-buffer
(insert-file-contents pkg-pig)
(treesit-parser-create 'piglet)
(let ((node (cdr (assoc 'sym (treesit-query-capture 'piglet piglet--find-package-name-query)))))
(if node (treesit-node-text node) (concat "file://" (directory-file-name (expand-file-name pkg-root))))))))))
(defun piglet--module-name ()
(treesit-node-text
(cdr (assoc 'name (treesit-query-capture 'piglet piglet--find-module-name-query)))))
(defun piglet-mode--is-def-sym-p (node)
(and (equal "symbol" (treesit-node-type node))
(equal "list" (treesit-node-type (treesit-node-parent node)))
(member (treesit-node-text (treesit-node-child (treesit-node-parent node) 1))
'("def" "defn" "defmacro"))
(equal node (treesit-node-child (treesit-node-parent node) 2))))
(define-derived-mode piglet-mode prog-mode "Piglet"
"Major mode for editing Piglet files."
(unless (treesit-ready-p 'piglet)
(if (yes-or-no-p "Tree-sitter grammar for Piglet not found. Install it now? ")
(treesit-install-language-grammar 'piglet)
(error "Tree-sitter for Piglet isn't available")))
(aggressive-indent-mode)
(treesit-parser-create 'piglet)
;; Font-lock.
(setq-local treesit-font-lock-feature-list piglet-mode--font-features)
(setq-local treesit-font-lock-settings piglet-mode--font-lock-settings)
;; Comments
(setq-local comment-start ";; ")
(setq-local comment-start-skip ";+ *")
(setq-local comment-end "")
;; Electric
(setq-local electric-indent-chars
(append "[]{}()" electric-indent-chars))
;; Indent
(setq-local treesit-simple-indent-rules piglet-ts--indent-rules)
;; Imenu
(setq-local treesit-defun-name-function #'treesit-node-text)
(setq-local treesit-simple-imenu-settings
'((nil "\\`symbol\\'" piglet-mode--is-def-sym-p nil)))
;; Navigation
;; (setq-local treesit-defun-type-regexp ...)
;; (setq-local treesit-defun-name-function #'piglet-mode--defun-name)
;; Imenu.
;; (setq-local treesit-simple-imenu-settings '())
(treesit-major-mode-setup)
(setq-local piglet-package-name
(if buffer-file-name
(piglet--package-name buffer-file-name)
(piglet--package-name default-directory))))
(add-to-list 'auto-mode-alist '("\\.pig\\'" . piglet-mode)) ;; currently in use
(add-to-list 'auto-mode-alist '("\\.pigl\\'" . piglet-mode)) ;; we're planning to use this instead
(provide 'piglet-mode)
(when nil
(treesit--install-language-grammar-1 nil 'piglet "https://github.com/piglet-lang/tree-sitter-piglet")
(module-load (expand-file-name "tree-sitter/libtree-sitter-piglet.so" user-emacs-directory))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; piglet-mode.el ends here