Skip to content

Improve command sanitation code #135

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
Feb 24, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master (unreleased)

* [#135](https://github.com/clojure-emacs/inf-clojure/pull/135): Improve command sanitation code.

## 2.1.0 (2018-01-02)

### New Features
Expand Down
16 changes: 10 additions & 6 deletions inf-clojure.el
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,10 @@ It requires a REPL PROC for inspecting the correct type."

(defun inf-clojure--single-linify (string)
"Convert a multi-line STRING in a single-line STRING.
It also reduces/adds redundant whitespace for readability. Note
that this function will transform the empty string in \" \" (it
adds an empty space)."
(replace-regexp-in-string "[ \\|\n]+" " " 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."
Expand Down Expand Up @@ -334,7 +334,10 @@ always be preferred over `comint-send-string`. It delegates to
the string for evaluation. Refer to `comint-simple-send` for
customizations."
(inf-clojure--set-repl-type proc)
(comint-simple-send proc string))
(let ((sanitized (inf-clojure--sanitize-command string)))
(when (not (string-empty-p 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 Expand Up @@ -555,6 +558,7 @@ to continue it."

(defun inf-clojure-preoutput-filter (str)
"Preprocess the output STR from interactive commands."
(inf-clojure--log-string str "<-RES----")
(cond
((string-prefix-p "inf-clojure-" (symbol-name (or this-command last-command)))
;; Remove subprompts and prepend a newline to the output string
Expand Down Expand Up @@ -1173,7 +1177,7 @@ STRING if present."
(concat tag "\n")
(concat (prin1-to-string tag) "\n")))
(let ((print-escape-newlines t))
(prin1-to-string string)))
(prin1-to-string (substring-no-properties string))))
nil
(expand-file-name inf-clojure--log-file-name
(inf-clojure-project-root))
Expand Down
20 changes: 20 additions & 0 deletions test/inf-clojure-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,24 @@
(expect (ict-bounds-string (inf-clojure-completion-bounds-of-expr-at-point))
:to-equal "deref")))))

(describe "inf-clojure--single-linify"
(it "replaces newlines with whitespace"
(expect (inf-clojure--single-linify "(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\") )"))

(it "returns empty string in case of only newline"
(expect (inf-clojure--single-linify "\n\n\n\n") :to-equal "")))

(describe "inf-clojure--sanitize-command"
(it "sanitizes the command correctly"
(expect (inf-clojure--sanitize-command "(doc println)") :to-equal "(doc println)\n"))

(it "trims newline at the right of a command"
(expect (inf-clojure--sanitize-command "(doc println)\n\n\n\n") :to-equal "(doc println)\n"))

(it "returns empty string when the command is empty"
(expect (inf-clojure--sanitize-command " ") :to-equal "")))

;;; inf-clojure-tests.el ends here