sorawee / fmt

A code formatter for Racket
Other
72 stars 6 forks source link

Quickscript #34

Open Metaxal opened 2 years ago

Metaxal commented 2 years ago

Here's a slightly improved quickscript: https://gist.github.com/Metaxal/f45ea1a893a1bdedaadbeafd11144e3d

It formats either the selection, or the whole program if the selection is empty.

It should probably call DrRacket's indenter afterwards, but I couldn't find the API for it, which means I would need to dig the keybinding function string name, get the keymap, etc.

sorawee commented 2 years ago

I don't think it should call the Racket indenter, so the current state is exactly what I want. Thanks again!

Metaxal commented 2 years ago

Note though that DrRacket has rules in Edit|Preferences|Editing|Indenting that are not taken into account by fmt, such as the extra regexps in particular. For example, if you fmt the quickscript linked above, it will move a lot of stuff way to the right, while DrRacket flushes it left due to the define-.* rule.

sorawee commented 2 years ago

I think the better fix is to make all def.* handled like define in fmt.

What I don't want to happen is DrRacket undoes correct indentation by fmt.

Metaxal commented 2 years ago

How about program-format accepting DrRacket-like rules and then in the quickscript obtaining DrRacket rules and passing them to program-format?

sorawee commented 2 years ago

The problem is that DrRacket rules provide a lot less information. Take define/contract as an example. I personally want it to be formatted like this:

(define/contract (f x) (-> integer? integer?)
  (+ x 1))

The indentation rule in DrRacket only says that whatever comes after the first line of define/contract should be indented like a body, but it doesn't say how to place (-> integer? integer?).

Both:

(define/contract (f x) (-> integer? integer?)
  (+ x 1))

and

(define/contract (f x) 
  (-> integer? integer?)
  (+ x 1))

are admissible formats consistent with the indentation rule in DrRacket. But fmt needs to know exactly which one you want.

Metaxal commented 2 years ago

Well, that's because it's an indenter, not a formatter as you well know. That doesn't mean that DrRacket's indentation rules should be entirely disregarded.

At least, is it possible to add the default indentation rules of DrRacket, even if we don't build a bridge between the two?

jessealama commented 1 year ago

Any traction on this? I use define/contract a lot, in this form:

(define/contract (foo bar)
  (-> integer? integer?)
  bar)

and am just now discovering that fmt turns this into

(define/contract (foo bar)
                 (-> integer? integer?)
                 bar)
sorawee commented 1 year ago

I added a basic support for define/contract in https://github.com/sorawee/fmt/commit/fa8fd5c2711dcaa6fa9f4aa5b7f371f99d823193. The full support will require more work.