Sift: The Lisp formatter I kept trying to write (+lint and LSP) (jointhefreeworld.org)

24 points by jjba23 a day ago

chriswarbo 2 hours ago

A tool I've found really valuable when writing Lisp is https://github.com/shcv/parenmedic

It compares the indentation to the parentheses, and complains if they don't agree. It doesn't care about indentation amount (like I-expressions/Wisp/etc.), only that lines following some opened parens get indented more; lines following some closed parens get indented less; and sibling lines are indented the same amount.

I find this much more accurate at spotting the cause of a problem, compared to simply counting whether the openers and closers balance; e.g. the latter will only spot a missing close parenthesis when it reaches the end of the file. This is especially useful for LLMs, which are terrible at counting parentheses.

Unfortunately, there can be some cases where an auto-indenter (e.g. elisp-autofmt) causes parenmedic to complain. One remedy is to alter the auto-indenter's behaviour by inserting a line-comment; but it's not ideal.

I wonder whether Sift would (knowingly or unknowingly) adhere to the rules parenmedic requires?

jhayward 28 minutes ago

It's fairly common in agentic work with lisps to use the OG of indentation-implies-parens, parinfer. https://github.com/parinfer

It's a little disappointing to not see a credit for it in parenmedic. The agentic interface looks interesting though.

cheshire_cat 3 hours ago

Am I getting paranoid or does this text read very LLMish?

jjba23 15 minutes ago

It's true it's produced by LLM, after my initial sketch/draft and many correction rounds. I just gave it a full revamp to the blog post, with more natural wording and succinter. Apologies for this

irthomasthomas 3 hours ago

You need to stop reading things and get your agent to read them for you.

bigfishrunning 2 hours ago

Inverse compression once again!

0c3ca83 3 hours ago

It's 100% AI. The formatter is also AI. I wonder if the AI cares about formatting.

It's always interesting to me when the people who have given up on writing code build tools around it. It seems like some form of inertia, performing a pointless ritual because it feels right to them.

I wonder what goes through their minds as they build tools to enable things that they no longer do.

0x20cowboy 2 hours ago

What an interesting take. I hadn't looked at it that way.

I’ve only vibe coded tools I’ve needed, used, or wanted to play with. And the vast majority were “disposable artefacts”.

However, my guess is people always wanted X. They didn't have the skill or time to build it so now they pull it from the aether to finally see it exist. Then, they themselves dont use it (or need it) so it just fades away.

I think you've hit on why the trend of “everyone can build an app, so no one really cares about your app” is happening.

jjba23 13 minutes ago

This tool is already helping humans and LLMs alike. It also helps steer towards best practices, consistent style, etc. I understand you might see this as a slop project, I can ensure you after daily driving Sift for a whole month already I can attest to the polish in the project. But curious if you try it, what you think about it.

0c3ca83 8 minutes ago

NuclearPM an hour ago

I wanted to make a Lua like language that was inspired by Rebol and Smalltalk and unified tables with functions. I wanted this for twenty years. Now Claude code writes in my custom language and it makes me smile.

kscarlet 23 minutes ago

It's also funny because anyone who actually wrote Lisp would find this bizarrely solving no problem.

Emacs already formats its own Lisp, so does all self-respecting Common Lisp editor/plugin. And they use precise information from the Lisp image (such as lambda list of user-defined macro), no need to reinvent a whole parser that only has worse information.

jjba23 7 minutes ago

Not to start a flame war here, but this comment is really "us vs them" . also using hyperboles (typical of LLM, are you even human? :D )

I use this tool daily to great satisfaction, I use agents indeed, I also hack manually in Emacs with my REPLs and linters and formatter.

layer8 2 hours ago

Absolutely the latter, though that doesn’t mean that you aren’t also getting paranoid.

rootnod3 2 hours ago

Nowadays I auto-assume LLM slop text.

bjoli 2 hours ago

I tried to nail down ubiiquitous formatting for a typed nl-like sexpr language I have been mostly vibe coding.

