Fork me on GitHub
#cider
<
2023-11-10
>
lassemaatta11:11:19

Not sure if this is a cider or rather a general clojure question, but: how should I write code dealing with Java objects such that cider could provide meaningful code completion candidates?

lassemaatta12:11:30

For example, let's say I have some Java object factory, which provides methods such as

public SomeClass createSomeClass() { return new SomeClass(); }

lassemaatta12:11:21

I'd like to write code such as

(let [object (doto (.createSomeClass factory)
                (.setFoo 1)        ;; <- I'd like completion here
                (.setBar 2))       ;; <- and here   
      quuz   (.getQuuz object)]  ;; <- but also here
  (.toString quuz))

lassemaatta12:11:07

at the moment it feels like I need to write

(let [^SomeClass object (doto ^SomeClass (.createSomeClass factory)
                           (.setFoo 1)
                           (.setBar 2))   
      quuz   (.getQuuz object)]
  (.toString quuz))
for cider to suggest the completions

lassemaatta12:11:40

Also, it looks like the autocomplete does not suggest any inherited methods, e.g. if SomeClass extends SomeOtherClass.

vemv14:11:57

As for the first question, is your only concern having to write type hints? There are many reasons to write them, I summarized them in https://github.com/jonase/eastwood#reflection

vemv14:11:33

> Also, it looks like the autocomplete does not suggest any inherited methods, I'll check. That would sound like a Compliment issue (@U06PNK4HG)

lassemaatta14:11:22

sure, I naturally have (set! *warn-on-reflection* true) like any good boy. But in this case those type hints are not strictly needed

vemv14:11:51

how is factory defined?

lassemaatta14:11:05

I don't have the code at hand at the moment, but something like (def ^ObjectFactory factory (ObjectFactory.)). The use case here is building a hierarchy tree of objects when doing XML generation from java classes.

vemv14:11:53

Yep, that type hint looks like a correct (type-hinted) declaration. Generally, type tags propagate nicely across def s, let s, -> and so on. It's a non-trivial problem to solve but Compliment solves a good chunk of it. Feel free to verify that for a good variety of simple (and not-so-simple) expressions, we offer the right completion for a given interop object. If you can check that a) it works for certain cases and b) it doesn't work for a certain code shape (e.g. let + doto), it would be reasonable to create an issue in Compliment.

lassemaatta14:11:13

I assume Compliment is alexander-yakushev/compliment? Sure, I can document a couple of examples of my problem there

👍 1
hugod14:11:21

When running tests with cider, every log message that is output to the repl also appears as an overlay and as a minibuffer message. Is there some way to disable this?

vemv14:11:08

Log output is not supposed to be handled that way and would warrant a GH issue, will be happy to attend it swiftly

👍 1
ag19:11:34

Is there a handy elisp function for evaling something in the repl and getting results? Something akin to shell-command-to-string or process-lines? I've been trying cider-interactive-eval but it seems to have so many bells & whistles, it doesn't look very straightforward.

ag19:11:12

Oh, I just found cider-sync-tooling-eval. seems like something I can use.

👍 1
jpmonettas20:11:18

In case you want to use cider-interactive-eval this should work :

(cider-interactive-eval "(+ 1 2)" nil nil `(("ns" "user")))