-
Notifications
You must be signed in to change notification settings - Fork 38
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
Also consider cljc files for namespaces-on-classpath #91
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice find!
Please bear in mind that there's https://github.com/clojure-emacs/clj-suitable for cljs completions.
Because namespaces-on-classpath
only returns the namespace name, it's safe (not dependent on reader conditionals) to return results for clj, cljc, and cljs alike.
It would seem desirable to craft a test for namespaces-on-classpath
ensuring that all 3 kinds of results are returned. I could contribute a commit if you happen to be in a rush.
Cheers - V
I quickly scanned clj-suitable. Do I understand your suggestion correctly in that we'd add
🙏🏻 |
Currently, the following is returned:
...it would seem easy enough to make it also include (Note that I chose the word Then, consumers like clj-suitable could remove entries where the |
Adjusted to allow for getting |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM as far as I'm concerned
@@ -93,11 +93,12 @@ | |||
(get-all-full-names prefix)) | |||
;; If prefix doesn't contain a period, using fuziness produces too many | |||
;; irrelevant candidates. | |||
(for [^String ns-str (utils/namespaces-on-classpath) | |||
(for [{^String ns-str :ns, ^String ext :extension} | |||
(utils/namespaces-on-classpath {:name-only false, :extensions #{"clj" "cljc"}}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly, in a future, the value of :extensions
could be derived from the surrounding context
. (See also)
e.g. if I'm a clj-suitable user, I might be able to convey :context :cljs
somehow here, and then :extensions #{"clj" "cljc"}}
as seen here would change, dynamically.
Just commenting that as something I'd intend to do while maintaining clj-suitable. Nothing in the PR is preventing that from happening - great work!
@vemv added tests as that turned out easier than expected. Hope that didn't cross any work you already did. |
Thank you for the PR! Since the scope of the fix has grown, let me grump and bikeshed a little bit. Maybe this is irrational, but computing and attaching this I'm also not a big fan of Let me know if any of this sounds dumb. |
Makes sense!
👍🏻 Should we keep the |
a6c4892
to
587dd71
Compare
I'd rather keep it at the callsite, to be honest. Especially since the new function doesn't return extensions anymore, it shouldn't look at them either. Just return the files and let the caller filter the list. |
src/compliment/utils.clj
Outdated
(for [^String file (all-files-on-classpath classpath) | ||
:when (and (or (.endsWith file ".clj") | ||
(.endsWith file ".cljc") | ||
(.endsWith file ".cljs")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this point, (re-find #"\.clj[cs]?$" file)
would be better.
src/compliment/utils.clj
Outdated
(.endsWith file ".cljs")) | ||
(not (.startsWith file "META-INF"))) | ||
:let [file (ensure-no-leading-slash file) | ||
[_ ^String nsname ^String extension] (re-matches #"[^\w]?(.+)\.(clj[sc]?)" file)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can actually move this :let
up and then use the fact that nsname matches as the filter.
src/compliment/utils.clj
Outdated
[_ ^String nsname ^String extension] (re-matches #"[^\w]?(.+)\.(clj[sc]?)" file)] | ||
:when nsname] | ||
(let [ns-str (.. nsname (replace resource-separator ".") (replace "_" "-"))] | ||
{:ns ns-str, :file file, :extension extension}))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I second @vemv, let's name the key :ns-str
.
src/compliment/utils.clj
Outdated
(transduce | ||
(map :ns) | ||
conj | ||
(namespaces&files-on-classpath {:extensions #{"clj" "cljc"}})))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need both into
and transduce
. It's either (into #{} (map :ns) coll)
or (transduce (map :ns) conj #{} coll)
.
src/compliment/utils.clj
Outdated
@@ -208,9 +232,9 @@ Note that should always have the same value, regardless of OS." | |||
(cache-last-result ::project-resources classpath | |||
(for [path classpath | |||
^String file (list-files path false) | |||
:when (not (or (empty? file) (.endsWith file ".clj") | |||
:when (not (or (empty? file) | |||
(.endsWith file ".clj") (.endsWith file ".cljc") (.endsWith file ".cljs") | |||
(.endsWith file ".jar") (.endsWith file ".class")))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also makes sense to pack it into a single regex at this point.
test/dummy.cljs
Outdated
@@ -0,0 +1,2 @@ | |||
;; For testing (compliment.t-utils) purpose |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better mention that it is for testing namespaces&files-on-classpath
directly.
@alexander-yakushev thanks for the comments! Changes applied. |
fwiw, LGTM! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few nitpicks before we are good to merge.
(nscl-matches? prefix ns-str) | ||
(.startsWith ns-str prefix))] | ||
{:candidate ns-str, :type :namespace}) | ||
(for [{^String ns-str :ns-str, ^String file :file} (utils/namespaces&files-on-classpath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keys match names now, {:keys [^String ns-str, ^String file]}
is preferred.
(.startsWith ns-str prefix))] | ||
{:candidate ns-str, :type :namespace}) | ||
(for [{^String ns-str :ns-str, ^String file :file} (utils/namespaces&files-on-classpath) | ||
:when (and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary line break after and
.
src/compliment/utils.clj
Outdated
:when nsname] | ||
(.. nsname (replace resource-separator ".") (replace "_" "-"))))))) | ||
(set (cache-last-result ::namespaces-on-classpath classpath | ||
(for [^String file (all-files-on-classpath classpath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
file
doesn't need a type hint anymore. Besides, none of the code around used binding pairs alignment, so I'd rather keep it that way.
src/compliment/utils.clj
Outdated
|
||
(defn project-resources | ||
"Returns a list of all non-code files in the current project." | ||
[] | ||
(let [classpath (classpath)] | ||
(cache-last-result ::project-resources classpath | ||
(for [path classpath | ||
(for [path classpath |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
src/compliment/utils.clj
Outdated
:when (not (or (empty? file) (.endsWith file ".clj") | ||
(.endsWith file ".jar") (.endsWith file ".class")))] | ||
:when (not (or (empty? file) | ||
(re-find #"\.(clj[cs]?|jar|class)" file)))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing $
at the end of the regex.
src/compliment/utils.clj
Outdated
:let [[_ ^String nsname] (re-matches #"[^\w]?(.+)\.clj" file)] | ||
:when nsname] | ||
(.. nsname (replace resource-separator ".") (replace "_" "-"))))))) | ||
(set (cache-last-result ::namespaces-on-classpath classpath |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
set
moved out of the caching form which is bad, we want to cache that transformation too. However, in the new function, set
doesn't achieve what it used to do before for strings, since it is unlikely that we'll have duplicate filenames (but there could be duplicate namespaces). So, to match the prior behavior, we need something like distinct-by
which does not exist. Or might just leave it as is for now (by removing set
) and maybe place a ;; TODO
for future deduplication.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point - added the TODO, but also wondering if it's relevant now that we return clj, cljc and cljs results?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is quite minor, to be honest. There is a slim chance that two same namespaces on the classpath come from different files, and somebody using this new function will show both of them (but not Compliment, since it performs its own deduplication later). And that is... not a big issue:).
e5d31fa
to
f4cab7a
Compare
...and introduce namespaces&files-on-classpath that yields collection of maps with e.g. :file. Fixes alexander-yakushev#90
Looks perfect now, thank you for following through! |
Fixes #90