Klimi's new dotfiles with stow.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

701 linhas
28 KiB

há 4 anos
  1. ;;; cider-client.el --- A layer of abstraction above low-level nREPL client code. -*- lexical-binding: t -*-
  2. ;; Copyright © 2013-2019 Bozhidar Batsov
  3. ;;
  4. ;; Author: Bozhidar Batsov <bozhidar@batsov.com>
  5. ;; This program is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;; This file is not part of GNU Emacs.
  16. ;;; Commentary:
  17. ;; A layer of abstraction above the low-level nREPL client code.
  18. ;;; Code:
  19. (require 'map)
  20. (require 'seq)
  21. (require 'subr-x)
  22. (require 'parseedn)
  23. (require 'clojure-mode)
  24. (require 'spinner)
  25. (require 'cider-compat)
  26. (require 'cider-connection)
  27. (require 'cider-common)
  28. (require 'cider-util)
  29. (require 'nrepl-client)
  30. ;;; Eval spinner
  31. (defcustom cider-eval-spinner-type 'progress-bar
  32. "Appearance of the evaluation spinner.
  33. Value is a symbol. The possible values are the symbols in the
  34. `spinner-types' variable."
  35. :type 'symbol
  36. :group 'cider
  37. :package-version '(cider . "0.10.0"))
  38. (defcustom cider-show-eval-spinner t
  39. "When true, show the evaluation spinner in the mode line."
  40. :type 'boolean
  41. :group 'cider
  42. :package-version '(cider . "0.10.0"))
  43. (defcustom cider-eval-spinner-delay 1
  44. "Amount of time, in seconds, after which the evaluation spinner will be shown."
  45. :type 'integer
  46. :group 'cider
  47. :package-version '(cider . "0.10.0"))
  48. (defun cider-spinner-start (buffer)
  49. "Start the evaluation spinner in BUFFER.
  50. Do nothing if `cider-show-eval-spinner' is nil."
  51. (when cider-show-eval-spinner
  52. (with-current-buffer buffer
  53. (spinner-start cider-eval-spinner-type nil
  54. cider-eval-spinner-delay))))
  55. (defun cider-eval-spinner-handler (eval-buffer original-callback)
  56. "Return a response handler to stop the spinner and call ORIGINAL-CALLBACK.
  57. EVAL-BUFFER is the buffer where the spinner was started."
  58. (lambda (response)
  59. ;; buffer still exists and
  60. ;; we've got status "done" from nrepl
  61. ;; stop the spinner
  62. (when (and (buffer-live-p eval-buffer)
  63. (let ((status (nrepl-dict-get response "status")))
  64. (or (member "done" status)
  65. (member "eval-error" status)
  66. (member "error" status))))
  67. (with-current-buffer eval-buffer
  68. (when spinner-current (spinner-stop))))
  69. (funcall original-callback response)))
  70. ;;; Evaluation helpers
  71. (defun cider-ns-form-p (form)
  72. "Check if FORM is an ns form."
  73. (string-match-p "^[[:space:]]*\(ns\\([[:space:]]*$\\|[[:space:]]+\\)" form))
  74. (defun cider-ns-from-form (ns-form)
  75. "Get ns substring from NS-FORM."
  76. (when (string-match "^[ \t\n]*\(ns[ \t\n]+\\([^][ \t\n(){}]+\\)" ns-form)
  77. (match-string-no-properties 1 ns-form)))
  78. (defvar-local cider-buffer-ns nil
  79. "Current Clojure namespace of some buffer.
  80. Useful for special buffers (e.g. REPL, doc buffers) that have to keep track
  81. of a namespace. This should never be set in Clojure buffers, as there the
  82. namespace should be extracted from the buffer's ns form.")
  83. (defun cider-current-ns (&optional no-default)
  84. "Return the current ns.
  85. The ns is extracted from the ns form for Clojure buffers and from
  86. `cider-buffer-ns' for all other buffers. If it's missing, use the current
  87. REPL's ns, otherwise fall back to \"user\". When NO-DEFAULT is non-nil, it
  88. will return nil instead of \"user\"."
  89. (or cider-buffer-ns
  90. (clojure-find-ns)
  91. (when-let* ((repl (cider-current-repl)))
  92. (buffer-local-value 'cider-buffer-ns repl))
  93. (if no-default nil "user")))
  94. (defun cider-path-to-ns (relpath)
  95. "Transform RELPATH to Clojure namespace.
  96. Remove extension and substitute \"/\" with \".\", \"_\" with \"-\"."
  97. (thread-last relpath
  98. (file-name-sans-extension)
  99. (replace-regexp-in-string "/" ".")
  100. (replace-regexp-in-string "_" "-")))
  101. (defun cider-expected-ns (&optional path)
  102. "Return the namespace string matching PATH, or nil if not found.
  103. If PATH is nil, use the path to the file backing the current buffer. The
  104. command falls back to `clojure-expected-ns' in the absence of an active
  105. nREPL connection."
  106. (if (cider-connected-p)
  107. (let* ((path (file-truename (or path buffer-file-name)))
  108. (relpath (thread-last (cider-classpath-entries)
  109. (seq-filter #'file-directory-p)
  110. (seq-map (lambda (dir)
  111. (when (file-in-directory-p path dir)
  112. (file-relative-name path dir))))
  113. (seq-filter #'identity)
  114. (seq-sort (lambda (a b)
  115. (< (length a) (length b))))
  116. (car))))
  117. (if relpath
  118. (cider-path-to-ns relpath)
  119. (clojure-expected-ns path)))
  120. (clojure-expected-ns path)))
  121. (defun cider-nrepl-op-supported-p (op &optional connection)
  122. "Check whether the CONNECTION supports the nREPL middleware OP."
  123. (nrepl-op-supported-p op (or connection (cider-current-repl nil 'ensure))))
  124. (defvar cider-version)
  125. (defun cider-ensure-op-supported (op)
  126. "Check for support of middleware op OP.
  127. Signal an error if it is not supported."
  128. (unless (cider-nrepl-op-supported-p op)
  129. (user-error "`%s' requires the nREPL op \"%s\" (provided by cider-nrepl)" this-command op)))
  130. (defun cider-nrepl-send-request (request callback &optional connection)
  131. "Send REQUEST and register response handler CALLBACK.
  132. REQUEST is a pair list of the form (\"op\" \"operation\" \"par1-name\"
  133. \"par1\" ... ).
  134. If CONNECTION is provided dispatch to that connection instead of
  135. the current connection. Return the id of the sent message."
  136. (nrepl-send-request request callback (or connection (cider-current-repl 'any 'ensure))))
  137. (defun cider-nrepl-send-sync-request (request &optional connection abort-on-input)
  138. "Send REQUEST to the nREPL server synchronously using CONNECTION.
  139. Hold till final \"done\" message has arrived and join all response messages
  140. of the same \"op\" that came along and return the accumulated response.
  141. If ABORT-ON-INPUT is non-nil, the function will return nil
  142. at the first sign of user input, so as not to hang the
  143. interface."
  144. (nrepl-send-sync-request request
  145. (or connection (cider-current-repl 'any 'ensure))
  146. abort-on-input))
  147. (defun cider-nrepl-send-unhandled-request (request &optional connection)
  148. "Send REQUEST to the nREPL CONNECTION and ignore any responses.
  149. Immediately mark the REQUEST as done. Return the id of the sent message."
  150. (let* ((conn (or connection (cider-current-repl 'any 'ensure)))
  151. (id (nrepl-send-request request #'ignore conn)))
  152. (with-current-buffer conn
  153. (nrepl--mark-id-completed id))
  154. id))
  155. (defun cider-nrepl-request:eval (input callback &optional ns line column additional-params connection)
  156. "Send the request INPUT and register the CALLBACK as the response handler.
  157. If NS is non-nil, include it in the request. LINE and COLUMN, if non-nil,
  158. define the position of INPUT in its buffer. ADDITIONAL-PARAMS is a plist
  159. to be appended to the request message. CONNECTION is the connection
  160. buffer, defaults to (cider-current-repl)."
  161. (let ((connection (or connection (cider-current-repl nil 'ensure))))
  162. (nrepl-request:eval input
  163. (if cider-show-eval-spinner
  164. (cider-eval-spinner-handler connection callback)
  165. callback)
  166. connection
  167. ns line column additional-params)
  168. (cider-spinner-start connection)))
  169. (defun cider-nrepl-sync-request:eval (input &optional connection ns)
  170. "Send the INPUT to the nREPL CONNECTION synchronously.
  171. If NS is non-nil, include it in the eval request."
  172. (nrepl-sync-request:eval input (or connection (cider-current-repl nil 'ensure)) ns))
  173. (defcustom cider-print-fn 'pprint
  174. "Sets the function to use for printing.
  175. nil to defer to nREPL to choose the printing function. This will use
  176. the bound value of \\=`nrepl.middleware.print/*print-fn*\\=`, which
  177. defaults to the equivalent of \\=`clojure.core/pr\\=`.
  178. `pr' to use the equivalent of \\=`clojure.core/pr\\=`.
  179. `pprint' to use \\=`clojure.pprint/pprint\\=` (this is the default).
  180. `fipp' to use the Fast Idiomatic Pretty Printer, approximately 5-10x
  181. faster than \\=`clojure.core/pprint\\=`.
  182. `puget' to use Puget, which provides canonical serialization of data on
  183. top of fipp, but at a slight performance cost.
  184. `zprint' to use zprint, a fast and flexible alternative to the libraries
  185. mentioned above.
  186. Alternatively can be the namespace-qualified name of a Clojure var whose
  187. function takes three arguments: the object to print, the
  188. \\=`java.io.PrintWriter\\=` to print on, and a (possibly nil) map of
  189. options. If the function cannot be resolved, will behave as if set to
  190. nil."
  191. :type '(choice (const nil)
  192. (const pr)
  193. (const pprint)
  194. (const fipp)
  195. (const puget)
  196. (const zprint)
  197. string)
  198. :group 'cider
  199. :package-version '(cider . "0.21.0"))
  200. (defcustom cider-print-options nil
  201. "A map of options that will be passed to `cider-print-fn'.
  202. Here's an example for `pprint':
  203. '((\"length\" 50) (\"right-margin\" 70))"
  204. :type 'list
  205. :group 'cider
  206. :package-version '(cider . "0.21.0"))
  207. (make-obsolete-variable 'cider-pprint-fn 'cider-print-fn "0.21")
  208. (make-obsolete-variable 'cider-pprint-options 'cider-print-options "0.21")
  209. (defcustom cider-print-quota (* 1024 1024)
  210. "A hard limit on the number of bytes to return from any printing operation.
  211. Set to nil for no limit."
  212. :type 'integer
  213. :group 'cider
  214. :package-version '(cider . "0.21.0"))
  215. (defun cider--print-fn ()
  216. "Return the value to send in the nrepl.middleware.print/print slot."
  217. (pcase cider-print-fn
  218. (`pr "cider.nrepl.pprint/pr")
  219. (`pprint "cider.nrepl.pprint/pprint")
  220. (`fipp "cider.nrepl.pprint/fipp-pprint")
  221. (`puget "cider.nrepl.pprint/puget-pprint")
  222. (`zprint "cider.nrepl.pprint/zprint-pprint")
  223. (_ cider-print-fn)))
  224. (defvar cider--print-options-mapping
  225. '((right-margin
  226. ((fipp . width) (puget . width) (zprint . width)))
  227. (length
  228. ((fipp . print-length) (puget . print-length) (zprint . max-length)))
  229. (level
  230. ((fipp . print-level) (puget . print-level) (zprint . max-depth))))
  231. "A mapping of print option for the various supported print engines.")
  232. (defun cider--print-option (name printer)
  233. "Convert the generic NAME to its PRINTER specific variant.
  234. E.g. pprint's right-margin would become width for fipp.
  235. The function is useful when you want to generate dynamically
  236. print options.
  237. NAME can be a string or a symbol. PRINTER has to be a symbol.
  238. The result will be a string."
  239. (let* ((name (cider-maybe-intern name))
  240. (result (cdr (assoc printer (cadr (assoc name cider--print-options-mapping))))))
  241. (symbol-name (or result name))))
  242. (defun cider--nrepl-print-request-map (&optional right-margin)
  243. "Map to merge into requests that require pretty-printing.
  244. RIGHT-MARGIN specifies the maximum column-width of the printed result, and
  245. is included in the request if non-nil."
  246. (let* ((width-option (cider--print-option "right-margin" cider-print-fn))
  247. (print-options (thread-last
  248. (map-merge 'hash-table
  249. `((,width-option ,right-margin))
  250. cider-print-options)
  251. (map-pairs)
  252. (seq-mapcat #'identity)
  253. (apply #'nrepl-dict))))
  254. (map-merge 'list
  255. `(("nrepl.middleware.print/stream?" "1"))
  256. (when cider-print-fn
  257. `(("nrepl.middleware.print/print" ,(cider--print-fn))))
  258. (when cider-print-quota
  259. `(("nrepl.middleware.print/quota" ,cider-print-quota)))
  260. (unless (nrepl-dict-empty-p print-options)
  261. `(("nrepl.middleware.print/options" ,print-options))))))
  262. (defun cider--nrepl-pr-request-map ()
  263. "Map to merge into requests that do not require pretty printing."
  264. (let ((print-options (thread-last cider-print-options
  265. (map-pairs)
  266. (seq-mapcat #'identity)
  267. (apply #'nrepl-dict))))
  268. (map-merge 'list
  269. `(("nrepl.middleware.print/print" "cider.nrepl.pprint/pr"
  270. "nrepl.middleware.print/stream?" nil))
  271. (unless (nrepl-dict-empty-p print-options)
  272. `(("nrepl.middleware.print/options" ,print-options)))
  273. (when cider-print-quota
  274. `(("nrepl.middleware.print/quota" ,cider-print-quota))))))
  275. (defun cider--nrepl-content-type-map ()
  276. "Map to be merged into an eval request to make it use content-types."
  277. '(("content-type" "true")))
  278. (defun cider-tooling-eval (input callback &optional ns connection)
  279. "Send the request INPUT to CONNECTION and register the CALLBACK.
  280. NS specifies the namespace in which to evaluate the request. Requests
  281. evaluated in the tooling nREPL session don't affect the thread-local
  282. bindings of the primary eval nREPL session (e.g. this is not going to
  283. clobber *1/2/3)."
  284. ;; namespace forms are always evaluated in the "user" namespace
  285. (nrepl-request:eval input
  286. callback
  287. (or connection (cider-current-repl nil 'ensure))
  288. ns nil nil nil 'tooling))
  289. (defun cider-sync-tooling-eval (input &optional ns connection)
  290. "Send the request INPUT to CONNECTION and evaluate in synchronously.
  291. NS specifies the namespace in which to evaluate the request. Requests
  292. evaluated in the tooling nREPL session don't affect the thread-local
  293. bindings of the primary eval nREPL session (e.g. this is not going to
  294. clobber *1/2/3)."
  295. ;; namespace forms are always evaluated in the "user" namespace
  296. (nrepl-sync-request:eval input
  297. (or connection (cider-current-repl nil 'ensure))
  298. ns
  299. 'tooling))
  300. (defun cider-library-present-p (lib-ns)
  301. "Check whether LIB-NS is present.
  302. If a certain well-known ns in a library is present we assume that library
  303. itself is present."
  304. (nrepl-dict-get (cider-sync-tooling-eval (format "(require '%s)" lib-ns)) "value"))
  305. ;;; Interrupt evaluation
  306. (defun cider-interrupt-handler (buffer)
  307. "Create an interrupt response handler for BUFFER."
  308. (nrepl-make-response-handler buffer nil nil nil nil))
  309. (defun cider-interrupt ()
  310. "Interrupt any pending evaluations."
  311. (interactive)
  312. ;; FIXME: does this work correctly in cljc files?
  313. (with-current-buffer (cider-current-repl nil 'ensure)
  314. (let ((pending-request-ids (cider-util--hash-keys nrepl-pending-requests)))
  315. (dolist (request-id pending-request-ids)
  316. (nrepl-request:interrupt
  317. request-id
  318. (cider-interrupt-handler (current-buffer))
  319. (cider-current-repl))))))
  320. (defun cider-nrepl-eval-session ()
  321. "Return the eval nREPL session id of the current connection."
  322. (with-current-buffer (cider-current-repl)
  323. nrepl-session))
  324. (defun cider-nrepl-tooling-session ()
  325. "Return the tooling nREPL session id of the current connection."
  326. (with-current-buffer (cider-current-repl)
  327. nrepl-tooling-session))
  328. (defun cider--var-choice (var-info)
  329. "Prompt to choose from among multiple VAR-INFO candidates, if required.
  330. This is needed only when the symbol queried is an unqualified host platform
  331. method, and multiple classes have a so-named member. If VAR-INFO does not
  332. contain a `candidates' key, it is returned as is."
  333. (let ((candidates (nrepl-dict-get var-info "candidates")))
  334. (if candidates
  335. (let* ((classes (nrepl-dict-keys candidates))
  336. (choice (completing-read "Member in class: " classes nil t))
  337. (info (nrepl-dict-get candidates choice)))
  338. info)
  339. var-info)))
  340. (defconst cider-info-form "
  341. (do
  342. (require 'clojure.java.io)
  343. (require 'clojure.walk)
  344. (if-let [var (resolve '%s)]
  345. (let [info (meta var)]
  346. (-> info
  347. (update :ns str)
  348. (update :name str)
  349. (update :file (comp str clojure.java.io/resource))
  350. (assoc :arglists-str (str (:arglists info)))
  351. (clojure.walk/stringify-keys)))))
  352. ")
  353. (defun cider-fallback-eval:info (var)
  354. "Obtain VAR metadata via a regular eval.
  355. Used only when the info nREPL middleware is not available."
  356. (let* ((response (cider-sync-tooling-eval (format cider-info-form var)))
  357. (var-info (nrepl-dict-from-hash (parseedn-read-str (nrepl-dict-get response "value")))))
  358. var-info))
  359. (defun cider-var-info (var &optional all)
  360. "Return VAR's info as an alist with list cdrs.
  361. When multiple matching vars are returned you'll be prompted to select one,
  362. unless ALL is truthy."
  363. (when (and var (not (string= var "")))
  364. (let ((var-info (if (cider-nrepl-op-supported-p "info")
  365. (cider-sync-request:info var)
  366. (cider-fallback-eval:info var))))
  367. (if all var-info (cider--var-choice var-info)))))
  368. (defun cider-member-info (class member)
  369. "Return the CLASS MEMBER's info as an alist with list cdrs."
  370. (when (and class member)
  371. (cider-sync-request:info nil class member)))
  372. ;;; Requests
  373. (declare-function cider-load-file-handler "cider-eval")
  374. (defun cider-request:load-file (file-contents file-path file-name &optional connection callback)
  375. "Perform the nREPL \"load-file\" op.
  376. FILE-CONTENTS, FILE-PATH and FILE-NAME are details of the file to be
  377. loaded. If CONNECTION is nil, use `cider-current-repl'. If CALLBACK
  378. is nil, use `cider-load-file-handler'."
  379. (cider-nrepl-send-request `("op" "load-file"
  380. "file" ,file-contents
  381. "file-path" ,file-path
  382. "file-name" ,file-name)
  383. (or callback
  384. (cider-load-file-handler (current-buffer)))
  385. connection))
  386. ;;; Sync Requests
  387. (defcustom cider-filtered-namespaces-regexps
  388. '("^cider.nrepl" "^refactor-nrepl" "^nrepl")
  389. "List of regexps used to filter out some vars/symbols/namespaces.
  390. When nil, nothing is filtered out. Otherwise, all namespaces matching any
  391. regexp from this list are dropped out of the \"ns-list\" op. Also,
  392. \"apropos\" won't include vars from such namespaces. This list is passed
  393. on to the nREPL middleware without any pre-processing. So the regexps have
  394. to be in Clojure format (with twice the number of backslashes) and not
  395. Emacs Lisp."
  396. :type '(repeat string)
  397. :safe #'listp
  398. :group 'cider
  399. :package-version '(cider . "0.13.0"))
  400. (defun cider-sync-request:apropos (query &optional search-ns docs-p privates-p case-sensitive-p)
  401. "Send \"apropos\" request for regexp QUERY.
  402. Optional arguments include SEARCH-NS, DOCS-P, PRIVATES-P, CASE-SENSITIVE-P."
  403. (let* ((query (replace-regexp-in-string "[ \t]+" ".+" query))
  404. (response (cider-nrepl-send-sync-request
  405. `("op" "apropos"
  406. "ns" ,(cider-current-ns)
  407. "query" ,query
  408. ,@(when search-ns `("search-ns" ,search-ns))
  409. ,@(when docs-p '("docs?" "t"))
  410. ,@(when privates-p '("privates?" "t"))
  411. ,@(when case-sensitive-p '("case-sensitive?" "t"))
  412. "exclude-regexps" ,cider-filtered-namespaces-regexps))))
  413. (if (member "apropos-regexp-error" (nrepl-dict-get response "status"))
  414. (user-error "Invalid regexp: %s" (nrepl-dict-get response "error-msg"))
  415. (nrepl-dict-get response "apropos-matches"))))
  416. (defun cider-sync-request:classpath ()
  417. "Return a list of classpath entries."
  418. (cider-ensure-op-supported "classpath")
  419. (thread-first '("op" "classpath")
  420. (cider-nrepl-send-sync-request)
  421. (nrepl-dict-get "classpath")))
  422. (defun cider-fallback-eval:classpath ()
  423. "Return a list of classpath entries using eval."
  424. (read (nrepl-dict-get (cider-sync-tooling-eval "(seq (.split (System/getProperty \"java.class.path\") \":\"))") "value")))
  425. (defun cider-classpath-entries ()
  426. "Return a list of classpath entries."
  427. (if (cider-nrepl-op-supported-p "classpath")
  428. (cider-sync-request:classpath)
  429. (cider-fallback-eval:classpath)))
  430. (defun cider-sync-request:complete (str context)
  431. "Return a list of completions for STR using nREPL's \"complete\" op.
  432. CONTEXT represents a completion context for compliment."
  433. (when-let* ((dict (thread-first `("op" "complete"
  434. "ns" ,(cider-current-ns)
  435. "symbol" ,str
  436. "context" ,context)
  437. (cider-nrepl-send-sync-request (cider-current-repl)
  438. 'abort-on-input))))
  439. (nrepl-dict-get dict "completions")))
  440. (defun cider-sync-request:complete-flush-caches ()
  441. "Send \"complete-flush-caches\" op to flush Compliment's caches."
  442. (cider-nrepl-send-sync-request (list "op" "complete-flush-caches"
  443. "session" (cider-nrepl-eval-session))
  444. 'abort-on-input))
  445. (defun cider-sync-request:info (symbol &optional class member)
  446. "Send \"info\" op with parameters SYMBOL or CLASS and MEMBER."
  447. (let ((var-info (thread-first `("op" "info"
  448. "ns" ,(cider-current-ns)
  449. ,@(when symbol `("symbol" ,symbol))
  450. ,@(when class `("class" ,class))
  451. ,@(when member `("member" ,member)))
  452. (cider-nrepl-send-sync-request (cider-current-repl)))))
  453. (if (member "no-info" (nrepl-dict-get var-info "status"))
  454. nil
  455. var-info)))
  456. (defun cider-sync-request:eldoc (symbol &optional class member)
  457. "Send \"eldoc\" op with parameters SYMBOL or CLASS and MEMBER."
  458. (when-let* ((eldoc (thread-first `("op" "eldoc"
  459. "ns" ,(cider-current-ns)
  460. ,@(when symbol `("symbol" ,symbol))
  461. ,@(when class `("class" ,class))
  462. ,@(when member `("member" ,member)))
  463. (cider-nrepl-send-sync-request (cider-current-repl)
  464. 'abort-on-input))))
  465. (if (member "no-eldoc" (nrepl-dict-get eldoc "status"))
  466. nil
  467. eldoc)))
  468. (defun cider-sync-request:eldoc-datomic-query (symbol)
  469. "Send \"eldoc-datomic-query\" op with parameter SYMBOL."
  470. (when-let* ((eldoc (thread-first `("op" "eldoc-datomic-query"
  471. "ns" ,(cider-current-ns)
  472. ,@(when symbol `("symbol" ,symbol)))
  473. (cider-nrepl-send-sync-request nil 'abort-on-input))))
  474. (if (member "no-eldoc" (nrepl-dict-get eldoc "status"))
  475. nil
  476. eldoc)))
  477. (defun cider-sync-request:spec-list (&optional filter-regex)
  478. "Get a list of the available specs in the registry.
  479. Optional argument FILTER-REGEX filters specs. By default, all specs are
  480. returned."
  481. (setq filter-regex (or filter-regex ""))
  482. (thread-first `("op" "spec-list"
  483. "filter-regex" ,filter-regex
  484. "ns" ,(cider-current-ns))
  485. (cider-nrepl-send-sync-request)
  486. (nrepl-dict-get "spec-list")))
  487. (defun cider-sync-request:spec-form (spec)
  488. "Get SPEC's form from registry."
  489. (thread-first `("op" "spec-form"
  490. "spec-name" ,spec
  491. "ns" ,(cider-current-ns))
  492. (cider-nrepl-send-sync-request)
  493. (nrepl-dict-get "spec-form")))
  494. (defun cider-sync-request:spec-example (spec)
  495. "Get an example for SPEC."
  496. (thread-first `("op" "spec-example"
  497. "spec-name" ,spec)
  498. (cider-nrepl-send-sync-request)
  499. (nrepl-dict-get "spec-example")))
  500. (defun cider-sync-request:ns-list ()
  501. "Get a list of the available namespaces."
  502. (thread-first `("op" "ns-list"
  503. "exclude-regexps" ,cider-filtered-namespaces-regexps)
  504. (cider-nrepl-send-sync-request)
  505. (nrepl-dict-get "ns-list")))
  506. (defun cider-sync-request:ns-vars (ns)
  507. "Get a list of the vars in NS."
  508. (thread-first `("op" "ns-vars"
  509. "ns" ,ns)
  510. (cider-nrepl-send-sync-request)
  511. (nrepl-dict-get "ns-vars")))
  512. (defun cider-sync-request:ns-path (ns)
  513. "Get the path to the file containing NS."
  514. (thread-first `("op" "ns-path"
  515. "ns" ,ns)
  516. (cider-nrepl-send-sync-request)
  517. (nrepl-dict-get "path")))
  518. (defun cider-sync-request:ns-vars-with-meta (ns)
  519. "Get a map of the vars in NS to its metadata information."
  520. (thread-first `("op" "ns-vars-with-meta"
  521. "ns" ,ns)
  522. (cider-nrepl-send-sync-request)
  523. (nrepl-dict-get "ns-vars-with-meta")))
  524. (defun cider-sync-request:ns-load-all ()
  525. "Load all project namespaces."
  526. (thread-first '("op" "ns-load-all")
  527. (cider-nrepl-send-sync-request)
  528. (nrepl-dict-get "loaded-ns")))
  529. (defun cider-sync-request:resource (name)
  530. "Perform nREPL \"resource\" op with resource name NAME."
  531. (thread-first `("op" "resource"
  532. "name" ,name)
  533. (cider-nrepl-send-sync-request)
  534. (nrepl-dict-get "resource-path")))
  535. (defun cider-sync-request:resources-list ()
  536. "Return a list of all resources on the classpath.
  537. The result entries are relative to the classpath."
  538. (when-let* ((resources (thread-first '("op" "resources-list")
  539. (cider-nrepl-send-sync-request)
  540. (nrepl-dict-get "resources-list"))))
  541. (seq-map (lambda (resource) (nrepl-dict-get resource "relpath")) resources)))
  542. (defun cider-sync-request:fn-refs (ns sym)
  543. "Return a list of functions that reference the function identified by NS and SYM."
  544. (cider-ensure-op-supported "fn-refs")
  545. (thread-first `("op" "fn-refs"
  546. "ns" ,ns
  547. "symbol" ,sym)
  548. (cider-nrepl-send-sync-request)
  549. (nrepl-dict-get "fn-refs")))
  550. (defun cider-sync-request:fn-deps (ns sym)
  551. "Return a list of function deps for the function identified by NS and SYM."
  552. (cider-ensure-op-supported "fn-deps")
  553. (thread-first `("op" "fn-deps"
  554. "ns" ,ns
  555. "symbol" ,sym)
  556. (cider-nrepl-send-sync-request)
  557. (nrepl-dict-get "fn-deps")))
  558. (defun cider-sync-request:format-code (code)
  559. "Perform nREPL \"format-code\" op with CODE."
  560. (thread-first `("op" "format-code"
  561. "code" ,code)
  562. (cider-nrepl-send-sync-request)
  563. (nrepl-dict-get "formatted-code")))
  564. (defun cider-sync-request:format-edn (edn right-margin)
  565. "Perform \"format-edn\" op with EDN and RIGHT-MARGIN."
  566. (let* ((request (thread-last
  567. (map-merge 'list
  568. `(("op" "format-edn")
  569. ("edn" ,edn))
  570. (cider--nrepl-print-request-map right-margin))
  571. (seq-mapcat #'identity)))
  572. (response (cider-nrepl-send-sync-request request))
  573. (err (nrepl-dict-get response "err")))
  574. (when err
  575. ;; err will be a stacktrace with a first line that looks like:
  576. ;; "clojure.lang.ExceptionInfo: Unmatched delimiter ]"
  577. (error (car (split-string err "\n"))))
  578. (nrepl-dict-get response "formatted-edn")))
  579. ;;; Dealing with input
  580. ;; TODO: Replace this with some nil handler.
  581. (defun cider-stdin-handler (&optional _buffer)
  582. "Make a stdin response handler for _BUFFER."
  583. (nrepl-make-response-handler (current-buffer)
  584. (lambda (_buffer _value))
  585. (lambda (_buffer _out))
  586. (lambda (_buffer _err))
  587. nil))
  588. (defun cider-need-input (buffer)
  589. "Handle an need-input request from BUFFER."
  590. (with-current-buffer buffer
  591. (let ((map (make-sparse-keymap)))
  592. (set-keymap-parent map minibuffer-local-map)
  593. (define-key map (kbd "C-c C-c") 'abort-recursive-edit)
  594. (let ((stdin (condition-case nil
  595. (concat (read-from-minibuffer "Stdin: " nil map) "\n")
  596. (quit nil))))
  597. (nrepl-request:stdin stdin
  598. (cider-stdin-handler buffer)
  599. (cider-current-repl))))))
  600. (provide 'cider-client)
  601. ;;; cider-client.el ends here