-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscala-cli-repl.el
284 lines (240 loc) · 10.1 KB
/
scala-cli-repl.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
;;; scala-cli-repl.el --- Scala CLI REPL in term mode.
;; Copyright (C) 2023 Andrea
;; Author: ag91 <andrea-dev@hotmail.com>
;; URL: https://github.com/ag91/scala-cli-repl
;; Package-Requires: ((emacs "24.3") (s "1.12.0") (scala-mode "0.23"))
;; Version: 0.1
;; Keywords: processes, scala-cli, term, scala
;;; License:
;; 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, 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Usage
;; (add-hook 'scala-mode-hook
;; (lambda ()
;; (scala-cli-repl-minor-mode t)))
;;; Code:
(require 'term)
(require 'comint)
(require 'scala-mode)
(require 's)
(require 'subr-x)
(defgroup scala-cli-repl nil
"A minor mode for a Scala-Cli REPL."
:group 'scala)
(defcustom scala-cli-repl-buffer-name "*Scala-Cli*"
"Buffer name for scala-cli."
:type 'string
:group 'scala-cli-repl)
(defcustom scala-cli-repl-program (executable-find "scala-cli")
"Program name for scala-cli."
:type 'string
:group 'scala-cli-repl)
(defcustom scala-cli-repl-program-args '()
"Arguments for scala-cli command."
:type '(repeat string)
:group 'scala-cli-repl)
(defcustom scala-cli-repl-prompt-regex "^scala> "
"Regex for scala-cli prompt."
:type 'string
:group 'scala-cli-repl)
(defcustom scala-cli-repl-run-hook nil
"Hook to run after starting an Scala-Cli REPL buffer."
:type 'hook
:group 'scala-cli-repl)
(defcustom scala-cli-repl-use-closure-for-eval t
"When set it sends your Scala code wrapped in brackets (e.g.,
{code}), which makes sure that your definitions don't alter the
outer scope of the console.
It may be that this affects semantics
(e.g., I got a 'error: can't existentially abstract over
parameterized type X' due to the closure, so this lets the user
to work around that."
:type 'hook
:group 'scala-cli-repl)
(defcustom scala-cli-load-repl-in-sbt-context nil
"Load REPL with Sbt project classpath of file."
:type 'boolean
:group 'scala-cli-repl)
(defvar scala-cli-repl-program-local-args '()
"Local args for scala-cli term repl program.")
(defun scala-cli-repl-get-process ()
"Return the active process associated with Scala CLI buffer."
(get-buffer-process scala-cli-repl-buffer-name))
(defun scala-cli-repl-get-buffer ()
"Return the current Scala CLI buffer."
(get-buffer scala-cli-repl-buffer-name))
(defun scala-cli-repl-is-alive? ()
"Return nil if there is no alive process."
(comint-check-proc scala-cli-repl-buffer-name))
(defun scala-cli-repl-check-process ()
"Check if there is an active scala-cli process."
(unless (scala-cli-repl-is-alive?) (error "Scala-Cli is not running")))
(defun scala-cli-repl-code-first-line (code)
"Get the first line of CODE."
(s-trim (car-safe (s-split "\n" code))))
(defun scala-cli--sbt-project-p (&optional dir)
"Return DIR if there is a build.sbt file in the project tree."
(let* ((dir (or dir (expand-file-name ".")))
(sbt-p (seq-filter (lambda (e) (equal e "build.sbt")) (directory-files dir)))
(next-dir (file-name-directory (directory-file-name (expand-file-name dir)))))
(if (or (equal next-dir dir) sbt-p)
(and sbt-p dir)
(scala-cli--sbt-project-p next-dir))))
(defun scala-cli--sbt-fullclasspath (dir)
"Extract classpath from sbt as a string in DIR."
(message "extracting sbt fullClasspath for %s... " dir)
(thread-last (shell-command-to-string (format "cd %s; sbt \"show fullClasspath\"" dir))
(s-replace-all '((")" . "") ("[info]" . "")))
(s-split "* Attributed(")
cdr
(seq-map (lambda (it) (s-trim (car (s-split "\n" it)))))
(s-join ":")))
;;;###autoload
(defun scala-cli-repl-send-defun ()
"Send the definition to the scala-cli buffer."
(interactive)
(scala-cli-repl-check-process)
(save-mark-and-excursion
(let (start end)
(scala-syntax:beginning-of-definition)
(setq start (point))
(scala-syntax:end-of-definition)
(setq end (point))
(let ((code (buffer-substring-no-properties start end)))
(comint-send-string scala-cli-repl-buffer-name code)
(comint-send-string scala-cli-repl-buffer-name "\n")
(message
(format "Sent: %s..." (scala-cli-repl-code-first-line code)))))))
;;;###autoload
(defun scala-cli-repl-send-region (start end)
"Send the region to the scala-cli buffer.
Argument START the start region.
Argument END the end region."
(interactive "r")
(scala-cli-repl-check-process)
(let ((code (buffer-substring-no-properties start end)))
(when scala-cli-repl-use-closure-for-eval (comint-send-string scala-cli-repl-buffer-name "{\n"))
(comint-send-string scala-cli-repl-buffer-name code)
(when scala-cli-repl-use-closure-for-eval(comint-send-string scala-cli-repl-buffer-name "\n}"))
(comint-send-string scala-cli-repl-buffer-name "\n")
(message
(format "Sent: %s..." (scala-cli-repl-code-first-line code)))))
;;;###autoload
(defun scala-cli-repl-send-buffer ()
"Send the buffer to the scala-cli buffer."
(interactive)
(save-mark-and-excursion
(goto-char (point-min))
(re-search-forward "^package .+\n+" nil t)
(scala-cli-repl-send-region (point) (point-max))))
;;;###autoload
(defun scala-cli-repl-load-file (file-name)
"Load a file to the scala-cli buffer.
Argument FILE-NAME the file name."
(interactive (comint-get-source "Load Scala file: " nil '(scala-mode) t))
(comint-check-source file-name)
(with-temp-buffer
(insert-file-contents file-name)
(scala-cli-repl-send-buffer)))
;;;###autoload
(defun scala-cli-repl ()
"Run an Scala-Cli REPL."
(interactive)
(unless (executable-find scala-cli-repl-program)
(error (format "%s is not found." scala-cli-repl-program)))
(unless (comint-check-proc scala-cli-repl-buffer-name)
(ignore-errors (kill-buffer scala-cli-repl-buffer-name))
(setq scala-cli-repl-program-local-args scala-cli-repl-program-args)
(when-let ((dir (and scala-cli-load-repl-in-sbt-context (scala-cli--sbt-project-p))))
(setq scala-cli-repl-program-local-args
(append scala-cli-repl-program-local-args
(list "--extra-jars" (scala-cli--sbt-fullclasspath dir)))))
(message (format "Run: %s %s"
scala-cli-repl-program
(s-join " " scala-cli-repl-program-local-args)))
(with-current-buffer
(apply 'term-ansi-make-term
scala-cli-repl-buffer-name
scala-cli-repl-program
nil
scala-cli-repl-program-local-args)
(term-char-mode)
(term-set-escape-char ?\C-x)
(setq-local term-prompt-regexp scala-cli-repl-prompt-regex)
(setq-local term-scroll-show-maximum-output t)
(setq-local term-scroll-to-bottom-on-output t)
(run-hooks 'scala-cli-repl-run-hook)))
(pop-to-buffer scala-cli-repl-buffer-name))
;;;###autoload
(defalias 'run-scala-cli 'scala-cli-repl)
(defun scala-cli-repl--send-string (string)
"Send the code to the scala-cli buffer.
Argument STRING the code to send."
(scala-cli-repl-check-process)
(when scala-cli-repl-use-closure-for-eval (comint-send-string scala-cli-repl-buffer-name "{\n"))
(comint-send-string scala-cli-repl-buffer-name string)
(when scala-cli-repl-use-closure-for-eval (comint-send-string scala-cli-repl-buffer-name "\n}"))
(comint-send-string scala-cli-repl-buffer-name "\n")
(message
(format "Sent: %s..." (scala-cli-repl-code-first-line string))))
;;;###autoload
(defun scala-cli-convert-sbt-dependencies (dependencies fn)
"Substitute sbt-style DEPENDENCIES with scala-cli ones, and apply FN to that."
(thread-last dependencies
(s-replace-all '((" " . "") ("\"" . "") ("%" . ":")))
(funcall fn)))
(defun scala-cli-convert-and-kill-deps ()
"Kill sbt dependency in Mill format."
(interactive)
(if (region-active-p)
(funcall 'scala-cli-convert-sbt-dependencies (substring-no-properties (funcall region-extract-function)) 'kill-new)
(error "You need to highlight the dependency in your region to use this")))
(defun scala-cli-repl-import-ivy-dependencies-from-sbt ()
"Try to import ivy dependencies from sbt file.
Currently only form like
libraryDependencies += \"com.typesafe.akka\" %% \"akka-actor\" % \"2.5.21\"
is available."
(interactive)
(when-let* ((file-name "build.sbt")
(path (locate-dominating-file default-directory file-name))
(file (concat path file-name)))
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-min))
(let ((regex "libraryDependencies[ ]+\\+=[ ]+\"\\(.+?\\)\"[ ]+%\\{1,2\\}[ ]+\"\\(.+?\\)\"[ ]+%\\{1,2\\}[ ]+\"\\(.+?\\)\"")
(res))
(while (re-search-forward regex nil t)
(add-to-list
'res
(format "import $ivy.`%s::%s:%s`" (match-string 1) (match-string 2) (match-string 3))
t))
(scala-cli-repl--send-string (s-join "\n" res))))))
(defvar scala-cli-repl-minor-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-`") 'scala-cli-repl)
(define-key map (kbd "C-c C-z") 'scala-cli-repl)
(define-key map (kbd "C-c C-e") 'scala-cli-repl-send-defun)
(define-key map (kbd "C-c C-r") 'scala-cli-repl-send-region)
(define-key map (kbd "C-c C-b") 'scala-cli-repl-send-buffer)
(define-key map (kbd "C-c C-l") 'scala-cli-repl-load-file)
map)
"Keymap while function ‘scala-cli-repl-minor-mode’ is active.")
;;;###autoload
(define-minor-mode scala-cli-repl-minor-mode
"Minor mode for interacting with an Scala-Cli REPL."
:keymap scala-cli-repl-minor-mode-map)
(provide 'scala-cli-repl)
;;; scala-cli-repl.el ends here