ITADN
kkinnear/zprint
README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈

zprint

zprint 是一个库和命令行工具,为 Clojure 代码和 Clojure/EDN 结构提供多种美观打印功能。它几乎可以满足任何人的需求。因此,它支持 多种主要的源代码格式化方法。

cljdoc badge

快速入门

概述

zprint 的功能远不止正确缩进代码。之前

(defn change-start-column [new-start-column style-vec [inline-comment-index
  start-column spaces-before :as comment-vec]] (if (zero? inline-comment-index)
  style-vec (let [delta-spaces (- new-start-column start-column) new-spaces
  (+ spaces-before delta-spaces) previous-element-index (dec
  inline-comment-index) [s c e :as previous-element] (nth style-vec
  previous-element-index) new-previous-element (cond (= e :indent) [(str "\n"
  (blanks new-spaces)) c e] (= e :whitespace) [(str (blanks new-spaces))
  c e 26] :else nil)] (assoc style-vec previous-element-index
  new-previous-element))))

之后

(defn change-start-column
  [new-start-column style-vec
   [inline-comment-index start-column spaces-before :as comment-vec]]
  (if (zero? inline-comment-index)
    style-vec
    (let [delta-spaces (- new-start-column start-column)
          new-spaces (+ spaces-before delta-spaces)
          previous-element-index (dec inline-comment-index)
          [s c e :as previous-element] (nth style-vec previous-element-index)
          new-previous-element
            (cond (= e :indent) [(str "\n" (blanks new-spaces)) c e]
                  (= e :whitespace) [(str (blanks new-spaces)) c e 26]
                  :else nil)]
      (assoc style-vec previous-element-index new-previous-element))))

近期新增!

  • 此前已有多项请求,要求对“尾部右”字符进行差异化处理。这些字符用于终止一个集合: )]},它们要么独占一行(因为 :respect-nl 或因为注释的存在)。有些人请求将这些字符 缩进到相关结构的缩进级别。另一些人则请求(除非注释 要求如此)将它们提升到上一行,使它们不再是 “尾部右”字符。这两种能力 现在均已支持,前者通过 :indent-trailing-right? 实现, 后者通过 :collapse-trailing-right? 实现,适用于列表、向量、映射 和集合。详情请参阅参考手册。
    • :no-wrap-after 已存在一段时间。它允许你 指定一个元素,以便你希望下一个元素位于 同一行。它已被完全重新实现,现在 在许多更多情况下都能正常工作。
    • 带标签字面量的格式化已完全重新实现。
    • zprint 可执行文件现在能够直接定位并处理 多个文件。请参阅参考手册,并查找 {:files {:glob "<filespec>"}} 键值对以获取选项映射。
    • 样式 :sort-require 会对 ns 宏中的 requires 进行排序, 正如 how-to-ns 所建议的那样。 这将与 :ns-justify 良好地互操作,但请务必将 :sort-require 放在 :ns-justify 的左侧(即之前)。 这也会排序 :refer 向量的元素。
    • :ns-justify 已修改以支持显式参数 用于 :require:require-macros:import 中的变异性 ns 宏的 部分。 它们是 :require-max-variance:require-macros-max-variance:import-max-variance,并且可以 在样式映射中使用。 例如 {:style-call :ns-justify :require-max-variance 1000} 会尽可能尝试对齐 :require 列表中的 ns 宏。
    • 现在提供了适用于运行在 Apple Silicon 上的 macOS 的新预编译二进制文件。 虽然 macOS Intel 二进制文件在 Apple Silicon 上运行正常,但 Apple Silicon 二进制文件的运行速度明显(高达 3 倍)更快! 从发布版本中下载 zprintma-1.3.0 以获取 Apple Silicon 版本。
    • 现在可以将 zprint 作为 babashka 任务运行,或使用 bbin。 它启动非常快,即使在非常大的文件上,运行速度也快于 uberjar。 如果使用任务, 您无需安装新版本, 只需编辑 bb.edn。 有关简单细节,请参见 此处
    • 所有更改

