Skip to content

Sanitation should skip comments #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions inf-clojure.el
Original file line number Diff line number Diff line change
Expand Up @@ -305,27 +305,29 @@ It requires a REPL PROC for inspecting the correct type."
(setq-local inf-clojure-repl-type repl-type))
inf-clojure-repl-type))

(defun inf-clojure--single-linify (string)
(defun inf-clojure--whole-comment-line-p (string)
"Return true iff STRING is a whole line semicolon comment."
(string-match-p "^\s*;" string))

(defun inf-clojure--make-single-line (string)
"Convert a multi-line STRING in a single-line STRING.
It also reduces redundant whitespace for readability."
(thread-last string
(replace-regexp-in-string "[ \\|\n]+" " ")
(replace-regexp-in-string " $" "")))

(defun inf-clojure--trim-newline-right (string)
"Trim newlines (only) in STRING."
(if (string-match "\n+\\'" string)
(replace-match "" t t string)
string))
It also reduces redundant whitespace for readability and removes
comments."
(let* ((lines (seq-filter (lambda (s) (not (inf-clojure--whole-comment-line-p s)))
(split-string string "[\r\n]" t))))
(mapconcat (lambda (s)
(if (not (string-match-p ";" s))
(replace-regexp-in-string "\s+" " " s)
(concat s "\n")))
lines " ")))

(defun inf-clojure--sanitize-command (command)
"Sanitize COMMAND for sending it to a process.
An example of things that this function does is to add a final
newline at the end of the form. Return an empty string if the
sanitized command is empty."
(let* ((linified (inf-clojure--single-linify command))
(sanitized (inf-clojure--trim-newline-right linified)))
(if (or (string-blank-p linified) (string-blank-p sanitized))
(let ((sanitized (inf-clojure--make-single-line command)))
(if (string-blank-p sanitized)
""
(concat sanitized "\n"))))

Expand All @@ -339,9 +341,8 @@ the string for evaluation. Refer to `comint-simple-send` for
customizations."
(inf-clojure--set-repl-type proc)
(let ((sanitized (inf-clojure--sanitize-command string)))
(when (not (string-empty-p sanitized))
(inf-clojure--log-string sanitized "----CMD->")
(comint-simple-send proc sanitized))))
(inf-clojure--log-string sanitized "----CMD->")
(comint-simple-send proc sanitized)))

(defcustom inf-clojure-load-form "(clojure.core/load-file \"%s\")"
"Format-string for building a Clojure expression to load a file.
Expand Down
18 changes: 13 additions & 5 deletions test/inf-clojure-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,23 @@
(expect (ict-bounds-string (inf-clojure-completion-bounds-of-expr-at-point))
:to-equal "deref")))))

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

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

(it "returns empty string in case of only newline"
(expect (inf-clojure--single-linify "\n\n\n\n") :to-equal "")))
(it "returns empty string when the line is only newlines"
(expect (inf-clojure--make-single-line "\n\n\n\n") :to-equal ""))

(it "removes comments when on their own line"
(expect (inf-clojure--make-single-line "(do\n(println \"hello world\")\n ;; remove me\n)") :to-equal "(do (println \"hello world\") )"))

(it "preserves newlines of inline comments"
(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 )"))

)

(describe "inf-clojure--sanitize-command"
(it "sanitizes the command correctly"
Expand Down