Giter Club home page Giter Club logo

cljfmt's Introduction

cljfmt Test Status Release Status

cljfmt is a tool for detecting and fixing formatting errors in Clojure code.

Its defaults are based on the Clojure Style Guide, but it also has many customization options to suit a particular project or team.

It is not the goal of the project to provide a one-to-one mapping between a Clojure syntax tree and formatted text; rather the intent is to correct formatting errors with minimal changes to the existing structure of the text.

If you want format completely unstructured Clojure code, the zprint project may be more suitable.

Usage

cljfmt integrates with many existing build tools, or can be used as a library. As an end user, you have the choice of:

Standalone

The fastest way to run cljfmt is via a precompiled binary. If you're using Linux or MacOS, you can run the following command to install the binary into /usr/local/bin:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/weavejester/cljfmt/HEAD/install.sh)"

You can also run this command to update cljfmt to the latest version.

If you're using Windows, there's a zipped up binary you can download and extract manually.

To use cljfmt to check for formatting errors in your project, run:

cljfmt check

And to fix those errors:

cljfmt fix

Use --help for a list of all the command-line options.

For persistent configuration, you can use a configuration file.

Clojure Tools

The official Clojure CLI supports installation of thirdparty tools. To install cljfmt as a tool, run:

clj -Ttools install io.github.weavejester/cljfmt '{:git/tag "0.10.5"}' :as cljfmt

To use the tool to check for formatting errors in your project, run:

clj -Tcljfmt check

And to fix those errors:

clj -Tcljfmt fix

Leiningen

Leiningen is a popular Clojure build tool. To use cljfmt with Leiningen, add the following plugin to your project.clj file:

:plugins [[dev.weavejester/lein-cljfmt "0.10.5"]]

To use the plugin to check code for formatting errors, run:

lein cljfmt check

And to fix those errors:

lein cljfmt fix

To configure it, put a :cljfmt key and a map of options in your project file. See the configuration section for more details.

Library

cljfmt can be run as a library that formats a string of Clojure code. First, add the dependency:

{:deps {dev.weavejester/cljfmt {:mvn/version "0.10.5"}}}

Then use the library:

(require '[cljfmt.core :as fmt])

(fmt/reformat-string "(defn sum [x y]\n(+ x y))")
;; => "(defn sum [x y]\n  (+ x y))"

To use load the configuration for the current directory:

(require '[cljfmt.config :as cfg])

(fmt/reformat-string "(+ x\ny)" (cfg/load-config))
;; => "(+ x\n   y)"

Editor Integration

You can also use cljfmt via your editor. Several Clojure editing environments have support for cljfmt baked in:

Configuration

In most environments, cljfmt will look for the following configuration files in the current and parent directories:

  • .cljfmt.edn
  • .cljfmt.clj
  • cljfmt.edn
  • cljfmt.clj

The configuration file should contain a map of options.

In Leiningen, the configuration is found in on the :cljfmt key in the project map:

:cljfmt {}

Formatting Options

  • :indentation? - true if cljfmt should correct the indentation of your code. Defaults to true.

  • :indents - a map of var symbols to indentation rules, i.e. {symbol [& rules]}. See INDENTS.md for a complete explanation.

  • :alias-map - a map of namespace alias strings to fully qualified namespace names. This option is unnecessary in most cases, because cljfmt will parse the ns declaration in each file. See INDENTS.md.

  • :remove-surrounding-whitespace? - true if cljfmt should remove whitespace surrounding inner forms. This will convert (  foo  ) to (foo). Defaults to true.

  • :remove-trailing-whitespace? - true if cljfmt should remove trailing whitespace in lines. This will convert (foo)   \n to (foo)\n. Defaults to true.

  • :insert-missing-whitespace? - true if cljfmt should insert whitespace missing from between elements. This will convert (foo(bar)) to (foo (bar)). Defaults to true.

  • :remove-consecutive-blank-lines? - true if cljfmt should collapse consecutive blank lines. This will convert (foo)\n\n\n(bar) to (foo)\n\n(bar). Defaults to true.

  • :remove-multiple-non-indenting-spaces? - true if cljfmt should remove multiple non indenting spaces. For example, this will convert {:a 1     :b 2} to {:a 1 :b 2}. Defaults to false.

  • :split-keypairs-over-multiple-lines? - true if cljfmt should break hashmaps onto multiple lines. This will convert {:a 1 :b 2} to {:a 1\n:b 2}. Defaults to false.

  • :sort-ns-references? - true if cljfmt should alphanumerically sort the requires, imports and other references in the ns forms at the top of your namespaces. Defaults to false.

Runtime Options

  • :file-pattern - a regular expression to decide which files to scan. Defaults to #”\.clj[csx]?$”.

  • :parallel? - true if cljfmt should process files in parallel. Defaults to false.

  • :paths - determines which files and directories to recursively search for Clojure files. Defaults to checking src and test, except in Leiningen where the :source-paths and :test-paths keys are used instead.

    Paths can also be passed as command line arguments. If the path is -, then the input is STDIN, and the output STDOUT.

License

Copyright © 2023 James Reeves

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

cljfmt's People

Contributors

weavejester avatar lread avatar or avatar arrdem avatar replomancer avatar vemv avatar bsless avatar matthewdarling avatar sloxy avatar pez avatar borkdude avatar jetmind avatar luisthiamnye avatar mainej avatar r6eve avatar pfeodrippe avatar noisesmith avatar j-cr avatar venantius avatar tomjkidd avatar rgm avatar ul avatar rbxbx avatar pedrorgirardi avatar pcarlisle avatar grzm avatar mwfogleman avatar doby162 avatar mpenet avatar maio 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.