Home | History | Annotate | Download | only in format
      1 // Copyright 2012 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 // Package format implements standard formatting of Go source.
      6 package format
      7 
      8 import (
      9 	"bytes"
     10 	"fmt"
     11 	"go/ast"
     12 	"go/parser"
     13 	"go/printer"
     14 	"go/token"
     15 	"io"
     16 )
     17 
     18 var config = printer.Config{Mode: printer.UseSpaces | printer.TabIndent, Tabwidth: 8}
     19 
     20 const parserMode = parser.ParseComments
     21 
     22 // Node formats node in canonical gofmt style and writes the result to dst.
     23 //
     24 // The node type must be *ast.File, *printer.CommentedNode, []ast.Decl,
     25 // []ast.Stmt, or assignment-compatible to ast.Expr, ast.Decl, ast.Spec,
     26 // or ast.Stmt. Node does not modify node. Imports are not sorted for
     27 // nodes representing partial source files (for instance, if the node is
     28 // not an *ast.File or a *printer.CommentedNode not wrapping an *ast.File).
     29 //
     30 // The function may return early (before the entire result is written)
     31 // and return a formatting error, for instance due to an incorrect AST.
     32 //
     33 func Node(dst io.Writer, fset *token.FileSet, node interface{}) error {
     34 	// Determine if we have a complete source file (file != nil).
     35 	var file *ast.File
     36 	var cnode *printer.CommentedNode
     37 	switch n := node.(type) {
     38 	case *ast.File:
     39 		file = n
     40 	case *printer.CommentedNode:
     41 		if f, ok := n.Node.(*ast.File); ok {
     42 			file = f
     43 			cnode = n
     44 		}
     45 	}
     46 
     47 	// Sort imports if necessary.
     48 	if file != nil && hasUnsortedImports(file) {
     49 		// Make a copy of the AST because ast.SortImports is destructive.
     50 		// TODO(gri) Do this more efficiently.
     51 		var buf bytes.Buffer
     52 		err := config.Fprint(&buf, fset, file)
     53 		if err != nil {
     54 			return err
     55 		}
     56 		file, err = parser.ParseFile(fset, "", buf.Bytes(), parserMode)
     57 		if err != nil {
     58 			// We should never get here. If we do, provide good diagnostic.
     59 			return fmt.Errorf("format.Node internal error (%s)", err)
     60 		}
     61 		ast.SortImports(fset, file)
     62 
     63 		// Use new file with sorted imports.
     64 		node = file
     65 		if cnode != nil {
     66 			node = &printer.CommentedNode{Node: file, Comments: cnode.Comments}
     67 		}
     68 	}
     69 
     70 	return config.Fprint(dst, fset, node)
     71 }
     72 
     73 // Source formats src in canonical gofmt style and returns the result
     74 // or an (I/O or syntax) error. src is expected to be a syntactically
     75 // correct Go source file, or a list of Go declarations or statements.
     76 //
     77 // If src is a partial source file, the leading and trailing space of src
     78 // is applied to the result (such that it has the same leading and trailing
     79 // space as src), and the result is indented by the same amount as the first
     80 // line of src containing code. Imports are not sorted for partial source files.
     81 //
     82 // Caution: Tools relying on consistent formatting based on the installed
     83 // version of gofmt (for instance, such as for presubmit checks) should
     84 // execute that gofmt binary instead of calling Source.
     85 //
     86 func Source(src []byte) ([]byte, error) {
     87 	fset := token.NewFileSet()
     88 	file, sourceAdj, indentAdj, err := parse(fset, "", src, true)
     89 	if err != nil {
     90 		return nil, err
     91 	}
     92 
     93 	if sourceAdj == nil {
     94 		// Complete source file.
     95 		// TODO(gri) consider doing this always.
     96 		ast.SortImports(fset, file)
     97 	}
     98 
     99 	return format(fset, file, sourceAdj, indentAdj, src, config)
    100 }
    101 
    102 func hasUnsortedImports(file *ast.File) bool {
    103 	for _, d := range file.Decls {
    104 		d, ok := d.(*ast.GenDecl)
    105 		if !ok || d.Tok != token.IMPORT {
    106 			// Not an import declaration, so we're done.
    107 			// Imports are always first.
    108 			return false
    109 		}
    110 		if d.Lparen.IsValid() {
    111 			// For now assume all grouped imports are unsorted.
    112 			// TODO(gri) Should check if they are sorted already.
    113 			return true
    114 		}
    115 		// Ungrouped imports are sorted by default.
    116 	}
    117 	return false
    118 }
    119