Fork me on GitHub
#lsp
<
2024-04-28
>
dpsutton16:04:40

Does anyone know the cause of the following behavior? We have a macro api/defendpoint that wraps around compojure and offers some nice amenities. Sometimes when my cursor is in the form everything behaves as normal. Sometimes the entire form is highlighted, and navigation just goes to the top of the form. Screenshots in 🧵

dpsutton16:04:11

Point in the form and just the current token is highlighted.

dpsutton16:04:24

Navigation works as expected. I can go to the definition of that symbol

dpsutton16:04:56

Here the entirety of the form is highlighted. Navigation just goes to the top of the current form

dpsutton16:04:53

That macro is configed with :macroexpand in .clj-kondo/config.edn and the definition of it’s expansion is

(defmacro defendpoint
  "Macro for api.common/defendpoint*"
  [method route & body]
  (let [method-fn  (symbol "compojure.core" (str method))
        route-name (route-fn-name method route)]
    `(do
       ~method-fn ;; register usaged
       (defn ~route-name ~@body))))

ericdallo17:04:03

That's a know behavior of custom macros that have hooks

ericdallo17:04:31

I had the same on nubank/state-flow macros, but there is a way to fix that that we Implemented on clj-kondo to support those cases

ericdallo18:04:03

Ah it was not state-flow, it was another internal macro, but I can share the hook code:

(ns nubank.common-graphql.lacinia.sweet
  (:require [clj-kondo.hooks-api :as hooks]))

(defn defresolver
  "Expands to: `(def my-resolver (do ...))`"
  [{:keys [node]}]
  (let [[{name-kw :k} & tail] (rest (:children node))]
    {:node (with-meta
             (hooks/list-node
              [(hooks/token-node 'def)
               (with-meta (symbol (str (namespace name-kw)
                                       "-"
                                       (name name-kw)))
                 (meta name-kw))
               (hooks/list-node
                (concat [(hooks/token-node 'do)]
                        tail))])
             (meta node))
     :defined-by 'common-graphql.lacinia.sweet/defresolver}))

ericdallo18:04:26

The secret here is to use the with-meta to define the meta of the expanded code with the macro code

👀 1