-
-
Notifications
You must be signed in to change notification settings - Fork 247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
clojure-expected-ns and custom :source-paths #372
Comments
If we had the classpath from cider, we could just use it directly rather than resorting to filesystem hacks. The following function seems to work (but depends on (defun expected-ns-from-classpath ()
(let* ((file-path (buffer-file-name))
(rp (reduce
(lambda (r p)
(let ((rp (when (string/starts-with file-path p)
(substring file-path (length p)))))
(if (and r rp (< (length rp) (length r)))
rp
(or rp r))))
(cider-sync-request:classpath)
:initial-value nil)))
(when rp
(let* ((ns (substring rp 1 (- (length (file-name-extension rp t)))))
(ns (replace-regexp-in-string "/" "." ns))
(ns (replace-regexp-in-string "_" "-" ns)))
ns)))) Perhaps this function should be added to cider rather than adding more hacks to |
That's a reasonable approach.
I'm guessing this is some pseudocode, right? :-) I'll have to dwell on it a bit, as it's not immediately apparent to me what this is supposed to do. In general, I'm not opposed to adding such functionality to cider, just keep in mind that not everyone is using cider - some people use inf-clojure, some use monroe, etc. Maybe it's worth implementing both ideas, maybe just the second one. |
It's actually working code, sorry it's not clear, I'm not as fluent in elisp as clojure. It first finds the relative path ( I'm thinking this might make a nice addition to cider. Then tools could use cider (if available) to find the expected namespace. The main use case I have in mind is |
This version of the function may be a little more clear: (defun expected-ns ()
(let* ((file-path (buffer-file-name))
(relpath (->> (cider-sync-request:classpath)
(-map (lambda (p)
(when (string/starts-with file-path p)
(substring file-path (length p)))))
(-filter 'identity)
(-sort #'string<)
(car))))
(when relpath
(->> (substring relpath 1) ; remove leading /
(file-name-sans-extension)
(replace-regexp-in-string "/" ".")
(replace-regexp-in-string "_" "-"))))) |
Sounds good. Just a few things in mind:
With this in mind - feel free to create a cider PR. |
Sounds good. I'll start with a PR to get the function into cider. Then we can change the cljr dependency and move other things around separately. |
coolio! |
See also clojure-emacs/cider#1622 |
(clojure-expected-ns)
returns the wrong ns if you organize project.clj with :source-paths ["src/server" "src/client" "src/common"].clojure-expected-ns
just chops off the "src/" from the path and makes a ns out of the rest, so it adds an extra e.g. "server." prefix.It already special-cases "src/clj" and "src/cljs" via a regexp. What if we just made that regexp a defcustom for people who organize their project with custom source-paths?
If that's the right fix, I'd be happy to take a stab at a PR. If there's a better way, I'd love to hear it!
The text was updated successfully, but these errors were encountered: