Skip to content

Commit b5c46ac

Browse files
committed
Minimise the number of requests made during REPL init
New option, cider-repl-init-code, is a list of strings of Clojure code to evaluate when starting a REPL (with bindings for set!-able vars in place). Options cider-repl-print-length and cider-repl-print-level are now obsolete. Set the initial namespace of the REPL using the :ns response slot of an empty eval message, rather than evaluating (str *ns*). The only other request now made during init is to evaluate cider-repl-init-code.
1 parent b2965d4 commit b5c46ac

File tree

4 files changed

+35
-85
lines changed

4 files changed

+35
-85
lines changed

cider-repl.el

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,18 @@ you'd like to use the default Emacs behavior use
163163
:type 'symbol
164164
:group 'cider-repl)
165165

166-
(defcustom cider-repl-print-length 100
167-
"Initial value for *print-length* set during REPL start."
168-
:type 'integer
169-
:group 'cider
170-
:package-version '(cider . "0.17.0"))
166+
(make-obsolete-variable 'cider-repl-print-length 'cider-print-options "0.21")
167+
(make-obsolete-variable 'cider-repl-print-level 'cider-print-options "0.21")
171168

172-
(defcustom cider-repl-print-level nil
173-
"Initial value for *print-level* set during REPL start."
174-
:type 'integer
175-
:group 'cider
176-
:package-version '(cider . "0.17.0"))
169+
(defvar cider-repl-require-repl-utils-code
170+
"(clojure.core/apply clojure.core/require clojure.main/repl-requires)")
171+
172+
(defcustom cider-repl-init-code (list cider-repl-require-repl-utils-code)
173+
"Clojure code to evaluate when starting a REPL.
174+
Will be evaluated with bindings for set!-able vars in place."
175+
:type '(list string)
176+
:group 'cider-repl
177+
:package-version '(cider . "0.21.0"))
177178

178179
(defcustom cider-repl-display-help-banner t
179180
"When non-nil a bit of help text will be displayed on REPL start."
@@ -252,51 +253,37 @@ This cache is stored in the connection buffer.")
252253
(cider-refresh-dynamic-font-lock ns-dict))))))))))
253254

254255
(declare-function cider-set-buffer-ns "cider-mode")
255-
(defun cider-repl-set-initial-ns (buffer)
256-
"Require standard REPL util functions and set the ns of the REPL's BUFFER.
257-
Namespace is \"user\" by default, but can be overridden in apps like
258-
lein (:init-ns). Both of these operations need to be done as a sync
259-
request at the beginning of the session. Bundling them together for
260-
efficiency."
261-
;; we don't want to get a timeout during init
262-
(let ((nrepl-sync-request-timeout nil))
256+
(defun cider-repl--set-initial-ns (buffer)
257+
"Set the initial namespace of the REPL's BUFFER.
258+
This is \"user\" by default, but can be overridden (e.g. the Leiningen
259+
:init-ns option)."
260+
(let* ((response (nrepl-send-sync-request
261+
(thread-first (nrepl--eval-request "")
262+
(lax-plist-put "inhibit-cider-middleware" "true"))
263+
(cider-current-repl)))
264+
(ns (nrepl-dict-get response "ns" "user")))
263265
(with-current-buffer buffer
264-
(let* ((response (nrepl-send-sync-request
265-
(lax-plist-put (nrepl--eval-request "(str *ns*)")
266-
"inhibit-cider-middleware" "true")
267-
(cider-current-repl)))
268-
(initial-ns (or (read (nrepl-dict-get response "value"))
269-
"user")))
270-
(cider-set-buffer-ns initial-ns)))))
266+
(cider-set-buffer-ns ns))))
271267

272268
(defun cider-repl-require-repl-utils ()
273269
"Require standard REPL util functions into the current REPL."
274270
(interactive)
275271
(nrepl-send-sync-request
276272
(lax-plist-put
277273
(nrepl--eval-request
278-
"(when (clojure.core/resolve 'clojure.main/repl-requires)
279-
(clojure.core/map clojure.core/require clojure.main/repl-requires))")
274+
cider-repl-require-repl-utils-code)
280275
"inhibit-cider-middleware" "true")
281276
(cider-current-repl)))
282277

