2016-07-07 51 views
1

我經常發現自己打字(ns user)並按C + cM + n反覆當我工作clojure源代碼。問題是我經常使用像sourcedoc這樣的函數,它們在clojure.repl之內,我不想把它們放到我的命名空間中:require。在這種情況下,有經驗的clojurian在做什麼?在interacitve開發過程中使用clojure.repl函數的最佳做法是什麼?

澄清:我知道clojure的命名空間是如何工作的。我想實現的目標是能夠呼叫(source myfunc),(doc myfunc)等,而無需在REPL 中使用完全限定的名稱,而無需在每個名稱空間中都需要使用來自clojure.repl的功能。

+1

在[我的工作流(http://codereview.stackexchange.com/q/126376/82369),我想保持一個單獨的源文件來保存我的'user'命名空間,所有它的依賴關係。例如,當我不使用[Component](https://github.com/stuartsierra/component)時,我的'dev/user.clj'文件的內容通常如下所示:'(ns user(:需要[clojure.pprint:參考[pprint]] [clojure.repl:參考[doc源]]))' –

+1

您提到'Cc Mn'。如果您在Emacs中使用CIDER,則「M-。」與「source」等效。和'C-d d'' doc'。你不需要輸入'myfunc',這個文本上已經有了點。 –

回答

2

感謝您清理你所要求的東西。

Leiningen有一個叫做功能:注射您可以combine with vinyasa如果你把這樣的事情在你的個人資料leiningen得到這樣的效果:

〜/雷音/ profiles.clj:

{:user {:plugins [] 
     :dependencies [[im.chit/vinyasa "0.1.8"]] 
     :injections [(require 'vinyasa.inject) 
         (vinyasa.inject/inject 
         'clojure.core '> 
         '[[clojure.repl doc source] 
         [clojure.pprint pprint pp]])]}} 

因爲這是在你的profiles.clj中,它只會影響你。從事該項目的其他人不會受到影響。


因爲注入clojure.core會讓我感到有點不安,所以我遵循vinyasa作者的建議並注入一個名爲空間的名稱空間。這是由我的個人資料爲我工作的每個項目製作的。這個命名空間總是存在,這使得這些函數即使在尚未引用clojure.core的新創建的命名空間中也能正常工作。

我的〜/ .lein/profiles.clj:

{:user 
    {:plugins [] 
    :dependencies [[spyscope "0.1.4"] 
        [org.clojure/tools.namespace "0.2.4"] 
        [io.aviso/pretty "0.1.8"] 
        [im.chit/vinyasa "0.4.7"]] 
    :injections 
    [(require 'spyscope.core) 
    (require '[vinyasa.inject :as inject]) 
    (require 'io.aviso.repl) 
    (inject/in ;; the default injected namespace is `.` 

       ;; note that `:refer, :all and :exclude can be used 
       [vinyasa.inject :refer [inject [in inject-in]]] 
       [clojure.pprint :refer [pprint]] 
       [clojure.java.shell :refer [sh]] 
       [clojure.repl :refer [doc source]] 
       [vinyasa.maven pull] 
       [vinyasa.reflection .> .? .* .% .%> .& .>ns .>var])]}} 

它是這樣工作的:

hello.core> (./doc first) 
------------------------- 
clojure.core/first 
([coll]) 
    Returns the first item in the collection. Calls seq on its 
    argument. If coll is nil, returns nil. 
nil 
hello.core> (in-ns 'new-namespace) 
#namespace[new-namespace] 
new-namespace> (./doc first) 
nil 
new-namespace> (clojure.core/refer-clojure) 
nil 
new-namespace> (./doc first) 
------------------------- 
clojure.core/first 
([coll]) 
    Returns the first item in the collection. Calls seq on its 
    argument. If coll is nil, returns nil. 
nil 
+0

我編輯了我的問題。 –

+1

我編輯回答你的新問題 –

1

要做到這一點,你可以使用vinyasa庫,特別是其inject功能。基本上你需要從clojure.repl命名空間添加需要的功能到clojure.core命名空間。在你不需要明確地要求它們之後。請參閱以下內容:

user> (require '[vinyasa.inject :refer [inject]]) 
nil 

;; injecting `source` and `doc` symbols to clojure.core 
user> (inject '[clojure.core [clojure.repl source doc]]) 
[] 

;; switching to some other namespace 
user> (require 'my-project.core) 
nil 
user> (in-ns 'my-project.core) 
#namespace[my-project.core] 

;; now those functions are accessible w/o qualifier 
my-project.core> (doc vector) 
------------------------- 
clojure.core/vector 
([] [a] [a b] [a b c] [a b c d] [a b c d e] [a b c d e f] [a b c d e f & args]) 
    Creates a new vector containing the args. 
nil 
相關問題