GoPLS Viewer

Home|gopls/godoc/snippet.go
1// Copyright 2009 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// This file contains the infrastructure to create a code
6// snippet for search results.
7//
8// Note: At the moment, this only creates HTML snippets.
9
10package godoc
11
12import (
13    "bytes"
14    "fmt"
15    "go/ast"
16    "go/token"
17)
18
19type Snippet struct {
20    Line int
21    Text string // HTML-escaped
22}
23
24func (p *PresentationnewSnippet(fset *token.FileSetdecl ast.Declid *ast.Ident) *Snippet {
25    // TODO instead of pretty-printing the node, should use the original source instead
26    var buf1 bytes.Buffer
27    p.writeNode(&buf1nilfsetdecl)
28    // wrap text with <pre> tag
29    var buf2 bytes.Buffer
30    buf2.WriteString("<pre>")
31    FormatText(&buf2buf1.Bytes(), -1trueid.Namenil)
32    buf2.WriteString("</pre>")
33    return &Snippet{fset.Position(id.Pos()).Linebuf2.String()}
34}
35
36func findSpec(list []ast.Specid *ast.Identast.Spec {
37    for _spec := range list {
38        switch s := spec.(type) {
39        case *ast.ImportSpec:
40            if s.Name == id {
41                return s
42            }
43        case *ast.ValueSpec:
44            for _n := range s.Names {
45                if n == id {
46                    return s
47                }
48            }
49        case *ast.TypeSpec:
50            if s.Name == id {
51                return s
52            }
53        }
54    }
55    return nil
56}
57
58func (p *PresentationgenSnippet(fset *token.FileSetd *ast.GenDeclid *ast.Ident) *Snippet {
59    s := findSpec(d.Specsid)
60    if s == nil {
61        return nil //  declaration doesn't contain id - exit gracefully
62    }
63
64    // only use the spec containing the id for the snippet
65    dd := &ast.GenDecl{
66        Doc:    d.Doc,
67        TokPosd.Pos(),
68        Tok:    d.Tok,
69        Lparend.Lparen,
70        Specs:  []ast.Spec{s},
71        Rparend.Rparen,
72    }
73
74    return p.newSnippet(fsetddid)
75}
76
77func (p *PresentationfuncSnippet(fset *token.FileSetd *ast.FuncDeclid *ast.Ident) *Snippet {
78    if d.Name != id {
79        return nil //  declaration doesn't contain id - exit gracefully
80    }
81
82    // only use the function signature for the snippet
83    dd := &ast.FuncDecl{
84        Doc:  d.Doc,
85        Recvd.Recv,
86        Named.Name,
87        Typed.Type,
88    }
89
90    return p.newSnippet(fsetddid)
91}
92
93// NewSnippet creates a text snippet from a declaration decl containing an
94// identifier id. Parts of the declaration not containing the identifier
95// may be removed for a more compact snippet.
96func NewSnippet(fset *token.FileSetdecl ast.Declid *ast.Ident) *Snippet {
97    // TODO(bradfitz, adg): remove this function.  But it's used by indexer, which
98    // doesn't have a *Presentation, and NewSnippet needs a TabWidth.
99    var p Presentation
100    p.TabWidth = 4
101    return p.NewSnippet(fsetdeclid)
102}
103
104// NewSnippet creates a text snippet from a declaration decl containing an
105// identifier id. Parts of the declaration not containing the identifier
106// may be removed for a more compact snippet.
107func (p *PresentationNewSnippet(fset *token.FileSetdecl ast.Declid *ast.Ident) *Snippet {
108    var s *Snippet
109    switch d := decl.(type) {
110    case *ast.GenDecl:
111        s = p.genSnippet(fsetdid)
112    case *ast.FuncDecl:
113        s = p.funcSnippet(fsetdid)
114    }
115
116    // handle failure gracefully
117    if s == nil {
118        var buf bytes.Buffer
119        fmt.Fprintf(&buf`<span class="alert">could not generate a snippet for <span class="highlight">%s</span></span>`id.Name)
120        s = &Snippet{fset.Position(id.Pos()).Linebuf.String()}
121    }
122    return s
123}
124
MembersX
Presentation.newSnippet.fset
Presentation.genSnippet.id
Presentation.funcSnippet.dd
Presentation.NewSnippet.id
Presentation.newSnippet.p
Presentation.newSnippet
Presentation.funcSnippet.p
Presentation.NewSnippet.fset
Snippet
Snippet.Text
Presentation.genSnippet.d
Presentation.funcSnippet.id
Presentation.NewSnippet
Presentation.NewSnippet.s
Snippet.Line
Presentation.newSnippet.decl
findSpec
Presentation.genSnippet.fset
Presentation.genSnippet.dd
NewSnippet.decl
NewSnippet.id
Presentation.newSnippet.buf2
findSpec.list
Presentation.funcSnippet.fset
Presentation.funcSnippet.d
Presentation.NewSnippet.p
Presentation.newSnippet.id
findSpec.RangeStmt_968.BlockStmt.BlockStmt.RangeStmt_1113.n
NewSnippet.fset
Presentation.newSnippet.buf1
findSpec.RangeStmt_968.spec
Presentation.funcSnippet
NewSnippet
NewSnippet.p
findSpec.id
Presentation.genSnippet.p
Presentation.genSnippet
Presentation.genSnippet.s
Presentation.NewSnippet.decl
Presentation.NewSnippet.BlockStmt.buf
Members
X