Fork me on GitHub
#clj-kondo
<
2023-02-28
>
lassemaatta12:02:01

How does the output of a clj-kondo analyze-call hook interact with the symbol highlighting in emacs+clojure-lsp?

borkdude12:02:33

depends what your hook outputs

lassemaatta12:02:43

I'm working on a hook for a macro. I think I got everything correct, and the output of the hook looks fine

lassemaatta12:02:23

but when I move the point in emacs inside some call site of the macro, emacs likes to highlight the whole macro form instead of just the symbol at point

borkdude12:02:43

ah, this is probably a location metadata issue

borkdude12:02:06

each incoming node has locations as metadata. when you create new nodes and they don't have locations, they inherit it from the parent node

lassemaatta12:02:17

Ah, I see. What I'm working on is a macro (actually several) which are called a bit like this

(defsomething name-of-the-thing
  "an optional docstring"
  {:a-map "with some parameters"
   :options [:and-some :extra-args]}
  [some-arg arguments]
  (foo)
  (bar some-arg)
  (baz extra-arg))
and I'm transforming it into something like
(do
  (defn name-of-the-thing
        "an optional docstring"
        [some-arg arguments and-some extra-args]
        (foo)
        (bar some-arg)
        (baz extra-arg))
  (def name-of-the-thing-options
       {:a-map "with-some-parameters"}))

borkdude12:02:57

makes sense

borkdude12:02:18

you could also put the options on the metadata of the var maybe?

borkdude12:02:28

I mean in your real code, not clj-kondo

lassemaatta12:02:33

yeah, the real macro is quite complex filled with all sorts of nasty magic, the representation above is just what the hook produces which is a simplification of the real thing

lassemaatta12:02:57

but I'll try that with-meta stuff, thanks for the tip

borkdude12:02:35

yes, or vary-meta to not remove previous metadata

lassemaatta12:02:18

is there any documentation regarding this location metadata thing? or is it rather an implementation detail of clj-kondo?

ericdallo12:02:46

Good point, I don't recall having any docs about that detail

borkdude12:02:26

It's not really clear. A PR to clear that up is more than welcome

lassemaatta13:02:44

Right, I think I got my problem fixed, thanks a lot for the help 👍 Btw, is there some "one clever trick" to see how the source node(s) location metadata(s) gets mapped to the output node(s)? I'm testing this in the REPL with api/parse-string so I guess I could perhaps walk the node and merge the metadata with the clj-rewrite nodes under some key and tap> them out to at least see the values..

borkdude13:02:08

not that I know of

rafaeldelboni21:02:39

Hey hello folks, I have the following project structure and I'm trying to call kondo/run! to get the analysis of the file from the right panel, but when I evaluate the (def test-analysis...) my repl gets stuck What I'm doing wrong?

borkdude21:02:19

Define "REPL gets stuck". Maybe the output is too large?

rafaeldelboni21:02:15

My fans go crazy, but it stays there forever.

rafaeldelboni21:02:01

I thought about the output, thats why I declared it into a def to see if was the problem.

dpsutton21:02:27

i was doing this yesterday. I’m on a recent m1 macbook pro and it takes 25 seconds for me. How long have you waited?

kondo=> (time (do (clj-kondo/run! {:lint ["src"
                                          ;; and your other source paths
                                          ]
                                   :config {:analysis true
                                            :output {:format :edn}}
                                   :config-dir ".clj-kondo"})
                  :done))
"Elapsed time: 25110.974458 msecs"

rafaeldelboni21:02:49

maybe a minute, I will wait more hahaha

borkdude21:02:04

try a lesser amount of code first? ;)

borkdude21:02:29

oh wait it's only src/test.clj?

dpsutton21:02:43

also, i think :lint needs a seqable collection right?

borkdude21:02:52

yes, it needs to be a vector

borkdude21:02:12

maybe a single val is supported, don't remember

dpsutton21:02:25

otherwise it tries to lint “s”, then “r”, “c” … each saying file not found

rafaeldelboni21:02:50

oh wow, ok! Thanks a lot!

dpsutton21:02:58

:findings [{:filename "c",
             :type :file,
             :col 0,
             :row 0,
             :message "file does not exist",
             :level :error}
            {:filename "r",
             :type :file,
             :col 0,
             :row 0,
             :message "file does not exist",
             :level :error}
            {:filename "s",
             :type :file,
             :col 0,
             :row 0,
             :message "file does not exist",
             :level :error}]

borkdude21:02:04

still it's weird that it makes fans spin :)

rafaeldelboni21:02:06

yeah the full path to the file was big: /Users/rafael.delboni/Workspaces/clj/poc-scrapper/src/test.clj

rafaeldelboni21:02:14

if it was trying each char

rafaeldelboni21:02:09

Side note: I just parsed an entire jar, including downloading, unzipping and parsing with only 37 lines of clojure, this is so cool hahaha (sorry)

(ns scratch
  "FIXME: my new org.corfield.new/scratch project."
  (:require [babashka.fs :as fs]
            [clj-kondo.core :as kondo]
            [ :as io]
            [clojure.tools.deps :as deps]))

(defn unpack-jar!
  [local-jar-path target-dir]
  (let [jar-contents-dir (io/file target-dir)]
    (fs/delete-tree jar-contents-dir)
    (fs/unzip local-jar-path jar-contents-dir)
    jar-contents-dir))

(defn get-jar
  [project version]
  (-> (deps/resolve-deps {:deps {project {:mvn/version version}}
                          :mvn/repos {"central" {:url ""},
                                      "clojars" {:url ""}}}
                         nil)
      (get project)
      :paths
      first))

(defn get-jar-files
  [project version]
  (-> (get-jar project version)
      (unpack-jar! (str project "/" version))
      (fs/glob "**{.clj,cljc,cljs}")))

(defn kondo-run!
  [paths]
  (-> {:lint paths
       :config {:output {:format :edn}
                :analysis true}}
      kondo/run!
      :analysis))

(comment
  (let [project 'babashka/fs
        version "0.2.16"]
    (-> (get-jar-files project version)
        kondo-run!)))

borkdude21:02:56

You don't need to unpack the jar, clj-kondo can analyze jar files, so this could become one line

borkdude21:02:35

well, except for the tools deps part ;)

rafaeldelboni21:02:03

So I can do (kondo/run! {:lint [jar-path] ...])?

borkdude21:02:21

you can pass an entire classpath as well

borkdude21:02:30

[classpath]