Fork me on GitHub
#parinfer
<
2017-07-10
>
shaunlebron00:07:04

my feeling is that this cursorDx solution is a partial one, but still useful enough for an early version, since it is not introducing more problems to Indent Mode, but reducing them

cfleming00:07:21

Here’s a test case:

((if some-condition
   print
   println) {:foo 1
             :bar 2})

shaunlebron00:07:48

and the smart mode can be a complete solution that takes an array of changes, but will require some work

cfleming00:07:07

If you wrap the whole (if some-condition ... ), then the print and println should be indented by 1, and the map by 2.

cfleming00:07:09

It gets really hard with overlapping changes, too.

shaunlebron00:07:15

do you control the wrap operation?

shaunlebron00:07:31

could it replaced with a single change like atom does?

shaunlebron00:07:39

(not paredit I mean)

cfleming00:07:16

Well, I could do, but that then goes down the path of special-casing editing ops.

cfleming00:07:33

It really needs a general solution to be robust.

cfleming00:07:49

And I can’t special-case my way out of renaming, either.

cfleming00:07:43

like:

(my-fn (if some-condition
         print
         println) my-fn {:foo 1
                         :bar 2})

cfleming00:07:06

If I rename my-fn there, for example.

cfleming00:07:27

(very contrived, of course, but I’m sure there are plenty of real cases).

shaunlebron00:07:27

I think the changes option is doable for handling renames like this

shaunlebron00:07:40

I track both input and output coordinates as I parse the text

shaunlebron00:07:00

i’ll have to think more about it, but it feels doable

cfleming00:07:04

Here’s how I think this might work:

cfleming00:07:04

Really, the cursorDx at the moment is “from some point in the file (the new end offset of a change), indents must be changed by dx until a close paren”

cfleming00:07:22

Is that correct?

cfleming00:07:33

So it seems like you could accept a list of those in result-document-order, and essentially keep a stack of them - they’re scoped by the parens, really.

cfleming00:07:02

When you encounter a new one, it gets pushed on the stack, and when you reach the corresponding close paren, you pop it.

cfleming00:07:28

The total dx at any point in time is the sum of the dxes on the stack.

cfleming00:07:04

At least in IntelliJ, calculating that list is relatively easy.

cfleming00:07:16

I think that’s right, and it actually should be relatively simple to implement.

cfleming00:07:47

It works nicely with the way parinfer works right now.

shaunlebron00:07:41

“result-document-order”?

cfleming00:07:26

Really, a cursorDx is the dx value and the offset in the doc after which to apply it.

cfleming00:07:42

So just ordered by the offsets in the document.

cfleming00:07:29

Whoever gives them to you will have to take care that they’re in the correct order, and also that they’re updated to use the co-ordinates in the resulting document.

shaunlebron00:07:01

what would you call this option? it should replace cursorDx

cfleming00:07:19

Or editDx, perhaps.

cfleming00:07:53

It’s the change in the indentation caused by a particular change or edit.

cfleming00:07:29

But perhaps just call it changes? It will be a list of [offset dx] pairs.

shaunlebron00:07:05

maybe I should just take the changes and compute the [offset dx] internally

cfleming00:07:14

I suspect there are hairy edge cases here if the changes are not balanced, thinking about it.

shaunlebron00:07:15

and then we can use that option for other things later

cfleming00:07:37

Imagine that a user selects some text including an open paren, and then replaces it with text containing no parens.

shaunlebron00:07:22

what problem would that cause?

cfleming00:07:40

Because these will be applied using the parens for scoping. Let me see if I can think of a case.

cfleming00:07:40

(test {:foo 1
       :bar 2})

cfleming00:07:01

Imagine that the user selects test { and replaces it with foo.

cfleming00:07:30

I guess that would work, actually, since the closing } will be removed during processing anyway

cfleming00:07:25

I wonder if there are tricky cases when the user deletes closing parens.

cfleming00:07:44

Anyway, I guess we’ll find them soon enough.

shaunlebron00:07:57

yeah, I couldn’t think of a problem with it actually, other than causing extra character insertion that could throw off the other change coordinates if I’m doing it incorrectly

shaunlebron00:07:25

will look at this after dinner, thanks so much guys. couldn’t have done this myself

cfleming00:07:32

Thanks to you too - I’m really excited for this change!

cfleming00:07:57

Actually….

cfleming00:07:20

Even more exciting, I think this can be used for the partial update change.

cfleming00:07:31

As you’re processing, you record more or less the same data you’re doing now, but don’t make any modifications.

