logoalt Hacker News

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

26 points • by jjba23 • yesterday at 9:22 PM • 30 comments • view on HN

Comments

chriswarbo • today at 5:24 PM

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?

➕ show 1 reply
cheshire_cat • today at 3:45 PM

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

➕ show 5 replies
bjoli • today at 5:28 PM

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 • today at 5:25 PM

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.

➕ show 1 reply
dukoid • today at 3:50 PM

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:)

➕ show 2 replies
Surac • today at 5:16 PM

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

➕ show 1 reply
shahabaz1997 • today at 5:47 PM

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.

➕ show 1 reply