A Common indentation for pretty printing libraries
Overview
This library provides a Common indentation for other libraries that include a pretty printing functionality.
Consult the documentation and usage description for further information:
Maven
This library is hosted in the Maven Central Repository. You can use it with the following coordinates:
<dependency>
<groupId>net.markenwerk</groupId>
<artifactId>utils-text-indentation</artifactId>
<version>1.1.2</version>
</dependency>
Motivation
The goal of this library is to avoid unnecessary and probably inconsistent repetition of a configurable indentation for formatted text output (e.g. pretty printing).
Usage
This library provides the Indentation
interface.
Using an Indentation
An Indentation
provides the following two methods to create a character sequence that needs to be appended at the beginning of a line, with the result that the the line is indented by the given level.
Indentation indentation = ...;
// get an indentation prefix for the given level
indentation.get(level);
// append an indentation prefix for the given level
indentation.append(appendable, level);
An Indentation
provides the following two methods that optionally append a line break to create the character sequence that needs to be appended at the end of a line, with the result that the next line is indented by the given level. This includes the necessary line break characters.
Indentation indentation = ...;
// get an indentation prefix for the given level
indentation.get(level, true);
// append an indentation prefix for the given level
indentation.append(appendable, level, true);
It is also possible to check, if an Indentation
will have any visible effect.
Indentation indentation = ...;
// check if the indentation will return non-empty strings
boolean prettyPrinting = indentation.isVisible();
The result of isVisible()
is useful, if a pretty printer needs to behave differently (e.g. add whitespace between separators). Even if isVisible()
returns false
, getPrefix(int)
and getLineBreak(int)
will always return a [String
] that is not null
. This means that it is safe to use these methods without any checks. An Indentation
that has no visible effect won't include the line break characters.
Whitespace Indentation
This library provides the WhitespaceIndentation
that can be safely configured to create indentation strings with the most common whitespace characters (tab and space).
// indentation with four spaces
Indentation spaceIndentation = new WhitespaceIndentation(Whitespace.SPACE, 4);
// indentation with one tab and windows style line break
Indentation tabIndentation = new WhitespaceIndentation(Whitespace.TAB, 1, LineBreak.WINDOWS);
Invisible Indentation
This library provides the InvisibleIndentation
that has no visible effect.
// indentation without visible effects
Indentation spaceIndentation = new InvisibleIndentation();