Fork me on GitHub
#joyride
<
2022-10-26
>
pez10:10:43

I'm running into a strange problem that is probably just me not understanding things correctly. I've adapted @seancorfield’s clojuredocs script like so:

(ns z-joylib.clojuredocs
  (:require ["$v0" :refer [ranges]]
            ["vscode" :as vscode]
            [clojure.string :as string]
            [joyride.core :as joyride]))

(defn clojuredocs-url [symbol-string]
  (let [qualified-symbol-string (some->> symbol-string
                                         symbol
                                         resolve
                                         symbol
                                         str)]
    (when qualified-symbol-string
      (str "" 
           (-> qualified-symbol-string
               (string/replace "?" "%3f") ; clean up ? ! &
               (string/replace "!" "%21")
               (string/replace "&" "%26"))))))

(defn lookup-current-form-or-selection []
  (try (let [[_ lookup] (ranges.currentForm)
              url (clojuredocs-url lookup)]
         (if url
           (vscode/commands.executeCommand "simpleBrowser.show" url)
           (vscode/window.showInformationMessage
            (str "clojuredocs.cljs, can't resolve: " lookup))))
      (catch :default e
        (println (str "Clojuredocs lookup error: " e)))))

(when (= (joyride/invoked-script) joyride/*file*)
  (lookup-current-form-or-selection))
Then I require it from my user_activate.cljs:
(require '[z-joylib.clojuredocs])
Then I have a keyboard shortcut defined like so:
{
        "command": "joyride.runCode",
        "args": "(z-joylib.clojuredocs/lookup-current-form-or-selection)",
        "key": "ctrl+alt+c d",
    }
Now the strange (to me) thing. Invoking the shortcut fails with "can't resolve <symbol>". It works if I invoke it using Joyride: Run User Script.... And from then on I can use the keybinding too, making the whole thing extra strange. I've tried adding :reload to the user_activate.cljs require. Doesn't make a difference. (Probably shouldn't?).

pez10:10:53

Note that this version of the script uses SCI to resolve the symbols. Where Sean's script uses the Clojure REPL for it. So if Joyride does not include some thing that Clojure includes, resolve will fail with this approach.

seancorfield19:10:33

I have had weird things happen like this if I try to eval command snippets in a file before I've eval'd any code via the REPL (using tap> is what it complains it cannot resolve). As soon as I've evaluated the ns in a file, command snippets work.

pez19:10:03

Is this generally with Calva, @seancorfield? I think it could have to do with that (clojure.core/refer-clojure) needs to be called before things really start to work, and that this somehow gets called when an ns form is evaluated. It's the reason why I always tell people to start with loading their files. (Though I think evaluating its ns form does the trick and some people write files that are not really loadable, which I find to be a strange habit.)

seancorfield19:10:55

Generally, yeah, and that sounds about right that it needs to refer Clojure's core stuff to get the party started...

pez19:10:04

@U04V15CAJ, do you have any idea why I get this behaviour?

borkdude20:10:59

I was at the speakers dinner of #C047C0BE3HC and have yet to catch up on all of this...

🙏 1
borkdude20:10:05

I will install a reminder for Monday 10:00 AM in case I don't report back here before then. A bit busy this weekend...

pez20:10:14

Thanks! There is no hurry. Enjoy the weekend!

borkdude20:10:50

> As soon as I've evaluated the ns in a file, command snippets work. You may need to use a fully qualified function name in your snippets, as the namespace in which the code gets executed may change depending on what is evaluated

borkdude20:10:33

If you can make an issue with smallest repro possible, I'll take another look next week

pez21:10:57

> probably just me not understanding things correctly I think this was the case here. I was trying to make Joyride/SCI resolve things which it might not have required. My dog fooding test was stupid here, because I tried it on symbols in my clojuredocs.cljs script itself, making it seem like it worked when that file was freshly loaded. But in reality it could only resolve things that it had required itself. :man-facepalming: (This probably is hard to unpack, but anyway, I should remove this example from the Joyride repo. 😄 )

pez23:10:53

I fixed the script instead. Now using the REPL connection if it is there, otherwise just look up the word unresolved.