From 2c4b447e0985c43b134d44ce30b6c6ba738f0385 Mon Sep 17 00:00:00 2001 From: Andrea Richiardi Date: Fri, 23 Feb 2018 19:45:47 -0800 Subject: [PATCH 1/2] Skip sanitation of comments --- inf-clojure.el | 30 ++++++++++++++++-------------- test/inf-clojure-tests.el | 18 +++++++++++++----- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/inf-clojure.el b/inf-clojure.el index c44694b..de64f39 100644 --- a/inf-clojure.el +++ b/inf-clojure.el @@ -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")))) diff --git a/test/inf-clojure-tests.el b/test/inf-clojure-tests.el index 6147dd3..09b224e 100644 --- a/test/inf-clojure-tests.el +++ b/test/inf-clojure-tests.el @@ -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" From ca8f6828d2fc58eb34467e70205e5e862edffcbc Mon Sep 17 00:00:00 2001 From: Andrea Richiardi Date: Fri, 23 Feb 2018 19:46:49 -0800 Subject: [PATCH 2/2] Send string even when empty I fixed it once why not fixing it twice, see #120. --- inf-clojure.el | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/inf-clojure.el b/inf-clojure.el index de64f39..730cf86 100644 --- a/inf-clojure.el +++ b/inf-clojure.el @@ -341,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.