From 26c50a267091864d2d2dd14a1cc3447780f0a77d Mon Sep 17 00:00:00 2001 From: Bozhidar Batsov Date: Thu, 27 Aug 2015 20:24:17 +0300 Subject: Add CIDER wrappers for nrepl-send-request and nrepl-send-sync-request One more step towards decoupling CIDER and nrepl-client. --- cider-client.el | 52 ++++++++++++++++++++++++++++---------------- cider-debug.el | 12 +++++----- cider-inspector.el | 48 ++++++++++++++++++++-------------------- cider-interaction.el | 48 ++++++++++++++++++++-------------------- cider-macroexpansion.el | 2 +- cider-test.el | 30 ++++++++++++------------- cider.el | 2 +- test/cider-tests--no-auto.el | 2 +- test/cider-tests.el | 2 +- 9 files changed, 106 insertions(+), 92 deletions(-) diff --git a/cider-client.el b/cider-client.el index b162834f..1b8f38cb 100644 --- a/cider-client.el +++ b/cider-client.el @@ -98,7 +98,7 @@ Also close associated REPL and server buffers." (declare-function cider-popup-buffer-mode "cider-interaction") (define-derived-mode cider-connections-buffer-mode cider-popup-buffer-mode - "CIDER Connections" + "CIDER Connections" "CIDER Connections Buffer Mode. \\{cider-connections-buffer-mode-map} \\{cider-popup-buffer-mode-map}" @@ -238,6 +238,20 @@ Refreshes EWOC." "Check whether the current connection supports the nREPL middleware OP." (nrepl-op-supported-p op (cider-current-repl-buffer))) +(defun cider-nrepl-send-request (request callback) + "Send REQUEST and register response handler CALLBACK. +REQUEST is a pair list of the form (\"op\" \"operation\" \"par1-name\" +\"par1\" ... )." + (nrepl-send-request request callback (cider-current-repl-buffer))) + +(defun cider-nrepl-send-sync-request (request &optional abort-on-input) + "Send REQUEST to the nREPL server synchronously. +Hold till final \"done\" message has arrived and join all response messages +of the same \"op\" that came along. +If ABORT-ON-INPUT is non-nil, the function will return nil at the first +sign of user input, so as not to hang the interface." + (nrepl-send-sync-request request (cider-current-repl-buffer) abort-on-input)) + (defun cider-tooling-eval (input callback &optional ns) "Send the request INPUT and register the CALLBACK as the response handler. NS specifies the namespace in which to evaluate the request." @@ -324,13 +338,13 @@ unless ALL is truthy." "Perform the nREPL \"load-file\" op. FILE-CONTENTS, FILE-PATH and FILE-NAME are details of the file to be loaded. If CALLBACK is nil, use `cider-load-file-handler'." - (nrepl-send-request (list "op" "load-file" - "session" (cider-current-session) - "file" file-contents - "file-path" file-path - "file-name" file-name) - (or callback - (cider-load-file-handler (current-buffer))))) + (cider-nrepl-send-request (list "op" "load-file" + "session" (cider-current-session) + "file" file-contents + "file-path" file-path + "file-name" file-name) + (or callback + (cider-load-file-handler (current-buffer))))) ;;; Sync Requests @@ -344,7 +358,7 @@ loaded. If CALLBACK is nil, use `cider-load-file-handler'." ,@(when docs-p '("docs?" "t")) ,@(when privates-p '("privates?" "t")) ,@(when case-sensitive-p '("case-sensitive?" "t"))) - (nrepl-send-sync-request) + (cider-nrepl-send-sync-request) (nrepl-dict-get "apropos-matches"))) (declare-function cider-ensure-op-supported "cider-interaction") @@ -353,7 +367,7 @@ loaded. If CALLBACK is nil, use `cider-load-file-handler'." (cider-ensure-op-supported "classpath") (-> (list "op" "classpath" "session" (cider-current-session)) - (nrepl-send-sync-request) + (cider-nrepl-send-sync-request) (nrepl-dict-get "classpath"))) (defun cider-sync-request:complete (str context) @@ -363,7 +377,7 @@ loaded. If CALLBACK is nil, use `cider-load-file-handler'." "ns" (cider-current-ns) "symbol" str "context" context) - (nrepl-send-sync-request (cider-current-repl-buffer) 'abort-on-input))) + (cider-nrepl-send-sync-request (cider-current-repl-buffer) 'abort-on-input))) (nrepl-dict-get dict "completions"))) (defun cider-sync-request:info (symbol &optional class member) @@ -374,7 +388,7 @@ loaded. If CALLBACK is nil, use `cider-load-file-handler'." ,@(when symbol (list "symbol" symbol)) ,@(when class (list "class" class)) ,@(when member (list "member" member))) - (nrepl-send-sync-request)))) + (cider-nrepl-send-sync-request)))) (if (member "no-info" (nrepl-dict-get var-info "status")) nil var-info))) @@ -387,7 +401,7 @@ loaded. If CALLBACK is nil, use `cider-load-file-handler'." ,@(when symbol (list "symbol" symbol)) ,@(when class (list "class" class)) ,@(when member (list "member" member))) - (nrepl-send-sync-request (cider-current-repl-buffer) 'abort-on-input))) + (cider-nrepl-send-sync-request (cider-current-repl-buffer) 'abort-on-input))) (if (member "no-eldoc" (nrepl-dict-get eldoc "status")) nil eldoc))) @@ -396,7 +410,7 @@ loaded. If CALLBACK is nil, use `cider-load-file-handler'." "Get a list of the available namespaces." (-> (list "op" "ns-list" "session" (cider-current-session)) - (nrepl-send-sync-request) + (cider-nrepl-send-sync-request) (nrepl-dict-get "ns-list"))) (defun cider-sync-request:ns-vars (ns) @@ -404,27 +418,27 @@ loaded. If CALLBACK is nil, use `cider-load-file-handler'." (-> (list "op" "ns-vars" "session" (cider-current-session) "ns" ns) - (nrepl-send-sync-request) + (cider-nrepl-send-sync-request) (nrepl-dict-get "ns-vars"))) (defun cider-sync-request:resource (name) "Perform nREPL \"resource\" op with resource name NAME." (-> (list "op" "resource" "name" name) - (nrepl-send-sync-request) + (cider-nrepl-send-sync-request) (nrepl-dict-get "resource-path"))) (defun cider-sync-request:resources-list () "Perform nREPL \"resource\" op with resource name NAME." (-> (list "op" "resources-list") - (nrepl-send-sync-request) + (cider-nrepl-send-sync-request) (nrepl-dict-get "resources-list"))) (defun cider-sync-request:format-code (code) "Perform nREPL \"format-code\" op with CODE." (-> (list "op" "format-code" "code" code) - (nrepl-send-sync-request) + (cider-nrepl-send-sync-request) (nrepl-dict-get "formatted-code"))) (defun cider-sync-request:format-edn (edn &optional right-margin) @@ -432,7 +446,7 @@ loaded. If CALLBACK is nil, use `cider-load-file-handler'." (let* ((response (-> (list "op" "format-edn" "edn" edn) (append (and right-margin (list "right-margin" right-margin))) - (nrepl-send-sync-request))) + (cider-nrepl-send-sync-request))) (err (nrepl-dict-get response "err"))) (when err ;; err will be a stacktrace with a first line that looks like: diff --git a/cider-debug.el b/cider-debug.el index ee98cd8f..824ba53e 100644 --- a/cider-debug.el +++ b/cider-debug.el @@ -120,7 +120,7 @@ This variable must be set before starting the repl connection." (defun cider--debug-handle-instrumented-defs (defs ns) "Update display of NS according to instrumented DEFS." (-when-let (buf (-first (lambda (b) (with-current-buffer b - (string= ns (cider-current-ns)))) + (string= ns (cider-current-ns)))) (buffer-list))) (with-current-buffer buf (cider--update-instrumented-defs defs)))) @@ -128,7 +128,7 @@ This variable must be set before starting the repl connection." (defun cider-browse-instrumented-defs () "List all instrumented definitions." (interactive) - (-if-let (all (-> (nrepl-send-sync-request (list "op" "debug-instrumented-defs")) + (-if-let (all (-> (cider-nrepl-send-sync-request (list "op" "debug-instrumented-defs")) (nrepl-dict-get "list"))) (with-current-buffer (cider-popup-buffer cider-browse-ns-buffer t) (let ((inhibit-read-only t)) @@ -159,7 +159,7 @@ This variable must be set before starting the repl connection." (defun cider--debug-init-connection () "Initialize a connection with the cider.debug middleware." - (nrepl-send-request + (cider-nrepl-send-request (append '("op" "init-debugger") (when cider-debug-print-level (list "print-level" cider-debug-print-level)) @@ -214,8 +214,8 @@ Each element of LOCALS should be a list of at least two elements." (apply #'max (mapcar (lambda (l) (string-width (car l))) locals)))) ;; A format string to build a format string. :-P (mapconcat (lambda (l) (format (format " %%%ds: %%s\n" left-col-width) - (propertize (car l) 'face 'font-lock-variable-name-face) - (cider-font-lock-as-clojure (cadr l)))) + (propertize (car l) 'face 'font-lock-variable-name-face) + (cider-font-lock-as-clojure (cadr l)))) locals "")) "")) @@ -393,7 +393,7 @@ specific message." (symbol-name last-command-event) (cdr (assq last-command-event cider--debug-mode-commands-alist))) nil)) - (nrepl-send-request + (cider-nrepl-send-request (list "op" "debug-input" "input" (or command ":quit") "key" (or key (nrepl-dict-get cider--debug-mode-response "key"))) #'ignore) diff --git a/cider-inspector.el b/cider-inspector.el index 40cdfc62..8651eae2 100644 --- a/cider-inspector.el +++ b/cider-inspector.el @@ -123,56 +123,56 @@ Used for all inspector nREPL ops." (defun cider-inspect-expr (expr ns) (cider--prep-interactive-eval expr) - (nrepl-send-request (append (nrepl--eval-request expr ns) - (list "inspect" "true" - "page-size" (or cider-inspector-page-size 32))) - (cider-inspector-response-handler (current-buffer)))) + (cider-nrepl-send-request (append (nrepl--eval-request expr ns) + (list "inspect" "true" + "page-size" (or cider-inspector-page-size 32))) + (cider-inspector-response-handler (current-buffer)))) (defun cider-inspector-pop () (interactive) - (nrepl-send-request (list "op" "inspect-pop" - "session" (cider-current-session)) - (cider-inspector-response-handler (current-buffer)))) + (cider-nrepl-send-request (list "op" "inspect-pop" + "session" (cider-current-session)) + (cider-inspector-response-handler (current-buffer)))) (defun cider-inspector-push (idx) - (nrepl-send-request (list "op" "inspect-push" - "idx" (number-to-string idx) - "session" (cider-current-session)) - (cider-inspector-response-handler (current-buffer)))) + (cider-nrepl-send-request (list "op" "inspect-push" + "idx" (number-to-string idx) + "session" (cider-current-session)) + (cider-inspector-response-handler (current-buffer)))) (defun cider-inspector-refresh () (interactive) - (nrepl-send-request (list "op" "inspect-refresh" - "session" (cider-current-session)) - (cider-inspector-response-handler (current-buffer)))) + (cider-nrepl-send-request (list "op" "inspect-refresh" + "session" (cider-current-session)) + (cider-inspector-response-handler (current-buffer)))) (defun cider-inspector-next-page () "Jump to the next page when inspecting a paginated sequence/map. Does nothing if already on the last page." (interactive) - (nrepl-send-request (list "op" "inspect-next-page" - "session" (cider-current-session)) - (cider-inspector-response-handler (current-buffer)))) + (cider-nrepl-send-request (list "op" "inspect-next-page" + "session" (cider-current-session)) + (cider-inspector-response-handler (current-buffer)))) (defun cider-inspector-prev-page () "Jump to the previous page when expecting a paginated sequence/map. Does nothing if already on the first page." (interactive) - (nrepl-send-request (list "op" "inspect-prev-page" - "session" (cider-current-session)) - (cider-inspector-response-handler (current-buffer)))) + (cider-nrepl-send-request (list "op" "inspect-prev-page" + "session" (cider-current-session)) + (cider-inspector-response-handler (current-buffer)))) (defun cider-inspector-set-page-size (page-size) "Set the page size in pagination mode to the specified value. Current page will be reset to zero." (interactive "nPage size:") - (nrepl-send-request (list "op" "inspect-set-page-size" - "session" (cider-current-session) - "page-size" page-size) - (cider-inspector-response-handler (current-buffer)))) + (cider-nrepl-send-request (list "op" "inspect-set-page-size" + "session" (cider-current-session) + "page-size" page-size) + (cider-inspector-response-handler (current-buffer)))) ;; Render Inspector from Structured Values (defun cider-irender (buffer str) diff --git a/cider-interaction.el b/cider-interaction.el index fe18c95e..b99ab646 100644 --- a/cider-interaction.el +++ b/cider-interaction.el @@ -519,16 +519,16 @@ variable, but the order can be changed. For instance, the function `cider-make-connection-default' can be used to move a connection to the head of the list, so that it will take precedence over other connections associated with the same project." -(or (-first (lambda (conn) - (-when-let (conn-proj-dir (with-current-buffer (get-buffer conn) - nrepl-project-dir)) - (equal (file-truename project-directory) - (file-truename conn-proj-dir)))) - cider-connections) - (-first (lambda (conn) - (with-current-buffer (get-buffer conn) - (not nrepl-project-dir))) - cider-connections))) + (or (-first (lambda (conn) + (-when-let (conn-proj-dir (with-current-buffer (get-buffer conn) + nrepl-project-dir)) + (equal (file-truename project-directory) + (file-truename conn-proj-dir)))) + cider-connections) + (-first (lambda (conn) + (with-current-buffer (get-buffer conn) + (not nrepl-project-dir))) + cider-connections))) (defun cider-assoc-project-with-connection (&optional project connection) "Associate a Clojure PROJECT with an nREPL CONNECTION. @@ -1071,7 +1071,7 @@ thing at point." "Get the path to the file containing NS." (-> (list "op" "ns-path" "ns" ns) - nrepl-send-sync-request + cider-nrepl-send-sync-request (nrepl-dict-get "path"))) (defun cider--find-ns (ns &optional other-window) @@ -1457,7 +1457,7 @@ into a new error buffer." "Display the last exception for SESSION, with middleware support." ;; Causes are returned as a series of messages, which we aggregate in `causes' (let (causes) - (nrepl-send-request + (cider-nrepl-send-request (append (list "op" "stacktrace" "session" session) (when cider-stacktrace-print-length @@ -2041,7 +2041,7 @@ On failure, read a symbol name using PROMPT and call CALLBACK with that." (-> (list "op" "toggle-trace-var" "ns" (cider-current-ns) "sym" symbol) - (nrepl-send-sync-request))) + (cider-nrepl-send-sync-request))) (defun cider--toggle-trace-var (sym) (let* ((trace-response (cider-sync-request:toggle-trace-var sym)) @@ -2069,7 +2069,7 @@ opposite of what that option dictates." (cider-ensure-op-supported "toggle-trace-ns") (-> (list "op" "toggle-trace-ns" "ns" ns) - (nrepl-send-sync-request))) + (cider-nrepl-send-sync-request))) (defun cider-toggle-trace-ns (query) "Toggle ns tracing. @@ -2114,7 +2114,7 @@ opposite of what that option dictates." (cider-read-symbol-name "Undefine symbol: " (lambda (sym) - (nrepl-send-request + (cider-nrepl-send-request (list "op" "undef" "ns" (cider-current-ns) "symbol" sym) @@ -2186,14 +2186,14 @@ stale code from any deleted files may not be completely unloaded." (clear? (>= arg 16)) (refresh-all? (>= arg 4))) (when cider-refresh-show-log-buffer (cider-popup-buffer-display log-buffer)) - (when clear? (nrepl-send-sync-request (list "op" "refresh-clear"))) - (nrepl-send-request (append (list "op" (if refresh-all? "refresh-all" "refresh") - "print-length" cider-stacktrace-print-length - "print-level" cider-stacktrace-print-level) - (when cider-refresh-before-fn (list "before" cider-refresh-before-fn)) - (when cider-refresh-after-fn (list "after" cider-refresh-after-fn))) - (lambda (response) - (cider-refresh--handle-response response log-buffer))))) + (when clear? (cider-nrepl-send-sync-request (list "op" "refresh-clear"))) + (cider-nrepl-send-request (append (list "op" (if refresh-all? "refresh-all" "refresh") + "print-length" cider-stacktrace-print-length + "print-level" cider-stacktrace-print-level) + (when cider-refresh-before-fn (list "before" cider-refresh-before-fn)) + (when cider-refresh-after-fn (list "after" cider-refresh-after-fn))) + (lambda (response) + (cider-refresh--handle-response response log-buffer))))) (defun cider-file-string (file) "Read the contents of a FILE and return as a string." @@ -2422,7 +2422,7 @@ VAR is a fully qualified Clojure variable name as a string." With a prefix argument, prompt for function to run instead of -main." (interactive (list (when current-prefix-arg (read-string "Function name: ")))) (let ((name (or function "-main"))) - (-when-let (response (nrepl-send-sync-request + (-when-let (response (cider-nrepl-send-sync-request (list "op" "ns-list-vars-by-name" "name" name))) (-if-let (vars (split-string (substring (nrepl-dict-get response "var-list") 1 -1))) (cider-interactive-eval diff --git a/cider-macroexpansion.el b/cider-macroexpansion.el index 4349b0a9..223dff50 100644 --- a/cider-macroexpansion.el +++ b/cider-macroexpansion.el @@ -72,7 +72,7 @@ The default for DISPLAY-NAMESPACES is taken from (symbol-name cider-macroexpansion-display-namespaces))) (append (when cider-macroexpansion-print-metadata (list "print-meta" "true"))) - (nrepl-send-sync-request) + (cider-nrepl-send-sync-request) (nrepl-dict-get "expansion"))) (defun cider-macroexpand-undo (&optional arg) diff --git a/cider-test.el b/cider-test.el index 2b1d260e..df43097a 100644 --- a/cider-test.el +++ b/cider-test.el @@ -177,7 +177,7 @@ (defun cider-test-stacktrace-for (ns var index) "Display stacktrace for the erring NS VAR test with the assertion INDEX." (let (causes) - (nrepl-send-request + (cider-nrepl-send-request (append (list "op" "test-stacktrace" "session" (cider-current-session) "ns" ns "var" var "index" index) @@ -351,19 +351,19 @@ With the actual value, the outermost '(not ...)' s-expression is removed." ;; TODO: Figure out what to do when the metadata is missing (when (and file line (not (cider--tooling-file-p file))) (save-excursion - (goto-char (point-min)) - (forward-line (1- line)) - (forward-whitespace 1) - (forward-char) - (let ((beg (point))) - (forward-sexp) - (let ((overlay (make-overlay beg (point)))) - (overlay-put overlay 'font-lock-face (cider-test-type-face type)) - (overlay-put overlay 'type type) - (overlay-put overlay 'help-echo message) - (overlay-put overlay 'message message) - (overlay-put overlay 'expected expected) - (overlay-put overlay 'actual actual)))))))) + (goto-char (point-min)) + (forward-line (1- line)) + (forward-whitespace 1) + (forward-char) + (let ((beg (point))) + (forward-sexp) + (let ((overlay (make-overlay beg (point)))) + (overlay-put overlay 'font-lock-face (cider-test-type-face type)) + (overlay-put overlay 'type type) + (overlay-put overlay 'help-echo message) + (overlay-put overlay 'message message) + (overlay-put overlay 'expected expected) + (overlay-put overlay 'actual actual)))))))) (defun cider-test-highlight-problems (ns results) "Highlight all non-passing tests in the NS test RESULTS." @@ -419,7 +419,7 @@ Upon test completion, results are echoed and a test report is optionally displayed. When test failures/errors occur, their sources are highlighted." (cider-test-clear-highlights) (message "Testing...") - (nrepl-send-request + (cider-nrepl-send-request (list "ns" ns "op" (if retest "retest" "test") "tests" tests "session" (cider-current-session)) (lambda (response) diff --git a/cider.el b/cider.el index 0d72ac28..90575365 100644 --- a/cider.el +++ b/cider.el @@ -215,7 +215,7 @@ REPL started by the server process filter." (with-current-buffer cljs-buffer (setq cider-repl-type "cljs") (setq nrepl-sibling-buffer-alist alist) - (nrepl-send-request + (cider-nrepl-send-request (list "op" "eval" "ns" (cider-current-ns) "session" nrepl-session diff --git a/test/cider-tests--no-auto.el b/test/cider-tests--no-auto.el index d3b23ec3..1d0facc7 100644 --- a/test/cider-tests--no-auto.el +++ b/test/cider-tests--no-auto.el @@ -45,7 +45,7 @@ leading line of all dashes and trailing nil (when no doc is present) are removed from the latter. Remaining content is compared for string equality." (let ((repl-doc (with-temp-buffer (let ((form (format "(clojure.repl/doc %s)" sym))) - (insert (nrepl-dict-get (nrepl-send-sync-request form) + (insert (nrepl-dict-get (cider-nrepl-send-sync-request form) "out")) (goto-char (point-min)) (while (re-search-forward "^ nil\n" nil t) diff --git a/test/cider-tests.el b/test/cider-tests.el index 6d45b7af..fe049a4e 100644 --- a/test/cider-tests.el +++ b/test/cider-tests.el @@ -133,7 +133,7 @@ (should (equal (cider-repl--banner) "; CIDER 0.5.1 (Java 1.7, Clojure 1.5.1, nREPL 0.2.1)"))))) (ert-deftest test-cider-var-info () - (noflet ((nrepl-send-sync-request (list) + (noflet ((cider-nrepl-send-sync-request (list) '(dict "arglists" "([] [x] [x & ys])" "ns" "clojure.core" -- cgit v1.2.3