参见 zprint:

  • 经典 zprint -- 忽略函数定义中的空白, 并使用多种启发式规则格式化代码,使其看起来像手工格式化的代码 (查看示例
  • 保留空行 -- 类似于 经典 zprint,但函数定义内的空行会被保留, 其余代码则被格式化为美观的样子 (查看示例
  • 仅缩进 -- 与 经典 zprint 大不相同 -- 代码永远不会改变行,它只是在 其所在的行上被正确缩进 (查看示例

此外,zprint 在 REPL 中使用 非常方便。

使用 zprint:

获取 zprint:

获取非默认格式:

无需学习如何配置 zprint:

也许现有的“样式”之一能满足您的需求。 您只需 在命令行上或作为 zprint 调用的第三个参数 添加 {:style ...}。 例如,{:style :community}{:style :respect-bl}

一些常用的样式:

学习如何更改 zprint 的格式化行为:

我想更改...

用法

cljdoc badge

bb compatible

Clojure 1.9, 1.10, 1.10.3, 1.11.1, 1.12.0:

Leiningen(via Clojars])

Clojars Project

Clojurescript:

zprint 已在以下每个环境中进行了测试:

  • figwheel-main 0.2.16 (Clojurescript 1.11.4)
  • shadow-cljs 2.18.0
  • planck 2.28.0 (Clojurescript 1.11.132)

It requires tools.reader at least 1.0.5, which all of the environments above contain.

Clojure 1.8:

最后一个使用 Clojure 1.8 构建的 zprint 版本是 [zprint "0.4.15"]。

除了 zprint 依赖项之外,在使用 Clojure 1.8 时,你还需要 包含以下库:

[clojure-future-spec "1.9.0-alpha17"]

zprint 参考

测试与开发

有关测试和开发的信息,请参见 此处

注意:已将默认分支更改为 main

贡献者

许多人为 zprint 做出了贡献,其中一些人并未出现在 GitHub 上,因为我是手动集成他们的代码或建议的。 感谢所有出色的贡献!

  • 发现并修复 ~ @stuff 被格式化为 ~@stuff 的问题:@frenchy64
  • 在 babashka 中暴露 sci.core:@borkdude
  • 在 babashka 中运行测试:@borkdude
  • 额外的颜色和 color-map 条目:@RingMan
  • rewrite-cljs 依赖项更新为 0.4.5 @rundis/
  • Readme 更新:@mathiasn, @Quezion, @vemv, @arichiardi, @bhurlow, @kommen.
  • --url--url-only:@coltnz
  • 使用 UTF-8 区域设置来构建原生镜像:@mynomoto
  • 建议/鼓励实现 :respect-bl:@agriffis
  • 线程安全性建议:@fazzone
  • 用于增强向量格式的 :option-fn:fn-format:@milankinen
  • 修复 spec.cljc 中缺失的 require:@Quezion
  • 更正 readme:@agriffis
  • 修复嵌套 reader 条件:@rgould1
  • 澄清并添加了 clj 用法的有用示例:@bherrmann7
  • Sublime text 插件说明:@ekinnear
  • ns 宏使用主体缩进:@pesterhazy
  • 针对国际字符和 graalVM 原生镜像的修复建议:@huahaiy

感谢所有贡献修复的人,以及所有报告问题的人。 我非常感谢所有帮助让 zprint 对每个人变得更好的人!

致谢

zprint 的核心是 rewrite-clj 库,该库最初由 Yannick Scherer 创建,由 Magnus Rundberget 移植到 Clojurescript,最近由 Lee Read 合并为一个受支持、有文档且持续更新的单一库。这是一个很棒的库!如果没有 rewrite-clj 作为基础,我不会尝试 zprint

此外,允许安全地从文件中读取包含函数的选项映射,这得益于 Michael Borkent (@borkdude) 的 Small Clojure Interpreter sci。这是对 Clojure 的一个设计精良且实现完善的补充,集成到 zprint 中几乎不需要任何努力。

License

Copyright © 2016-2025 Kim Kinnear

Distributed under the MIT License. See the file LICENSE for details.