How to clear REPL in cider mode?

I do not want to clear the text output of REPL; I mean cleaning up all the results evaluated in the REPL. During development, Cc Cq and Cc Mj repeatedly have low efficiency.


UPDATE

There might be some bad debugging behavior. I'm not sure how other people develop programs with CIDER, but I really need the functionality mentioned above. I think other developers are also facing the same problems as mine.

For example, at the top of the clojure prog element, I use declare to declare a function foo , which is used by another function bar , and foo is implemented after bar , Then I Cc Ck , etc., And the prog goes well. Later, I sometimes deleted the foo declaration. What's happening? Prog is still going well. really ? Then I do all my work and successfully complete the CIDER REPL session.

Calamity in the morning: Symbol foo not found!

This is my story. So, no one has ever encountered similar problems?

+5
source share
5 answers

Try the (refresh) function in the clojure.tools.namespace.repl namespace:

The update function scans all directories in the path to the Clojure source files, reads their ns declarations, plots their dependencies, and downloads them in the order of dependency.

https://github.com/clojure/tools.namespace#reloading-code-usage

It doesn't seem to remove the vars declared in the user namespace that I entered in the REPL, but it does:

... unload (delete) namespaces that have been changed to remove old definitions.

We usually add this plus a few other useful things to the user namespace, so it loads in the REPL at startup:

 (ns user (:require [clojure.tools.namespace.repl :refer [refresh]] [clojure.repl :refer [doc source]] [clojure.pprint :refer [pprint pp]] [midje.repl :as midje] [clojure.stacktrace :as st])) 

To save this code separately from the main and test sources, put it in a file in <project root>/dev/user.clj , then add the following to the lein project.clj file:

 :profiles {:dev {:source-paths ["dev"]}} 

(ps, although this is not a question that you want to answer, for those who see this answer and want to clear the text in CID REPL, it is Cc Mo ( https://github.com/clojure-emacs/cider )

+9
source

As already noted, the β€œright” solution is to use the Stuart Sierra component library.

But since you are working in CIDER, you can use Cc Cx to run cider-refresh , which will reload your project and thereby recreate your initial state.

+6
source

In EMACS, when I work with Clojure in cider mode, I use:

Cc mo in replica buffer

it is attached to cider-repl-clear-buffer

+3
source

If you are dealing with a lot of things that have a state that you want to clear in order to have a clean development environment, you might consider doing one of the following:

1.) Review your design and see how much of this state is really needed. In many situations, you can use stateful atoms, refs, or other stateless objects unnecessarily, and if you take a more functional approach, you won’t have to clarify the development environment often.

Assuming legitimate reasons for using the state:

2.) You can destroy the namespace and all contents with the clojure remove-ns function: for example. for a namespace called user.fancy-namespace , you can clear NS by running (remove-ns 'user.fancy-namespace') and then just reevaluating the namespace. This works well for cleaning up one namespace, but if the state objects that need to be cleaned up are in different namespaces, tedious execution may be required for each namespace.

3.) The Stuart Sierra component library was designed to manage components that include state. It is very useful for managing database connections, memcache clients, etc., but for its full use you will need to re-archive your project.

+1
source

As others have mentioned, you only need to clear repl if you have variables that contain status information. For non-state load-bearing components, it is enough to reload the source buffer (re-evaluate it).

One very interesting workflow management method that has stateful components is the Stuart Seirra component infrastructure. See http://youtu.be/13cmHf_kt-Q

Another approach is to write code using defonce rather than def, which allows you to reload the source code without overriding state variables.

If, on the other hand, you want to do this in order to clear the definitions of defn or defmacro that you don't need, that is, clear the "polution" from your repl, then, frankly, I would not bother. If nothing calls defn or a macro, it really doesn't matter.

+1
source

Source: https://habr.com/ru/post/1212826/


All Articles