• Stars
    star
    1,112
  • Rank 41,754 (Top 0.9 %)
  • Language
    Clojure
  • License
    Eclipse Public Li...
  • Created about 10 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tool for formatting Clojure code

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.

More Repositories

1

compojure

A concise routing library for Ring/Clojure
Clojure
4,029
star
2

hiccup

Fast library for rendering HTML in Clojure
Clojure
2,571
star
3

integrant

Micro-framework for data-driven architecture
Clojure
1,232
star
4

environ

Library for managing environment variables in Clojure
Clojure
923
star
5

medley

A lightweight library of useful Clojure functions
Clojure
867
star
6

codox

Clojure documentation tool
Clojure
667
star
7

ragtime

Database-independent migration library
Clojure
608
star
8

lein-ring

Ring plugin for Leiningen
Clojure
501
star
9

hashp

A better "prn" for debugging
Clojure
442
star
10

eftest

Fast and pretty Clojure test runner
Clojure
424
star
11

reagi

An FRP library for Clojure and ClojureScript
Clojure
232
star
12

clout

HTTP route-matching library for Clojure
Clojure
230
star
13

ataraxy

A data-driven Ring routing and destructuring library
Clojure
209
star
14

crypto-password

Library for securely hashing passwords
Clojure
204
star
15

clj-aws-s3

S3 client library for Clojure
Clojure
198
star
16

clojure-toolbox.com

Source to clojure-toolbox.com
CSS
179
star
17

reloaded.repl

REPL functions to support the reloaded workflow
Clojure
178
star
18

clucy

Clojure interface to Lucene
Clojure
172
star
19

haslett

A lightweight WebSocket library for ClojureScript
Clojure
172
star
20

integrant-repl

Reloaded workflow functions for Integrant
Clojure
158
star
21

lein-beanstalk

Leiningen plugin for Amazon's Elastic Beanstalk service
Clojure
149
star
22

ring-oauth2

OAuth 2.0 client middleware for Ring
Clojure
144
star
23

brutha

Simple ClojureScript interface to React
Clojure
139
star
24

progrock

A functional Clojure progress bar for the command line
Clojure
134
star
25

lein-auto

A Leiningen plugin that executes tasks when files are modifed
Clojure
132
star
26

ns-tracker

Library to keep track of changes to Clojure source files
Clojure
114
star
27

meta-merge

A standalone implementation of Leiningen's meta-merge function
Clojure
105
star
28

ring-mock

Library to create mock ring requests for unit tests
Clojure
86
star
29

ring-anti-forgery

Ring middleware to prevent CSRF attacks
Clojure
76
star
30

crypto-random

Clojure library for generating cryptographically secure random bytes and strings
Clojure
72
star
31

crouton

HTML parsing library for Clojure
Clojure
68
star
32

comb

Clojure templating library
Clojure
67
star
33

ittyon

Library to manage distributed state for games
Clojure
58
star
34

compojure-example

An example Compojure project
Clojure
57
star
35

hiccup-bootstrap

Twitter's bootstrap in Hiccup
Clojure
57
star
36

lein-generate

Leiningen plugin for generating source file templates
Clojure
54
star
37

euclidean

Fast, immutable math for 3D geometries in Clojure
Clojure
52
star
38

impi

ClojureScript library for using Pixi.js through immutable data
Clojure
51
star
39

ring-server

Clojure
51
star
40

valip

Validations library for Clojure 1.2
Clojure
51
star
41

rotary

DynamoDB API for Clojure
Clojure
47
star
42

flupot

ClojureScript functions for creating React elements
Clojure
45
star
43

re-rand

Clojure library to generate random strings from regular expressions
Clojure
43
star
44

ring-webjars

Ring middleware to serve assets from WebJars
Clojure
36
star
45

ring-refresh

A Clojure middleware library for Ring that automatically triggers a browser refresh
Clojure
33
star
46

abrade

Clojure library for web scraping
Clojure
32
star
47

ring-jetty-component

A component for the standard Ring Jetty adapter
Clojure
32
star
48

tcp-server

