简体   繁体   English

如何在苹果酒模式下清除REPL?

[英]How to clear the REPL in cider-mode?

I am not meaning cleaning up the text output of REPL; 我不是要清理REPL的文本输出; I mean cleaning up all evaluated results in REPL. 我的意思是清理REPL中的所有评估结果。 During developing, repeatedly Cc Cq and Cc Mj is low efficiency. 在开发过程中,反复的Cc CqCc Mj效率低。


UPDATE UPDATE

There may be some bad debug behaviour of mine. 我的调试行为可能有些不好。 I am not sure how other people develop progs with CIDER, but I really need the functionality mentioned above. 我不确定其他人如何用CIDER开发prog,但我真的需要上面提到的功能。 I guess other developers also encounter same problems as mine. 我猜其他开发人员也会遇到与我相同的问题。

For example, at the top of a clojure prog unit, I use declare to declare a function foo , which is used by another function bar , and foo is implemented after bar . 例如,在clojure prog单元的顶部,我使用declare来声明一个函数foo ,它被另一个函数bar ,而foo是在bar之后实现的。 Then, I Cc Ck , etc, and the prog goes well. 然后,我Cc Ck等,编程进展顺利。 Later, I removed the forward declaration of foo occasionally. 后来,我偶尔删除了foo的前向声明。 What does happen? 怎么了? The prog still goes well. 编程仍然顺利。 REALLY ? 真的吗? Then, I commit my whole job and terminate the CIDER REPL session happily. 然后,我完成整个工作并愉快地终止CIDER REPL会话。

Disaster on morning: Symbol foo not found! 早上的灾难:没有找到符号foo

That's my story. 那是我的故事。 So, nobody has ever encountered similar problems? 那么,没有人遇到类似的问题?

Try the (refresh) function in the clojure.tools.namespace.repl namespace: 尝试clojure.tools.namespace.repl命名空间中的(refresh)函数:

The refresh function will scan all the directories on the classpath for Clojure source files, read their ns declarations, build a graph of their dependencies, and load them in dependency order. 刷新函数将扫描类路径上Clojure源文件的所有目录,读取其ns声明,构建其依赖关系图,并按依赖顺序加载它们。

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

It doesn't seem to remove the vars declared in the user namespace, which I've typed into the REPL, but it does: 它似乎没有删除在user命名空间中声明的变量,我已经在REPL中输入,但它确实:

...unload (remove) the namespaces that changed to clear out any old definitions. ...卸载(删除)更改的名称空间以清除任何旧定义。

We generally add that plus a few other useful things to the user namespace, so it's loaded into the REPL on startup: 我们通常会在user命名空间中添加一些其他有用的东西,因此它会在启动时加载到REPL中:

(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 keep that code separate from your main and test sources, put that in a file at <project root>/dev/user.clj , then add the following to your lein project.clj file: 要将该代码与main和test源分开,请将其放在<project root>/dev/user.clj的文件中,然后将以下内容添加到lein project.clj文件中:

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

(ps although it's not the question you want answered, for those seeing this answer and wanting to clear the text in the Cider REPL, it's Cc Mo ( https://github.com/clojure-emacs/cider ) (ps虽然这不是你想要回答的问题,对于那些看到这个答案并希望清除Cider REPL中文本的人来说,它是Cc Mohttps://github.com/clojure-emacs/cider

Like others have already pointed out, the 'proper' solution is to use Stuart Sierra's component library. 像其他人已经指出的那样,“正确”的解决方案是使用Stuart Sierra的组件库。

But since you're running in CIDER, you can use Cc Cx to run cider-refresh , which will reload your project and thereby recreate your initial state. 但是,由于您在CIDER中运行,您可以使用Cc Cx来运行cider-refresh ,这将重新加载您的项目,从而重新创建您的初始状态。

In EMACS when I works with Clojure in cider-mode, I use: 在EMACS中,当我在苹果酒模式下使用Clojure时,我使用:

Cc Mo in the repl buffer 在repl缓冲区中的Cc Mo.

it is bound to cider-repl-clear-buffer 它与cider-repl-clear-buffer绑定

If you're dealing with a large number of things which have state which you want to clear to have a clean developing environment you might consider doing one of the following: 如果您正在处理大量具有您想要清除的状态以具有干净的开发环境的事物,您可以考虑执行以下操作之一:

1.) Reassess your design and see how much of that state is actually necessary. 1.)重新评估您的设计,看看实际需要多少状态。 In many situations you might be using atoms, refs, or other stateful items unnecessarily and if you adopt a more functional approach, you won't find yourself needing to clear up your development environment as often. 在许多情况下,您可能会不必要地使用原子,引用或其他有状态项目,如果采用更具功能性的方法,您将不会发现自己需要经常清理开发环境。

Presuming legitimate reasons for using state: 假定使用州的合法理由:

2.) You can wipe out a namespace and all of it's contents by using the clojure function remove-ns : eg for a namespace called user.fancy-namespace you can clean the NS out by running (remove-ns 'user.fancy-namespace') and then simply re-evaluating the namespace. 2.)您可以使用clojure函数remove-ns来清除命名空间及其所有内容:例如,对于名为user.fancy-namespace您可以通过运行(remove-ns 'user.fancy-namespace') NS (remove-ns 'user.fancy-namespace')然后只需重新评估命名空间。 This works well for cleaning up a single namespace, but if the stateful items you need cleaned are in other namespaces, it can get tedious to do this for every namespace involved. 这适用于清理单个命名空间,但如果需要清理的有状态项位于其他命名空间中,则对于涉及的每个命名空间执行此操作会非常繁琐。

3.) Stuart Sierra's component library was designed to manage components which involve state. 3.)Stuart Sierra的组件库旨在管理涉及国家的组件。 Very useful for managing DB connections, memcache clients, etc, but will require a re-architecting of your project in order to make full use of it. 对于管理数据库连接,内存缓存客户端等非常有用,但需要对项目进行重新架构才能充分利用它。

As mentioned by others, it is only necessary to clear the repl if you have variables which are holding state information. 正如其他人所提到的,只有在你拥有保存状态信息的变量时才需要清除repl。 For non-state carrying components, just reloading the source buffer (re-evaluating it) is sufficient. 对于非状态承载组件,只需重新加载源缓冲区(重新评估它)就足够了。

One very interesting way to manage a workflow which does have components that track state is Stuart Seirra's component framework. 管理具有跟踪状态的组件的工作流的一种非常有趣的方式是Stuart Seirra的组件框架。 See http://youtu.be/13cmHf_kt-Q http://youtu.be/13cmHf_kt-Q

Another approach is to write your code using defonce rather than def, which will allow you to reload your source code without redefining state variables. 另一种方法是使用defonce而不是def来编写代码,这将允许您重新加载源代码而无需重新定义状态变量。

If on the other hand, you want to do this to clean up defn or defmacro definitions you don't need ie clean out 'polution' from your repl, then to be honest, I wouldn't bother. 如果另一方面,你想这样做来清理defn或defmacro定义,你不需要从你的repl中清除'polution',那么说实话,我不会打扰。 If nothing is calling a defn or macro, it really doesn't matter. 如果没有什么东西可以调用defn或macro,那真的没关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM