-
Notifications
You must be signed in to change notification settings - Fork 1
/
eglot-luau.el
320 lines (277 loc) · 12.9 KB
/
eglot-luau.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
308
309
310
311
312
313
314
315
316
317
318
319
320
;;; eglot-luau.el --- Luau language server integration for eglot -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Kenneth Loeffler
;; Author: Kenneth Loeffler <kenloef@gmail.com>
;; Version: 0.2.0
;; Keywords: roblox, luau, tools
;; URL: https://github.com/kennethloeffler/eglot-luau
;; Package-Requires: ((emacs "29.1") (eglot "1.17"))
;; This file is not part of GNU Emacs.
;; This program 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.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides a luau-lsp
;; (https://github.com/JohnnyMorganz/luau-lsp) integration for eglot.
;;; Code:
(require 'eglot)
(require 'json)
;; Storage directory for type definition and doc resource files
(defconst eglot-luau-storage-directory (concat user-emacs-directory
"luau-lsp-storage/"))
;; URL providing Roblox API docs
(defconst eglot-luau-roblox-docs-url "https://raw.githubusercontent.com/MaximumADHD/Roblox-Client-Tracker/roblox/api-docs/en-us.json")
;; URL providing latest Roblox version
(defconst eglot-luau-roblox-version-url "https://raw.githubusercontent.com/CloneTrooper1019/Roblox-Client-Tracker/roblox/version.txt")
;; URL providing Roblox's current Luau FFlag configuration
(defconst eglot-luau-current-roblox-fflags-url
"https://clientsettingscdn.roblox.com/v1/settings/application?applicationName=PCDesktopClient")
;; User customization
(defgroup eglot-luau nil
"Customization for luau-lsp."
:prefix 'eglot-luau-
:group 'tools)
(defcustom eglot-luau-rojo-sourcemap-enabled
nil
"Whether to use Rojo to generate a sourcemap for projects."
:type 'boolean
:group 'eglot-luau)
(defcustom eglot-luau-rojo-sourcemap-includes-non-scripts
nil
"Whether to tell Rojo to include non-script instances in its sourcemap."
:type 'boolean
:group 'eglot-luau)
(defcustom eglot-luau-auto-update-roblox-types
nil
"Whether to automatically update Roblox types on server initialization."
:type 'boolean
:group 'eglot-luau)
(defcustom eglot-luau-auto-update-roblox-docs
nil
"Whether to automatically update Roblox API docs on server initialization."
:type 'boolean
:group 'eglot-luau)
(defcustom eglot-luau-roblox-security-level
"PluginSecurity"
"The security level to use for Roblox API type defintions."
:type 'string
:options '("None" "LocalUserSecurity" "PluginSecurity" "RobloxScriptSecurity")
:group 'eglot-luau)
(defcustom eglot-luau-rojo-project-path
"default.project.json"
"A relative path to the Rojo project to use for sourcemap generation."
:type 'string
:group 'eglot-luau)
(defcustom eglot-luau-fflags-enabled
t
"Whether the language server will have any FFlags enabled."
:type 'boolean
:group 'eglot-luau)
(defcustom eglot-luau-fflag-overrides
'()
"Custom set FFlags passed to the language server."
:type '(alist :key-type (string :tag "Name")
:value-type (string :tag "Value"))
:group 'eglot-luau)
(defcustom eglot-luau-custom-type-files
'()
"List of paths to type definition files to provide to the language server."
:type '(repeat string)
:group 'eglot-luau)
(defcustom eglot-luau-custom-doc-files
'()
"List of paths to doc files to provide to the language server."
:type '(repeat string)
:group 'eglot-luau)
(defcustom eglot-luau-server-executable
"luau-lsp"
"Path to the luau-lsp server executable."
:type 'string
:group 'eglot-luau)
(defcustom eglot-luau-sync-fflags
t
"Whether to sync Luau FFlags with Roblox's current configuration."
:type 'boolean
:group 'eglot-lua)
(defun eglot-luau--ensure-storage-directory ()
"Create luau-lsp storage folder if it doesn't exist."
(if (not (file-directory-p eglot-luau-storage-directory))
(make-directory eglot-luau-storage-directory)))
(defun eglot-luau--roblox-types-url ()
"Return a URL that responds with Roblox type information."
(format
"http://raw.githubusercontent.com/JohnnyMorganz/luau-lsp/main/scripts/globalTypes.%s.d.luau"
eglot-luau-roblox-security-level))
(defun eglot-luau--roblox-types-storage-uri ()
"Return where to store type definition files."
(expand-file-name (concat eglot-luau-storage-directory
(format "roblox-global-types-%s"
eglot-luau-roblox-security-level))))
(defun eglot-luau--roblox-docs-storage-uri ()
"Return where to store doc files."
(expand-file-name (concat eglot-luau-storage-directory "roblox-api-docs")))
(defun eglot-luau--roblox-version-storage-uri ()
"Return where to store version files."
(expand-file-name (concat eglot-luau-storage-directory "roblox-version")))
(defun eglot-luau--which-files-need-update ()
"Return a list of bools that indicate which Roblox resources should be updated.
Each bool in the list indicates whether the types or docs file
need updates, respectively."
(list (and eglot-luau-auto-update-roblox-types
(not (file-exists-p (eglot-luau--roblox-types-storage-uri))))
(and eglot-luau-auto-update-roblox-docs
(not (file-exists-p (eglot-luau--roblox-docs-storage-uri))))))
(defun eglot-luau--is-outdated ()
"Compare versions of locally stored Roblox docs/types with the latest versions.
Return a list of bools that indicate whether the types and/or
docs files, respectively, need to be updated. Respects the
`eglot-luau-auto-update-roblox-types' and
`eglot-luau-auto-update-roblox-docs' settings."
(if (not (or eglot-luau-auto-update-roblox-types
eglot-luau-auto-update-roblox-docs))
;; Don't do anything if user doesn't want Roblox types or docs
'(nil nil)
(eglot-luau--ensure-storage-directory)
(with-temp-buffer
(url-insert-file-contents eglot-luau-roblox-version-url)
(let* ((version-file (eglot-luau--roblox-version-storage-uri))
(stored-version (if (file-exists-p version-file)
(with-temp-buffer (insert-file-contents version-file)
(buffer-string))
"")))
(write-file version-file)
(if (not (string= stored-version (buffer-string)))
(list eglot-luau-auto-update-roblox-types
eglot-luau-auto-update-roblox-docs)
(eglot-luau--which-files-need-update))))))
(defun eglot-luau--build-server-command-list ()
"Return a list of strings that used to spawn the luau-lsp server process."
(let ((command-list (list "lsp" eglot-luau-server-executable)))
(let ((types-uri (eglot-luau--roblox-types-storage-uri))
(docs-uri (eglot-luau--roblox-docs-storage-uri)))
(if (file-exists-p types-uri)
(push (format "--definitions=%s" types-uri) command-list))
(if (file-exists-p docs-uri)
(push (format "--docs=%s" docs-uri) command-list)))
(if eglot-luau-custom-type-files
(dolist (file eglot-luau-custom-type-files)
(push (format "--definitions=%s" file) command-list)))
(if eglot-luau-custom-doc-files
(dolist (file eglot-luau-custom-doc-files)
(push (format "--docs=%s" file) command-list)))
(if (not eglot-luau-fflags-enabled)
(push "--no-flags-enabled" command-list))
(if eglot-luau-fflag-overrides
(dolist (fflag eglot-luau-fflag-overrides)
(push (format "--flag:%s=%s" (car fflag) (cadr fflag)) command-list)))
(if eglot-luau-sync-fflags
(let ((fflags (cdar (with-temp-buffer
(with-demoted-errors
"Error while fetching Roblox FFlags: %s"
(url-insert-file-contents
eglot-luau-current-roblox-fflags-url)
(json-read))))))
(dolist (fflag fflags)
(let* ((name (symbol-name (car fflag)))
(trimmed-name (if (string-prefix-p "FFlagLuau" name)
(string-trim-left name "FFlag")
name))
(value (cdr fflag)))
(if (not (assoc trimmed-name eglot-luau-fflag-overrides))
(push (format "--flag:%s=%s" trimmed-name value)
command-list))))))
(nreverse command-list)))
(defun eglot-luau--build-rojo-command-list ()
"Return a list of strings that can be used to start the Rojo process."
(let ((command-list (list "--watch"
"sourcemap.json" "--output"
"sourcemap" "rojo")))
(push (expand-file-name eglot-luau-rojo-project-path)
command-list)
(if eglot-luau-rojo-sourcemap-includes-non-scripts
(push "--include-non-scripts" command-list))
(nreverse command-list)))
(defun eglot-luau--rojo-process-filter (_process output)
"Process filter that displays any errors during Rojo sourcemap generation.
If OUTPUT contains an error message, display the output in a pop-up buffer."
(if (string-match "error" output)
(with-output-to-temp-buffer (get-buffer-create
"*luau-lsp sourcemap error*")
(princ
(format
"eglot-luau attempted to generate a sourcemap using the following command:
%s
...but the command outputted an error:
%s"
(mapconcat #'identity (eglot-luau--build-rojo-command-list) " ")
output)))))
(defun eglot-luau--make-rojo-process (server &rest _)
"Handle the Rojo process for SERVER.
SERVER must support luau in `lua-mode' buffers. Fails when Rojo
is not installed, or when a file at
`eglot-luau-rojo-project-path' cannot be found."
(if-let ((is-sourcemap-enabled eglot-luau-rojo-sourcemap-enabled)
(is-luau-server (member '(lua-mode . "luau") (slot-value server 'languages)))
(is-rojo-installed (executable-find "rojo")))
(let* ((rojo-process (make-process
:name "luau-lsp-rojo-sourcemap"
:command (eglot-luau--build-rojo-command-list)
:filter #'eglot-luau--rojo-process-filter
:noquery t))
(advice-name (concat (process-name rojo-process)
"-shutdown-advice")))
;; eglot does not provide any hooks for server shutdown, so to
;; know when to kill the Rojo process, we have to advise
;; `eglot-shutdown' temporarily. This is a bit ugly and goes
;; against elisp code conventions, but it's the only way to
;; do it right now!
(advice-add
#'eglot-shutdown
:after (lambda (server-shutting-down &rest _)
(when (eq server server-shutting-down)
(if (process-live-p rojo-process)
(kill-process rojo-process))
(advice-remove #'eglot-shutdown advice-name)))
'((name . advice-name))))
(if (and is-sourcemap-enabled
is-luau-server
(not is-rojo-installed))
(with-output-to-temp-buffer (get-buffer-create
"*luau-lsp sourcemap error*")
(princ "eglot-luau-rojo-sourcemap-enabled is non-nil, but Rojo is not on the path")))))
;;;###autoload
(defun eglot-luau-setup ()
"Set up luau-lsp for use in `lua-mode' buffers.
If `eglot-luau-auto-update-roblox-types' and/or
`eglot-luau-auto-update-roblox-docs' are non-nil, attempt to
download latest Roblox type defintions and/or docs.
If `eglot-luau-rojo-sourcemap-enabled' is non-nil, attempt to
start a Rojo process to generate a sourcemap."
(pcase-let ((`(,types-need-update ,docs-need-update)
(with-demoted-errors
"Error while fetching Roblox version: %s"
(eglot-luau--is-outdated))))
(if types-need-update
(with-temp-buffer
(with-demoted-errors
"Error while updating Roblox global types: %s"
(url-insert-file-contents (eglot-luau--roblox-types-url))
(write-file (eglot-luau--roblox-types-storage-uri)))))
(if docs-need-update
(with-temp-buffer
(with-demoted-errors
"Error while updating Roblox docs: %s"
(url-insert-file-contents eglot-luau-roblox-docs-url)
(write-file (eglot-luau--roblox-docs-storage-uri))))))
(add-hook 'eglot-server-initialized-hook #'eglot-luau--make-rojo-process)
(add-to-list 'eglot-server-programs
`((lua-mode :language-id "luau")
. ,(eglot-luau--build-server-command-list))))
(provide 'eglot-luau)
;;; eglot-luau.el ends here