283-
(defun cider-repl--build-config-expression ()
284-
"Build the initial config expression."
285-
(when (or cider-repl-print-length cider-repl-print-level)
286-
(concat
287-
"(do"
288-
(when cider-repl-print-length (format " (set! *print-length* %d)" cider-repl-print-length))
289-
(when cider-repl-print-level (format " (set! *print-level* %d)" cider-repl-print-level))
290-
")")))
291-
292-
(defun cider-repl-set-config ()
293-
"Set an inititial REPL configuration."
278+
(defun cider-repl-eval-init-code ()
279+
"Evaluate `cider-repl-init-code' in the current REPL."
294280
(interactive)
295-
(when-let* ((config-expression (cider-repl--build-config-expression)))
281+
(when cider-repl-init-code
296282
(nrepl-send-sync-request
297-
(lax-plist-put
298-
(nrepl--eval-request config-expression)
299-
"inhibit-cider-middleware" "true")
283+
(thread-first cider-repl-init-code
284+
(string-join "\n")
285+
(nrepl--eval-request)
286+
(lax-plist-put "inhibit-cider-middleware" "true"))
300287
(cider-current-repl))))
301288

302289
(defun cider-repl-init (buffer &optional no-banner)
@@ -316,9 +303,8 @@ client process connection. Unless NO-BANNER is non-nil, insert a banner."
316303
;; against user config
317304
(set-buffer orig-buffer)))
318305
((pred identity) (pop-to-buffer buffer)))
319-
(cider-repl-set-initial-ns buffer)
320-
(cider-repl-require-repl-utils)
321-
(cider-repl-set-config)
306+
(cider-repl--set-initial-ns buffer)
307+
(cider-repl-eval-init-code)
322308
(unless no-banner
323309
(cider-repl--insert-banner-and-prompt buffer))
324310
(with-current-buffer buffer

doc/repl/basic_usage.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Please, avoid killing REPL buffers with <kbd>C-c C-k</kbd>
4949

5050
## Known Limitations
5151

52-
Unfortunately the REPL doesn't handle very well big output and they can cause slowdowns and even lockups.
53-
Make sure to clean your REPL buffers from time to time if you notice any slowdowns related to lots of
54-
content in the REPL. It's also prudent to configure result printing with some reasonable setting for
55-
`*print-length*` and `*print-level*` (or `cider-pprint-options` if you're making use of pretty-printing).
52+
Performance can degrade when the REPL buffer grows very large. This is
53+
especially true if `cider-repl-use-clojure-font-lock` is enabled. You can use
54+
`cider-repl-clear-output` to either clear the result of the previous evaluation,
55+
or with a prefix argument clear the entire REPL buffer.

doc/repl/configuration.md

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -184,30 +184,6 @@ behavior if you don't like it.
184184
Alternatively, you can toggle this behaviour on and off using <kbd>M-x
185185
cider-repl-toggle-content-types</kbd>.
186186

187-
Currently, the feature doesn't work well with pretty-printing in the REPL,
188-
so we don't advise you to enable both features at the same time.
189-
190-
## Limiting printed output in the REPL
191-
192-
Accidentally printing large objects can be detrimental to your
193-
productivity. Clojure provides the `*print-length*` var which, if set,
194-
controls how many items of each collection the printer will print. You
195-
can supply a default value for REPL sessions via the `repl-options`
196-
section of your Leiningen project's configuration.
197-
198-
```clojure
199-
:repl-options {:init (set! *print-length* 50)}
200-
```
201-
202-
You can also set `cider-repl-print-length` to an appropriate value (it
203-
defaults to 100). If both `*print-length` and
204-
`cider-repl-print-length` are set, CIDER's setting will take precedence
205-
over the value set through Leiningen.
206-
207-
The preceeding discussion also applies to Clojure's `*print-level*`
208-
variable. The corresponding CIDER variable is
209-
`cider-repl-print-level`, set to `nil` by default.
210-
211187
## Customizing the initial REPL namespace
212188

213189
Normally, the CIDER REPL will start in the `user` namespace. You can

test/cider-repl-tests.el

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,6 @@ PROPERTY shoudl be a symbol of either 'text, 'ansi-context or
142142
(expect (cider--pretty-print-width)
143143
:to-equal fill-column))))
144144

145-
(describe "cider-repl--build-config-expression"
146-
(it "returns nil when all the config values are nil"
147-
(let ((cider-repl-print-length nil)
148-
(cider-repl-print-level nil))
149-
(expect (cider-repl--build-config-expression) :to-equal nil)))
150-
(it "returns an when any the config values are non-nil"
151-
(let ((cider-repl-print-length 10)
152-
(cider-repl-print-level 10))
153-
(expect (cider-repl--build-config-expression)
154-
:to-equal
155-
"(do (set! *print-length* 10) (set! *print-level* 10))"))))
156-
157145
(describe "cider-locref-at-point"
158146
(it "works with stdout-stacktrace refs"
159147
(with-temp-buffer

0 commit comments

Comments
 (0)