cfleming00:07:20

When you encounter a change, you know when the previous open paren was. You then process, making modifications, until that paren is balanced again.

cfleming00:07:11

Then continue without making modifications until you reach another change.

cfleming00:07:19

In fact, it’s possible you could have two modes - scanning and only checking that parens are balanced, and then a mode where you’re processing, basically exactly as you do now. You just need to set up the correct data at the start of the open paren previous to the change.

shaunlebron01:07:08

ha, not following yet

cfleming01:07:31

I’m looking after our daughter now, I’ll try to explain better later.

cfleming01:07:51

But I think this might be the change which achieves everything I wanted from parinfer 🙂

cfleming03:07:12

@shaunlebron So I basically wanted to change two things from parinfer, which I was planning to investigate. One was removing the indent/paren mode difference. The other was only processing the parts of the file (sexps, probably) affected by a particular change. This would mean that people wouldn’t have to pre-format their files on file open, and it would not be a requirement that the whole file complied with the indent rules - just the parts that they’re editing. My idea was to detect while processing the affected part of the file if that particular section didn’t comply with the indent rules, and to show an error at that point. So if the user tried an action in a part of the file which couldn’t support indent mode, parinfer just wouldn’t get run, they would be notified and they would have to fix it by hand, reformat it or something similar.

cfleming03:07:37

This change you’re making fixes the first, and I believe it can also be made to do the second.

cfleming03:07:46

I think the best way to do this would be to start scanning the file in a non-modifying mode. This would scan the file contents much like parinfer does at the moment, but it would not actually modify the file, it would just record the paren stack, the current horizontal position and the position of the last open paren (that might be implicitly stored in the paren stack).

cfleming03:07:29

Then, when you encounter one of these changes, you switch to processing exactly like parinfer does now, or at least like your new modified version does. You record the last open paren you saw. You update the file, but critically you stop processing as soon as the last open paren you recorded earlier is balanced. This means that the paren trail handling is a little trickier since you don’t just delete all close parens wholesale, you only do it until they’re balanced. Once your original open paren is balanced again, you switch back to your non-modifying scanning mode again until you hit another change.

cfleming04:07:28

If you ever get to a stage where the parens can’t balance, you barf with an error - this is like your new v2 warning mode. Similarly, you barf with an error if the sections you’re actually processing don’t obey the indent mode rules - I’m not sure if this requires more information to be tracked than is currently, but I suspect it’s a relatively simple change.

cfleming04:07:00

Obviously I haven’t implemented this, but in my head it works perfectly 🙂

cfleming04:07:09

The only bit that I don’t have quite clear in my head (been too long since I looked at all the details) is that the close paren tracking & matching will work correctly - I’d need to try it.

cfleming04:07:35

Actually, thinking about this, I’m not sure it will work - I don’t know if parinfer can work out when processing should stop, since normally it relies on removing all close parens and putting them back where it infers them.

cfleming04:07:17

I think it will probably need the original range of the sexp from the original document, which gets back to the complexity around tracking ranges across all the changes.

shaunlebron17:07:06

just thinking through an example:

(defn foo
  ([a]
     (foo a 123)) ;; <--- what happens when adjusting indentation of this line?
  ([a b]
     (+ a b)))

shaunlebron17:07:39

having a hard time piecing together expected behavior here, maybe we can work from this example to see what would be partially processed

shaunlebron17:07:50

some questions: 1. should dedenting this line by one space cause it to enter the parent form? 2. should dedenting all the way cause it to adopt everything below it?

shaunlebron17:07:01

@rgdelato: I published 2.5.2

shaunlebron17:07:30

you can either run with that, or wait for a cursorDx to be replaced with a changes array option

shaunlebron17:07:16

i’m leaving for a road trip tomorrow, and I may not finish the changes thing today

rgdelato17:07:21

cool, have fun on your trip!

cfleming18:07:49

@shaunlebron > maybe I should just take the changes and compute the [offset dx] internally I think this would be a good idea BTW, then you have more context if you want it later. It means explaining the concept of the different co-ordinate spaces and specifying which you want though.

cfleming18:07:04

More thoughts later

shaunlebron19:07:47

thanks colin!

shaunlebron19:07:08

streaming here for the changes option: http://twitch.tv/shaunlebron

cfleming22:07:08

@shaunlebron So that’s a tricky case to start with 🙂

cfleming22:07:41

One thing I’m not clear on is if when you encounter a change, if you need to reprocess from the last opening paren. I’m not sure how much data you would need to collect to begin processing immediately when encountering a change.

