format

package
v1.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

Functions

This section is empty.

Types

type IndentStyle

type IndentStyle int

An IndentStyle represents the indentation strategy used for formatting a sequence of values.

const (
	// IndentNormal is for sequences that introduce no special indentation.
	//   [1
	//    2]
	IndentNormal IndentStyle = iota
	// IndentList is the default list indentation.
	//   (foo bar
	//        baz)
	IndentList
	// IndentListBody is for list forms which have bodies. For these forms,
	// subsequent lines are indented two spaces, rather than being aligned.
	// Forms like this include many language functions and macros like def
	// and defn.
	//   (def x
	//     3)
	//   (defn foo []
	//     bar)
	IndentListBody
	// IndentLet is for let-like forms. This is like IndentListBody, except
	// that the first parameter consists of let-style bindings (the
	// even-numbered ones are indented).
	//   (let [foo
	//           bar])
	IndentLet
	// IndentFor is for for-like forms. This is like IndentLet, except
	// that the let-style bindings can contain ":let" introducing another
	// let-style binding (the even-numbered ones are indented).
	//   (for [foo
	//           bar
	//         :let [x
	//                 baz])
	IndentFor

	// IndentLetfn is for letfn or anything that looks like it, where the
	// binding vector contains function bodies that should be themselves
	// indented using IndentListBody.
	//   (letfn [(twice [x]
	//              (* x 2))
	//           (six-times [y]
	//              (* (twice y) 3))]
	//     (println "Twice 15 =" (twice 15))
	//     (println "Six times 15 =" (six-times 15)))
	IndentLetfn
	// IndentDeftype is used for macros similar to deftype that define
	// functions/methods that themselves should be indented using
	// IndentListBody.
	//   (defrecord Foo [x y z]
	//     Xer
	//     (foobar [this]
	//       this)
	//     (baz [this a b c]
	//       (+ a b c)))
	IndentDeftype
	// IndentCond0 is like IndentListBody but the even-numbered arguments
	// are further indented by two.
	//   (cond
	//     (> a 10)
	//       foo
	//     (> a 5)
	//       bar)
	// As a special case, to account for condp, when the symbol :>> occurs
	// in an even-numbered position, the parity of the subsequent arguments
	// is offset by one.
	IndentCond0
	// IndentCond1 is like IndentCond0 except that it ignores 1 body
	// parameter.
	//   (case x
	//     "one"
	//       1
	//     "two"
	//       2)
	IndentCond1
	// IndentCond2 is like IndentCond0 except that it ignores 2 body
	// parameters.
	//   (condp = value
	//     1
	//       "one"
	//     2
	//       "two"
	//     3
	//       "three")
	IndentCond2
	// IndentCond4 is like IndentCond0 except that it ignores 4 body
	// parameters.
	IndentCond4
)

type Printer

type Printer struct {

	// IndentChar is the character used for indentation
	// (by default, ' ' is used).
	IndentChar rune
	// IndentOverrides allow setting specific indentation styles for forms.
	IndentOverrides map[string]IndentStyle
	// ThreadFirstStyleOverrides allow specifying custom thread-first
	// macros.
	ThreadFirstStyleOverrides map[string]ThreadFirstStyle

	// Transforms toggles the set of transformations to apply.
	// This map overrides values in DefaultTransforms.
	Transforms map[Transform]bool
	// contains filtered or unexported fields
}

A Printer writes a parse tree with proper indentation.

func NewPrinter

func NewPrinter(w io.Writer) *Printer

NewPrinter creates a printer to the given writer.

func (*Printer) PrintTree

func (p *Printer) PrintTree(t *parse.Tree) (err error)

PrintTree writes t to p's writer.

type ThreadFirstStyle

type ThreadFirstStyle int

A ThreadFirst style represents a variety of thread-first macro.

const (
	// ThreadFirstNormal is for thread-first macros that take one argument
	// and thread through all remaining forms. -> and some-> follow this
	// pattern.
	ThreadFirstNormal ThreadFirstStyle = iota
	// ThreadFirstCondArrow is the style used by cond->, which takes one
	// argument and then threads through every other form thereafter.
	ThreadFirstCondArrow
)

type Transform

type Transform int

A Transform is some tree transformation that can be applied after parsing and before printing. Some Transforms may use some heuristics that cause them to change code semantics in certain cases; these are clearly indicated and none of these are enabled by default.

const (
	// TransformSortImportRequire sorts :import, :require, and :require-macros
	// declarations in ns blocks.
	TransformSortImportRequire Transform = iota

	// TransformEnforceNSStyle applies a few common ns style rules based on
	// "How to ns". See the README for a list of the rules.
	//
	// TODO: add more "How to ns" conventions such as sorting the vectors
	// within a :require clause. See
	// https://github.com/cespare/goclj/pull/85#issuecomment-777754824
	// for a discussion about this.
	TransformEnforceNSStyle

	// TransformRemoveTrailingNewlines removes extra newlines following
	// sequence-like forms, so that parentheses are written on the same
	// line. For example,
	//
	//   (foo bar
	//    )
	//
	// becomes
	//
	//   (foo bar)
	//
	TransformRemoveTrailingNewlines

	// TransformFixDefnArglistNewline moves the arg vector of defns to the
	// same line, if appropriate:
	//
	//   (defn foo
	//     [x] ...)
	//
	// becomes
	//
	//   (defn foo [x]
	//     ...)
	//
	// if there's no newline after the arg list.
	TransformFixDefnArglistNewline

	// TransformFixDefmethodDispatchValNewline moves the dispatch-val of a
	// defmethod to the same line, so that
	//
	//   (defmethod foo
	//     :bar
	//     [x] ...)
	//
	// becomes
	//
	//   (defmethod foo :bar
	//     [x] ...)
	//
	TransformFixDefmethodDispatchValNewline

	// TransformRemoveExtraBlankLines consolidates consecutive blank lines
	// into a single blank line.
	TransformRemoveExtraBlankLines

	// TransformFixIfNewlineConsistency ensures that if one arm of an if
	// expression is preceded by a newline, the other arm is as well.
	// Both of these:
	//
	//   (if foo? a
	//     b)
	//   (if foo?
	//     a b)
	//
	// become
	//
	//   (if foo?
	//     a
	//     b)
	//
	// This applies to similar forms and macros such as if-not, if-let, and
	// so on. One-line expressions such as (if foo? a b) are not affected.
	TransformFixIfNewlineConsistency

	// TransformUseToRequire consolidates :require and :use blocks inside ns
	// declarations, rewriting them using :require if possible.
	//
	// It is not enabled by default.
	TransformUseToRequire

	// TransformRemoveUnusedRequires uses some simple heuristics to remove
	// some unused :require statements:
	//
	//   [foo :as x] ; if there is no x/y in the ns, this is removed
	//   [foo :refer [x]] ; if x does not appear in the ns, this is removed
	//
	// To preserve a :require statement which is only present for its
	// side-effects, require it as vector with a single namespace, and
	// don't use :as or :refer:
	//
	//   [foo]
	//
	// It is not enabled by default.
	TransformRemoveUnusedRequires
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL