Git Product home page Git Product logo

zprint's Introduction

zprint

zprint is a library and command line tool providing a variety of pretty printing capabilities for both Clojure code and Clojure/EDN structures. It can meet almost anyone's needs. As such, it supports a number of major source code formatting approaches.

cljdoc badge

Quickstart

Overview

zprint does far more than just properly indent code. Before:

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

After:

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

Recent Additions!

  • A new pre-compiled binary is available for macOS running on Apple Silicon. While the macOS Intel binary runs fine on Apple Silicon, the Apple Silicon binary runs considerably (up to 3x) faster! Download zprintma-1.2.8 from the release to get the Apple Silicon version.
  • You can now run zprint as a babashka task or use bbin. It starts very quickly and runs faster than the uberjar on even very large files. If using a task, you don't need to install a new version, just edit bb.edn. See the simple detals here
  • Important updates and fixes for comment wrapping changes first available in 1.2.6. Avoid 1.2.6, use 1.2.7 or later.
  • Comment wrapping has been considerably altered. When working on the stability fixes for 1.2.5, the largest remaining problem was comment wrapping causing changes to the formatting in subsequent runs. In addition, the comment wrapping has been very simplistic since its inception, leaving wrapped comments looking pretty bad. There is a new capability called {:comment {:smart-wrap? true}} which will now word wrap comments cleanly. It will also repair most of the problems that the simplistic wrapping produced in the past. It is now the default, in no small part to repair the problems of the past. If you are working to minimize changes when running zprint, I would recommend running it once over your code before you disable it, as will clean up most of the problems that were added by zprint in the past. You can disable it with {:comment {:smart-wrap? false}}. You can also configure it to minimize the amount of word wrapping it does, while still allowing it to do much better than the previous default by using {:style :minimal-smart-wrap}. You need to have {:comment {:smart-wrap? true}} to use :minimal-smart-wrap. Smart wrap works hard to not wrap things like numbered or bulleted lists. If you have a case where it wraps something that it shouldn't, please submit an issue. It is likely that it can be fixed with a configuration change. See the reference manual for more details on how to configure smart wrap.
  • You can now specify some keys to come last in a map as well as some keys to appear first in a sorted map. The {:map {:key-order [...]}} configuration places all of the keys prior to the distinguished key :| at the front of the map, and all of the keys after the :| key at the end of the map.
  • All changes

See zprint:

  • classic zprint -- ignores whitespace in function definitions and formats code with a variety of heuristics to look as good as hand-formatted code (see examples)
  • respect blank lines -- similar to classic zprint, but blank lines inside of function defintions are retained, while code is otherwise formatted to look beautiful (see examples)
  • indent only -- very different from classic zprint -- no code ever changes lines, it is only correctly indented on whatever line it was already on (see examples)

In addition, zprint is very handy to use at the REPL.

Use zprint:

Get zprint:

Get something other than the default formatting:

Without learning how to configure zprint:

Maybe one of the existing "styles" will meet your needs. All you have to do is put {:style ...} on the command line or as the third argument to a zprint call. For example, {:style :community} or {:style :respect-bl}.

Some commonly used styles:

Learn how to alter zprint's formatting behavior:

I want to change...

Usage

cljdoc badge

bb compatible

Clojure 1.9, 1.10, 1.10.3, 1.11.1, 1.12.0-alpha3:

Leiningen (via Clojars)

Clojars Project

Clojurescript:

zprint has been tested in each of the following environments:

  • figwheel-main 0.2.16 (Clojurescript 1.11.4)
  • shadow-cljs 2.18.0
  • planck 2.26.0 (Clojurescript 1.10.914)

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

Clojure 1.8:

The last zprint release built with Clojure 1.8 was [zprint "0.4.15"].

In addition to the zprint dependency, you also need to include the following library when using Clojure 1.8:

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

The zprint Reference

Testing and Development

Information on testing and development can be found here.

Note: Changed the default branch to main.

Contributors

A number of folks have contributed to zprint, not all of whom show up on GitHub because I have integrated the code or suggestions manually. Thanks for all of the great contributions!

  • Exposing sci.core in babashka: @borkdude
  • Tests running in babashka: @borkdude
  • Additional colors and color-map entries: @RingMan
  • Updated rewrite-cljs dependency to 0.4.5 @rundis/
  • Readme updates: @mathiasn, @Quezion, @vemv, @arichiardi, @bhurlow, @kommen.
  • --url and --url-only: @coltnz
  • Use UTF-8 locale to build the native image: @mynomoto
  • Suggestion/encouragement to implement :respect-bl: @griffis
  • Thread safety suggestions: @fazzone
  • :option-fn and :fn-format for enhanced vector formatting: @milankinen
  • Fixed missing require in spec.cljc: @Quezion
  • Corrected readme: @griffis
  • Fixed nested reader conditional: @rgould1
  • Clarified and added useful example for clj usage: @bherrmann7
  • Sublime text plugin instructions: @ekinnear
  • Use body indentation for the ns macro: @pesterhazy
  • Suggested fix for international chars and graalVM native image: @huahaiy

Thanks to everyone who has contributed fixes as well as everyone who has reported an issue. I really appreciate all of the help making zprint better for everybody!

Acknowledgements

At the core of zprint is the rewrite-clj library originally created by Yannick Scherer, ported to Clojurescript by Magnus Rundberget, and recently merged into a single, supported, documented, and updated library by Lee Read. This is a great library! I would not have attempted zprint if rewrite-clj didn't exist to build upon.

Additionally, allowing options maps containing functions to be read from files safely is made possible by sci, the Small Clojure Interpreter by Michael Borkent (@borkdude). This is a very well designed and implemented addition to Clojure that required almost no effort to integrate into zprint.

License

Copyright © 2016-2023 Kim Kinnear

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

zprint's People

Contributors

kkinnear avatar ringman avatar zharinov avatar brettrowberry avatar borkdude avatar mynomoto avatar vemv avatar robhanlon22 avatar quezion avatar pesterhazy avatar milankinen avatar vise890 avatar martinklepsch avatar rundis avatar kwrooijen avatar ekinnear avatar coltnz avatar bhurlow avatar arichiardi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.