Skip to content

Commit 675e9be

Browse files
dpsuttonbbatsov
authored andcommitted
Handle newlines between forms for inf-clojure-eval-buffer
This sends the contents of the buffer. However, newlines cause the prompt to be returned. You can see this with even a regular clojure repl: ```bash ❯❯❯ clojure Clojure 1.10.2 user=> user=> user=> ``` (note using `clj` has rlwrap which hides a bit of this so make sure to use `clojure`). But what we can do is make sure to transform ```clojure (defn foo [] ...) (defn bar [] ...) ``` into ```clojure (defn foo [] ...) (defn bar [] ...) ``` So that the newlines don't trigger more repl prompts. Real world usage below: Before: ```clojure parse=> nil parse=> parse=> nil parse=> parse=> #'parse/data parse=> parse=> #'parse/parse-where parse=> parse=> #'parse/keywords parse=> parse=> #'parse/tokenize parse=> parse=> #'parse/parse parse=> parse=> #'parse/translate-where parse=> parse=> nil parse=> parse=> #'parse/query parse=> parse=> #'parse/query-test parse=> parse=> #'parse/bonus-points-test parse=> ``` After: ```clojure user=> nil parse=> nil parse=> #'parse/data parse=> #'parse/parse-where parse=> #'parse/keywords parse=> parse=> #'parse/parse parse=> #'parse/translate-where parse=> nil parse=> #'parse/query parse=> #'parse/query-test parse=> #'parse/bonus-points-test parse=> ```
1 parent b4193fc commit 675e9be

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

inf-clojure.el

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -683,16 +683,40 @@ HOST is the host the process is running on, PORT is where it's listening."
683683
(interactive "shost: \nnport: ")
684684
(inf-clojure (cons host port)))
685685

686+
(defun inf-clojure--forms-without-newlines (str)
687+
"Remove newlines between toplevel forms.
688+
STR is a string of contents to be evaluated. When sending
689+
multiple forms to a socket repl, each newline triggers a prompt.
690+
So we replace all newlines between top level forms but not inside
691+
of forms."
692+
(condition-case nil
693+
(with-temp-buffer
694+
(progn
695+
(clojurec-mode)
696+
(insert str)
697+
(whitespace-cleanup)
698+
(goto-char (point-min))
699+
(while (not (eobp))
700+
(while (looking-at "\n")
701+
(delete-char 1))
702+
(unless (eobp)
703+
(clojure-forward-logical-sexp))
704+
(unless (eobp)
705+
(forward-char)))
706+
(buffer-substring-no-properties (point-min) (point-max))))
707+
(scan-error str)))
708+
686709
(defun inf-clojure-eval-region (start end &optional and-go)
687710
"Send the current region to the inferior Clojure process.
688711
Sends substring between START and END. Prefix argument AND-GO
689712
means switch to the Clojure buffer afterwards."
690713
(interactive "r\nP")
691-
;; drops newlines at the end of the region
692-
(let ((str (replace-regexp-in-string
693-
"[\n]+\\'" ""
694-
(buffer-substring-no-properties start end))))
695-
(inf-clojure--send-string (inf-clojure-proc) str))
714+
(let* ((str (buffer-substring-no-properties start end))
715+
;; newlines over a socket repl between top level forms cause
716+
;; a prompt to be returned. so here we dump the region into a
717+
;; temp buffer, and delete all newlines between the forms
718+
(formatted (inf-clojure--forms-without-newlines str)))
719+
(inf-clojure--send-string (inf-clojure-proc) formatted))
696720
(when and-go (inf-clojure-switch-to-repl t)))
697721

698722
(defun inf-clojure-eval-string (code)

test/inf-clojure-tests.el

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,35 @@
119119
(it "only removes whitespace at the end of the command - fix 152"
120120
(expect (inf-clojure--sanitize-command "1 5") :to-equal "1 5\n")))
121121

122+
(describe "inf-clojure--forms-without-newlines"
123+
(it "removes newlines between toplevel forms"
124+
(expect (inf-clojure--forms-without-newlines
125+
"(def foo 3)\n\n\n(def bar 4)")
126+
:to-equal "(def foo 3)\n(def bar 4)"))
127+
(it "doesn't remove newlines inside forms or strings"
128+
(expect (inf-clojure--forms-without-newlines
129+
"
130+
131+
(defn foo []
132+
133+
:foo)
134+
135+
136+
(def thing \"this
137+
138+
is a string\")
139+
140+
(defn bar [])")
141+
;; note no leading newline, newlines inside defn remain,
142+
;; newlines inside string remain
143+
:to-equal "(defn foo []
144+
145+
:foo)
146+
(def thing \"this
147+
148+
is a string\")
149+
(defn bar [])")))
150+
122151

123152
(describe "inf-clojure--update-feature"
124153
(it "updates new forms correctly"

0 commit comments

Comments
 (0)