From dfce0225434e5bd8aaf31edeed0019cd8a9b6550 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 14 Dec 2022 19:07:52 +0100 Subject: [PATCH] Fix #288: escape regex when searching for completions in REPL (#289) * Fix #288: escape regex when searching for completions in REPL --- CHANGELOG.md | 4 ++++ deps.edn | 2 +- script/nbb_nrepl_tests.clj | 15 +++++++++++++++ src/nbb/impl/repl_utils.cljs | 5 ++++- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3410b112..cf221d11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ For a list of breaking changes, check [here](#breaking-changes). [Nbb](https://github.com/babashka/nbb): Scripting in Clojure on Node.js using [SCI](https://github.com/babashka/sci) +## 1.1.152 + +- [#288](https://github.com/babashka/nbb/issues/288): escape regex when searching for completions in REPL + ## 1.1.151 (2022-12-01) - Add support for executing function using [babashka.cli](https://github.com/babashka/cli): diff --git a/deps.edn b/deps.edn index 3b7e2fd3..5f97c5b4 100644 --- a/deps.edn +++ b/deps.edn @@ -16,7 +16,7 @@ #_{:local/root "../babashka/sci"} #_{:mvn/version "0.3.5"} {:git/url "https://github.com/babashka/sci" - :git/sha "3d0a6e0ba050c288c5e5985423b77735eef9cf05"} + :git/sha "280865a9850d3befd5239bf42b3aa016230bc0c4"} org.clojure/tools.cli {:mvn/version "1.0.214"} com.cognitect/transit-cljs {:mvn/version "0.8.280"} #_#_prismatic/schema {:mvn/version "1.3.0"} diff --git a/script/nbb_nrepl_tests.clj b/script/nbb_nrepl_tests.clj index c455babb..8d105cba 100755 --- a/script/nbb_nrepl_tests.clj +++ b/script/nbb_nrepl_tests.clj @@ -180,6 +180,12 @@ msg (read-reply in session @id) status (:status msg) _ (is (= ["done"] status))]) + (bencode/write-bencode os {"op" "eval" "code" "(def *** 1)" + "session" session "id" (new-id!)}) + (let [_ (read-reply in session @id) + msg (read-reply in session @id) + status (:status msg) + _ (is (= ["done"] status))]) (testing "SCI var completions" (bencode/write-bencode os {"op" "complete" "symbol" "nbb/" @@ -189,6 +195,15 @@ completions (set (map read-msg completions))] (is (contains? completions {:candidate "nbb/load-string", :ns "nbb.core"})) (is (contains? completions {:candidate "nbb/await", :ns "nbb.core"})))) + (testing "special characters" + (bencode/write-bencode os + {"op" "complete" "symbol" "*" + "session" session "id" (new-id!)}) + (let [msg (read-reply in session @id) + completions (:completions msg) + completions (set (map read-msg completions))] + (is (contains? completions {:candidate "***", :ns "user"})) + (is (contains? completions {:candidate "*print-readably*", :ns "clojure.core"})))) (testing "JS import completions" (bencode/write-bencode os {"op" "complete" "symbol" "fs/" diff --git a/src/nbb/impl/repl_utils.cljs b/src/nbb/impl/repl_utils.cljs index dd273561..f7554638 100644 --- a/src/nbb/impl/repl_utils.cljs +++ b/src/nbb/impl/repl_utils.cljs @@ -19,8 +19,11 @@ syms (remove #(str/starts-with? (str %) "nbb.internal") syms)] syms)) +(defn literal-regex [s] + (re-pattern (str/replace s #"[.*+?^${}()|\[\]\\]" "\\$&"))) + (defn match [_alias->ns ns->alias query [sym-ns sym-name qualifier]] - (let [pat (re-pattern query)] + (let [pat (literal-regex query)] (or (when (and (= :unqualified qualifier) (re-find pat sym-name)) [sym-ns sym-name]) (when sym-ns