]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer: Revert API change of CL 4274075.
authorRobert Griesemer <gri@golang.org>
Tue, 22 Mar 2011 21:14:26 +0000 (14:14 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 22 Mar 2011 21:14:26 +0000 (14:14 -0700)
Revert changes to printer.Config. Pass in the
nodeSizes map trough an internal helper function.

R=golang-dev, rsc1
CC=golang-dev
https://golang.org/cl/4309042

src/cmd/godoc/godoc.go
src/cmd/gofix/main.go
src/cmd/gofix/main_test.go
src/cmd/gofmt/gofmt.go
src/pkg/go/printer/nodes.go
src/pkg/go/printer/printer.go

index 71be62fb476fcf1ec175bc83376b0451c2b56ae1..41bd37ad665044e88fc3995cab3aab25e6c7179c 100644 (file)
@@ -374,7 +374,7 @@ func writeNode(w io.Writer, fset *token.FileSet, x interface{}) {
        //           with an another printer mode (which is more efficiently
        //           implemented in the printer than here with another layer)
        mode := printer.TabIndent | printer.UseSpaces
-       (&printer.Config{Mode: mode, Tabwidth: *tabwidth}).Fprint(&tconv{output: w}, fset, x)
+       (&printer.Config{mode, *tabwidth}).Fprint(&tconv{output: w}, fset, x)
 }
 
 
index ca4becfa756ab9318a22cb35bb6c62c552dc7b20..9ca2ddb461cb68ef34c685adc962c5603f2737f1 100644 (file)
@@ -127,7 +127,7 @@ func processFile(filename string, useStdin bool) os.Error {
        fmt.Fprintf(os.Stderr, "%s: %s\n", filename, buf.String()[1:])
 
        buf.Reset()
-       _, err = (&printer.Config{Mode: printerMode, Tabwidth: tabWidth}).Fprint(&buf, fset, file)
+       _, err = (&printer.Config{printerMode, tabWidth}).Fprint(&buf, fset, file)
        if err != nil {
                return err
        }
index ffcbe7cb0994cd44d17dee233804de0a1ea5bcb7..e4d0f60cce6c10c293057330adc2abd82c29a79b 100644 (file)
@@ -37,7 +37,7 @@ func parseFixPrint(t *testing.T, fn func(*ast.File) bool, desc, in string) (out
 
        var buf bytes.Buffer
        buf.Reset()
-       _, err = (&printer.Config{Mode: printerMode, Tabwidth: tabWidth}).Fprint(&buf, fset, file)
+       _, err = (&printer.Config{printerMode, tabWidth}).Fprint(&buf, fset, file)
        if err != nil {
                t.Errorf("%s: printing: %v", desc, err)
                return
@@ -60,7 +60,7 @@ func parseFixPrint(t *testing.T, fn func(*ast.File) bool, desc, in string) (out
        }
 
        buf.Reset()
-       _, err = (&printer.Config{Mode: printerMode, Tabwidth: tabWidth}).Fprint(&buf, fset, file)
+       _, err = (&printer.Config{printerMode, tabWidth}).Fprint(&buf, fset, file)
        if err != nil {
                t.Errorf("%s: printing: %v", desc, err)
                return
index 943877aa27b3cc34c62adc377b7284c2fcbdc2e1..a688c8184c43ef289add02c3c14fadd1bec4f8ac 100644 (file)
@@ -103,7 +103,7 @@ func processFile(f *os.File) os.Error {
        }
 
        var buf bytes.Buffer
-       _, err = (&printer.Config{Mode: printerMode, Tabwidth: *tabWidth}).Fprint(&buf, fset, file)
+       _, err = (&printer.Config{printerMode, *tabWidth}).Fprint(&buf, fset, file)
        if err != nil {
                return err
        }
index f55a641975885bd98c2ebf95f364acb4a942edba..2238b6bedc80ba518b209846d67e29230a95c3ca 100644 (file)
@@ -1318,9 +1318,9 @@ func (p *printer) nodeSize(n ast.Node, maxSize int) (size int) {
        // nodeSize computation must be indendent of particular
        // style so that we always get the same decision; print
        // in RawFormat
-       cfg := Config{Mode: RawFormat, nodeSizes: p.nodeSizes}
+       cfg := Config{Mode: RawFormat}
        var buf bytes.Buffer
-       if _, err := cfg.Fprint(&buf, p.fset, n); err != nil {
+       if _, err := cfg.fprint(&buf, p.fset, n, p.nodeSizes); err != nil {
                return
        }
        if buf.Len() <= maxSize {
index 6c925ad4fd8f6df4187d921b0fbbbf2542113008..a43e4a12c774688a0fa362dc16d390cfdfe5ad19 100644 (file)
@@ -98,15 +98,19 @@ type printer struct {
        comments        []*ast.CommentGroup // may be nil
        cindex          int                 // current comment index
        useNodeComments bool                // if not set, ignore lead and line comments of nodes
+
+       // Cache of already computed node sizes.
+       nodeSizes map[ast.Node]int
 }
 
 
-func (p *printer) init(output io.Writer, cfg *Config, fset *token.FileSet) {
+func (p *printer) init(output io.Writer, cfg *Config, fset *token.FileSet, nodeSizes map[ast.Node]int) {
        p.output = output
        p.Config = *cfg
        p.fset = fset
        p.errors = make(chan os.Error)
        p.buffer = make([]whiteSpace, 0, 16) // whitespace sequences are short
+       p.nodeSizes = nodeSizes
 }
 
 
@@ -986,25 +990,13 @@ const (
 
 // A Config node controls the output of Fprint.
 type Config struct {
-       Mode      uint             // default: 0
-       Tabwidth  int              // default: 8
-       nodeSizes map[ast.Node]int // memoized node sizes as computed by nodeSize
+       Mode     uint // default: 0
+       Tabwidth int  // default: 8
 }
 
 
-// Fprint "pretty-prints" an AST node to output and returns the number
-// of bytes written and an error (if any) for a given configuration cfg.
-// Position information is interpreted relative to the file set fset.
-// The node type must be *ast.File, or assignment-compatible to ast.Expr,
-// ast.Decl, ast.Spec, or ast.Stmt.
-//
-func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node interface{}) (int, os.Error) {
-       // only if Fprint is called recursively (via nodeSize)
-       // does cfg.nodeSizes exist - set it up otherwise
-       if cfg.nodeSizes == nil {
-               cfg.nodeSizes = make(map[ast.Node]int)
-       }
-
+// fprint implements Fprint and takes a nodesSizes map for setting up the printer state.
+func (cfg *Config) fprint(output io.Writer, fset *token.FileSet, node interface{}, nodeSizes map[ast.Node]int) (int, os.Error) {
        // redirect output through a trimmer to eliminate trailing whitespace
        // (Input to a tabwriter must be untrimmed since trailing tabs provide
        // formatting information. The tabwriter could provide trimming
@@ -1033,7 +1025,7 @@ func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node interface{
 
        // setup printer and print node
        var p printer
-       p.init(output, cfg, fset)
+       p.init(output, cfg, fset, nodeSizes)
        go func() {
                switch n := node.(type) {
                case ast.Expr:
@@ -1080,6 +1072,17 @@ func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node interface{
 }
 
 
+// Fprint "pretty-prints" an AST node to output and returns the number
+// of bytes written and an error (if any) for a given configuration cfg.
+// Position information is interpreted relative to the file set fset.
+// The node type must be *ast.File, or assignment-compatible to ast.Expr,
+// ast.Decl, ast.Spec, or ast.Stmt.
+//
+func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node interface{}) (int, os.Error) {
+       return cfg.fprint(output, fset, node, make(map[ast.Node]int))
+}
+
+
 // Fprint "pretty-prints" an AST node to output.
 // It calls Config.Fprint with default settings.
 //