cfleming22:07:43

So I think assuming that either you reprocess from the opening paren, or you do have enough data from the initial scanning, when adjusting the indentation of that line the last opening paren will be the ([a]...

cfleming22:07:54

i.e. we’ll try to process the whole arity 1 body.

cfleming22:07:31

But as soon as we start processing it, we’ll find that it’s not indented according to the indent mode rules, and fail with an error stating that.

cfleming22:07:47

Let’s start with an example that actually works first.

cfleming22:07:05

(let [{:keys [refer rename]} filters
      name (get rename place-name place-name)]
  (when (or (= :all refer) ; <-- replace this when with an if
            (contains? refer name))
    name))

cfleming22:07:23

After replacing:

cfleming22:07:49

(let [{:keys [refer rename]} filters
      name (get rename place-name place-name)]
  (if (or (= :all refer) ; <-- change is right after if, dx = -2
            (contains? refer name))
    name))

cfleming22:07:42

Now, when you run parinfer, you’ll scan the file, not modifying anything but just checking parens are balanced, until you hit the change.

cfleming22:07:28

So your last open paren is right before the if, and you start processing. I’m assuming that at that point you’re processing in paren mode, because you have an active dx.

cfleming22:07:29

I haven’t seen that code, so I don’t know exactly how that works, but I’m assuming that dx is in scope for the following block, i.e. the (or ...) sexp, so that all gets indented correctly:

cfleming22:07:53

(let [{:keys [refer rename]} filters
      name (get rename place-name place-name)]
  (if (or (= :all refer)
          (contains? refer name)) ; <- this gets indented by dx
    name))

cfleming22:07:18

then you continue processing until the end of name). At that point you realise that your original open paren (if... is now balanced, so you go back to scanning. Since there are no more changes, you’re done.

cfleming22:07:07

So you only made edits inside the if sexp, and the forms in the let binding vector could have been incorrectly indented (according to indent mode rules), and that wouldn’t have been a problem as long as the parens are correctly balanced.

cfleming22:07:38

If something inside the if block were incorrectly indented, you’d fail with an error.

cfleming22:07:45

Here’s another case:

cfleming22:07:28

(let [{:keys [refer rename]} filters
      name (get rename place-name place-name)]
  (if (or (= :all refer) ; <- caret is now right before (or…, and I type or paste (and 
          (contains? refer name))
    name))

cfleming22:07:50

Because I’m going to make my conditional more complicated.

cfleming22:07:41

So now we have:

(let [{:keys [refer rename]} filters
      name (get rename place-name place-name)]
  (if (and (or (= :all refer) ; <- and is currently unbalanced, following code is not indented
          (contains? refer name))
    name))

cfleming22:07:28

Again, assuming that dx is in scope for the or block, you process that in paren mode:

cfleming22:07:53

(let [{:keys [refer rename]} filters
      name (get rename place-name place-name)]
  (if (and (or (= :all refer) ; <- and is still unbalanced
               (contains? refer name))
    name))

cfleming22:07:52

Then at the end of the (or...) block, you’re processing in indent mode again as normal, and add the closing paren for the (and...

cfleming22:07:07

(let [{:keys [refer rename]} filters
      name (get rename place-name place-name)]
  (if (and (or (= :all refer)
               (contains? refer name)))
    name))

cfleming22:07:34

Then as before, you keep processing until the end of name), switch to scanning and run to the end.

cfleming22:07:01

The tricky part here (and in the previous case) is how to handle the paren trail for name))

cfleming23:07:48

Actually, looking back at the existing paren trail rules, I’m not sure they need to be any different.

cfleming23:07:40

The one tricky bit might be: IIRC at the end of a file, if there are any remaining outstanding close parens they’re inserted at the end to ensure everything is balanced.

cfleming23:07:21

Should that happen here when the open paren before the change is balanced? I guess by definition there can be no outstanding close parens at that point.

shaunlebron23:07:20

2.6.0 pushed with changes option that replaces cursorDx

shaunlebron23:07:00

@rgdelato @chrisoakman we can start integrating the changes option into atom-parinfer now

shaunlebron23:07:02

I’ll try to update the site demo tonight if I have time 🙂

cfleming23:07:12

don’t sweat it.

cfleming23:07:34

Assuming this looks good in Atom I’ll port to Kotlin and try it in IntelliJ.

cfleming23:07:44

And I’ll experiment with the partial change thing too.

cfleming23:07:46

Thanks for this!