Fork me on GitHub
#calva
<
2021-11-24
>
Samuel McHugh11:11:31

Hello, both Emacs and Cursive have some options to automatically add require declarations to your namespace with just a function name. For example imaging I type out json/to-json in my file and I have cheshire as a dependency in my deps.edn. It would be cool to be able to do a keyboard shortcut with my cursor over json/to-json and for Calva to automatically add [cheshire :as json] under my existing (:require ). Here it is in Emacs https://github.com/clojure-emacs/clj-refactor.el/blob/master/examples/add-missing-libspec.gif. I’m wondering if it could be done in Calva using clojure.repl/apropos . Does that jump out as not possible for any reason? I’m not able to follow along how it’s done at https://github.com/clojure-emacs/clj-refactor.el/blob/master/clj-refactor.el#L2958

👍 3
pez17:11:12

Agree. Is this something that #lsp can help with, @UKFSJSM38?

ericdallo17:11:05

This already exists on clojure-lsp and even use to work better than clj-refactor :p Is just code actions, check: https://clojure-lsp.io/features/#code-actions

pez18:11:51

Awesome! Can you file an issue on the Calva repo to implement this, please, @U0151JJA3FT?

pez18:11:09

Though we already support code actions. Is there something special for this one, @UKFSJSM38?

pez18:11:59

Oh, we actually already support it. 😃 Though in my work project I get a huge list for something like (s/def …)

pez18:11:10

So huge it is not really helpful 😃 I don’t thing def is defined in all those. Are there attempts made to filter the list?

Samuel McHugh18:11:04

oh wow I had no idea

Samuel McHugh18:11:16

But yes, the analysis must just look at the alias you put and the namespaces on the class-path and does some kind of fuzzy match. It doesn't actually check that there is a function defined in those namespaces which matches what you're looking for. For example if I put str/trim and run the code action it gives me the correct option for clojure.string but it will also give me manifold.stream as an option. So if you use a really short alias like s which can fit many many namespaces it isn't too clever.

Samuel McHugh18:11:36

Still, I actually didn't even know to check the lightbulb for this kinda stuff so it's already really useful in 90% of circumstances

Samuel McHugh18:11:03

But theoretically if you combined this fuzzy searching with the clojure.repl/apropos you could narrow in on the only reasonable option in many cases. For example if I do (clojure.repl/apropos 'trim) I get

(cider.nrepl.inlined-deps.orchard.v0v7v1.orchard.java/trim-one-dot
clojure.string/trim
clojure.string/trim-newline 
clojure.string/triml
clojure.string/trimr)
So I could filter out manifold.stream from my list of suggestions since it doesn't appear here.

ericdallo18:11:29

Code actions don't work on calva ATM

ericdallo18:11:16

You can trigger code actions with Ctrl + .

Samuel McHugh18:11:53

Erm maybe I don't know what a code action is but the little lightbulb works for me, but maybe my Calva is out of date or something. Also there is a command calva.refactor.addMissingLibspec

ericdallo18:11:22

This is a code action:

Samuel McHugh18:11:27

calva.refactor.addMissingLibspec will randomly not work for me for some declarations though 🤷

ericdallo18:11:47

calling refactors manually are not recommended, the code actions are though

ericdallo18:11:03

but like the issue I mentioned, it seems they are not working for calva yet c/c @U9A1RLFNV

👍 1
Samuel McHugh18:11:36

Okay, I do see the issue that most of the ctrl +`.` options don't do anything when selected (like toggling function privacy) but the libspec insert is one example that does work for me. So maybe it's using something else under the hood.

ericdallo18:11:28

Not really, all code actions are commands under the hood, but adding that to the issue may help understand the root cause

Samuel McHugh19:11:33

https://github.com/BetterThanTomorrow/calva/issues/1373#issuecomment-978152785 Put a little gif on your issue. I can maybe have a look tomorrow to see what the difference may be and if there are any other working code actions.

👍 1