Fork me on GitHub
#cursive
<
2023-03-15
>
Thomas Moerman09:03:37

Not sure if it's a Cursive or IntelliJ problem: sometimes ^{:deprecated "why"} are not rendered as deprecated (using strikethrough) for example. In some ns'es it's ok, in others it's ignored.

2
onetom09:03:02

i found this deprecation meta-data handling a great feature too! i haven't used it much with a reason value though. we only have 2 deprecated namespaces in our main code-base at the moment, but 15 deprecated functions and 2 deprecated tests too. the :deprecated markers also serve as technical debt indicator too.

onetom09:03:27

i haven't seen +noticed+ issues with it yet, though

Thomas Moerman10:03:42

I like using it during refactorings, to have a better perspective on what's outdated

cfleming02:03:30

I just checked the code, and as far as I can tell this should be working. I’d be interested in a repro case.

markbastian14:03:55

Is there any sort of “reformat selection” capability in cursive? If not, I would so love this as a feature.

markbastian14:03:59

Ah, there is! Opt+Cmd+L

kenny17:03:03

Does Cursive support :require :rename?

serioga17:03:43

What do you mean?

kenny17:03:38

https://clojurescript.org/about/differences#_namespaces > :require supports :as, :refer, and :rename … > :rename specifies a map from referred var names to different symbols (and can be used to prevent clashes)

serioga17:03:47

Did you face problem with it Cursive?

serioga17:03:54

I'll check soon

kenny18:03:42

Seems to be an issue another user just reported https://github.com/cursive-ide/cursive/issues/2780

serioga18:03:03

This code works fine for me, no issues:

(ns test-rename
  (:require [clojure.string :refer [join] :rename {join str+}]))

(comment
  (str+ "-" ["a" "b"])
  )

serioga18:03:28

the code sample in the issue above is just broken

kenny19:03:29

To be clear - we’re talking about Cursive supporting the semantics of rename, not the functionality of the code.

serioga19:03:04

I'm talking about the same

onetom08:03:13

@U0HJNJWJH thanks for looking into my ticket! indeed my sample code was syntactically incorrect; my bad. i was too confident in my ability to type Clojure code up, without any syntax highlighting or paren balancing, right into the GitHub issue's <textarea> I've just tried what's the behaviour in Cursive 1.12.8-eap4-2023.1 and it seems slightly different from what I remembered, but still a bit problematic, if 1. Cmd-/ Comment with Line Comment the (str+ "-" ["a" "b"]) line 2. :refer [join] becomes underlined as an Unused refers warning 3. Optimize imports removes the :refer [join] 4. uncomment (str+ "-" ["a" "b"]) 5. program broken after REPL restart

nREPL server started on port 62035 on host localhost - 
Loading dev/xxx.clj... done
(str+ "-" ["a" "b"])
Syntax error compiling at (dev/xxx.clj:7:3).
Unable to resolve symbol: str+ in this context
However, if I just use the #_ comment reader syntax, this does NOT happen! What I was trying to say, is that I would consider the mention of a symbol in the :rename clause a usage of that symbol, so that symbol wouldn't be removed, even if it's not used anywhere else in that scope.

cfleming02:03:31

I’ll look at onetom’s case soon. @U083D6HK9, are you seeing this or a different problem? :rename was broken for quite some time, but I fixed it a while back.

🙏 2
kenny22:03:24

Oh, that’s what I get for reading quickly. No, I think my problem is different — it’s not resolving them at all. See attached screenshot.

serioga06:03:37

You need :refer as well.

kenny13:03:38

Hmm. It could be added, I suppose. This is in an existing codebase that’s working with the above code. Reading the CLJS doc carefully does make me think the existing code is incorrect and happens to work correctly.

serioga13:03:18

well, for me this code without :refer just does not work

(ns test-rename
  (:require [clojure.string :rename {join str+}]))

(str+ "-" ["a" "b"])

serioga13:03:22

but cljs in Cursive can have its own issues

serioga13:03:32

my suggestion is just “don't use :rename” 🙂

serioga13:03:35

this is more clear and reliable:

(def e-db rf/reg-event-db)

kenny15:03:31

I agree! I like to use aliases for all the things. The existing codebase has this convention, so I will follow it. I also asked in #clojurescript and it would seem this works by happenstance https://clojurians.slack.com/archives/C03S1L9DN/p1679147048145799

👍 2
onetom16:03:28

@U0HJNJWJH the problem with not using :rename, but a different var — eg. (def e-db rf/reg-event-db) —, is that we are losing access to metadata, like :arglists or :doc:

(ns xxx
  (:require [clojure.string :as str :refer [join] :rename {join str+}]))
=> nil

(def str-join str/join)
=> #'xxx/str-join

(clojure.repl/doc str+)
-------------------------
clojure.string/join
([coll] [separator coll])
  Returns a string of all elements in coll, as returned by (seq coll),
   separated by an optional separator.
=> nil

(clojure.repl/doc str-join)
-------------------------
xxx/str-join
=> nil
which means losing F1 help in Cursive too. So :rename is very much a useful built-in capability, which can help with 1. making code more concise 2. resolving naming conflicts 3. adjusting a confusing name of some code in a library while maintaining identical behaviour as using the original var.

serioga16:03:01

using str/join is just fine, nothing more is really needed. personally I prefer to always use library vars with namespace.

onetom16:03:22

it's not about str/join specifically; that's just an example, which anyone should understand easily. an actual use-case is more like this:

[gini.rmap :as rmap :refer [rmap ref] :rename {ref $}]
while :rename is a rarely needed capability, we have this line in our code-base literally 69 times. it means, that the Optimize imports... action has 69 potential namespaces to break our code... and using (rmap/ref :some-key) instead ($ :some-key) makes a lot of difference in readability, when there are a lot of occurrences of $s.

onetom16:03:03

(rmap
  {:system  (str (gensym "in-mem-sys-"))
   :client  (d/client (-> ($ :cfg) (assoc :system ($ :system))))
   :db-name (str (gensym "db-"))
   :db-ref  (ensure-db! ($ :client) ($ :db-name))
   :conn    (d/connect ($ :client) ($ :db-ref))
   :val     (d/db ($ :conn))
   :branch  (d/with-db ($ :conn))
   :txrs    (ensure-txs! ($ :conn) ($ :txs))})
vs
(rmap/rmap
  {:system  (str (gensym "in-mem-sys-"))
   :client  (d/client (-> (rmap/ref :cfg) (assoc :system (rmap/ref :system))))
   :db-name (str (gensym "db-"))
   :db-ref  (ensure-db! (rmap/ref :client) (rmap/ref :db-name))
   :conn    (d/connect (rmap/ref :client) (rmap/ref :db-ref))
   :val     (d/db (rmap/ref :conn))
   :branch  (d/with-db (rmap/ref :conn))
   :txrs    (ensure-txs! (rmap/ref :conn) (rmap/ref :txs))})

serioga16:03:11

rmap/rmap is fine, no guesses where rmap symbol is coming from (the code above or another ns) again: for me rmap/rmap is more readable 🙂

onetom16:03:51

u must be a java programmer, if u r fine with that kind of superfluous repetition 🙂

serioga16:03:23

(rmap
  {:system  (str (gensym "in-mem-sys-"))
   :client  (d/client (-> ($ :cfg) (assoc :system ($ :system))))
   :db-name (str (gensym "db-"))
   :db-ref  (ensure-db! ($ :client) ($ :db-name))
   :conn    (d/connect ($ :client) ($ :db-ref))
   :val     (d/db ($ :conn))
   :branch  (d/with-db ($ :conn))
   :txrs    (ensure-txs! ($ :conn) ($ :txs))})
vs
(rmap/rmap
  {:system  (str (gensym "in-mem-sys-"))
   :client  (d/client (-> (rmap/ref :cfg) (assoc :system (rmap/ref :system))))
   :db-name (str (gensym "db-"))
   :db-ref  (ensure-db! (rmap/ref :client) (rmap/ref :db-name))
   :conn    (d/connect (rmap/ref :client) (rmap/ref :db-ref))
   :val     (d/db (rmap/ref :conn))
   :branch  (d/with-db (rmap/ref :conn))
   :txrs    (ensure-txs! (rmap/ref :conn) (rmap/ref :txs))})
second I can read without looking in ns declaration for first I have no clue what exactly goes on

onetom16:03:33

if it would have been called rmap/make, i wouldn't bother :referring it either

serioga16:03:16

> u must be a java programmer, if u r fine with that kind of superfluous repetition I write Clojure too long to value explicit code with ns aliases. That's why I stopped using :refer to avoid any ambiguity. At the moment I only :refer deftest and testing in tests :-P

🙂 2
serioga16:03:03

you can see my code without using :refer here https://github.com/strojure

serioga16:03:49

and yes, there is zmap/zmap there :-)

onetom16:03:10

i prefer NS aliases too, but there are cases where such verbosity negatively affects readability. these asd/asd style names i can't really understand though; but that's not a :rename question, but a :refer or not to :refer question 🙂

onetom16:03:50

for the record, the rmap in my examples is this lib: https://github.com/aroemers/rmap

kenny15:03:06

Tried adding :refer to a :require with a :rename and it seems Cursive does not recognize the renamed vars still.

onetom18:03:04

@U083D6HK9 maybe it's a cljs specific issue? i was also wondering if those vars are actually macros, but as i see in the source code, they are not: https://github.com/day8/re-frame/blob/master/src/re_frame/core.cljc what do they mean by #!clj though, in the docstring of reg-event-db?

Example Usage:
      #!clj
      (reg-event-db
        :token
        (fn [db event]
          (assoc db :some-key (get event 2)))  ;; return updated db
did u take that screenshot of the problematic NS clause from a .cljc or a .cljs file? (im just shooting in the dark here)

kenny02:03:43

Could be. Screenshot is from a cljs file.