Class DocBuilder
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Doc.LevelA possibly earlierDoc.Levelfor appending text, à la Philip Wadler.private final Doc.Levelprivate final ArrayDeque<Doc.Level> -
Constructor Summary
Constructors -
Method Summary
-
Field Details
-
base
-
stack
-
appendLevel
A possibly earlierDoc.Levelfor appending text, à la Philip Wadler.Processing
Docs presents a subtle problem. Suppose we have aDocfor to an assignment node,a = b, with an optionalDoc.Breakfollowing the=. Suppose we have 5 characters to write it, so that we think we don't need the break. Unfortunately, thisDoclies in an expression statementDocfor the statementa = b;and this statement does not fit in 3 characters. This is why many formatters sometimes emit lines that are too long, or cheat by using a narrower line length to avoid such problems.One solution to this problem is not to decide whether a
Doc.Levelshould be broken until later (in this case, after the semicolon has been seen). A simpler approach is to rewrite theDocas here, so that the semicolon moves inside the innerDoc, and we can decide whether to break thatDocwithout seeing later text.
-
-
Constructor Details
-
DocBuilder
public DocBuilder()Start to build aDocBuilder.
-
-
Method Details
-
withOps
Add a list ofOps to theOpsBuilder.- Parameters:
ops- theOps- Returns:
- the
OpsBuilder
-
open
Open a newDoc.Level.- Parameters:
plusIndent- the extra indent for theDoc.Level
-
close
void close()Close the currentDoc.Level. -
add
- Parameters:
doc- theDoc
-
breakDoc
- Parameters:
breakDoc- theDoc.Break
-
build
Return theDoc.- Returns:
- the
Doc
-
toString
-