Skip to content

Commit 85c3280

Browse files
committed
extract and test inf-clojure--forms-without-newlines
1 parent 89b80c0 commit 85c3280

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

inf-clojure.el

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,29 @@ 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
@@ -692,22 +715,7 @@ means switch to the Clojure buffer afterwards."
692715
;; newlines over a socket repl between top level forms cause
693716
;; a prompt to be returned. so here we dump the region into a
694717
;; temp buffer, and delete all newlines between the forms
695-
(formatted (condition-case nil
696-
(with-temp-buffer
697-
(progn
698-
(clojurec-mode)
699-
(insert str)
700-
(whitespace-cleanup)
701-
(goto-char (point-min))
702-
(while (not (eobp))
703-
(while (looking-at "\n")
704-
(delete-char 1))
705-
(unless (eobp)
706-
(clojure-forward-logical-sexp))
707-
(unless (eobp)
708-
(forward-char)))
709-
(buffer-substring-no-properties (point-min) (point-max))))
710-
(scan-error str))))
718+
(formatted (inf-clojure--forms-without-newlines str)))
711719
(inf-clojure--send-string (inf-clojure-proc) formatted))
712720
(when and-go (inf-clojure-switch-to-repl t)))
713721

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)