Skip to content

Commit c3217c3

Browse files
arichiardibbatsov
authored andcommitted
Skip sanitation of comments
1 parent f420c8a commit c3217c3

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

inf-clojure.el

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -305,27 +305,29 @@ It requires a REPL PROC for inspecting the correct type."
305305
(setq-local inf-clojure-repl-type repl-type))
306306
inf-clojure-repl-type))
307307

308-
(defun inf-clojure--single-linify (string)
308+
(defun inf-clojure--whole-comment-line-p (string)
309+
"Return true iff STRING is a whole line semicolon comment."
310+
(string-match-p "^\s*;" string))
311+
312+
(defun inf-clojure--make-single-line (string)
309313
"Convert a multi-line STRING in a single-line STRING.
310-
It also reduces redundant whitespace for readability."
311-
(thread-last string
312-
(replace-regexp-in-string "[ \\|\n]+" " ")
313-
(replace-regexp-in-string " $" "")))
314-
315-
(defun inf-clojure--trim-newline-right (string)
316-
"Trim newlines (only) in STRING."
317-
(if (string-match "\n+\\'" string)
318-
(replace-match "" t t string)
319-
string))
314+
It also reduces redundant whitespace for readability and removes
315+
comments."
316+
(let* ((lines (seq-filter (lambda (s) (not (inf-clojure--whole-comment-line-p s)))
317+
(split-string string "[\r\n]" t))))
318+
(mapconcat (lambda (s)
319+
(if (not (string-match-p ";" s))
320+
(replace-regexp-in-string "\s+" " " s)
321+
(concat s "\n")))
322+
lines " ")))
320323

321324
(defun inf-clojure--sanitize-command (command)
322325
"Sanitize COMMAND for sending it to a process.
323326
An example of things that this function does is to add a final
324327
newline at the end of the form. Return an empty string if the
325328
sanitized command is empty."
326-
(let* ((linified (inf-clojure--single-linify command))
327-
(sanitized (inf-clojure--trim-newline-right linified)))
328-
(if (or (string-blank-p linified) (string-blank-p sanitized))
329+
(let ((sanitized (inf-clojure--make-single-line command)))
330+
(if (string-blank-p sanitized)
329331
""
330332
(concat sanitized "\n"))))
331333

test/inf-clojure-tests.el

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,23 @@
111111
(expect (ict-bounds-string (inf-clojure-completion-bounds-of-expr-at-point))
112112
:to-equal "deref")))))
113113

114-
(describe "inf-clojure--single-linify"
114+
(describe "inf-clojure--make-single-line"
115115
(it "replaces newlines with whitespace"
116-
(expect (inf-clojure--single-linify "(do\n(println \"hello world\")\n)") :to-equal "(do (println \"hello world\") )"))
116+
(expect (inf-clojure--make-single-line "(do\n(println \"hello world\")\n)") :to-equal "(do (println \"hello world\") )"))
117117

118118
(it "does not leave whitespace at the end"
119-
(expect (inf-clojure--single-linify "(do\n(println \"hello world\")\n)\n\n") :to-equal "(do (println \"hello world\") )"))
119+
(expect (inf-clojure--make-single-line "(do\n(println \"hello world\")\n)\n\n") :to-equal "(do (println \"hello world\") )"))
120120

121-
(it "returns empty string in case of only newline"
122-
(expect (inf-clojure--single-linify "\n\n\n\n") :to-equal "")))
121+
(it "returns empty string when the line is only newlines"
122+
(expect (inf-clojure--make-single-line "\n\n\n\n") :to-equal ""))
123+
124+
(it "removes comments when on their own line"
125+
(expect (inf-clojure--make-single-line "(do\n(println \"hello world\")\n ;; remove me\n)") :to-equal "(do (println \"hello world\") )"))
126+
127+
(it "preserves newlines of inline comments"
128+
(expect (inf-clojure--make-single-line "(do\n(println \"hello world\") ;; don't remove this\n)") :to-equal "(do (println \"hello world\") ;; don't remove this\n )"))
129+
130+
)
123131

124132
(describe "inf-clojure--sanitize-command"
125133
(it "sanitizes the command correctly"

0 commit comments

Comments
 (0)