2013-07-17 01:17:12 -06:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package godoc is a work-in-progress (2013-07-17) package to
|
|
|
|
// begin splitting up the godoc binary into multiple pieces.
|
|
|
|
//
|
|
|
|
// This package comment will evolve over time as this package splits
|
|
|
|
// into smaller pieces.
|
2014-12-08 21:00:58 -07:00
|
|
|
package godoc // import "golang.org/x/tools/godoc"
|
2013-07-17 01:17:12 -06:00
|
|
|
|
|
|
|
import (
|
2018-07-17 15:23:18 -06:00
|
|
|
"bufio"
|
2013-07-17 01:17:12 -06:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/doc"
|
|
|
|
"go/format"
|
|
|
|
"go/printer"
|
|
|
|
"go/token"
|
2014-03-14 16:58:22 -06:00
|
|
|
htmltemplate "html/template"
|
2013-07-17 01:17:12 -06:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
pathpkg "path"
|
|
|
|
"regexp"
|
2013-12-04 08:37:01 -07:00
|
|
|
"strconv"
|
2013-07-17 01:17:12 -06:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Fake relative package path for built-ins. Documentation for all globals
|
2019-02-18 11:49:49 -07:00
|
|
|
// (not just exported ones) will be shown for packages in this directory,
|
|
|
|
// and there will be no association of consts, vars, and factory functions
|
|
|
|
// with types (see issue 6645).
|
2013-07-18 18:27:53 -06:00
|
|
|
const builtinPkgPath = "builtin"
|
2013-07-17 01:17:12 -06:00
|
|
|
|
|
|
|
// FuncMap defines template functions used in godoc templates.
|
|
|
|
//
|
|
|
|
// Convention: template function names ending in "_html" or "_url" produce
|
|
|
|
// HTML- or URL-escaped strings; all other function results may
|
|
|
|
// require explicit escaping in the template.
|
2013-07-17 21:14:09 -06:00
|
|
|
func (p *Presentation) FuncMap() template.FuncMap {
|
|
|
|
p.initFuncMapOnce.Do(p.initFuncMap)
|
|
|
|
return p.funcMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Presentation) TemplateFuncs() template.FuncMap {
|
|
|
|
p.initFuncMapOnce.Do(p.initFuncMap)
|
|
|
|
return p.templateFuncs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Presentation) initFuncMap() {
|
|
|
|
if p.Corpus == nil {
|
|
|
|
panic("nil Presentation.Corpus")
|
|
|
|
}
|
|
|
|
p.templateFuncs = template.FuncMap{
|
|
|
|
"code": p.code,
|
|
|
|
}
|
|
|
|
p.funcMap = template.FuncMap{
|
|
|
|
// various helpers
|
|
|
|
"filename": filenameFunc,
|
|
|
|
"repeat": strings.Repeat,
|
2017-12-22 18:30:39 -07:00
|
|
|
"since": p.Corpus.pkgAPIInfo.sinceVersionFunc,
|
2013-07-17 21:14:09 -06:00
|
|
|
|
|
|
|
// access to FileInfos (directory listings)
|
|
|
|
"fileInfoName": fileInfoNameFunc,
|
|
|
|
"fileInfoTime": fileInfoTimeFunc,
|
|
|
|
|
|
|
|
// access to search result information
|
|
|
|
"infoKind_html": infoKind_htmlFunc,
|
|
|
|
"infoLine": p.infoLineFunc,
|
|
|
|
"infoSnippet_html": p.infoSnippet_htmlFunc,
|
|
|
|
|
|
|
|
// formatting of AST nodes
|
|
|
|
"node": p.nodeFunc,
|
|
|
|
"node_html": p.node_htmlFunc,
|
|
|
|
"comment_html": comment_htmlFunc,
|
2014-04-07 13:54:28 -06:00
|
|
|
"sanitize": sanitizeFunc,
|
2013-07-17 21:14:09 -06:00
|
|
|
|
|
|
|
// support for URL attributes
|
2016-09-16 06:42:50 -06:00
|
|
|
"pkgLink": pkgLinkFunc,
|
|
|
|
"srcLink": srcLinkFunc,
|
|
|
|
"posLink_url": newPosLink_urlFunc(srcPosLinkFunc),
|
|
|
|
"docLink": docLinkFunc,
|
|
|
|
"queryLink": queryLinkFunc,
|
|
|
|
"srcBreadcrumb": srcBreadcrumbFunc,
|
|
|
|
"srcToPkgLink": srcToPkgLinkFunc,
|
2013-07-17 21:14:09 -06:00
|
|
|
|
|
|
|
// formatting of Examples
|
|
|
|
"example_html": p.example_htmlFunc,
|
|
|
|
"example_name": p.example_nameFunc,
|
|
|
|
"example_suffix": p.example_suffixFunc,
|
|
|
|
|
2014-03-14 16:58:22 -06:00
|
|
|
// formatting of analysis information
|
|
|
|
"callgraph_html": p.callgraph_htmlFunc,
|
|
|
|
"implements_html": p.implements_htmlFunc,
|
|
|
|
"methodset_html": p.methodset_htmlFunc,
|
|
|
|
|
2013-07-17 21:14:09 -06:00
|
|
|
// formatting of Notes
|
|
|
|
"noteTitle": noteTitle,
|
2015-04-17 17:09:18 -06:00
|
|
|
|
|
|
|
// Number operation
|
|
|
|
"multiply": multiply,
|
2017-01-18 11:52:36 -07:00
|
|
|
|
|
|
|
// formatting of PageInfoMode query string
|
|
|
|
"modeQueryString": modeQueryString,
|
2018-01-28 07:23:55 -07:00
|
|
|
|
|
|
|
// check whether to display third party section or not
|
|
|
|
"hasThirdParty": hasThirdParty,
|
2018-08-11 02:03:17 -06:00
|
|
|
|
|
|
|
// get the no. of columns to split the toc in search page
|
|
|
|
"tocColCount": tocColCount,
|
2013-07-17 21:14:09 -06:00
|
|
|
}
|
2013-11-01 11:20:32 -06:00
|
|
|
if p.URLForSrc != nil {
|
|
|
|
p.funcMap["srcLink"] = p.URLForSrc
|
|
|
|
}
|
|
|
|
if p.URLForSrcPos != nil {
|
|
|
|
p.funcMap["posLink_url"] = newPosLink_urlFunc(p.URLForSrcPos)
|
|
|
|
}
|
2013-12-04 08:37:01 -07:00
|
|
|
if p.URLForSrcQuery != nil {
|
|
|
|
p.funcMap["queryLink"] = p.URLForSrcQuery
|
|
|
|
}
|
2013-07-17 01:17:12 -06:00
|
|
|
}
|
|
|
|
|
2015-04-17 17:09:18 -06:00
|
|
|
func multiply(a, b int) int { return a * b }
|
|
|
|
|
2013-07-17 01:17:12 -06:00
|
|
|
func filenameFunc(path string) string {
|
|
|
|
_, localname := pathpkg.Split(path)
|
|
|
|
return localname
|
|
|
|
}
|
|
|
|
|
|
|
|
func fileInfoNameFunc(fi os.FileInfo) string {
|
|
|
|
name := fi.Name()
|
|
|
|
if fi.IsDir() {
|
|
|
|
name += "/"
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
func fileInfoTimeFunc(fi os.FileInfo) string {
|
|
|
|
if t := fi.ModTime(); t.Unix() != 0 {
|
|
|
|
return t.Local().String()
|
|
|
|
}
|
|
|
|
return "" // don't return epoch if time is obviously not set
|
|
|
|
}
|
|
|
|
|
|
|
|
// The strings in infoKinds must be properly html-escaped.
|
|
|
|
var infoKinds = [nKinds]string{
|
|
|
|
PackageClause: "package clause",
|
|
|
|
ImportDecl: "import decl",
|
|
|
|
ConstDecl: "const decl",
|
|
|
|
TypeDecl: "type decl",
|
|
|
|
VarDecl: "var decl",
|
|
|
|
FuncDecl: "func decl",
|
|
|
|
MethodDecl: "method decl",
|
|
|
|
Use: "use",
|
|
|
|
}
|
|
|
|
|
|
|
|
func infoKind_htmlFunc(info SpotInfo) string {
|
|
|
|
return infoKinds[info.Kind()] // infoKind entries are html-escaped
|
|
|
|
}
|
|
|
|
|
2013-07-17 21:14:09 -06:00
|
|
|
func (p *Presentation) infoLineFunc(info SpotInfo) int {
|
2013-07-17 01:17:12 -06:00
|
|
|
line := info.Lori()
|
|
|
|
if info.IsIndex() {
|
2013-07-17 21:14:09 -06:00
|
|
|
index, _ := p.Corpus.searchIndex.Get()
|
2013-07-17 01:17:12 -06:00
|
|
|
if index != nil {
|
|
|
|
line = index.(*Index).Snippet(line).Line
|
|
|
|
} else {
|
|
|
|
// no line information available because
|
|
|
|
// we don't have an index - this should
|
|
|
|
// never happen; be conservative and don't
|
|
|
|
// crash
|
|
|
|
line = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return line
|
|
|
|
}
|
|
|
|
|
2013-07-17 21:14:09 -06:00
|
|
|
func (p *Presentation) infoSnippet_htmlFunc(info SpotInfo) string {
|
2013-07-17 01:17:12 -06:00
|
|
|
if info.IsIndex() {
|
2013-07-17 21:14:09 -06:00
|
|
|
index, _ := p.Corpus.searchIndex.Get()
|
2013-07-17 01:17:12 -06:00
|
|
|
// Snippet.Text was HTML-escaped when it was generated
|
|
|
|
return index.(*Index).Snippet(info.Lori()).Text
|
|
|
|
}
|
|
|
|
return `<span class="alert">no snippet text available</span>`
|
|
|
|
}
|
|
|
|
|
2013-07-17 21:14:09 -06:00
|
|
|
func (p *Presentation) nodeFunc(info *PageInfo, node interface{}) string {
|
2013-07-17 01:17:12 -06:00
|
|
|
var buf bytes.Buffer
|
2018-07-17 15:23:18 -06:00
|
|
|
p.writeNode(&buf, info, info.FSet, node)
|
2013-07-17 01:17:12 -06:00
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2013-07-17 21:14:09 -06:00
|
|
|
func (p *Presentation) node_htmlFunc(info *PageInfo, node interface{}, linkify bool) string {
|
2013-07-17 01:17:12 -06:00
|
|
|
var buf1 bytes.Buffer
|
2018-07-17 15:23:18 -06:00
|
|
|
p.writeNode(&buf1, info, info.FSet, node)
|
2013-07-17 01:17:12 -06:00
|
|
|
|
|
|
|
var buf2 bytes.Buffer
|
2013-07-17 21:14:09 -06:00
|
|
|
if n, _ := node.(ast.Node); n != nil && linkify && p.DeclLinks {
|
2013-07-17 01:17:12 -06:00
|
|
|
LinkifyText(&buf2, buf1.Bytes(), n)
|
2016-11-29 16:53:36 -07:00
|
|
|
if st, name := isStructTypeDecl(n); st != nil {
|
|
|
|
addStructFieldIDAttributes(&buf2, name, st)
|
|
|
|
}
|
2013-07-17 01:17:12 -06:00
|
|
|
} else {
|
|
|
|
FormatText(&buf2, buf1.Bytes(), -1, true, "", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf2.String()
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:53:36 -07:00
|
|
|
// isStructTypeDecl checks whether n is a struct declaration.
|
|
|
|
// It either returns a non-nil StructType and its name, or zero values.
|
|
|
|
func isStructTypeDecl(n ast.Node) (st *ast.StructType, name string) {
|
|
|
|
gd, ok := n.(*ast.GenDecl)
|
|
|
|
if !ok || gd.Tok != token.TYPE {
|
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
if gd.Lparen > 0 {
|
|
|
|
// Parenthesized type. Who does that, anyway?
|
|
|
|
// TODO: Reportedly gri does. Fix this to handle that too.
|
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
if len(gd.Specs) != 1 {
|
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
ts, ok := gd.Specs[0].(*ast.TypeSpec)
|
|
|
|
if !ok {
|
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
st, ok = ts.Type.(*ast.StructType)
|
|
|
|
if !ok {
|
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
return st, ts.Name.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// addStructFieldIDAttributes modifies the contents of buf such that
|
|
|
|
// all struct fields of the named struct have <span id='name.Field'>
|
|
|
|
// in them, so people can link to /#Struct.Field.
|
|
|
|
func addStructFieldIDAttributes(buf *bytes.Buffer, name string, st *ast.StructType) {
|
|
|
|
if st.Fields == nil {
|
|
|
|
return
|
|
|
|
}
|
2017-01-20 11:35:00 -07:00
|
|
|
// needsLink is a set of identifiers that still need to be
|
|
|
|
// linked, where value == key, to avoid an allocation in func
|
|
|
|
// linkedField.
|
|
|
|
needsLink := make(map[string]string)
|
|
|
|
|
2016-11-29 16:53:36 -07:00
|
|
|
for _, f := range st.Fields.List {
|
|
|
|
if len(f.Names) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fieldName := f.Names[0].Name
|
2017-01-20 11:35:00 -07:00
|
|
|
needsLink[fieldName] = fieldName
|
2016-11-30 16:49:04 -07:00
|
|
|
}
|
2017-01-20 11:35:00 -07:00
|
|
|
var newBuf bytes.Buffer
|
|
|
|
foreachLine(buf.Bytes(), func(line []byte) {
|
|
|
|
if fieldName := linkedField(line, needsLink); fieldName != "" {
|
|
|
|
fmt.Fprintf(&newBuf, `<span id="%s.%s"></span>`, name, fieldName)
|
|
|
|
delete(needsLink, fieldName)
|
|
|
|
}
|
|
|
|
newBuf.Write(line)
|
|
|
|
})
|
|
|
|
buf.Reset()
|
|
|
|
buf.Write(newBuf.Bytes())
|
2016-11-30 16:49:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// foreachLine calls fn for each line of in, where a line includes
|
|
|
|
// the trailing "\n", except on the last line, if it doesn't exist.
|
|
|
|
func foreachLine(in []byte, fn func(line []byte)) {
|
|
|
|
for len(in) > 0 {
|
|
|
|
nl := bytes.IndexByte(in, '\n')
|
|
|
|
if nl == -1 {
|
|
|
|
fn(in)
|
|
|
|
return
|
2016-11-29 16:53:36 -07:00
|
|
|
}
|
2016-11-30 16:49:04 -07:00
|
|
|
fn(in[:nl+1])
|
|
|
|
in = in[nl+1:]
|
2016-11-29 16:53:36 -07:00
|
|
|
}
|
2016-11-30 16:49:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// commentPrefix is the line prefix for comments after they've been HTMLified.
|
|
|
|
var commentPrefix = []byte(`<span class="comment">// `)
|
|
|
|
|
2017-01-20 11:35:00 -07:00
|
|
|
// linkedField determines whether the given line starts with an
|
2019-11-19 13:32:09 -07:00
|
|
|
// identifier in the provided ids map (mapping from identifier to the
|
2017-01-20 11:35:00 -07:00
|
|
|
// same identifier). The line can start with either an identifier or
|
|
|
|
// an identifier in a comment. If one matches, it returns the
|
|
|
|
// identifier that matched. Otherwise it returns the empty string.
|
|
|
|
func linkedField(line []byte, ids map[string]string) string {
|
2016-11-30 16:49:04 -07:00
|
|
|
line = bytes.TrimSpace(line)
|
|
|
|
|
|
|
|
// For fields with a doc string of the
|
|
|
|
// conventional form, we put the new span into
|
|
|
|
// the comment instead of the field.
|
|
|
|
// The "conventional" form is a complete sentence
|
|
|
|
// per https://golang.org/s/style#comment-sentences like:
|
|
|
|
//
|
|
|
|
// // Foo is an optional Fooer to foo the foos.
|
|
|
|
// Foo Fooer
|
|
|
|
//
|
|
|
|
// In this case, we want the #StructName.Foo
|
|
|
|
// link to make the browser go to the comment
|
|
|
|
// line "Foo is an optional Fooer" instead of
|
|
|
|
// the "Foo Fooer" line, which could otherwise
|
|
|
|
// obscure the docs above the browser's "fold".
|
|
|
|
//
|
|
|
|
// TODO: do this better, so it works for all
|
|
|
|
// comments, including unconventional ones.
|
2019-11-20 20:43:00 -07:00
|
|
|
line = bytes.TrimPrefix(line, commentPrefix)
|
2017-01-20 11:35:00 -07:00
|
|
|
id := scanIdentifier(line)
|
|
|
|
if len(id) == 0 {
|
|
|
|
// No leading identifier. Avoid map lookup for
|
|
|
|
// somewhat common case.
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ids[string(id)]
|
|
|
|
}
|
|
|
|
|
|
|
|
// scanIdentifier scans a valid Go identifier off the front of v and
|
|
|
|
// either returns a subslice of v if there's a valid identifier, or
|
|
|
|
// returns a zero-length slice.
|
|
|
|
func scanIdentifier(v []byte) []byte {
|
|
|
|
var n int // number of leading bytes of v belonging to an identifier
|
|
|
|
for {
|
|
|
|
r, width := utf8.DecodeRune(v[n:])
|
|
|
|
if !(isLetter(r) || n > 0 && isDigit(r)) {
|
|
|
|
break
|
2016-11-30 16:49:04 -07:00
|
|
|
}
|
2017-01-20 11:35:00 -07:00
|
|
|
n += width
|
2016-11-30 16:49:04 -07:00
|
|
|
}
|
2017-01-20 11:35:00 -07:00
|
|
|
return v[:n]
|
|
|
|
}
|
|
|
|
|
|
|
|
func isLetter(ch rune) bool {
|
|
|
|
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= utf8.RuneSelf && unicode.IsLetter(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isDigit(ch rune) bool {
|
|
|
|
return '0' <= ch && ch <= '9' || ch >= utf8.RuneSelf && unicode.IsDigit(ch)
|
2016-11-30 16:49:04 -07:00
|
|
|
}
|
|
|
|
|
2013-07-17 01:17:12 -06:00
|
|
|
func comment_htmlFunc(comment string) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
// TODO(gri) Provide list of words (e.g. function parameters)
|
|
|
|
// to be emphasized by ToHTML.
|
|
|
|
doc.ToHTML(&buf, comment, nil) // does html-escaping
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2014-04-07 13:54:28 -06:00
|
|
|
// sanitizeFunc sanitizes the argument src by replacing newlines with
|
|
|
|
// blanks, removing extra blanks, and by removing trailing whitespace
|
|
|
|
// and commas before closing parentheses.
|
|
|
|
func sanitizeFunc(src string) string {
|
|
|
|
buf := make([]byte, len(src))
|
|
|
|
j := 0 // buf index
|
|
|
|
comma := -1 // comma index if >= 0
|
|
|
|
for i := 0; i < len(src); i++ {
|
|
|
|
ch := src[i]
|
|
|
|
switch ch {
|
|
|
|
case '\t', '\n', ' ':
|
|
|
|
// ignore whitespace at the beginning, after a blank, or after opening parentheses
|
|
|
|
if j == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if p := buf[j-1]; p == ' ' || p == '(' || p == '{' || p == '[' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// replace all whitespace with blanks
|
|
|
|
ch = ' '
|
|
|
|
case ',':
|
|
|
|
comma = j
|
|
|
|
case ')', '}', ']':
|
|
|
|
// remove any trailing comma
|
|
|
|
if comma >= 0 {
|
|
|
|
j = comma
|
|
|
|
}
|
|
|
|
// remove any trailing whitespace
|
|
|
|
if j > 0 && buf[j-1] == ' ' {
|
|
|
|
j--
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
comma = -1
|
|
|
|
}
|
|
|
|
buf[j] = ch
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
// remove trailing blank, if any
|
|
|
|
if j > 0 && buf[j-1] == ' ' {
|
|
|
|
j--
|
|
|
|
}
|
|
|
|
return string(buf[:j])
|
|
|
|
}
|
|
|
|
|
2013-07-17 01:17:12 -06:00
|
|
|
type PageInfo struct {
|
2017-08-02 14:37:59 -06:00
|
|
|
Dirname string // directory containing the package
|
|
|
|
Err error // error or nil
|
|
|
|
GoogleCN bool // page is being served from golang.google.cn
|
2013-07-17 01:17:12 -06:00
|
|
|
|
2017-01-18 11:52:36 -07:00
|
|
|
Mode PageInfoMode // display metadata from query string
|
|
|
|
|
2013-07-17 01:17:12 -06:00
|
|
|
// package info
|
2014-01-30 04:28:19 -07:00
|
|
|
FSet *token.FileSet // nil if no package documentation
|
|
|
|
PDoc *doc.Package // nil if no package documentation
|
|
|
|
Examples []*doc.Example // nil if no example code
|
|
|
|
Notes map[string][]*doc.Note // nil if no package Notes
|
|
|
|
PAst map[string]*ast.File // nil if no AST with package exports
|
|
|
|
IsMain bool // true for package main
|
|
|
|
IsFiltered bool // true if results were filtered
|
2013-07-17 01:17:12 -06:00
|
|
|
|
2014-03-14 16:58:22 -06:00
|
|
|
// analysis info
|
|
|
|
TypeInfoIndex map[string]int // index of JSON datum for type T (if -analysis=type)
|
|
|
|
AnalysisData htmltemplate.JS // array of TypeInfoJSON values
|
|
|
|
CallGraph htmltemplate.JS // array of PCGNodeJSON values (if -analysis=pointer)
|
|
|
|
CallGraphIndex map[string]int // maps func name to index in CallGraph
|
|
|
|
|
2013-07-17 01:17:12 -06:00
|
|
|
// directory info
|
|
|
|
Dirs *DirList // nil if no directory information
|
|
|
|
DirTime time.Time // directory time stamp
|
|
|
|
DirFlat bool // if set, show directory in a flat (non-indented) manner
|
|
|
|
}
|
|
|
|
|
|
|
|
func (info *PageInfo) IsEmpty() bool {
|
|
|
|
return info.Err != nil || info.PAst == nil && info.PDoc == nil && info.Dirs == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func pkgLinkFunc(path string) string {
|
|
|
|
// because of the irregular mapping under goroot
|
|
|
|
// we need to correct certain relative paths
|
2014-06-27 08:25:57 -06:00
|
|
|
path = strings.TrimPrefix(path, "/")
|
|
|
|
path = strings.TrimPrefix(path, "src/")
|
|
|
|
path = strings.TrimPrefix(path, "pkg/")
|
|
|
|
return "pkg/" + path
|
2013-07-17 01:17:12 -06:00
|
|
|
}
|
|
|
|
|
2017-08-03 16:01:02 -06:00
|
|
|
// srcToPkgLinkFunc builds an <a> tag linking to the package
|
|
|
|
// documentation of relpath.
|
2016-09-16 06:42:50 -06:00
|
|
|
func srcToPkgLinkFunc(relpath string) string {
|
|
|
|
relpath = pkgLinkFunc(relpath)
|
2017-08-03 16:01:02 -06:00
|
|
|
relpath = pathpkg.Dir(relpath)
|
|
|
|
if relpath == "pkg" {
|
2016-09-16 06:42:50 -06:00
|
|
|
return `<a href="/pkg">Index</a>`
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(`<a href="/%s">%s</a>`, relpath, relpath[len("pkg/"):])
|
|
|
|
}
|
|
|
|
|
|
|
|
// srcBreadcrumbFun converts each segment of relpath to a HTML <a>.
|
|
|
|
// Each segment links to its corresponding src directories.
|
|
|
|
func srcBreadcrumbFunc(relpath string) string {
|
|
|
|
segments := strings.Split(relpath, "/")
|
|
|
|
var buf bytes.Buffer
|
|
|
|
var selectedSegment string
|
|
|
|
var selectedIndex int
|
|
|
|
|
|
|
|
if strings.HasSuffix(relpath, "/") {
|
|
|
|
// relpath is a directory ending with a "/".
|
|
|
|
// Selected segment is the segment before the last slash.
|
|
|
|
selectedIndex = len(segments) - 2
|
|
|
|
selectedSegment = segments[selectedIndex] + "/"
|
|
|
|
} else {
|
|
|
|
selectedIndex = len(segments) - 1
|
|
|
|
selectedSegment = segments[selectedIndex]
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range segments[:selectedIndex] {
|
|
|
|
buf.WriteString(fmt.Sprintf(`<a href="/%s">%s</a>/`,
|
|
|
|
strings.Join(segments[:i+1], "/"),
|
|
|
|
segments[i],
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString(`<span class="text-muted">`)
|
|
|
|
buf.WriteString(selectedSegment)
|
|
|
|
buf.WriteString(`</span>`)
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2013-11-01 11:20:32 -06:00
|
|
|
func newPosLink_urlFunc(srcPosLinkFunc func(s string, line, low, high int) string) func(info *PageInfo, n interface{}) string {
|
|
|
|
// n must be an ast.Node or a *doc.Note
|
|
|
|
return func(info *PageInfo, n interface{}) string {
|
|
|
|
var pos, end token.Pos
|
|
|
|
|
|
|
|
switch n := n.(type) {
|
|
|
|
case ast.Node:
|
|
|
|
pos = n.Pos()
|
|
|
|
end = n.End()
|
|
|
|
case *doc.Note:
|
|
|
|
pos = n.Pos
|
|
|
|
end = n.End
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("wrong type for posLink_url template formatter: %T", n))
|
|
|
|
}
|
2013-07-17 01:17:12 -06:00
|
|
|
|
2013-11-01 11:20:32 -06:00
|
|
|
var relpath string
|
|
|
|
var line int
|
|
|
|
var low, high int // selection offset range
|
2013-07-17 01:17:12 -06:00
|
|
|
|
2013-11-01 11:20:32 -06:00
|
|
|
if pos.IsValid() {
|
|
|
|
p := info.FSet.Position(pos)
|
|
|
|
relpath = p.Filename
|
|
|
|
line = p.Line
|
|
|
|
low = p.Offset
|
|
|
|
}
|
|
|
|
if end.IsValid() {
|
|
|
|
high = info.FSet.Position(end).Offset
|
|
|
|
}
|
|
|
|
|
|
|
|
return srcPosLinkFunc(relpath, line, low, high)
|
2013-07-17 01:17:12 -06:00
|
|
|
}
|
2013-11-01 11:20:32 -06:00
|
|
|
}
|
2013-07-17 01:17:12 -06:00
|
|
|
|
2013-11-01 11:20:32 -06:00
|
|
|
func srcPosLinkFunc(s string, line, low, high int) string {
|
2014-02-21 17:51:51 -07:00
|
|
|
s = srcLinkFunc(s)
|
2013-07-17 01:17:12 -06:00
|
|
|
var buf bytes.Buffer
|
2013-11-01 11:20:32 -06:00
|
|
|
template.HTMLEscape(&buf, []byte(s))
|
2013-07-17 01:17:12 -06:00
|
|
|
// selection ranges are of form "s=low:high"
|
|
|
|
if low < high {
|
|
|
|
fmt.Fprintf(&buf, "?s=%d:%d", low, high) // no need for URL escaping
|
|
|
|
// if we have a selection, position the page
|
|
|
|
// such that the selection is a bit below the top
|
|
|
|
line -= 10
|
|
|
|
if line < 1 {
|
|
|
|
line = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// line id's in html-printed source are of the
|
|
|
|
// form "L%d" where %d stands for the line number
|
|
|
|
if line > 0 {
|
|
|
|
fmt.Fprintf(&buf, "#L%d", line) // no need for URL escaping
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func srcLinkFunc(s string) string {
|
2014-02-21 17:51:51 -07:00
|
|
|
s = pathpkg.Clean("/" + s)
|
2014-09-10 07:02:54 -06:00
|
|
|
if !strings.HasPrefix(s, "/src/") {
|
|
|
|
s = "/src" + s
|
2014-02-21 17:51:51 -07:00
|
|
|
}
|
|
|
|
return s
|
2013-07-17 01:17:12 -06:00
|
|
|
}
|
|
|
|
|
2013-12-04 08:37:01 -07:00
|
|
|
// queryLinkFunc returns a URL for a line in a source file with a highlighted
|
|
|
|
// query term.
|
|
|
|
// s is expected to be a path to a source file.
|
|
|
|
// query is expected to be a string that has already been appropriately escaped
|
|
|
|
// for use in a URL query.
|
|
|
|
func queryLinkFunc(s, query string, line int) string {
|
|
|
|
url := pathpkg.Clean("/"+s) + "?h=" + query
|
|
|
|
if line > 0 {
|
|
|
|
url += "#L" + strconv.Itoa(line)
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2013-11-21 09:55:42 -07:00
|
|
|
func docLinkFunc(s string, ident string) string {
|
2014-06-27 08:25:57 -06:00
|
|
|
return pathpkg.Clean("/pkg/"+s) + "/#" + ident
|
2013-11-21 09:55:42 -07:00
|
|
|
}
|
|
|
|
|
2013-07-17 21:14:09 -06:00
|
|
|
func (p *Presentation) example_htmlFunc(info *PageInfo, funcName string) string {
|
2013-07-17 01:17:12 -06:00
|
|
|
var buf bytes.Buffer
|
|
|
|
for _, eg := range info.Examples {
|
|
|
|
name := stripExampleSuffix(eg.Name)
|
|
|
|
|
|
|
|
if name != funcName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// print code
|
|
|
|
cnode := &printer.CommentedNode{Node: eg.Code, Comments: eg.Comments}
|
2013-07-17 21:14:09 -06:00
|
|
|
code := p.node_htmlFunc(info, cnode, true)
|
2013-07-17 01:17:12 -06:00
|
|
|
out := eg.Output
|
|
|
|
wholeFile := true
|
|
|
|
|
|
|
|
// Additional formatting if this is a function body.
|
|
|
|
if n := len(code); n >= 2 && code[0] == '{' && code[n-1] == '}' {
|
|
|
|
wholeFile = false
|
|
|
|
// remove surrounding braces
|
|
|
|
code = code[1 : n-1]
|
|
|
|
// unindent
|
2017-02-07 15:34:43 -07:00
|
|
|
code = replaceLeadingIndentation(code, strings.Repeat(" ", p.TabWidth), "")
|
2013-07-17 01:17:12 -06:00
|
|
|
// remove output comment
|
|
|
|
if loc := exampleOutputRx.FindStringIndex(code); loc != nil {
|
|
|
|
code = strings.TrimSpace(code[:loc[0]])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out the playground code in standard Go style
|
|
|
|
// (use tabs, no comment highlight, etc).
|
|
|
|
play := ""
|
2013-07-17 21:14:09 -06:00
|
|
|
if eg.Play != nil && p.ShowPlayground {
|
2013-07-17 01:17:12 -06:00
|
|
|
var buf bytes.Buffer
|
2018-07-31 11:27:32 -06:00
|
|
|
eg.Play.Comments = filterOutBuildAnnotations(eg.Play.Comments)
|
2013-07-17 01:17:12 -06:00
|
|
|
if err := format.Node(&buf, info.FSet, eg.Play); err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
} else {
|
|
|
|
play = buf.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop output, as the output comment will appear in the code.
|
|
|
|
if wholeFile && play == "" {
|
|
|
|
out = ""
|
|
|
|
}
|
|
|
|
|
2013-07-17 21:51:17 -06:00
|
|
|
if p.ExampleHTML == nil {
|
2013-07-17 01:17:12 -06:00
|
|
|
out = ""
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2013-07-17 21:51:17 -06:00
|
|
|
err := p.ExampleHTML.Execute(&buf, struct {
|
2013-07-17 01:17:12 -06:00
|
|
|
Name, Doc, Code, Play, Output string
|
2017-08-02 14:37:59 -06:00
|
|
|
GoogleCN bool
|
|
|
|
}{eg.Name, eg.Doc, code, play, out, info.GoogleCN})
|
2013-07-17 01:17:12 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2018-07-31 11:27:32 -06:00
|
|
|
func filterOutBuildAnnotations(cg []*ast.CommentGroup) []*ast.CommentGroup {
|
|
|
|
if len(cg) == 0 {
|
|
|
|
return cg
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range cg {
|
|
|
|
if !strings.HasPrefix(cg[i].Text(), "+build ") {
|
|
|
|
// Found the first non-build tag, return from here until the end
|
|
|
|
// of the slice.
|
|
|
|
return cg[i:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There weren't any non-build tags, return an empty slice.
|
|
|
|
return []*ast.CommentGroup{}
|
|
|
|
}
|
|
|
|
|
2013-07-17 01:17:12 -06:00
|
|
|
// example_nameFunc takes an example function name and returns its display
|
|
|
|
// name. For example, "Foo_Bar_quux" becomes "Foo.Bar (Quux)".
|
2013-07-17 21:14:09 -06:00
|
|
|
func (p *Presentation) example_nameFunc(s string) string {
|
2013-07-17 01:17:12 -06:00
|
|
|
name, suffix := splitExampleName(s)
|
|
|
|
// replace _ with . for method names
|
|
|
|
name = strings.Replace(name, "_", ".", 1)
|
|
|
|
// use "Package" if no name provided
|
|
|
|
if name == "" {
|
|
|
|
name = "Package"
|
|
|
|
}
|
|
|
|
return name + suffix
|
|
|
|
}
|
|
|
|
|
|
|
|
// example_suffixFunc takes an example function name and returns its suffix in
|
|
|
|
// parenthesized form. For example, "Foo_Bar_quux" becomes " (Quux)".
|
2013-07-17 21:14:09 -06:00
|
|
|
func (p *Presentation) example_suffixFunc(name string) string {
|
2013-07-17 01:17:12 -06:00
|
|
|
_, suffix := splitExampleName(name)
|
|
|
|
return suffix
|
|
|
|
}
|
|
|
|
|
2014-03-14 16:58:22 -06:00
|
|
|
// implements_html returns the "> Implements" toggle for a package-level named type.
|
|
|
|
// Its contents are populated from JSON data by client-side JS at load time.
|
|
|
|
func (p *Presentation) implements_htmlFunc(info *PageInfo, typeName string) string {
|
|
|
|
if p.ImplementsHTML == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
index, ok := info.TypeInfoIndex[typeName]
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := p.ImplementsHTML.Execute(&buf, struct{ Index int }{index})
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// methodset_html returns the "> Method set" toggle for a package-level named type.
|
|
|
|
// Its contents are populated from JSON data by client-side JS at load time.
|
|
|
|
func (p *Presentation) methodset_htmlFunc(info *PageInfo, typeName string) string {
|
|
|
|
if p.MethodSetHTML == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
index, ok := info.TypeInfoIndex[typeName]
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := p.MethodSetHTML.Execute(&buf, struct{ Index int }{index})
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// callgraph_html returns the "> Call graph" toggle for a package-level func.
|
|
|
|
// Its contents are populated from JSON data by client-side JS at load time.
|
|
|
|
func (p *Presentation) callgraph_htmlFunc(info *PageInfo, recv, name string) string {
|
|
|
|
if p.CallGraphHTML == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if recv != "" {
|
|
|
|
// Format must match (*ssa.Function).RelString().
|
|
|
|
name = fmt.Sprintf("(%s).%s", recv, name)
|
|
|
|
}
|
|
|
|
index, ok := info.CallGraphIndex[name]
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := p.CallGraphHTML.Execute(&buf, struct{ Index int }{index})
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2013-07-17 01:17:12 -06:00
|
|
|
func noteTitle(note string) string {
|
|
|
|
return strings.Title(strings.ToLower(note))
|
|
|
|
}
|
|
|
|
|
|
|
|
func startsWithUppercase(s string) bool {
|
|
|
|
r, _ := utf8.DecodeRuneInString(s)
|
|
|
|
return unicode.IsUpper(r)
|
|
|
|
}
|
|
|
|
|
2016-04-13 17:31:55 -06:00
|
|
|
var exampleOutputRx = regexp.MustCompile(`(?i)//[[:space:]]*(unordered )?output:`)
|
2013-07-17 01:17:12 -06:00
|
|
|
|
|
|
|
// stripExampleSuffix strips lowercase braz in Foo_braz or Foo_Bar_braz from name
|
|
|
|
// while keeping uppercase Braz in Foo_Braz.
|
|
|
|
func stripExampleSuffix(name string) string {
|
|
|
|
if i := strings.LastIndex(name, "_"); i != -1 {
|
|
|
|
if i < len(name)-1 && !startsWithUppercase(name[i+1:]) {
|
|
|
|
name = name[:i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitExampleName(s string) (name, suffix string) {
|
|
|
|
i := strings.LastIndex(s, "_")
|
|
|
|
if 0 <= i && i < len(s)-1 && !startsWithUppercase(s[i+1:]) {
|
|
|
|
name = s[:i]
|
|
|
|
suffix = " (" + strings.Title(s[i+1:]) + ")"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name = s
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-07 15:34:43 -07:00
|
|
|
// replaceLeadingIndentation replaces oldIndent at the beginning of each line
|
|
|
|
// with newIndent. This is used for formatting examples. Raw strings that
|
|
|
|
// span multiple lines are handled specially: oldIndent is not removed (since
|
|
|
|
// go/printer will not add any indentation there), but newIndent is added
|
|
|
|
// (since we may still want leading indentation).
|
|
|
|
func replaceLeadingIndentation(body, oldIndent, newIndent string) string {
|
|
|
|
// Handle indent at the beginning of the first line. After this, we handle
|
|
|
|
// indentation only after a newline.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if strings.HasPrefix(body, oldIndent) {
|
|
|
|
buf.WriteString(newIndent)
|
|
|
|
body = body[len(oldIndent):]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use a state machine to keep track of whether we're in a string or
|
|
|
|
// rune literal while we process the rest of the code.
|
|
|
|
const (
|
|
|
|
codeState = iota
|
|
|
|
runeState
|
|
|
|
interpretedStringState
|
|
|
|
rawStringState
|
|
|
|
)
|
|
|
|
searchChars := []string{
|
|
|
|
"'\"`\n", // codeState
|
|
|
|
`\'`, // runeState
|
|
|
|
`\"`, // interpretedStringState
|
|
|
|
"`\n", // rawStringState
|
|
|
|
// newlineState does not need to search
|
|
|
|
}
|
|
|
|
state := codeState
|
|
|
|
for {
|
|
|
|
i := strings.IndexAny(body, searchChars[state])
|
|
|
|
if i < 0 {
|
|
|
|
buf.WriteString(body)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
c := body[i]
|
|
|
|
buf.WriteString(body[:i+1])
|
|
|
|
body = body[i+1:]
|
|
|
|
switch state {
|
|
|
|
case codeState:
|
|
|
|
switch c {
|
|
|
|
case '\'':
|
|
|
|
state = runeState
|
|
|
|
case '"':
|
|
|
|
state = interpretedStringState
|
|
|
|
case '`':
|
|
|
|
state = rawStringState
|
|
|
|
case '\n':
|
|
|
|
if strings.HasPrefix(body, oldIndent) {
|
|
|
|
buf.WriteString(newIndent)
|
|
|
|
body = body[len(oldIndent):]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case runeState:
|
|
|
|
switch c {
|
|
|
|
case '\\':
|
|
|
|
r, size := utf8.DecodeRuneInString(body)
|
|
|
|
buf.WriteRune(r)
|
|
|
|
body = body[size:]
|
|
|
|
case '\'':
|
|
|
|
state = codeState
|
|
|
|
}
|
|
|
|
|
|
|
|
case interpretedStringState:
|
|
|
|
switch c {
|
|
|
|
case '\\':
|
|
|
|
r, size := utf8.DecodeRuneInString(body)
|
|
|
|
buf.WriteRune(r)
|
|
|
|
body = body[size:]
|
|
|
|
case '"':
|
|
|
|
state = codeState
|
|
|
|
}
|
|
|
|
|
|
|
|
case rawStringState:
|
|
|
|
switch c {
|
|
|
|
case '`':
|
|
|
|
state = codeState
|
|
|
|
case '\n':
|
|
|
|
buf.WriteString(newIndent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2018-07-17 15:23:18 -06:00
|
|
|
// writeNode writes the AST node x to w.
|
|
|
|
//
|
|
|
|
// The provided fset must be non-nil. The pageInfo is optional. If
|
|
|
|
// present, the pageInfo is used to add comments to struct fields to
|
|
|
|
// say which version of Go introduced them.
|
|
|
|
func (p *Presentation) writeNode(w io.Writer, pageInfo *PageInfo, fset *token.FileSet, x interface{}) {
|
2013-07-17 01:17:12 -06:00
|
|
|
// convert trailing tabs into spaces using a tconv filter
|
|
|
|
// to ensure a good outcome in most browsers (there may still
|
|
|
|
// be tabs in comments and strings, but converting those into
|
|
|
|
// the right number of spaces is much harder)
|
|
|
|
//
|
|
|
|
// TODO(gri) rethink printer flags - perhaps tconv can be eliminated
|
|
|
|
// with an another printer mode (which is more efficiently
|
|
|
|
// implemented in the printer than here with another layer)
|
2018-07-17 15:23:18 -06:00
|
|
|
|
|
|
|
var pkgName, structName string
|
|
|
|
var apiInfo pkgAPIVersions
|
|
|
|
if gd, ok := x.(*ast.GenDecl); ok && pageInfo != nil && pageInfo.PDoc != nil &&
|
|
|
|
p.Corpus != nil &&
|
|
|
|
gd.Tok == token.TYPE && len(gd.Specs) != 0 {
|
|
|
|
pkgName = pageInfo.PDoc.ImportPath
|
|
|
|
if ts, ok := gd.Specs[0].(*ast.TypeSpec); ok {
|
|
|
|
if _, ok := ts.Type.(*ast.StructType); ok {
|
|
|
|
structName = ts.Name.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
apiInfo = p.Corpus.pkgAPIInfo[pkgName]
|
|
|
|
}
|
|
|
|
|
|
|
|
var out = w
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if structName != "" {
|
|
|
|
out = &buf
|
|
|
|
}
|
|
|
|
|
2013-07-17 01:17:12 -06:00
|
|
|
mode := printer.TabIndent | printer.UseSpaces
|
2018-07-17 15:23:18 -06:00
|
|
|
err := (&printer.Config{Mode: mode, Tabwidth: p.TabWidth}).Fprint(&tconv{p: p, output: out}, fset, x)
|
2013-07-17 01:17:12 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2018-07-17 15:23:18 -06:00
|
|
|
|
2019-09-11 00:14:36 -06:00
|
|
|
// Add comments to struct fields saying which Go version introduced them.
|
2018-07-17 15:23:18 -06:00
|
|
|
if structName != "" {
|
|
|
|
fieldSince := apiInfo.fieldSince[structName]
|
|
|
|
typeSince := apiInfo.typeSince[structName]
|
|
|
|
// Add/rewrite comments on struct fields to note which Go version added them.
|
|
|
|
var buf2 bytes.Buffer
|
|
|
|
buf2.Grow(buf.Len() + len(" // Added in Go 1.n")*10)
|
|
|
|
bs := bufio.NewScanner(&buf)
|
|
|
|
for bs.Scan() {
|
|
|
|
line := bs.Bytes()
|
|
|
|
field := firstIdent(line)
|
|
|
|
var since string
|
|
|
|
if field != "" {
|
|
|
|
since = fieldSince[field]
|
|
|
|
if since != "" && since == typeSince {
|
|
|
|
// Don't highlight field versions if they were the
|
|
|
|
// same as the struct itself.
|
|
|
|
since = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if since == "" {
|
|
|
|
buf2.Write(line)
|
|
|
|
} else {
|
|
|
|
if bytes.Contains(line, slashSlash) {
|
|
|
|
line = bytes.TrimRight(line, " \t.")
|
|
|
|
buf2.Write(line)
|
|
|
|
buf2.WriteString("; added in Go ")
|
|
|
|
} else {
|
|
|
|
buf2.Write(line)
|
|
|
|
buf2.WriteString(" // Go ")
|
|
|
|
}
|
|
|
|
buf2.WriteString(since)
|
|
|
|
}
|
|
|
|
buf2.WriteByte('\n')
|
|
|
|
}
|
|
|
|
w.Write(buf2.Bytes())
|
|
|
|
}
|
2013-07-17 01:17:12 -06:00
|
|
|
}
|
|
|
|
|
2018-07-17 15:23:18 -06:00
|
|
|
var slashSlash = []byte("//")
|
|
|
|
|
2014-04-07 13:54:28 -06:00
|
|
|
// WriteNode writes x to w.
|
|
|
|
// TODO(bgarcia) Is this method needed? It's just a wrapper for p.writeNode.
|
2013-07-17 21:14:09 -06:00
|
|
|
func (p *Presentation) WriteNode(w io.Writer, fset *token.FileSet, x interface{}) {
|
2018-07-17 15:23:18 -06:00
|
|
|
p.writeNode(w, nil, fset, x)
|
|
|
|
}
|
|
|
|
|
|
|
|
// firstIdent returns the first identifier in x.
|
|
|
|
// This actually parses "identifiers" that begin with numbers too, but we
|
|
|
|
// never feed it such input, so it's fine.
|
|
|
|
func firstIdent(x []byte) string {
|
|
|
|
x = bytes.TrimSpace(x)
|
|
|
|
i := bytes.IndexFunc(x, func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsNumber(r) })
|
|
|
|
if i == -1 {
|
|
|
|
return string(x)
|
|
|
|
}
|
|
|
|
return string(x[:i])
|
2013-07-17 21:14:09 -06:00
|
|
|
}
|