Clojure TCP server library
Clojure
32
star
49

intentions

Multimethods that combine rather than override inherited behavior
Clojure
31
star
50

compojure-template

Compojure project template for Leiningen
Clojure
28
star
51

suspendable

A Clojure library to add suspend and resume methods to Component
Clojure
27
star
52

ring-serve

Ring development web server
Clojure
25
star
53

decorate

Clojure macros for decorating functions
Clojure
24
star
54

crypto-equality

A small Clojure library for securely comparing strings or byte arrays
Clojure
24
star
55

resauce

Clojure library for handling JVM resources
Clojure
24
star
56

fact

Unit testing library for Clojure (no longer in active dev)
Clojure
23
star
57

dotfiles

My configuration files
Emacs Lisp
21
star
58

inquest

A library for non-invasive monitoring in Clojure
Clojure
20
star
59

evaljs

Evaluate Javascript code and libraries in Clojure
Clojure
20
star
60

fish-git

Git completions and functions for the Fish Shell
18
star
61

snowball-stemmer

Snowball Stemmer for Clojure
Java
17
star
62

hop

An experimental declarative build tool for Clojure
Clojure
16
star
63

build

Clojure
15
star
64

coercer

Library to convert Clojure data into different types
Clojure
14
star
65

whorl

Generate unique fingerprints for Clojure data structures
Clojure
14
star
66

clojure-over-ajax

Ajax Clojure REPL based on why's Try Ruby
JavaScript
13
star
67

flupot-pixi

A ClojureScript wrapper around react-pixi
Clojure
13
star
68

websocket-example

Small example Ring/Aleph project for demonstrating websockets
Clojure
12
star
69

ring-json-response

Ring responses in JSON
Clojure
11
star
70

crumpets

Clojure library for dealing with color
Clojure
10
star
71

duct-hikaricp-component

Clojure component for managing a HikariCP connection pool
Clojure
10
star
72

clojure-dbm

Clojure interface to key-value databases
Clojure
9
star
73

hanami

A Clojure utility library for Heroku web applications
Clojure
9
star
74

strowger

A ClojureScript library for managing DOM events
Clojure
9
star
75

crypto-keystore

Clojure library for dealing with Java keystores
Clojure
8
star
76

substream

Stream subclassing in Clojure
Clojure
7
star
77

clj-daemon

Clojure daemon to avoid JVM startup time
Clojure
7
star
78

ring-reload-modified

Ring middleware that automatically reloads modifed source files
Clojure
7
star
79

duct-ragtime-component

Clojure component for managing migrations with Ragtime
Clojure
5
star
80

ring-honeybadger

Ring middleware for sending errors to HoneyBadger
Clojure
4
star
81

imprimatur

Data visualization library for ClojureScript and React
Clojure
4
star
82

hassium

Another Clojure MongoDB library
Clojure
4
star
83

contributing

Contributor's Guide
4
star
84

lein-template

Clojure
4
star
85

delegance

A Clojure library for remote evaluation
Clojure
3
star
86

po

A command-line tool for organizing project-specific scripts
Go
3
star
87

lein-version-script

A Leiningen plugin to set the project version from a shell script
Clojure
3
star
88

capra

An extensible package manager for Clojure
Clojure
3
star
89

eclair

Clojure
3
star
90

wrepl

Web-based Clojure REPL
Clojure
2
star
91

pocketses

Personal Wiki template that uses Gollum
CSS
2
star
92

clj-less

LESS interpreter for Clojure (http://lesscss.org)
Clojure
2
star
93

dewdrop

Web UI framework
2
star
94

ubitcoin

Bitcoin GUI for Ubuntu
Python
2
star
95

clojure-sandbox

Miscellaneous Clojure libraries that needed a home
Clojure
2
star
96

capra-server

RESTful package server
Clojure
1
star
97

delegance-aws

Library to integrate Delegance with Amazon Web Services
Clojure
1
star
98

weavejester.github.com

JavaScript
1
star
99

dojo-poetry

Code for Clojure Dojo 2012-08-28
Clojure
1
star
100

databstract

1
star