Sift 发布 Lisp 系语言格式化与 LSP 工具

原标题:Sift: LSP, Lint, Format Lisp, Scheme and Emacs Lisp
58AI 研判分

开源项目 Sift 发布 1.27.3 版本,为 Scheme、Emacs Lisp 与 Common Lisp 提供幂等格式化器、规则化 linter 与语言服务器。其格式化被定义为文本上的不动点,完整保留注释、空行与 reader macro,且测试套件对每个 fixture 都断言该性质。

报道正文

#+attr_html: 160 sift logo [[file.svg]]

SPDX-License-Identifier: MIT

#+title: sift #+subtitle: an idempotent formatter, a rule-based linter and a language server for Scheme, Emacs Lisp and Common Lisp #+author: Josep Bigorra #+email: [email protected] #+options: toc num #+language: en #+description: A dialect-aware formatter, linter and language server for Scheme, Emacs Lisp and Common Lisp. Formatting is a fixed point on text that preserves every comment, blank line and reader macro, and every fixture in the test suite asserts it. #+keywords: scheme emacs-lisp common-lisp formatter linter lsp guile emacs static-analysis

an idempotent formatter, a rule-based linter and a language server for Scheme, Emacs Lisp and Common Lisp

[[https://github.com/jjba23/sift/actions/workflows/ci.yml][https://github.com/jjba23/sift/actions/workflows/ci.yml/badge.svg]] [[https://github.com/jjba23/sift/releases][https://img.shields.io/badge/release-1.27.3-2f6f4f.svg]] [[https://opensource.org/license/mit][https://img.shields.io/badge/license-MIT-2f6f4f.svg]] [[https://www.gnu.org/software/guile/][https://img.shields.io/badge/Guile-3.0%2B-1e5b3a.svg]] [[https://github.com/jjba23/guix-jointhefreeworld][https://img.shields.io/badge/Guix-channel-6f4e9c.svg]] [[https://github.com/jjba23/nix-jointhefreeworld][https://img.shields.io/badge/Nix-flake-5277c3.svg]] [[https://microsoft.github.io/language-server-protocol/][https://img.shields.io/badge/LSP-server-2f6f4f.svg]] [[file/rules.org][https://img.shields.io/badge/rules-92-2f6f4f.svg]]

sift reads a Lisp source file, reports what is ill-formed in it, and rewrites its whitespace. The rewrite is a total function on text and a fixed point: formatting a formatted file returns it unchanged, and a comment, a blank line, a #; datum comment or a #. read-time form appears in the output in the place it was in the input. Both properties are asserted over every fixture in the suite, so neither is a promise that expires quietly.

Three dialects are read, each by the rules of its own report rather than by a compromise between them: Scheme, Emacs Lisp and Common Lisp. A square bracket is a vector in one of them and a clause in the next, and sift does not average the two.

[[file/sift-demo.gif]]

[[file/sift-lint-format.gif]]

Install it with guix install sift or nix run, or [[#quick-start][clone the repository]] and run ./bin/sift version. The implementation is Guile 3.0 and the standard library, with no C dependency and no build step, because a Scheme program has none; sift doctor reports the interpreter it runs on, the library it loaded and the rules it found.

The same program is a language server (sift lsp). The findings, the fixes and the formatting are computed once and delivered either as text on standard output or as LSP responses, so Emacs through Eglot, Neovim and any other client of the protocol are told exactly what a pipeline is told.

It ships as one executable and one Emacs package, under the MIT license. The premise is the ordinary one: a Lisp program is a datum with a read syntax, and a formatter that treats it as such can be exact and cheap at the same time.

#+begin_quote Check, format, lint. sift check prints the patch it would make and exits 1; sift format prints the formatted file; sift lint names the rule that fired, its severity and its position. The editor and the pipeline receive these same answers. Every line in that recording is real output, on files small enough to read in the picture. #+end_quote

  • Scope and audience :PROPERTIES: :CUSTOM_ID: audience :END:
  • Students of Lisp. Each dialect is read by its own report, so the tool in front of a learner enforces the conventions of the language being learned rather than a fourth one invented by a formatter.
  • Guile and Guix code bases, whose cond clauses are written with square brackets and whose build files are gexps, want a formatter that agrees with the tree next door instead of re-deciding it.
  • Emacs package authors who need the docstring, lexical-binding, interactive and provide rules in Flymake and in CI, from one program rather than three.
  • Common Lisp projects in which package prefixes, LOOP keyword style and dangling parentheses are convention, and are preserved rather than normalised away.
  • Polyglot Lisp repositories, which get one executable, one configuration file and one CI step instead of three of each.
  • Editor and tool authors, who take the findings as data: --format=json, --format=sarif, GitHub annotations, or sift lsp.

#+begin_quote Lossless rewriting is the whole of the argument, and the rest follows from it: a diff whose hunks are the change, a fix that replaces one range, a rule that can be silenced without editing the code it objects to. #+end_quote

  • Properties and guarantees :PROPERTIES: :CUSTOM_ID: why :END:
  • Byte-exact preservation. Comments, blank lines, #; datum comments, #. read-time forms and #<...> objects return exactly as they were read. Formatting is a fixed point: the second run changes nothing. The suite asserts preservation and idempotence over every fixture.
  • The dialect is determined, not assumed. Three dialects, one program: character literals, read macros, #| ... |# block comments, #; datum comments, package prefixes, LOOP and docstring conventions follow the dialect's own report rather than a compromise. So does a square bracket, which is a vector in Emacs Lisp and a clause in Scheme, and #{ ... }#, which is a symbol and only Guile reads. Guile is the dialect sift is written in and the one its reader knows most closely; the other two are read from their specifications.
  • Three jobs, three commands. Formatting, linting and fixing are separate by design: format prints, --write writes, lint reports, lint --fix fixes, and ci asks the formatting question and the lint question in one pass without touching the tree.
  • Ninety-two rules, each with its rationale. Every rule has a name, a stable code (SIFT012), a sentence, the dialects it applies to, an example and its own test file. sift explain RULE reads the reasoning out of the program, and [[file/rules.org][doc/rules.org]] is generated from the rule records, so the book cannot drift from the code it describes.
  • A fix that refuses to guess.

(注:更多技术实现细节与完整 API 文档请查阅原项目 README)