}
var txpix = [NCOL]draw.Color{
- draw.Yellow, /* yellow */
- draw.Cyan, /* cyan */
- draw.Green, /* lime green */
- draw.GreyBlue, /* slate */
- draw.Red, /* red */
- draw.GreyGreen, /* olive green */
- draw.Blue, /* blue */
+ draw.Yellow, /* yellow */
+ draw.Cyan, /* cyan */
+ draw.Green, /* lime green */
+ draw.GreyBlue, /* slate */
+ draw.Red, /* red */
+ draw.GreyGreen, /* olive green */
+ draw.Blue, /* blue */
draw.Color(0xFF55AAFF), /* pink */
draw.Color(0xFFAAFFFF), /* lavender */
draw.Color(0xBB005DFF), /* maroon */
}
-func (p *printer) writeNewlines(n int) {
+func (p *printer) writeNewlines(n int, useFF bool) {
if n > 0 {
if n > maxNewlines {
n = maxNewlines
}
- p.write(newlines[0:n])
- }
-}
-
-
-func (p *printer) writeFormfeeds(n int) {
- if n > 0 {
- if n > maxNewlines {
- n = maxNewlines
+ if useFF {
+ p.write(formfeeds[0:n])
+ } else {
+ p.write(newlines[0:n])
}
- p.write(formfeeds[0:n])
}
}
// use formfeeds to break columns before a comment;
// this is analogous to using formfeeds to separate
// individual lines of /*-style comments
- p.writeFormfeeds(pos.Line - p.last.Line)
+ p.writeNewlines(pos.Line-p.last.Line, true)
}
}
// writeCommentSuffix writes a line break after a comment if indicated
// and processes any leftover indentation information. If a line break
// is needed, the kind of break (newline vs formfeed) depends on the
-// pending whitespace.
+// pending whitespace. writeCommentSuffix returns true if a pending
+// formfeed was dropped from the whitespace buffer.
//
-func (p *printer) writeCommentSuffix(needsLinebreak bool) {
+func (p *printer) writeCommentSuffix(needsLinebreak bool) (droppedFF bool) {
for i, ch := range p.buffer {
switch ch {
case blank, vtab:
// don't loose indentation information
case newline, formfeed:
// if we need a line break, keep exactly one
+ // but remember if we dropped any formfeeds
if needsLinebreak {
needsLinebreak = false
} else {
+ if ch == formfeed {
+ droppedFF = true
+ }
p.buffer[i] = ignore
}
}
if needsLinebreak {
p.write([]byte{'\n'})
}
+
+ return
}
// and prints it together with the buffered whitespace (i.e., the whitespace
// that needs to be written before the next token). A heuristic is used to mix
// the comments and whitespace. The isKeyword parameter indicates if the next
-// token is a keyword or not.
+// token is a keyword or not. intersperseComments returns true if a pending
+// formfeed was dropped from the whitespace buffer.
//
-func (p *printer) intersperseComments(next token.Position, isKeyword bool) {
+func (p *printer) intersperseComments(next token.Position, isKeyword bool) (droppedFF bool) {
isFirst := true
needsLinebreak := false
var last *ast.Comment
// follows on the same line: separate with an extra blank
p.write([]byte{' '})
}
- p.writeCommentSuffix(needsLinebreak)
+ return p.writeCommentSuffix(needsLinebreak)
}
p.pos = next
if data != nil {
- p.flush(next, isKeyword)
+ droppedFF := p.flush(next, isKeyword)
// intersperse extra newlines if present in the source
// (don't do this in flush as it will cause extra newlines
- // at the end of a file)
- p.writeNewlines(next.Line - p.pos.Line)
+ // at the end of a file) - use formfeeds if we dropped one
+ // before
+ p.writeNewlines(next.Line-p.pos.Line, droppedFF)
p.writeItem(next, data, tag)
}
// Flush prints any pending comments and whitespace occuring
-// textually before the position of the next item.
+// textually before the position of the next item. Flush returns
+// true if a pending formfeed character was dropped from the
+// whitespace buffer as a result of interspersing comments.
//
-func (p *printer) flush(next token.Position, isKeyword bool) {
- // if there are comments before the next item, intersperse them
+func (p *printer) flush(next token.Position, isKeyword bool) (droppedFF bool) {
if p.commentBefore(next) {
- p.intersperseComments(next, isKeyword)
+ // if there are comments before the next item, intersperse them
+ droppedFF = p.intersperseComments(next, isKeyword)
+ } else {
+ // otherwise, write any leftover whitespace
+ p.writeWhitespace(len(p.buffer))
}
- // write any leftover whitespace
- p.writeWhitespace(len(p.buffer))
+ return
}