Source code formatter?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Source code formatter?

paul wisehart
Hi!,
Does anyone use a source code formatter?
Which one?
thanks!


Reply | Threaded
Open this post in threaded view
|

Re: Source code formatter?

ipcore
I don't know of any dedicated source code formatter for Scheme. Piping
expressions through Chicken's pretty printer[1] can be (ab)used for
this, though. (Credit goes to kooda for showing me this trick)

[1] https://wiki.call-cc.org/man/5/Module%20(chicken%20pretty-print)

On 1/11/21 5:36 AM, Paul Wisehart wrote:
> Hi!,
> Does anyone use a source code formatter?
> Which one?
> thanks!
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Source code formatter?

Théo Cavignac
The problem with pretty printer is that it is not designed for source:
it does not keep comments and blank lines.
It would be cool if someone cared to write an actual scheme source formater.
I did not find any such tool in any implementation I know.

On Mon, Jan 11, 2021 at 2:55 PM <[hidden email]> wrote:
I don't know of any dedicated source code formatter for Scheme. Piping
expressions through Chicken's pretty printer[1] can be (ab)used for
this, though. (Credit goes to kooda for showing me this trick)

[1] https://wiki.call-cc.org/man/5/Module%20(chicken%20pretty-print)

On 1/11/21 5:36 AM, Paul Wisehart wrote:
> Hi!,
> Does anyone use a source code formatter?
> Which one?
> thanks!
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Source code formatter?

Lassi Kortela
> The problem with pretty printer is that it is not designed for source:
> it does not keep comments and blank lines.

+1

Scheme reader that preserves them:

- https://github.com/weinholt/laesare

Work-in-progress formatters:

- https://github.com/paines/scmfmt (uses Chicken pretty printer)

- https://github.com/lispunion/code-formatter

Chez Scheme, Gambit, Gauche, Chibi-Scheme also ship a pretty-printer.

At the moment, most people probably use indent-region in Emacs. Emacs
can also run in batch mode. There's probably something similar for Vim.

If any one feels like solving the problem for real, I'm happy to advise.
This is the outline:

- Expand a reader like laesare to cover all the variants of S-expression
syntax (Scheme, CL, Emacs Lisp, Clojure are very similar; no sense in
having a different algorithm for each).

- Add a matching printer by consulting "the literature". Marc Feeley of
Gambit has written a popular pretty-printer. Wadler has written a paper
from first principles:
<http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf>.

- A good pretty printer should automatically figure out where to put
line breaks, joining and splitting lines of code as needed. This is by
far the hardest part of pretty-printing; naive approaches are
susceptible to combinatorial explosion.

Reply | Threaded
Open this post in threaded view
|

Re: Source code formatter?

Vasilij Schneidermann-2
In reply to this post by paul wisehart
Hi Paul,

Evan Hanson wrote one: https://git.foldling.org/schematic/

I don't use source code formatters though, Emacs does the indentation
parts far better than anything else, leaving strategic placement of line
breaks to the programmer.

Vasilij

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Source code formatter?

Evan Hanson-4
Hi Paul,

On 2021-01-11 16:53, Vasilij Schneidermann wrote:
> Evan Hanson wrote one: https://git.foldling.org/schematic/

I did. I actually forgot about that! But I did, a while ago, and I've
been using it ever since, which I guess means it works?

You can install the "schematic" egg and try piping Scheme through the
"schematic-format" command to see whether it will work for your use
case. Theoretically it ought to work with other Schemes as well,
although I only ever tested the R7RS compatibility with Chibi and
Gauche.

Note that, despite the name, this is just an indenter, not a formatter
i.e. it won't wrap lines or rewrite your code in any way. I tend to
agree with Vasilij that such things are better left to the programmer.
But, for the sole purpose of auto-indentation, I think it's pretty OK.
The man page is available in Markdown format here:

    https://git.foldling.org/schematic/tree/master/schematic-format.1.md

I have this command hooked up to the '=' keybinding in Vim, so that (for
example) the keystroke '=ap' auto-indents the current paragraph:

    autocmd FileType scheme setlocal equalprg=schematic-format\ -b\ ~/.lispwords

I've attached my lispwords file in case it's useful. It includes a
handful of CHICKEN's special forms that have funky indentation
conventions. Dunno if any of this is helpful, but if you do give it a
try and run into issues just shout and I'll try to help!

All the best,

Evan

.lispwords (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Source code formatter?

paul wisehart
In reply to this post by paul wisehart
Thanks for the replies!
This gives me some stuff to play with.

I know about emacs, and frequently use it just to format
code.
But I mostly use vi these days.

I pipe the buffer thru gofmt for go code, and thru indent for
C code.  I like this work flow.

thanks!,
--
Paul