diff --git a/CHANGELOG.md b/CHANGELOG.md index 31e1fa2..6701933 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/inf-clojure.el b/inf-clojure.el index 385b149..51ecfee 100644 --- a/inf-clojure.el +++ b/inf-clojure.el @@ -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." @@ -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. @@ -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 @@ -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)) diff --git a/test/inf-clojure-tests.el b/test/inf-clojure-tests.el index 994801c..6147dd3 100644 --- a/test/inf-clojure-tests.el +++ b/test/inf-clojure-tests.el @@ -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