Internals question: I'm trying to find the references (invocations) to a function for a test file. As part of this, I'm using this (hardcoded for testing) call (q/find-element-from-sym db 'user 'foo-bar-2) to find foo-bar-2 elements in this file:
(defn foo-bar-2 []
(println "hi"))
(foo-bar-2)
This works fine in a unit test, but in the editor (Calva), with the same code, q/find-element-from-sym returns nil.
There is no namespace in either the test or the editor file.
Any idea what might cause this? (more in thread)we define what is internal vs external according to the source-paths which are defined in the clojure-lsp startup
so it's likely that in calva, the source-paths are not including that file, so clojure-lsp is considering external of the proejct
one quick way to debug that, is call server info (I believe there is a calva command for that) which should show the classpath + source-paths considered by clojure-lsp
Ah, I see. You're right that the file in Calva isn't in the source path. But... what does that mean for q/find-element-from-sym? Does that just not work for external files, despite the data being in the database? If so, do I 1) sort through the database myself (without clojure-lsp.queries) to find the element, 2) use a different clojure-lsp.queries API or 3) assume that the user knows that some refactorings won't work for external files?
Hm... I could try q/find-element-under-cursor.
can you elaborate what kind of problem you are trying to solve for me to understand why you got to q/find-element-from-sym
some functions from clojure-lsp.queries were not supposed to be public, I never managed to create a public API for it, but maybe we should
q/find-element-under-cursor, although its name, it checks only for internal-analysis https://github.com/clojure-lsp/clojure-lsp/blob/70daed87e9858b13e3d878df183b16fda808d735/lib/src/clojure_lsp/queries.clj#L823 especially for performance reasons
q/find-element-under-cursor is more likely to work
Ah yeah, I saw that, which made me suspicious when I saw the external: true part I'm working on an inline-function refactoring. I walked up from the cursor to the function name (after the defn) and then I'm trying to find the definition in the database so I can find all the call points to inline via q/find-references
I'll try q/find-element-under-cursor, I don't know how I didn't see that at first
I see, for internal usage, q/find-elemnt-under-cursor and q/find-definition are the ones
you can see that q/find-element-under-cursor has lots of usages across clojure-lsp
That did the trick! Thanks!
I looked into the database with (get-in db [:analysis uri]) and found the 'foo-bar-2 symbol. The unit tests had:
external? false,
while in the editor, the same code has:
external? true,
Here's the diff from the database: