Fork me on GitHub
#calva
<
2018-11-24
>
maxp14:11:26

Please let me know how to disable calva-fmt?

maxp14:11:42

I mean TAB key behaviour.

pez14:11:22

Assign some other key to the command should work?

maxp14:11:54

Ok, got it.

maxp14:11:26

there are two or three bindings for TAB that are very confusing

pez14:11:44

Oh, yes, I am fixing that as we speak.

pez14:11:08

It was an experiment that slipped out on the marketplace.

maxp14:11:52

Personally I like use test editor as an editor with paredit assistance but not as real "lisp" editor 🙂

maxp14:11:16

Personally I like use text editor as an editor with paredit assistance but not as real "lisp" editor 🙂

pez14:11:37

I can pack a vsix for you that have more sane key bindings.

maxp14:11:57

great idea

pez14:11:06

Not sure what a real lisp editor is?

maxp14:11:46

It really hard constantly to switch the mind between different text modes

pez14:11:53

I released the fix on the marketplace, should show up soon there. It has only one tab key binding, as it used to be.

pez14:11:14

I could update it from the marketplace now, please try it and let me know what you think about it.

maxp15:11:58

One TAB key binding? which one?

pez15:11:41

Tab formats the current form, calva-fmt.formatCurrentForm

pez15:11:07

If you don’t fancy that, just assign another key to that command.

maxp15:11:11

but how it should work? Nothing happens when I press TAB

pez15:11:41

If your current form is already nicely formatted, nothing happens.

pez15:11:52

Try mess the formatting up 😃

maxp15:11:21

Probably I already disabled fmt options...

maxp15:11:40

I'm so stupid... really don't understanf what formatCurrentForm shoudl do 🙂

maxp15:11:20

but Ctrl+I works as I expect

maxp15:11:00

but it indents only for one space, my tab settings set to "Spaces:2"

pez15:11:21

ctrl+i actually should implement parinfer’s tabstops, but I was lazy and started like this. I choose 1 space to make it work for nudging stuff just 1 space, while in hiccup code and such. You’ll have to type twice as many ctrl+i, sorry. 🙂

pez15:11:23

About you being stupid, I doubt it. Maybe something is broken… If you type this and have the cursor where the vertical bar is and press tab, what happens?

(-> foo
  |
bar)

pez15:11:22

What formatCurrentForm should do is find the closest enclosing form and format it. It’s a bit of a performance thing, because formatting the whole document can be a bit slow. There is a command for that too, and if you have a formatted document then formatting the current form when needed is enough to keep the document formatted.

maxp15:11:36

I can't because of parinfer - it adds closing )

maxp15:11:48

(-> foo )

pez15:11:07

Oh, that’s part of the README, Parinfer and Calva Formatter do not work together.

maxp15:11:09

and nothig happens

pez15:11:42

This latest release is an attempt to make it a bit less painful to drop parinfer. Therefore the Indent/dedent and Infer Perens commands where added.

lilactown19:11:27

@pez I saw your message in #clojure and decided to hack a bit…

(def opening (set "{[("))

(def closing (set "}])"))

(def mapping (apply assoc {} (interleave opening closing)))

(defn -enclosing [{:keys [queue first?]} c]
  (if (and (not first?) (empty? queue))
    (reduced {:queue (conj queue c)})
    {:first? false
     :queue (cond
              (opening c) (conj queue (mapping c))
              (closing c) (if (= c (peek queue))
                            (pop queue)
                            (reduced (conj queue c)))
              :else queue)}))

(defn enclosing? [text]
  (-> (reduce -enclosing? {:queue [] :first? true} text)
      (:queue)
      (empty?)))

(enclosing? "[]\n[]")

lilactown19:11:05

(tested in CLJ, not sure if there’s any trickiness with characters in CLJS)

lilactown19:11:00

seems to work in CLJS too

pez19:11:42

Oh, wonderful! I'll let you know how I fare with it. ❤️

pez20:11:44

Works like a charm. Can you talk some about it. Seems I can learn a bit about programming here.

lilactown20:11:46

the first problem to solve was just detecting if parens/brackets were balanced or not

lilactown20:11:36

I pretty much copied the approach I found here: https://codereview.stackexchange.com/a/180569

lilactown20:11:58

it creates a queue, adds a closing bracket to the queue each time an open bracket is detected, removes the last closing bracket if it matches

lilactown20:11:29

the way we know if it’s balanced at the end is if the queue is empty - all brackets that were added to the queue got popped off

lilactown20:11:35

once that was solved, the next problem was: how do we disallow "[][]", "() [a]", etc.?

lilactown20:11:21

the way I did that was by adding the check (and (not first?) (empty? queue))

lilactown20:11:41

basically, we want to know if the queue is empty (all the parens/brackets have been matched up to now) but we still have characters left to check

lilactown20:11:24

example:

(enclosing? "[][]")
;; args passed into `-enclosing` look like:
;; {:first? true :queue []} \[
;; {:first? false :queue [\]]} \]
;; {:first? false :queue []} \[
;; we stop here, because `first?` is false and `queue` is empty
;; we return the queue with the final character included in to represent that it was not fully balanced correctly

;; final returned value by -enclosing:
;; {:queue [\[]}

lilactown20:11:43

a valid string would look like this:

(enclosing? "(([][]))")

;; {:first? true :queue []} \(
;; {:first? false :queue [\)]} \(
;; {:first? false :queue [\) \)]} \[
;; {:first? false :queue [\) \) \]} \]
;; {:first? false :queue [\) \)]} \[
;; {:first? false :queue [\) \) \]} \]
;; {:first? false :queue [\) \)]} \)
;; {:first? false :queue [\)]} \)

;; we're all done, final returned value by -enclosing:
;; {:first? false :queue []}

pez20:11:04

Thanks a ton! I have struggled with this for quite some time and only come up with totally horrible solutions.

lilactown20:11:58

happy to help! I have lots of practice doing these little problems from doing coding challenges like code wars, Advent of Code and such 😛

lilactown20:11:12

I’m mentally preparing to do Advent of Code so I’m in that frame of mind already

lilactown20:11:39

also that Python solution ended up transferring to Clojure and the problem we actually needed to solve quite nicely

pez20:11:06

It sure did!

pez20:11:04

Just one more pic! 😃

pez20:11:16

Now I’ll pack a release of Calva Formatter with this fix.

lilactown20:11:32

yaaaaayyyy! 😄

lilactown20:11:14

btw I’m excited for Calva. I’m onboarding a few new devs at work and suggested they give it a shot in lieu of Cursive

pez20:11:17

That’s cool. Please help them with the dependency hell until I’ve added some jack-in functionality.

lilactown20:11:34

you mean adding nrepl? luckily these are already projects we’ve been developing for a bit so I just have them lein repl and connect

lilactown20:11:53

ahh actually I’m not sure that some of my projects include cider/nrepl. I’ll keep that in mind

pez21:11:36

Yeah, but Calva needs quite recent nREPL and such. Many people stumble on this and I think a lot of them give up without even asking me for help.

pez21:11:32

Yes, cider/nrepl too. Calva should offer ways to inject the dependencies in the repl startup. And that isn’t even very hard to solve, I just haven’t done it yet.

lilactown21:11:34

I’m sure its just tedious creating/maintaining all the possible project options.

lilactown21:11:56

clj / lein / shadow-cljs / figwheel

lilactown21:11:19

oh and boot. I always forget about boot

pez21:11:54

Lot’s of combinations. But right now I leave the users a bit abandoned assuming they know stuff about dependency chains and class path and stuff, when they probably (since they are using VS Code, are just trying out Clojure for the first time).

pez21:11:45

A big point with Calva is to make Clojure accessible, which is why it pains me that I haven’t spent more time on this problem. Maybe tomorrow! 🙂

lilactown21:11:48

the endeavor is much appreciated! if I had the time I would try hacking on it a bit myself. maybe in the future

pez21:11:39

You have just hacked on it. 😃

🤯 4