joyride

Jan 2025-05-28T21:41:36.049999Z

Hi guys! I am writing a script to search Clojars for a dep and insert the result at the cursor position. Technically it works, but the following function errors with "Maximum call stack size exceeded" and I just cannot figure out why - could you give me a hint? I just don't get where there could possibly occur a recursion.

(defn insert-at-cursor-position
  [text]
  (let [editor (.-activeTextEditor vscode/window)]
    (when editor
      (let [position (-> editor
                         (.-selection)
                         (.-active))]
        (when position
          (.edit editor (fn [builder]
                          (.insert builder position text))))))))

Jan 2025-05-28T21:42:51.778599Z

It errors both in REPL and when executed as a user script via keybindings

borkdude 2025-05-28T21:44:05.411199Z

sometimes you can get a stackoverflow in the REPL due to printing of circular objects

borkdude 2025-05-28T21:44:13.836169Z

e.g. js/globalThis

borkdude 2025-05-28T21:45:01.080199Z

Maybe try returning nil from the function to exclude that possibility?

Jan 2025-05-28T21:47:36.012129Z

Well, it really was that easy šŸ™‚ Thank you so much! But honestly I don't quite understand it. What happens there?

borkdude 2025-05-28T21:48:56.603119Z

.edit returns a promise right? I don't remember exactly but perhaps joyride prints the result of the promise and it contains a large object which causes the overflow

Jan 2025-05-28T21:49:58.942229Z

It returns a JS Thenable<boolean>, which seems to be a promise, yes

borkdude 2025-05-28T21:50:19.181949Z

hmm, just a boolean. nothing harmful I'd say

borkdude 2025-05-28T21:51:02.810659Z

I don't have vscode + joyride running at the moment, perhaps @pez could help debug this further

Jan 2025-05-28T21:51:48.881459Z

I thought there might be something obvious I was missing. I will try to find out what happens there. Anyway: thanks a lot!

šŸ‘ 1
pez 2025-05-28T21:54:00.999609Z

All I can say is that this happens to me a lot.

pez 2025-05-28T21:55:30.642889Z

Lovely script idea. I’d have use for that. Please feel encouraged to add as an example to the Joyride repo.

Jan 2025-05-28T21:58:25.937089Z

Sure, I will add it

borkdude 2025-05-28T22:02:03.266969Z

All I can say is that this happens to me a lot.My hunch is this is related to printing something. When invoking a script outside the REPL (via a keybindig), does it also happen and does joyride print anything in that case?

Jan 2025-05-28T22:06:42.745899Z

No not really. All it prints is

clojars.cljs evaluated.
=> 

borkdude 2025-05-28T22:07:03.720889Z

I mean when you didn't return nil

Jan 2025-05-28T22:07:55.270629Z

That is for the case without returning nil. With returning nil it is

clojars.cljs evaluated.
=> nil

borkdude 2025-05-28T22:08:23.397409Z

and without returning nil, did you get the stackoverflow? this would explain not seeing a value after =>

Jan 2025-05-28T22:10:24.562229Z

Yes without nil it overflows

borkdude 2025-05-28T22:13:13.745689Z

if you make a small function that just returns a promise, e.g.:

(defn foo [] (js/Promise.resolve true))
and call this in the REPL, do you get the same problem?

Jan 2025-05-28T22:14:53.397949Z

No that's no probem

Jan 2025-05-28T22:15:32.403929Z

šŸ‘ 1
borkdude 2025-05-28T22:15:59.065859Z

what if you print the value that .edit returns yourself? what happens in that case? you could try directly printing it or in a .then callback

Jan 2025-05-28T22:19:16.744849Z

(-> (.edit editor (fn [builder]
                              (.insert builder position text)))
              (p/then #(println %))))))))
Prints #<Promise[pending:27]>

borkdude 2025-05-28T22:20:36.159129Z

that doesn't make sense to me since the println should print the "contents" of the promise?

borkdude 2025-05-28T22:21:33.238729Z

could you try just .then instead of p/then?

borkdude 2025-05-28T22:22:11.009519Z

gonna call it a night

Jan 2025-05-28T22:23:33.631759Z

Thanks for helping debugging šŸ’Ŗ

Jan 2025-05-28T22:24:18.884359Z

The output with .then is

; true
#<js/Promise[~]>

pez 2025-05-28T22:29:09.675059Z

I use inline def to capture the unwrapped promise.

borkdude 2025-05-28T22:31:06.637999Z

true, but that wasn't the point here. in this case, printing forces the result to be nil (thus avoiding the SO) while still seeing the value. anyway, it would be nice if that SO could be fixed. But going to get some sleep now

Jan 2025-05-28T22:32:13.304459Z

I will also finish for today. I will create a PR for the Joyride example for the script tomorrow. Thank you guys

pez 2025-05-29T06:41:05.726189Z

I would love if we can solve the SO. One of those things that I have grown used to, but that obviously is not good Ux/Dx.

Jan 2025-05-29T09:20:21.325459Z

I added issue and PR for the script

šŸŽ‰ 1
borkdude 2025-05-29T09:23:03.433199Z

@pez I suggest logging the exception + stacktrace in the console or so and just displaying the message, so we can find out more details about the SO?

šŸ‘ 1