It is surprisingly hard. I ended up mostly copying the algorithm that can be used through raco (the racket command line tool). It is not particularly fast, only about 10lines of code per core-second, but it produces a good enough result. I had to leave some things to the user, but only about 5 forms.

joshmarlow 2 hours ago

I've been tinkering with common lisp again lately (via VSCode) and I've been looking for something like this! Going to play with it this weekend.

kscarlet an hour ago

OLIVE format using precise information from the CL image, why you need something like this?

dukoid 3 hours ago

Lisp people: Look at the elegance and simplicity of S-expressions! Isn't it a beauty?

Also Lisp people: Here is a thesis-sized article about formatting them properly O:)

guenthert 3 hours ago

From TFA: In the Lisp world the answer is usually “my editor does it”

And that's where the story ends for most of us. Author's set-up seems to have had CI tools fighting the editor. Most of us would have disabled the misguided CI auto-formatter ...

_ph_ 3 hours ago

Actually, due to the Lisp syntax, code formatting is usually a non-issue. It is mostly reduced to correct indenting, where a common style had been established very early on in the language history - which means really long ago. All editors, which are Lisp-aware usually do it very well.

The problem remaining is, that there are editors around, which are not Lisp aware and to my continuing confusion, lots of users, which use them. So there is from time to time the requirement to predictably reformat source files. Of course, also helps a lot with automatic generated code. Just feed it to the formatter. I wrote a tool comparable to gofmt some time ago and it is very helpful.

chriswarbo 2 hours ago

How does your claim apply to this? https://www.gnu.org/software/emacs/manual/html_node/elisp/In...

For example, how should this Emacs Lisp code be formatted?

    (foo
      bar
      baz
      quux)
According to Emacs (definitely a "Lisp-aware editor"), it depends on the definition of `foo`! If `foo` is a macro, it can use `(declare (indent ...))` to specify how its use-sites should be indented. If `foo` happens to be a function, or a macro which doesn't use `(declare (indent ...))`, then Emacs will indent it as above. However, if it's defined like this:

    (defmacro foo (&rest args)
      "Does foo to ARGS."
      (declare (indent 2))
      (blah))
Then Emacs will indent it as:

    (foo
        bar
        baz
      quux)
So, at the very least, a "Lisp-aware" formatter would need to resolve names, traverse load paths, etc. to find out whether each call is actually a macro, and whether the definition of such a macro declares an indent rule.

But it gets worse! Since we can also do:

    (defmacro foo (&rest args)
      "Does foo to ARGS."
      (declare (indent 'uh-oh))
      (blah))
With a definition like that, Emacs will choose how much indentation to use by calling the `uh-oh` function! That makes indentation of Emacs Lisp undecidable!

kazinator an hour ago

boxed 2 hours ago

From the article:

> Lisp is a tree of nested lists. The language knows it, and most of the tooling around it pretends otherwise (some with nasty regex magic too).

This was my experience with Clojure too. A lot of "tooling is super easy because it's all lists and code are data structures" and in practice the tooling wasn't there. At all.

jjba23 an hour ago

Surac 2 hours ago

i once have written a lisp parser for some static analysy tool. in the end i read the code backwards, nade things more easy. cant rember why

jjba23 an hour ago

that's quite an interesting thought

shahabaz1997 an hour ago

Question for the author: how does Sift decide indentation for user-defined macros? Since the right layout for a form like `with-foo` or `my-let` depends on how it is defined (body-style vs. plain call), I'd be curious whether you ship a fixed table of known forms, read indent hints from the source, or let users configure it per project. For a formatter that runs in CI, I'd guess predictability matters more than being perfectly right for every macro.

jjba23 17 minutes ago

It's all three, in a fixed precedence order, plus a name-shape guess as the last resort. The chain is in `list-indent` in `src/sift/format.scm` (~L895).

On your CI point: yes, that's the stated priority. Nothing is inferred from how a form is used, there's no scanning of other files or dependencies, and the result is idempotent.