1
0
mirror of https://github.com/golang/go synced 2024-11-12 06:40:22 -07:00

- support for alignment via tabs instead of blanks

- exclude a test due to syntax errors

R=r
OCL=19563
CL=19565
This commit is contained in:
Robert Griesemer 2008-11-18 19:25:43 -08:00
parent 2dd16a3208
commit 34533f06eb
4 changed files with 43 additions and 14 deletions

View File

@ -13,8 +13,11 @@ import IO "io"
import OS "os" import OS "os"
import TabWriter "tabwriter" import TabWriter "tabwriter"
var tabwith = Flag.Int("tabwidth", 4, nil, "tab width"); var (
var comments = Flag.Bool("comments", false, nil, "enable printing of comments"); usetabs = Flag.Bool("usetabs", false, nil, "align with tabs instead of blanks");
tabwidth = Flag.Int("tabwidth", 4, nil, "tab width");
comments = Flag.Bool("comments", false, nil, "enable printing of comments");
)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -594,7 +597,7 @@ func (P *Printer) Declaration(d *AST.Decl, parenthesized bool) {
func (P *Printer) Program(p *AST.Program) { func (P *Printer) Program(p *AST.Program) {
// TODO should initialize all fields? // TODO should initialize all fields?
P.writer = TabWriter.MakeTabWriter(OS.Stdout, 4); P.writer = TabWriter.MakeTabWriter(OS.Stdout, usetabs.BVal(), int(tabwidth.IVal()));
P.clist = p.comments; P.clist = p.comments;
P.cindex = 0; P.cindex = 0;

View File

@ -88,6 +88,7 @@ func (b *ByteArray) Append(s *[]byte) {
export type TabWriter struct { export type TabWriter struct {
// configuration // configuration
writer IO.Write; writer IO.Write;
usetabs bool;
tabwidth int; tabwidth int;
// current state // current state
@ -103,8 +104,9 @@ func (b *TabWriter) AddLine() {
} }
func (b *TabWriter) Init(writer IO.Write, tabwidth int) { func (b *TabWriter) Init(writer IO.Write, usetabs bool, tabwidth int) {
b.writer = writer; b.writer = writer;
b.usetabs = usetabs;
b.tabwidth = tabwidth; b.tabwidth = tabwidth;
b.buf.Init(1024); b.buf.Init(1024);
@ -141,15 +143,33 @@ func (b *TabWriter) Dump() {
} }
var Tabs = &[]byte{'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t'}
var Blanks = &[]byte{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '} var Blanks = &[]byte{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}
var Newline = &[]byte{'\n'} var Newline = &[]byte{'\n'}
func (b *TabWriter) WriteBlanks(n int) {
for n >= len(Blanks) { func (b *TabWriter) Padding(textwidth, cellwidth int) {
m, err := b.writer.Write(Blanks); n := cellwidth - textwidth;
n -= len(Blanks); if n < 0 {
panic("internal error");
}
if b.usetabs {
if cellwidth % b.tabwidth != 0 {
panic("internal error"); // cellwidth should be a multiple of tabwidth
}
n = (n + b.tabwidth - 1) / b.tabwidth;
for n > len(Tabs) {
m, err := b.writer.Write(Tabs);
n -= len(Tabs);
}
m, err := b.writer.Write(Tabs[0 : n]);
} else {
for n > len(Blanks) {
m, err := b.writer.Write(Blanks);
n -= len(Blanks);
}
m, err := b.writer.Write(Blanks[0 : n]);
} }
m, err := b.writer.Write(Blanks[0 : n]);
} }
@ -164,7 +184,7 @@ func (b *TabWriter) PrintLines(pos int, line0, line1 int) int {
} }
pos += w; pos += w;
if j < b.widths.Len() { if j < b.widths.Len() {
b.WriteBlanks(b.widths.At(j).(int) - w); b.Padding(w, b.widths.At(j).(int));
} }
} }
m, err := b.writer.Write(Newline); m, err := b.writer.Write(Newline);
@ -205,6 +225,11 @@ func (b *TabWriter) Format(pos int, line0, line1 int) int {
} }
// column block end // column block end
if b.usetabs {
// make width a multiple of the tab width
width = ((width + b.tabwidth - 1) / b.tabwidth) * b.tabwidth;
}
// format and print all columns to the right of this column // format and print all columns to the right of this column
// (we know the widths of this column and all columns to the left) // (we know the widths of this column and all columns to the left)
b.widths.Append(width); b.widths.Append(width);
@ -277,8 +302,8 @@ func (b *TabWriter) Write(buf *[]byte) (i int, err *OS.Error) {
} }
export func MakeTabWriter(writer IO.Write, tabwidth int) *TabWriter { export func MakeTabWriter(writer IO.Write, usetabs bool, tabwidth int) *TabWriter {
b := new(TabWriter); b := new(TabWriter);
b.Init(writer, tabwidth); b.Init(writer, usetabs, tabwidth);
return b; return b;
} }

View File

@ -22,7 +22,7 @@ apply1() {
#echo $1 $2 #echo $1 $2
case `basename $F` in case `basename $F` in
selftest1.go | func3.go | bug014.go | bug029.go | bug032.go | bug050.go | \ selftest1.go | func3.go | bug014.go | bug029.go | bug032.go | bug050.go | \
bug068.go | bug088.go | bug083.go | bug106.go ) ;; # skip - files contain syntax errors bug068.go | bug088.go | bug083.go | bug106.go | bug125.go ) ;; # skip - files contain syntax errors
* ) $1 $2; count ;; * ) $1 $2; count ;;
esac esac
} }

View File

@ -14,6 +14,7 @@ import (
var ( var (
usetabs = Flag.Bool("usetabs", false, nil, "align with tabs instead of blanks");
tabwidth = Flag.Int("tabwidth", 4, nil, "tab width"); tabwidth = Flag.Int("tabwidth", 4, nil, "tab width");
) )
@ -35,7 +36,7 @@ func Untab(name string, src *OS.FD, dst *TabWriter.TabWriter) {
func main() { func main() {
Flag.Parse(); Flag.Parse();
dst := TabWriter.MakeTabWriter(OS.Stdout, int(tabwidth.IVal())); dst := TabWriter.MakeTabWriter(OS.Stdout, usetabs.BVal(), int(tabwidth.IVal()));
if Flag.NArg() > 0 { if Flag.NArg() > 0 {
for i := 0; i < Flag.NArg(); i++ { for i := 0; i < Flag.NArg(); i++ {
name := Flag.Arg(i); name := Flag.Arg(i);