This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-03-20
Channels
- # beginners (7)
- # boot (39)
- # braid-chat (3)
- # braveandtrue (1)
- # cider (27)
- # cljsjs (15)
- # cljsrn (6)
- # clojars (18)
- # clojure (307)
- # clojure-art (1)
- # clojure-brasil (1)
- # clojure-italy (2)
- # clojure-poland (3)
- # clojure-russia (61)
- # clojure-sdn (2)
- # clojure-taiwan (4)
- # clojure-uk (5)
- # clojurebridge (7)
- # clojurescript (19)
- # core-async (1)
- # core-matrix (1)
- # cursive (35)
- # datomic (3)
- # emacs (51)
- # euroclojure (3)
- # hoplon (20)
- # jobs (1)
- # keechma (1)
- # mount (3)
- # off-topic (2)
- # om (177)
- # onyx (96)
- # parinfer (4)
- # pedestal (4)
- # re-frame (19)
- # reagent (7)
- # untangled (5)
When I create a new clojure file in emacs, it automatically puts a (ns foo.bar.baz)
line at the top of the file. Cool. Unfortunately, the namespace path is wrong because my lein profile specifies custom :source-paths
. Anyone know any emacs settings related to this? I'm not even sure if it's clojure-mode or cider or refactor-nrepl that is doing it.
Clj-refactor is adding the form. But I think it uses some function in clojure-mode to decide the namespace.
@malabarba: Thanks, that’s very helpful! sure enough, (clojure-expected-ns)
of clojure-mode.el returns the wrong ns. Now to see what that function is doing...
OK, looks like clojure-expected-ns
splits the path relative to project.clj or build.boot, then drops the first directory (usually “src”), then drops the next dir if it matches #"clj[scx]?”. So if your project’s :source-paths have “src“ or “src/clj” it works, but if it’s “src/client” or “src/server” then you get an extra “client.” or “server.” prefix to the generated namespaces.
Not sure what could be done about this without knowing about the runtime classpath or build tool config
Both cider-nrepl and refactor-nrepl know about the classpath. Perhaps an opportunity for a PR? ;)
@benedek I like the behavior, just want the correct ns to be added . I posted an issue with an idea for a kludgy fix on clojure-mode https://github.com/clojure-emacs/clojure-mode/issues/372
It would be awesome if there was a way to use cljr’s or cider’s knowledge of classpath to do it better
I agree. Check out cider-nrepl or refactor-nrepl. These would be right places. You can also find examples there which work with the classpath
cool, i hacked together a function that returns the expected ns using cider classpath (please excuse the elisp, not as familiar with elisp as clojure):
(defun clojure-expected-ns-from-classpath ()
(let* ((file-path (buffer-file-name))
(relpath (reduce (lambda (r p)
(let ((relpath (when (string/starts-with file-path p)
(substring file-path (length p)))))
(cond ((not relpath) r)
((not r) relpath)
((< (length relpath) (length r)) relpath)
(t r))))
(cider-sync-request:classpath)
:initial-value nil)))
(when relpath
(let* ((sans-type (substring
relpath 0 (- (length (file-name-extension relpath
t)))))
(ns (mapconcat 'identity (cdr (split-string sans-type "/")) "."))
(ns (replace-regexp-in-string "_" "-" ns)))
ns))))
Clojure mode does not have a nrepl middleware component afaik. I would say cider but @bozhidar @malabarba may think otherwise
Thanks! It’s fun learning some elisp, and also making me grateful I spend most of my time writing in clojure
Putting a function like this in cider makes sense. Then cljr could call cider-expected-ns
rather than clojure-expected-ns
.
cljr could call cider’s insert-expected-ns rather than clojure-mode’s. (cider would know better what ns to insert)
Oh yeah makes total sense. i wonder how often clojure-expected-ns is used when there is not a cider connection present. Personally my only use case is cljr-add-ns-to-blank-clj-files
.
as I mentioned on the ticket my only reservation is that such a feature might be useful for users of inf-clojure
as well
I've a few times that I wouldn't mind moving simple useful features from cljr to clojure-mode and cider
Awesome, I can submit a PR to cider. My only question is whether I get to use ->>
, -map
, and -filter
. They help express this function and cljr uses them, but cider currently does not. Are these built into elisp or some dependency? (I assume we don’t want to add new dependencies to cider just for this)