-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinf-iex-parser.el
149 lines (127 loc) · 5.17 KB
/
inf-iex-parser.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
;;; inf-iex-parser.el --- inf-iex code buffer parser -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Shi Tianshu
;; Author: Shi Tianshu <doglooksgood@gmail.com
;; Keywords:
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Functions for parsing code buffer.
;;; Code:
(require 'subr-x)
(require 'cl-lib)
(require 'dash)
(defun inf-iex--in-comment-p ()
"Returns non-nil if inside comment, else nil.
Result depends on syntax table's comment character."
(interactive)
(nth 4 (syntax-ppss)))
(defun inf-iex--exp-end-p ()
"If we are at the end of expression."
(and (zerop (car (syntax-ppss)))
(save-mark-and-excursion
(while (eq (char-before) 32)
(backward-char 1))
(not (looking-back "[,:]" 10)))))
(defun inf-iex--bound-of-current-exp ()
"Return the bounds of current expression."
(let ((beg (save-mark-and-excursion (back-to-indentation) (point)))
end)
(save-mark-and-excursion
(goto-char (line-end-position))
(while (not (inf-iex--exp-end-p))
(forward-line 1)
(goto-char (line-end-position)))
(setq end (point)))
(cons beg end)))
(defun inf-iex--remove-text-properties (text)
"Remove properties for TEXT."
(set-text-properties 0 (length text) nil text)
text)
(defun inf-iex--replace-module-constant (mod code)
"Replace __MODULE__ with current module name."
(replace-regexp-in-string "__MODULE__" mod code t t))
(defun inf-iex--parse-alias (mod &optional buf)
"Extract alias in BUF, consider module name is MOD."
(let ((buf (or buf (current-buffer)))
(result ())
(case-fold-search nil))
(with-current-buffer buf
(save-mark-and-excursion
(goto-char (point-min))
(while (re-search-forward "\\_<alias\\_>" nil t)
(unless (inf-iex--in-comment-p)
(let* ((bound (inf-iex--bound-of-current-exp))
(code (->> (buffer-substring-no-properties (car bound) (cdr bound))
(inf-iex--replace-module-constant mod))))
(push code result))))))
result))
(defun inf-iex--parse-import (mod &optional buf)
"Extract imports in BUF, consider module name is MOD."
(let ((buf (or buf (current-buffer)))
(result ())
(case-fold-search nil))
(with-current-buffer buf
(save-mark-and-excursion
(goto-char (point-min))
(while (re-search-forward "\\_<import\\_>" nil t)
(unless (inf-iex--in-comment-p)
(let* ((bound (inf-iex--bound-of-current-exp))
(code (->> (buffer-substring-no-properties (car bound) (cdr bound))
(inf-iex--replace-module-constant mod))))
(push code result))))))
result))
(defun inf-iex--parse-requires (mod &optional buf)
"Extract requires in BUF, consider module name is MOD."
(let ((buf (or buf (current-buffer)))
(result ())
(case-fold-search nil))
(with-current-buffer buf
(save-mark-and-excursion
(goto-char (point-min))
(while (re-search-forward "\\_<require\\_>" nil t)
(unless (inf-iex--in-comment-p)
(let* ((bound (inf-iex--bound-of-current-exp))
(code (->> (buffer-substring-no-properties (car bound) (cdr bound))
(inf-iex--replace-module-constant mod))))
(push code result))))))
result))
(defun inf-iex--format-eval-code (code)
"Format the CODE for sending to IEx.
Remove # and iex> for those code in comment.
Wrap with parenthese to support multiple lines.
"
(let* ((lines (split-string code "\n"))
(lines (mapcar (lambda (s) (string-trim s)) lines)))
(->> (string-join lines "\n")
(string-remove-prefix "# ")
(replace-regexp-in-string "^ *iex> *" "")
(replace-regexp-in-string "^ *#" "")
(replace-regexp-in-string "\n#" "\n")
(format "(%s)"))))
(defun inf-iex--make-setup-code (mod respawn &optional buf)
"Make the setup code for BUF or current buffer.
Arguments:
MOD - current module name.
RESPAWN - If we should respawn a new session.
"
(let* ((imports (inf-iex--parse-import mod buf))
(imports (if mod (cons (format "import %s" mod) imports) imports))
(aliases (inf-iex--parse-alias mod buf))
(requires (inf-iex--parse-requires mod buf)))
(concat (if respawn
"respawn\n"
"")
(format "%s\n%s\n%s"
(string-join aliases "\n")
(string-join requires "\n")
(string-join imports "\n")))))
(provide 'inf-iex-parser)
;;; inf-iex-parser.el ends here