Skip to content

Commit 1709246

Browse files
committed
Merge pull request #1174 from Malabarba/master
Define command to run -main
2 parents 14a3939 + 7bf27c7 commit 1709246

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## master (unreleased)
44

5+
* [#1174](https://github.com/clojure-emacs/cider/pull/1174): New command `cider-run`, runs the project's `-main` function.
56
* [#1164](https://github.com/clojure-emacs/cider/pull/1164): Fix an error in `cider-browse-ns--doc-at-point`.
67
* [#1149](https://github.com/clojure-emacs/cider/pull/1149): [Two new ways](https://github.com/clojure-emacs/cider#cider-debug) to debug code, the `#break` and `#dbg` reader macros.
78
* [#1165](https://github.com/clojure-emacs/cider/pull/1165): Extract `cider-set-relevant-connection` from `cider-switch-to-relevant-repl-buffer`.

cider-interaction.el

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,6 +2164,35 @@ restart the server."
21642164
(cider-jack-in prompt-project))
21652165
(error "Can't restart CIDER for unknown project"))))
21662166

2167+
(defvar cider--namespace-history nil
2168+
"History of user input for namespace prompts.")
2169+
2170+
(defun cider--var-namespace (var)
2171+
"Return the namespace of VAR.
2172+
VAR is a fully qualified Clojure variable name as a string."
2173+
(replace-regexp-in-string "\\(?:#'\\)?\\(.*\\)/.*" "\\1" var))
2174+
2175+
(defun cider-run (&optional function)
2176+
"Run -main or FUNCTION, prompting for its namespace if necessary.
2177+
With a prefix argument, prompt for function to run instead of -main."
2178+
(interactive (list (when current-prefix-arg (read-string "Function name: "))))
2179+
(let ((name (or function "-main")))
2180+
(-when-let (response (nrepl-send-sync-request
2181+
(list "op" "ns-list-vars-by-name" "name" name)))
2182+
(-if-let (vars (split-string (substring (nrepl-dict-get response "var-list") 1 -1)))
2183+
(cider-interactive-eval
2184+
(if (= (length vars) 1)
2185+
(concat "(" (car vars) ")")
2186+
(let* ((completions (mapcar #'cider--var-namespace vars))
2187+
(def (or (car cider--namespace-history)
2188+
(car completions))))
2189+
(format "(#'%s/%s)"
2190+
(completing-read (format "Namespace (%s): " def)
2191+
completions nil t nil
2192+
'cider--namespace-history def)
2193+
name))))
2194+
(user-error "No %s var defined in any namespace" name)))))
2195+
21672196
(provide 'cider-interaction)
21682197

21692198
;;; cider-interaction.el ends here

test/cider-tests.el

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@
178178
(nrepl-connection-buffers)))
179179
(should (equal (buffer-name b) (nrepl-current-connection-buffer))))))
180180

181+
(ert-deftest test-cider--var-namespace ()
182+
(should (string= (cider--var-namespace "#'a/var-two") "a"))
183+
(should (string= (cider--var-namespace "#'a-two/var") "a-two"))
184+
(should (string= (cider--var-namespace "#'a.two-three.b/var-c") "a.two-three.b"))
185+
(should (string= (cider--var-namespace "a/var-two") "a"))
186+
(should (string= (cider--var-namespace "a-two/var") "a-two"))
187+
(should (string= (cider--var-namespace "a.two-three.b/var-c") "a.two-three.b")))
188+
181189

182190
;;; response handling
183191
(ert-deftest test-cider-dbind-response ()

0 commit comments

Comments
 (0)