-
Notifications
You must be signed in to change notification settings - Fork 0
/
flix-mode.el
307 lines (264 loc) · 11.9 KB
/
flix-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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
;;; flix-mode.el --- Major mode for editing and interacting with flix source code -*- lexical-binding: t; -*-
;; Author: Cade Lueker
;; URL: https://github.com/CadeMichael/flix-mode
;; Version: 1.0.0
;; Keywords: languages
;;; Commentary:
;; Flix mode for syntax highlighting, interacting with `flix.jar', and flix repl
;; interopt. There is a flix-mode that has been archived, but provided all but one
;; line of the code used for syntax highlighting in this package. The repo cane be
;; found at https://github.com/jhckragh/flix-mode, and its author deserves props.
;; This package differs in that it allows users to interact with the `flix.jar' needed
;; in every flix project. It provides functions for downloading, intializing, running,
;; building, and passing commands to `flix.jar'.
;; Additionally this package provides basic repl interopt for the flix repl. Because
;; the flix repl reloads everytime a project file is saved and doesn't handle mutliline
;; input there is no send region function. However, you can send lines, restart, and
;; navigate between the flix buffer and flix repl buffer seamlessly.
;;; Code:
;; +-------------------------------------+
;; |Flix mode definition and highlighting|
;; +-------------------------------------+
(defconst flix-mode-keywords
'("alias" "and" "as" "case" "catch" "chan" "choose" "class" "def"
"default" "deref" "else" "enum" "exists" "false" "forall" "force"
"from" "get" "if" "import" "inline" "instance" "into" "lat" "law"
"lawless" "lazy" "let" "match" "matchEff" "mut" "namespace" "new"
"not" "opaque" "or" "override" "project" "pub" "query" "ref"
"reifyBool" "reifyEff" "reifyType" "rel" "scoped" "sealed" "select"
"set" "solve" "spawn" "true" "try" "type" "unlawful" "use" "where"
"with")
"Keywords recognized by `flix-mode'.")
(defvar flix-mode-font-lock-keywords
`(("\\_<Impure\\|null\\_>\\|\\?\\?\\?\\|\\?[_[:lower:]][_[:alnum:]]*" (0 font-lock-warning-face))
("\\_<Pure\\_>" (0 font-lock-function-name-face))
("\\_<\\(true\\|false\\)\\_>" (0 font-lock-builtin-face))
("\\<\\(\\sw+\\)(" (1 font-lock-function-name-face))
("let\\*?[ \t]+\\([_[:lower:]][_[:alnum:]]*\\)" (1 font-lock-variable-name-face))
("\\_<\\([_[:lower:]][_[:alnum:]]*\\)[ \t]*:[ \t_[:upper:]]" (1 font-lock-variable-name-face))
("\\_<\\([_[:upper:]][_[:alnum:]]*\\)\\_>" (0 font-lock-type-face))
("\\b[0-9]+\\b" (0 font-lock-constant-face))
(,(concat "\\_<" (regexp-opt flix-mode-keywords) "\\_>") (0 font-lock-keyword-face)))
"Keyword highlighting for `flix-mode'.")
(defvar flix-mode-syntax-table
(let ((st (make-syntax-table)))
(modify-syntax-entry ?\; "." st)
(modify-syntax-entry ?: "." st)
(modify-syntax-entry ?& "." st)
(modify-syntax-entry ?# "." st)
(modify-syntax-entry ?= "." st)
(modify-syntax-entry ?\" "\"" st)
(modify-syntax-entry ?\' "\"" st)
(modify-syntax-entry ?_ "w" st)
(modify-syntax-entry ?/ ". 124b" st)
(modify-syntax-entry ?* ". 23" st)
(modify-syntax-entry ?\n "> b" st)
st)
"Syntax table for `flix-mode'.")
(defcustom flix-mode-tab-width 4
"The tab width to use for indentation.")
(defun flix-mode--indent-further ()
(interactive)
(let ((new-indent (+ (current-indentation) flix-mode-tab-width)))
(if (> (current-column) (current-indentation))
(save-excursion (indent-line-to new-indent))
(indent-line-to new-indent))))
(defun flix-mode--indent-less ()
(interactive)
(save-excursion
(indent-line-to (max 0 (- (current-indentation) flix-mode-tab-width)))))
(defun flix-mode--newline-and-maybe-indent ()
(interactive)
(let ((indent-further (and (eolp) (looking-back "[{(=]")))
(prev-indent (current-indentation)))
(newline)
(if indent-further
(indent-line-to (+ prev-indent flix-mode-tab-width))
(indent-line-to prev-indent))))
(defvar flix-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-m" 'flix-mode--newline-and-maybe-indent)
(define-key map [return] 'flix-mode--newline-and-maybe-indent)
(define-key map "\C-j" 'flix-mode--newline-and-maybe-indent)
(define-key map [tab] 'flix-mode--indent-further)
(define-key map [backtab] 'flix-mode--indent-less)
map)
"Keymap for `flix-mode'.")
(require 'prog-mode)
;;;###autoload
(define-derived-mode flix-mode prog-mode "Flix"
"A major mode for editing Flix files."
:syntax-table flix-mode-syntax-table
(setq-local indent-tabs-mode nil)
(setq-local comment-start "//")
(setq-local comment-start-skip "\\(//+\\|/\\*+\\) *")
(setq-local comment-end "")
(setq-local font-lock-defaults '(flix-mode-font-lock-keywords))
(add-to-list 'electric-indent-chars ?\}))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.flix\\'" . flix-mode))
;; +-----------------------------+
;; |Flix repl based off of comint|
;; +-----------------------------+
(require 'comint)
(require 'ansi-color)
(defvar flix-jar-directory nil
"The directory from which to start the Flix REPL.")
(defvar flix-last-buffer nil
"The last flix buffer to call the repl.")
(defun +flix--repl-apply-ansi-color (string)
"Apply ANSI color codes to STRING."
(ansi-color-apply string))
(defun +flix--repl-stop-repl ()
"Stop the current Flix REPL process, if any."
(let ((buffer (get-buffer "*Flix REPL*")))
(when (and buffer (get-buffer-process buffer))
(kill-process (get-buffer-process buffer))
(kill-buffer buffer))))
(defun +flix--repl-start-repl ()
"Start a new Flix REPL process."
(let* ((default-directory (or flix-jar-directory (read-directory-name "Select directory with flix.jar: ")))
(buffer (get-buffer-create "*Flix REPL*")))
(unless (comint-check-proc buffer)
(with-current-buffer buffer
(setq flix-jar-directory default-directory)
(apply 'make-comint-in-buffer "Flix REPL" buffer "java" nil '("-jar" "flix.jar" "repl"))
(flix-repl-mode)))
(pop-to-buffer buffer)))
(defvar flix-repl-mode-map
(let ((map (nconc (make-sparse-keymap) comint-mode-map)))
(define-key map "\t" 'completion-at-point)
map)
"Basic mode map for `flix-repl-mode'.")
(defun +flix--repl-initialize ()
"Helper function to initialize `flix-repl'."
(setq comint-process-echoes t)
(setq comint-use-prompt-regexp t)
(add-hook 'comint-preoutput-filter-functions '+flix--repl-apply-ansi-color nil t))
(define-derived-mode flix-repl-mode comint-mode "Flix REPL"
"Major mode for `flix-repl'."
nil "Flix REPL"
(setq comint-prompt-regexp "flix> ")
(setq mode-line-process '(":%s"))
(setq comint-get-old-input (lambda () ""))
(setq comint-process-echoes t)
(setq comint-use-prompt-regexp t)
(set (make-local-variable 'paragraph-separate) "\\'")
(set (make-local-variable 'paragraph-start) comint-prompt-regexp)
(set (make-local-variable 'font-lock-defaults) '(nil t))
(set (make-local-variable 'comint-input-filter)
(lambda (str) (not (string-match "\\`\\s-*\\'" str))))
(set (make-local-variable 'comint-output-filter-functions)
(list 'comint-postoutput-scroll-to-bottom)))
(add-hook 'flix-repl-mode-hook '+flix--repl-initialize)
;; +-----------------------------------------+
;; |Functions for interacting with `flix.jar'|
;; +-----------------------------------------+
;;;###autoload
(defun +flix/install-jar ()
"Install flix.jar in a selected directory if it is not already present."
(interactive)
(let* ((default-directory (or flix-jar-directory default-directory))
(selected-dir (read-directory-name "Install into directory: " default-directory))
(jar-path (concat (file-name-as-directory selected-dir) "flix.jar"))
(download-url "https://github.com/flix/flix/releases/latest/download/flix.jar"))
(if (file-exists-p jar-path)
(message "flix.jar is already installed.")
(url-copy-file download-url jar-path t)
(message "flix.jar has been downloaded and installed.")
(setq flix-jar-directory selected-dir))))
;;;###autoload
(defun +flix/flix-command ()
"Run a command with the flix.jar in the specified directory."
(interactive)
(let* ((default-directory (or flix-jar-directory default-directory))
(selected-dir (read-directory-name "init project in: " default-directory))
(command (read-string "Command: "))
(jar-path (concat (file-name-as-directory selected-dir) "flix.jar")))
(if (file-exists-p jar-path)
(let ((default-directory selected-dir))
(message (concat "Running flix " command "..."))
(setq flix-jar-directory selected-dir)
(async-shell-command (concat "java -jar flix.jar " command)))
(message "flix.jar not found in the selected directory."))))
;;;###autoload
(defun +flix/init-project ()
"Initialize a flix project in the selected directory if flix.jar is present."
(interactive)
(let* ((default-directory (or flix-jar-directory default-directory))
(selected-dir (read-directory-name "init project in: " default-directory))
(jar-path (concat (file-name-as-directory selected-dir) "flix.jar")))
(if (file-exists-p jar-path)
(let ((default-directory selected-dir))
(message "initializing flix project...")
(setq flix-jar-directory selected-dir)
(shell-command "java -jar flix.jar init"))
(message "flix.jar not found in the selected directory."))))
;;;###autoload
(defun +flix/run-project ()
"Initialize a Flix project in the selected directory if flix.jar is present."
(interactive)
(let* ((default-directory (or flix-jar-directory default-directory))
(selected-dir (read-directory-name "Flix exe: " default-directory))
(jar-path (concat (file-name-as-directory selected-dir) "flix.jar")))
(if (file-exists-p jar-path)
(let ((default-directory selected-dir))
(message "Running Flix project...")
(setq flix-jar-directory selected-dir)
(async-shell-command "java -jar flix.jar run"))
(message "flix.jar not found in the selected directory."))))
;;;###autoload
(defun +flix/build-project ()
"Initialize a Flix project in the selected directory if flix.jar is present."
(interactive)
(let* ((default-directory (or flix-jar-directory default-directory))
(selected-dir (read-directory-name "Flix exe: " default-directory))
(jar-path (concat (file-name-as-directory selected-dir) "flix.jar")))
(if (file-exists-p jar-path)
(let ((default-directory selected-dir))
(message "Building Flix project...")
(setq flix-jar-directory selected-dir)
(async-shell-command "java -jar flix.jar build"))
(message "flix.jar not found in the selected directory."))))
;;;###autoload
(defun +flix/repl ()
"Run an inferior instance of `flix' inside Emacs."
(interactive)
(setq flix-last-buffer (current-buffer))
(+flix--repl-start-repl))
;;;###autoload
(defun +flix/goto-flix-buffer ()
"Switch to buffer that called `flix-repl'."
(interactive)
(if (buffer-live-p (get-buffer flix-last-buffer))
(switch-to-buffer-other-window flix-last-buffer)
(message "Flix buffer deleted")))
;;;###autoload
(defun +flix/goto-repl ()
"Switch to active Flix REPL."
(interactive)
(setq flix-last-buffer (current-buffer))
(if (get-buffer "*Flix REPL*")
(switch-to-buffer-other-window "*Flix REPL*")
(+flix/repl)))
;;;###autoload
(defun +flix/repl-restart ()
"Restart the Flix REPL process."
(interactive)
(+flix--repl-stop-repl)
(+flix--repl-start-repl))
;;;###autoload
(defun +flix/set-jar-directory ()
"Set the directory where flix.jar is located."
(interactive)
(let ((selected-dir (read-directory-name "Flix exe: " default-directory)))
(setq flix-jar-directory selected-dir)))
;;;###autoload
(defun +flix/send-line-to-repl ()
"Send the current line to the Flix REPL."
(interactive)
(let ((line (thing-at-point 'line t)))
(comint-send-string (get-buffer-process "*Flix REPL*") line)
(comint-send-string (get-buffer-process "*Flix REPL*") "\n")))
(provide 'flix-mode)
;;; flix-mode.el ends here