internal/godoc: delete indexing code
We no longer serve search from the golang.org site. Change-Id: I8b855cf664f175c0dc46a91e3017df1bebb28281 Reviewed-on: https://go-review.googlesource.com/c/website/+/293427 Trust: Russ Cox <rsc@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
This commit is contained in:
Родитель
d6a1a7b134
Коммит
6fbd1cb9fd
|
@ -12,9 +12,6 @@
|
|||
<link href="https://fonts.googleapis.com/css?family=Work+Sans:600|Roboto:400,700" rel="stylesheet">
|
||||
<link href="https://fonts.googleapis.com/css?family=Product+Sans&text=Supported%20by%20Google&display=swap" rel="stylesheet">
|
||||
<link type="text/css" rel="stylesheet" href="/lib/godoc/style.css">
|
||||
{{if .SearchBox}}
|
||||
<link rel="search" type="application/opensearchdescription+xml" title="godoc" href="/opensearch.xml" />
|
||||
{{end}}
|
||||
<script>window.initFuncs = [];</script>
|
||||
{{with .GoogleAnalytics}}
|
||||
<script>
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
|
||||
<ShortName>godoc</ShortName>
|
||||
<Description>The Go Programming Language</Description>
|
||||
<Tags>go golang</Tags>
|
||||
<Contact />
|
||||
<Url type="text/html" template="{{.BaseURL}}/search?q={searchTerms}" />
|
||||
<Image height="15" width="16" type="image/x-icon">/favicon.ico</Image>
|
||||
<OutputEncoding>UTF-8</OutputEncoding>
|
||||
<InputEncoding>UTF-8</InputEncoding>
|
||||
</OpenSearchDescription>
|
|
@ -1,66 +0,0 @@
|
|||
<!--
|
||||
Copyright 2009 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.
|
||||
-->
|
||||
|
||||
{{ $colCount := tocColCount .}}
|
||||
{{/* Generate the TOC */}}
|
||||
<nav class="search-nav" style="column-count: {{$colCount}}" role="navigation">
|
||||
{{range $key, $val := .Idents}}
|
||||
{{if $val}}
|
||||
<a href="#{{$key.Name}}">{{$key.Name}}</a>
|
||||
<br />
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
{{if not .Idents}}
|
||||
{{with .Pak}}
|
||||
<a href="#Packages">Package {{html $.Query}}</a>
|
||||
<br />
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
{{with .Hit}}
|
||||
{{with .Decls}}
|
||||
<a href="#Global">Package-level declarations</a>
|
||||
<br />
|
||||
{{range .}}
|
||||
{{$pkg_html := pkgLink .Pak.Path | html}}
|
||||
<a href="#Global_{{$pkg_html}}" class="indent">package {{html .Pak.Name}}</a>
|
||||
<br />
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{with .Others}}
|
||||
<a href="#Local">Local declarations and uses</a>
|
||||
<br />
|
||||
{{range .}}
|
||||
{{$pkg_html := pkgLink .Pak.Path | html}}
|
||||
<a href="#Local_{{$pkg_html}}" class="indent">package {{html .Pak.Name}}</a>
|
||||
<br />
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
{{with .Textual}}
|
||||
{{if $.Complete}}
|
||||
<a href="#Textual">{{html $.Found}} textual occurrences</a>
|
||||
{{else}}
|
||||
<a href="#Textual">More than {{html $.Found}} textual occurrences</a>
|
||||
{{end}}
|
||||
{{end}}
|
||||
</nav>
|
||||
|
||||
{{with .Alert}}
|
||||
<p>
|
||||
<span class="alert" style="font-size:120%">{{html .}}</span>
|
||||
</p>
|
||||
{{end}}
|
||||
{{with .Alt}}
|
||||
<p>
|
||||
<span class="alert" style="font-size:120%">Did you mean: </span>
|
||||
{{range .Alts}}
|
||||
<a href="search?q={{urlquery .}}" style="font-size:120%">{{html .}}</a>
|
||||
{{end}}
|
||||
</p>
|
||||
{{end}}
|
|
@ -1,64 +0,0 @@
|
|||
<!--
|
||||
Copyright 2009 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.
|
||||
-->
|
||||
{{$query_url := urlquery .Query}}
|
||||
{{if not .Idents}}
|
||||
{{with .Pak}}
|
||||
<h2 id="Packages">Package {{html $.Query}}</h2>
|
||||
<p>
|
||||
<table class="layout">
|
||||
{{range .}}
|
||||
{{$pkg_html := pkgLink .Pak.Path | html}}
|
||||
<tr><td><a href="/{{$pkg_html}}">{{$pkg_html}}</a></td></tr>
|
||||
{{end}}
|
||||
</table>
|
||||
</p>
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{with .Hit}}
|
||||
{{with .Decls}}
|
||||
<h2 id="Global">Package-level declarations</h2>
|
||||
{{range .}}
|
||||
{{$pkg_html := pkgLink .Pak.Path | html}}
|
||||
<h3 id="Global_{{$pkg_html}}">package <a href="/{{$pkg_html}}">{{html .Pak.Name}}</a></h3>
|
||||
{{range .Files}}
|
||||
{{$file := .File.Path}}
|
||||
{{range .Groups}}
|
||||
{{range .}}
|
||||
{{$line := infoLine .}}
|
||||
<a href="{{queryLink $file $query_url $line | html}}">{{$file}}:{{$line}}</a>
|
||||
{{infoSnippet_html .}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{with .Others}}
|
||||
<h2 id="Local">Local declarations and uses</h2>
|
||||
{{range .}}
|
||||
{{$pkg_html := pkgLink .Pak.Path | html}}
|
||||
<h3 id="Local_{{$pkg_html}}">package <a href="/{{$pkg_html}}">{{html .Pak.Name}}</a></h3>
|
||||
{{range .Files}}
|
||||
{{$file := .File.Path}}
|
||||
<a href="{{queryLink $file $query_url 0 | html}}">{{$file}}</a>
|
||||
<table class="layout">
|
||||
{{range .Groups}}
|
||||
<tr>
|
||||
<td width="25"></td>
|
||||
<th align="left" valign="top">{{index . 0 | infoKind_html}}</th>
|
||||
<td align="left" width="4"></td>
|
||||
<td>
|
||||
{{range .}}
|
||||
{{$line := infoLine .}}
|
||||
<a href="{{queryLink $file $query_url $line | html}}">{{$line}}</a>
|
||||
{{end}}
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</table>
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
|
@ -1,24 +0,0 @@
|
|||
<!--
|
||||
Copyright 2009 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.
|
||||
-->
|
||||
{{range $key, $val := .Idents}}
|
||||
{{if $val}}
|
||||
<h2 id="{{$key.Name}}">{{$key.Name}}</h2>
|
||||
{{range $val}}
|
||||
{{$pkg_html := pkgLink .Path | html}}
|
||||
{{if eq "Packages" $key.Name}}
|
||||
<a href="/{{$pkg_html}}">{{html .Path}}</a>
|
||||
{{else}}
|
||||
{{$doc_html := docLink .Path .Name| html}}
|
||||
<a href="/{{$pkg_html}}">{{html .Package}}</a>.<a href="{{$doc_html}}">{{.Name}}</a>
|
||||
{{end}}
|
||||
{{if .Doc}}
|
||||
<p>{{comment_html .Doc}}</p>
|
||||
{{else}}
|
||||
<p><em>No documentation available</em></p>
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
|
@ -1,42 +0,0 @@
|
|||
<!--
|
||||
Copyright 2009 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.
|
||||
-->
|
||||
{{$query_url := urlquery .Query}}
|
||||
{{with .Textual}}
|
||||
{{if $.Complete}}
|
||||
<h2 id="Textual">{{html $.Found}} textual occurrences</h2>
|
||||
{{else}}
|
||||
<h2 id="Textual">More than {{html $.Found}} textual occurrences</h2>
|
||||
<p>
|
||||
<span class="alert" style="font-size:120%">Not all files or lines containing "{{html $.Query}}" are shown.</span>
|
||||
</p>
|
||||
{{end}}
|
||||
<p>
|
||||
<table class="layout">
|
||||
{{range .}}
|
||||
{{$file := .Filename}}
|
||||
<tr>
|
||||
<td align="left" valign="top">
|
||||
<a href="{{queryLink $file $query_url 0}}">{{$file}}</a>:
|
||||
</td>
|
||||
<td align="left" width="4"></td>
|
||||
<th align="left" valign="top">{{len .Lines}}</th>
|
||||
<td align="left" width="4"></td>
|
||||
<td align="left">
|
||||
{{range .Lines}}
|
||||
<a href="{{queryLink $file $query_url .}}">{{html .}}</a>
|
||||
{{end}}
|
||||
{{if not $.Complete}}
|
||||
...
|
||||
{{end}}
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
{{if not $.Complete}}
|
||||
<tr><td align="left">...</td></tr>
|
||||
{{end}}
|
||||
</table>
|
||||
</p>
|
||||
{{end}}
|
|
@ -121,11 +121,6 @@ func readTemplates(p *godoc.Presentation) {
|
|||
p.GodocHTML = readTemplate("godoc.html")
|
||||
p.PackageHTML = readTemplate("package.html")
|
||||
p.PackageRootHTML = readTemplate("packageroot.html")
|
||||
p.SearchHTML = readTemplate("search.html")
|
||||
p.SearchDocHTML = readTemplate("searchdoc.html")
|
||||
p.SearchCodeHTML = readTemplate("searchcode.html")
|
||||
p.SearchTxtHTML = readTemplate("searchtxt.html")
|
||||
p.SearchDescXML = readTemplate("opensearch.xml")
|
||||
}
|
||||
|
||||
type fmtResponse struct {
|
||||
|
|
|
@ -93,7 +93,6 @@ func main() {
|
|||
|
||||
corpus := godoc.NewCorpus(fs)
|
||||
corpus.Verbose = *verbose
|
||||
corpus.IndexEnabled = false
|
||||
if err := corpus.Init(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -24,48 +24,6 @@ type Corpus struct {
|
|||
// Verbose logging.
|
||||
Verbose bool
|
||||
|
||||
// IndexEnabled controls whether indexing is enabled.
|
||||
IndexEnabled bool
|
||||
|
||||
// IndexFiles specifies a glob pattern specifying index files.
|
||||
// If not empty, the index is read from these files in sorted
|
||||
// order.
|
||||
IndexFiles string
|
||||
|
||||
// IndexThrottle specifies the indexing throttle value
|
||||
// between 0.0 and 1.0. At 0.0, the indexer always sleeps.
|
||||
// At 1.0, the indexer never sleeps. Because 0.0 is useless
|
||||
// and redundant with setting IndexEnabled to false, the
|
||||
// zero value for IndexThrottle means 0.9.
|
||||
IndexThrottle float64
|
||||
|
||||
// IndexInterval specifies the time to sleep between reindexing
|
||||
// all the sources.
|
||||
// If zero, a default is used. If negative, the index is only
|
||||
// built once.
|
||||
IndexInterval time.Duration
|
||||
|
||||
// IndexDocs enables indexing of Go documentation.
|
||||
// This will produce search results for exported types, functions,
|
||||
// methods, variables, and constants, and will link to the godoc
|
||||
// documentation for those identifiers.
|
||||
IndexDocs bool
|
||||
|
||||
// IndexGoCode enables indexing of Go source code.
|
||||
// This will produce search results for internal and external identifiers
|
||||
// and will link to both declarations and uses of those identifiers in
|
||||
// source code.
|
||||
IndexGoCode bool
|
||||
|
||||
// IndexFullText enables full-text indexing.
|
||||
// This will provide search results for any matching text in any file that
|
||||
// is indexed, including non-Go files (see whitelisted in index.go).
|
||||
// Regexp searching is supported via full-text indexing.
|
||||
IndexFullText bool
|
||||
|
||||
// MaxResults optionally specifies the maximum results for indexing.
|
||||
MaxResults int
|
||||
|
||||
// SummarizePackage optionally specifies a function to
|
||||
// summarize a package. It exists as an optimization to
|
||||
// avoid reading files to parse package comments.
|
||||
|
@ -78,13 +36,6 @@ type Corpus struct {
|
|||
// package listing.
|
||||
SummarizePackage func(pkg string) (summary string, showList, ok bool)
|
||||
|
||||
// IndexDirectory optionally specifies a function to determine
|
||||
// whether the provided directory should be indexed. The dir
|
||||
// will be of the form "/src/cmd/6a", "/doc/play",
|
||||
// "/src/io", etc.
|
||||
// If nil, all directories are indexed if indexing is enabled.
|
||||
IndexDirectory func(dir string) bool
|
||||
|
||||
// Send a value on this channel to trigger a metadata refresh.
|
||||
// It is buffered so that if a signal is not lost if sent
|
||||
// during a refresh.
|
||||
|
@ -95,9 +46,6 @@ type Corpus struct {
|
|||
fsModified util.RWValue // timestamp of last call to invalidateIndex
|
||||
docMetadata util.RWValue // mapping from paths to *Metadata
|
||||
|
||||
// SearchIndex is the search index in use.
|
||||
searchIndex util.RWValue
|
||||
|
||||
// flag to check whether a corpus is initialized or not
|
||||
initMu sync.RWMutex
|
||||
initDone bool
|
||||
|
@ -114,22 +62,10 @@ func NewCorpus(fs vfs.FileSystem) *Corpus {
|
|||
c := &Corpus{
|
||||
fs: fs,
|
||||
refreshMetadataSignal: make(chan bool, 1),
|
||||
|
||||
MaxResults: 1000,
|
||||
IndexEnabled: true,
|
||||
IndexDocs: true,
|
||||
IndexGoCode: true,
|
||||
IndexFullText: true,
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Corpus) CurrentIndex() (*Index, time.Time) {
|
||||
v, t := c.searchIndex.Get()
|
||||
idx, _ := v.(*Index)
|
||||
return idx, t
|
||||
}
|
||||
|
||||
func (c *Corpus) FSModifiedTime() time.Time {
|
||||
_, ts := c.fsModified.Get()
|
||||
return ts
|
||||
|
@ -156,6 +92,5 @@ func (c *Corpus) initFSTree() error {
|
|||
return errors.New("godoc: corpus fstree is nil")
|
||||
}
|
||||
c.fsTree.Set(dir)
|
||||
c.invalidateIndex()
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -69,11 +69,6 @@ func (p *Presentation) initFuncMap() {
|
|||
"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,
|
||||
|
@ -105,9 +100,6 @@ func (p *Presentation) initFuncMap() {
|
|||
|
||||
// check whether to display third party section or not
|
||||
"hasThirdParty": hasThirdParty,
|
||||
|
||||
// get the no. of columns to split the toc in search page
|
||||
"tocColCount": tocColCount,
|
||||
}
|
||||
if p.URLForSrc != nil {
|
||||
p.funcMap["srcLink"] = p.URLForSrc
|
||||
|
@ -142,48 +134,6 @@ func fileInfoTimeFunc(fi os.FileInfo) 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
|
||||
}
|
||||
|
||||
func (p *Presentation) infoLineFunc(info SpotInfo) int {
|
||||
line := info.Lori()
|
||||
if info.IsIndex() {
|
||||
index, _ := p.Corpus.searchIndex.Get()
|
||||
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
|
||||
}
|
||||
|
||||
func (p *Presentation) infoSnippet_htmlFunc(info SpotInfo) string {
|
||||
if info.IsIndex() {
|
||||
index, _ := p.Corpus.searchIndex.Get()
|
||||
// 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>`
|
||||
}
|
||||
|
||||
func (p *Presentation) nodeFunc(info *PageInfo, node interface{}) string {
|
||||
var buf bytes.Buffer
|
||||
p.writeNode(&buf, info, info.FSet, node)
|
||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -1,323 +0,0 @@
|
|||
// 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
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/website/internal/godoc/vfs/mapfs"
|
||||
)
|
||||
|
||||
func newCorpus(t *testing.T) *Corpus {
|
||||
c := NewCorpus(mapfs.New(map[string]string{
|
||||
"src/foo/foo.go": `// Package foo is an example.
|
||||
package foo
|
||||
|
||||
import "bar"
|
||||
|
||||
const Pi = 3.1415
|
||||
|
||||
var Foos []Foo
|
||||
|
||||
// Foo is stuff.
|
||||
type Foo struct{}
|
||||
|
||||
func New() *Foo {
|
||||
return new(Foo)
|
||||
}
|
||||
`,
|
||||
"src/bar/bar.go": `// Package bar is another example to test races.
|
||||
package bar
|
||||
`,
|
||||
"src/other/bar/bar.go": `// Package bar is another bar package.
|
||||
package bar
|
||||
func X() {}
|
||||
`,
|
||||
"src/skip/skip.go": `// Package skip should be skipped.
|
||||
package skip
|
||||
func Skip() {}
|
||||
`,
|
||||
"src/bar/readme.txt": `Whitelisted text file.
|
||||
`,
|
||||
"src/bar/baz.zzz": `Text file not whitelisted.
|
||||
`,
|
||||
}))
|
||||
c.IndexEnabled = true
|
||||
c.IndexDirectory = func(dir string) bool {
|
||||
return !strings.Contains(dir, "skip")
|
||||
}
|
||||
|
||||
if err := c.Init(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func TestIndex(t *testing.T) {
|
||||
for _, docs := range []bool{true, false} {
|
||||
for _, goCode := range []bool{true, false} {
|
||||
for _, fullText := range []bool{true, false} {
|
||||
c := newCorpus(t)
|
||||
c.IndexDocs = docs
|
||||
c.IndexGoCode = goCode
|
||||
c.IndexFullText = fullText
|
||||
c.UpdateIndex()
|
||||
ix, _ := c.CurrentIndex()
|
||||
if ix == nil {
|
||||
t.Fatal("no index")
|
||||
}
|
||||
t.Logf("docs, goCode, fullText = %v,%v,%v", docs, goCode, fullText)
|
||||
testIndex(t, c, ix)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexWriteRead(t *testing.T) {
|
||||
type key struct {
|
||||
docs, goCode, fullText bool
|
||||
}
|
||||
type val struct {
|
||||
buf *bytes.Buffer
|
||||
c *Corpus
|
||||
}
|
||||
m := map[key]val{}
|
||||
|
||||
for _, docs := range []bool{true, false} {
|
||||
for _, goCode := range []bool{true, false} {
|
||||
for _, fullText := range []bool{true, false} {
|
||||
k := key{docs, goCode, fullText}
|
||||
c := newCorpus(t)
|
||||
c.IndexDocs = docs
|
||||
c.IndexGoCode = goCode
|
||||
c.IndexFullText = fullText
|
||||
c.UpdateIndex()
|
||||
ix, _ := c.CurrentIndex()
|
||||
if ix == nil {
|
||||
t.Fatal("no index")
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
nw, err := ix.WriteTo(&buf)
|
||||
if err != nil {
|
||||
t.Fatalf("Index.WriteTo: %v", err)
|
||||
}
|
||||
m[k] = val{bytes.NewBuffer(buf.Bytes()), c}
|
||||
ix2 := new(Index)
|
||||
nr, err := ix2.ReadFrom(&buf)
|
||||
if err != nil {
|
||||
t.Fatalf("Index.ReadFrom: %v", err)
|
||||
}
|
||||
if nr != nw {
|
||||
t.Errorf("Wrote %d bytes to index but read %d", nw, nr)
|
||||
}
|
||||
testIndex(t, c, ix)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Test CompatibleWith
|
||||
for k1, v1 := range m {
|
||||
ix := new(Index)
|
||||
if _, err := ix.ReadFrom(v1.buf); err != nil {
|
||||
t.Fatalf("Index.ReadFrom: %v", err)
|
||||
}
|
||||
for k2, v2 := range m {
|
||||
if got, want := ix.CompatibleWith(v2.c), k1 == k2; got != want {
|
||||
t.Errorf("CompatibleWith = %v; want %v for %v, %v", got, want, k1, k2)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testIndex(t *testing.T, c *Corpus, ix *Index) {
|
||||
if _, ok := ix.words["Skip"]; ok {
|
||||
t.Errorf("the word Skip was found; expected it to be skipped")
|
||||
}
|
||||
checkStats(t, c, ix)
|
||||
checkImportCount(t, c, ix)
|
||||
checkPackagePath(t, c, ix)
|
||||
checkExports(t, c, ix)
|
||||
checkIdents(t, c, ix)
|
||||
}
|
||||
|
||||
// checkStats checks the Index's statistics.
|
||||
// Some statistics are only set when we're indexing Go code.
|
||||
func checkStats(t *testing.T, c *Corpus, ix *Index) {
|
||||
want := Statistics{}
|
||||
if c.IndexFullText {
|
||||
want.Bytes = 314
|
||||
want.Files = 4
|
||||
want.Lines = 21
|
||||
} else if c.IndexDocs || c.IndexGoCode {
|
||||
want.Bytes = 291
|
||||
want.Files = 3
|
||||
want.Lines = 20
|
||||
}
|
||||
if c.IndexGoCode {
|
||||
want.Words = 8
|
||||
want.Spots = 12
|
||||
}
|
||||
if got := ix.Stats(); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("Stats = %#v; want %#v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// checkImportCount checks the Index's import count map.
|
||||
// It is only set when we're indexing Go code.
|
||||
func checkImportCount(t *testing.T, c *Corpus, ix *Index) {
|
||||
want := map[string]int{}
|
||||
if c.IndexGoCode {
|
||||
want = map[string]int{
|
||||
"bar": 1,
|
||||
}
|
||||
}
|
||||
if got := ix.ImportCount(); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("ImportCount = %v; want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// checkPackagePath checks the Index's package path map.
|
||||
// It is set if at least one of the indexing options is enabled.
|
||||
func checkPackagePath(t *testing.T, c *Corpus, ix *Index) {
|
||||
want := map[string]map[string]bool{}
|
||||
if c.IndexDocs || c.IndexGoCode || c.IndexFullText {
|
||||
want = map[string]map[string]bool{
|
||||
"foo": {
|
||||
"foo": true,
|
||||
},
|
||||
"bar": {
|
||||
"bar": true,
|
||||
"other/bar": true,
|
||||
},
|
||||
}
|
||||
}
|
||||
if got := ix.PackagePath(); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("PackagePath = %v; want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// checkExports checks the Index's exports map.
|
||||
// It is only set when we're indexing Go code.
|
||||
func checkExports(t *testing.T, c *Corpus, ix *Index) {
|
||||
want := map[string]map[string]SpotKind{}
|
||||
if c.IndexGoCode {
|
||||
want = map[string]map[string]SpotKind{
|
||||
"foo": {
|
||||
"Pi": ConstDecl,
|
||||
"Foos": VarDecl,
|
||||
"Foo": TypeDecl,
|
||||
"New": FuncDecl,
|
||||
},
|
||||
"other/bar": {
|
||||
"X": FuncDecl,
|
||||
},
|
||||
}
|
||||
}
|
||||
if got := ix.Exports(); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("Exports = %v; want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// checkIdents checks the Index's indents map.
|
||||
// It is only set when we're indexing documentation.
|
||||
func checkIdents(t *testing.T, c *Corpus, ix *Index) {
|
||||
want := map[SpotKind]map[string][]Ident{}
|
||||
if c.IndexDocs {
|
||||
want = map[SpotKind]map[string][]Ident{
|
||||
PackageClause: {
|
||||
"bar": {
|
||||
{"bar", "bar", "bar", "Package bar is another example to test races."},
|
||||
{"other/bar", "bar", "bar", "Package bar is another bar package."},
|
||||
},
|
||||
"foo": {{"foo", "foo", "foo", "Package foo is an example."}},
|
||||
"other": {{"other/bar", "bar", "bar", "Package bar is another bar package."}},
|
||||
},
|
||||
ConstDecl: {
|
||||
"Pi": {{"foo", "foo", "Pi", ""}},
|
||||
},
|
||||
VarDecl: {
|
||||
"Foos": {{"foo", "foo", "Foos", ""}},
|
||||
},
|
||||
TypeDecl: {
|
||||
"Foo": {{"foo", "foo", "Foo", "Foo is stuff."}},
|
||||
},
|
||||
FuncDecl: {
|
||||
"New": {{"foo", "foo", "New", ""}},
|
||||
"X": {{"other/bar", "bar", "X", ""}},
|
||||
},
|
||||
}
|
||||
}
|
||||
if got := ix.Idents(); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("Idents = %v; want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIdentResultSort(t *testing.T) {
|
||||
ic := map[string]int{
|
||||
"/a/b/pkg1": 10,
|
||||
"/a/b/pkg2": 2,
|
||||
"/b/d/pkg3": 20,
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
ir []Ident
|
||||
exp []Ident
|
||||
}{
|
||||
{
|
||||
ir: []Ident{
|
||||
{"/a/b/pkg2", "pkg2", "MyFunc2", ""},
|
||||
{"/b/d/pkg3", "pkg3", "MyFunc3", ""},
|
||||
{"/a/b/pkg1", "pkg1", "MyFunc1", ""},
|
||||
},
|
||||
exp: []Ident{
|
||||
{"/b/d/pkg3", "pkg3", "MyFunc3", ""},
|
||||
{"/a/b/pkg1", "pkg1", "MyFunc1", ""},
|
||||
{"/a/b/pkg2", "pkg2", "MyFunc2", ""},
|
||||
},
|
||||
},
|
||||
{
|
||||
ir: []Ident{
|
||||
{"/a/a/pkg1", "pkg1", "MyFunc1", ""},
|
||||
{"/a/b/pkg1", "pkg1", "MyFunc1", ""},
|
||||
},
|
||||
exp: []Ident{
|
||||
{"/a/b/pkg1", "pkg1", "MyFunc1", ""},
|
||||
{"/a/a/pkg1", "pkg1", "MyFunc1", ""},
|
||||
},
|
||||
},
|
||||
} {
|
||||
if sort.Sort(byImportCount{tc.ir, ic}); !reflect.DeepEqual(tc.ir, tc.exp) {
|
||||
t.Errorf("got: %v, want %v", tc.ir, tc.exp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIdentFilter(t *testing.T) {
|
||||
ic := map[string]int{}
|
||||
for _, tc := range []struct {
|
||||
ir []Ident
|
||||
pak string
|
||||
exp []Ident
|
||||
}{
|
||||
{
|
||||
ir: []Ident{
|
||||
{"/a/b/pkg2", "pkg2", "MyFunc2", ""},
|
||||
{"/b/d/pkg3", "pkg3", "MyFunc3", ""},
|
||||
{"/a/b/pkg1", "pkg1", "MyFunc1", ""},
|
||||
},
|
||||
pak: "pkg2",
|
||||
exp: []Ident{
|
||||
{"/a/b/pkg2", "pkg2", "MyFunc2", ""},
|
||||
},
|
||||
},
|
||||
} {
|
||||
res := byImportCount{tc.ir, ic}.filter(tc.pak)
|
||||
if !reflect.DeepEqual(res, tc.exp) {
|
||||
t.Errorf("got: %v, want %v", res, tc.exp)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -25,7 +25,6 @@ type Page struct {
|
|||
GoogleCN bool // page is being served from golang.google.cn
|
||||
|
||||
// filled in by ServePage
|
||||
SearchBox bool
|
||||
Playground bool
|
||||
Version string
|
||||
GoogleAnalytics string
|
||||
|
@ -35,7 +34,6 @@ func (p *Presentation) ServePage(w http.ResponseWriter, page Page) {
|
|||
if page.Tabtitle == "" {
|
||||
page.Tabtitle = page.Title
|
||||
}
|
||||
page.SearchBox = p.Corpus.IndexEnabled
|
||||
page.Playground = p.ShowPlayground
|
||||
page.Version = runtime.Version()
|
||||
page.GoogleAnalytics = p.GoogleAnalytics
|
||||
|
|
|
@ -13,9 +13,6 @@ import (
|
|||
"golang.org/x/website/internal/godoc/vfs/httpfs"
|
||||
)
|
||||
|
||||
// SearchResultFunc functions return an HTML body for displaying search results.
|
||||
type SearchResultFunc func(p *Presentation, result SearchResult) []byte
|
||||
|
||||
// Presentation generates output from a corpus.
|
||||
type Presentation struct {
|
||||
Corpus *Corpus
|
||||
|
@ -30,12 +27,7 @@ type Presentation struct {
|
|||
ExampleHTML,
|
||||
GodocHTML,
|
||||
PackageHTML,
|
||||
PackageRootHTML,
|
||||
SearchHTML,
|
||||
SearchDocHTML,
|
||||
SearchCodeHTML,
|
||||
SearchTxtHTML,
|
||||
SearchDescXML *template.Template // If not nil, register a /opensearch.xml handler with this template.
|
||||
PackageRootHTML *template.Template
|
||||
|
||||
// TabWidth optionally specifies the tab width.
|
||||
TabWidth int
|
||||
|
@ -75,10 +67,6 @@ type Presentation struct {
|
|||
// the query string highlighted.
|
||||
URLForSrcQuery func(src, query string, line int) string
|
||||
|
||||
// SearchResults optionally specifies a list of functions returning an HTML
|
||||
// body for displaying search results.
|
||||
SearchResults []SearchResultFunc
|
||||
|
||||
// GoogleAnalytics optionally adds Google Analytics via the provided
|
||||
// tracking ID to each page.
|
||||
GoogleAnalytics string
|
||||
|
@ -89,8 +77,6 @@ type Presentation struct {
|
|||
}
|
||||
|
||||
// NewPresentation returns a new Presentation from a corpus.
|
||||
// It sets SearchResults to:
|
||||
// [SearchResultDoc SearchResultCode SearchResultTxt].
|
||||
func NewPresentation(c *Corpus) *Presentation {
|
||||
if c == nil {
|
||||
panic("nil Corpus")
|
||||
|
@ -102,11 +88,6 @@ func NewPresentation(c *Corpus) *Presentation {
|
|||
|
||||
TabWidth: 4,
|
||||
DeclLinks: true,
|
||||
SearchResults: []SearchResultFunc{
|
||||
(*Presentation).SearchResultDoc,
|
||||
(*Presentation).SearchResultCode,
|
||||
(*Presentation).SearchResultTxt,
|
||||
},
|
||||
}
|
||||
p.cmdHandler = handlerServer{
|
||||
p: p,
|
||||
|
@ -125,10 +106,6 @@ func NewPresentation(c *Corpus) *Presentation {
|
|||
p.cmdHandler.registerWithMux(p.mux)
|
||||
p.pkgHandler.registerWithMux(p.mux)
|
||||
p.mux.HandleFunc("/", p.ServeFile)
|
||||
p.mux.HandleFunc("/search", p.HandleSearch)
|
||||
if p.SearchDescXML != nil {
|
||||
p.mux.HandleFunc("/opensearch.xml", p.serveSearchDesc)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
|
|
|
@ -1,186 +0,0 @@
|
|||
// Copyright 2009 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
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type SearchResult struct {
|
||||
Query string
|
||||
Alert string // error or warning message
|
||||
|
||||
// identifier matches
|
||||
Pak HitList // packages matching Query
|
||||
Hit *LookupResult // identifier matches of Query
|
||||
Alt *AltWords // alternative identifiers to look for
|
||||
|
||||
// textual matches
|
||||
Found int // number of textual occurrences found
|
||||
Textual []FileLines // textual matches of Query
|
||||
Complete bool // true if all textual occurrences of Query are reported
|
||||
Idents map[SpotKind][]Ident
|
||||
}
|
||||
|
||||
func (c *Corpus) Lookup(query string) SearchResult {
|
||||
result := &SearchResult{Query: query}
|
||||
|
||||
index, timestamp := c.CurrentIndex()
|
||||
if index != nil {
|
||||
// identifier search
|
||||
if r, err := index.Lookup(query); err == nil {
|
||||
result = r
|
||||
} else if err != nil && !c.IndexFullText {
|
||||
// ignore the error if full text search is enabled
|
||||
// since the query may be a valid regular expression
|
||||
result.Alert = "Error in query string: " + err.Error()
|
||||
return *result
|
||||
}
|
||||
|
||||
// full text search
|
||||
if c.IndexFullText && query != "" {
|
||||
rx, err := regexp.Compile(query)
|
||||
if err != nil {
|
||||
result.Alert = "Error in query regular expression: " + err.Error()
|
||||
return *result
|
||||
}
|
||||
// If we get maxResults+1 results we know that there are more than
|
||||
// maxResults results and thus the result may be incomplete (to be
|
||||
// precise, we should remove one result from the result set, but
|
||||
// nobody is going to count the results on the result page).
|
||||
result.Found, result.Textual = index.LookupRegexp(rx, c.MaxResults+1)
|
||||
result.Complete = result.Found <= c.MaxResults
|
||||
if !result.Complete {
|
||||
result.Found-- // since we looked for maxResults+1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// is the result accurate?
|
||||
if c.IndexEnabled {
|
||||
if ts := c.FSModifiedTime(); timestamp.Before(ts) {
|
||||
// The index is older than the latest file system change under godoc's observation.
|
||||
result.Alert = "Indexing in progress: result may be inaccurate"
|
||||
}
|
||||
} else {
|
||||
result.Alert = "Search index disabled: no results available"
|
||||
}
|
||||
|
||||
return *result
|
||||
}
|
||||
|
||||
// SearchResultDoc optionally specifies a function returning an HTML body
|
||||
// displaying search results matching godoc documentation.
|
||||
func (p *Presentation) SearchResultDoc(result SearchResult) []byte {
|
||||
return applyTemplate(p.SearchDocHTML, "searchDocHTML", result)
|
||||
}
|
||||
|
||||
// SearchResultCode optionally specifies a function returning an HTML body
|
||||
// displaying search results matching source code.
|
||||
func (p *Presentation) SearchResultCode(result SearchResult) []byte {
|
||||
return applyTemplate(p.SearchCodeHTML, "searchCodeHTML", result)
|
||||
}
|
||||
|
||||
// SearchResultTxt optionally specifies a function returning an HTML body
|
||||
// displaying search results of textual matches.
|
||||
func (p *Presentation) SearchResultTxt(result SearchResult) []byte {
|
||||
return applyTemplate(p.SearchTxtHTML, "searchTxtHTML", result)
|
||||
}
|
||||
|
||||
// HandleSearch obtains results for the requested search and returns a page
|
||||
// to display them.
|
||||
func (p *Presentation) HandleSearch(w http.ResponseWriter, r *http.Request) {
|
||||
query := strings.TrimSpace(r.FormValue("q"))
|
||||
result := p.Corpus.Lookup(query)
|
||||
|
||||
var contents bytes.Buffer
|
||||
for _, f := range p.SearchResults {
|
||||
contents.Write(f(p, result))
|
||||
}
|
||||
|
||||
var title string
|
||||
if haveResults := contents.Len() > 0; haveResults {
|
||||
title = fmt.Sprintf(`Results for query: %v`, query)
|
||||
if !p.Corpus.IndexEnabled {
|
||||
result.Alert = ""
|
||||
}
|
||||
} else {
|
||||
title = fmt.Sprintf(`No results found for query %q`, query)
|
||||
}
|
||||
|
||||
body := bytes.NewBuffer(applyTemplate(p.SearchHTML, "searchHTML", result))
|
||||
body.Write(contents.Bytes())
|
||||
|
||||
p.ServePage(w, Page{
|
||||
Title: title,
|
||||
Tabtitle: query,
|
||||
Query: query,
|
||||
Body: body.Bytes(),
|
||||
GoogleCN: googleCN(r),
|
||||
})
|
||||
}
|
||||
|
||||
func (p *Presentation) serveSearchDesc(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/opensearchdescription+xml")
|
||||
data := map[string]interface{}{
|
||||
"BaseURL": fmt.Sprintf("http://%s", r.Host),
|
||||
}
|
||||
applyTemplateToResponseWriter(w, p.SearchDescXML, &data)
|
||||
}
|
||||
|
||||
// tocColCount returns the no. of columns
|
||||
// to split the toc table to.
|
||||
func tocColCount(result SearchResult) int {
|
||||
tocLen := tocLen(result)
|
||||
colCount := 0
|
||||
// Simple heuristic based on visual aesthetic in manual testing.
|
||||
switch {
|
||||
case tocLen <= 10:
|
||||
colCount = 1
|
||||
case tocLen <= 20:
|
||||
colCount = 2
|
||||
case tocLen <= 80:
|
||||
colCount = 3
|
||||
default:
|
||||
colCount = 4
|
||||
}
|
||||
return colCount
|
||||
}
|
||||
|
||||
// tocLen calculates the no. of items in the toc table
|
||||
// by going through various fields in the SearchResult
|
||||
// that is rendered in the UI.
|
||||
func tocLen(result SearchResult) int {
|
||||
tocLen := 0
|
||||
for _, val := range result.Idents {
|
||||
if len(val) != 0 {
|
||||
tocLen++
|
||||
}
|
||||
}
|
||||
// If no identifiers, then just one item for the header text "Package <result.Query>".
|
||||
// See searchcode.html for further details.
|
||||
if len(result.Idents) == 0 {
|
||||
tocLen++
|
||||
}
|
||||
if result.Hit != nil {
|
||||
if len(result.Hit.Decls) > 0 {
|
||||
tocLen += len(result.Hit.Decls)
|
||||
// We need one extra item for the header text "Package-level declarations".
|
||||
tocLen++
|
||||
}
|
||||
if len(result.Hit.Others) > 0 {
|
||||
tocLen += len(result.Hit.Others)
|
||||
// We need one extra item for the header text "Local declarations and uses".
|
||||
tocLen++
|
||||
}
|
||||
}
|
||||
// For "textual occurrences".
|
||||
tocLen++
|
||||
return tocLen
|
||||
}
|
|
@ -1,123 +0,0 @@
|
|||
// Copyright 2009 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.
|
||||
|
||||
// This file contains the infrastructure to create a code
|
||||
// snippet for search results.
|
||||
//
|
||||
// Note: At the moment, this only creates HTML snippets.
|
||||
|
||||
package godoc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/token"
|
||||
)
|
||||
|
||||
type Snippet struct {
|
||||
Line int
|
||||
Text string // HTML-escaped
|
||||
}
|
||||
|
||||
func (p *Presentation) newSnippet(fset *token.FileSet, decl ast.Decl, id *ast.Ident) *Snippet {
|
||||
// TODO instead of pretty-printing the node, should use the original source instead
|
||||
var buf1 bytes.Buffer
|
||||
p.writeNode(&buf1, nil, fset, decl)
|
||||
// wrap text with <pre> tag
|
||||
var buf2 bytes.Buffer
|
||||
buf2.WriteString("<pre>")
|
||||
FormatText(&buf2, buf1.Bytes(), -1, true, id.Name, nil)
|
||||
buf2.WriteString("</pre>")
|
||||
return &Snippet{fset.Position(id.Pos()).Line, buf2.String()}
|
||||
}
|
||||
|
||||
func findSpec(list []ast.Spec, id *ast.Ident) ast.Spec {
|
||||
for _, spec := range list {
|
||||
switch s := spec.(type) {
|
||||
case *ast.ImportSpec:
|
||||
if s.Name == id {
|
||||
return s
|
||||
}
|
||||
case *ast.ValueSpec:
|
||||
for _, n := range s.Names {
|
||||
if n == id {
|
||||
return s
|
||||
}
|
||||
}
|
||||
case *ast.TypeSpec:
|
||||
if s.Name == id {
|
||||
return s
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Presentation) genSnippet(fset *token.FileSet, d *ast.GenDecl, id *ast.Ident) *Snippet {
|
||||
s := findSpec(d.Specs, id)
|
||||
if s == nil {
|
||||
return nil // declaration doesn't contain id - exit gracefully
|
||||
}
|
||||
|
||||
// only use the spec containing the id for the snippet
|
||||
dd := &ast.GenDecl{
|
||||
Doc: d.Doc,
|
||||
TokPos: d.Pos(),
|
||||
Tok: d.Tok,
|
||||
Lparen: d.Lparen,
|
||||
Specs: []ast.Spec{s},
|
||||
Rparen: d.Rparen,
|
||||
}
|
||||
|
||||
return p.newSnippet(fset, dd, id)
|
||||
}
|
||||
|
||||
func (p *Presentation) funcSnippet(fset *token.FileSet, d *ast.FuncDecl, id *ast.Ident) *Snippet {
|
||||
if d.Name != id {
|
||||
return nil // declaration doesn't contain id - exit gracefully
|
||||
}
|
||||
|
||||
// only use the function signature for the snippet
|
||||
dd := &ast.FuncDecl{
|
||||
Doc: d.Doc,
|
||||
Recv: d.Recv,
|
||||
Name: d.Name,
|
||||
Type: d.Type,
|
||||
}
|
||||
|
||||
return p.newSnippet(fset, dd, id)
|
||||
}
|
||||
|
||||
// NewSnippet creates a text snippet from a declaration decl containing an
|
||||
// identifier id. Parts of the declaration not containing the identifier
|
||||
// may be removed for a more compact snippet.
|
||||
func NewSnippet(fset *token.FileSet, decl ast.Decl, id *ast.Ident) *Snippet {
|
||||
// TODO(bradfitz, adg): remove this function. But it's used by indexer, which
|
||||
// doesn't have a *Presentation, and NewSnippet needs a TabWidth.
|
||||
var p Presentation
|
||||
p.TabWidth = 4
|
||||
return p.NewSnippet(fset, decl, id)
|
||||
}
|
||||
|
||||
// NewSnippet creates a text snippet from a declaration decl containing an
|
||||
// identifier id. Parts of the declaration not containing the identifier
|
||||
// may be removed for a more compact snippet.
|
||||
func (p *Presentation) NewSnippet(fset *token.FileSet, decl ast.Decl, id *ast.Ident) *Snippet {
|
||||
var s *Snippet
|
||||
switch d := decl.(type) {
|
||||
case *ast.GenDecl:
|
||||
s = p.genSnippet(fset, d, id)
|
||||
case *ast.FuncDecl:
|
||||
s = p.funcSnippet(fset, d, id)
|
||||
}
|
||||
|
||||
// handle failure gracefully
|
||||
if s == nil {
|
||||
var buf bytes.Buffer
|
||||
fmt.Fprintf(&buf, `<span class="alert">could not generate a snippet for <span class="highlight">%s</span></span>`, id.Name)
|
||||
s = &Snippet{fset.Position(id.Pos()).Line, buf.String()}
|
||||
}
|
||||
return s
|
||||
}
|
|
@ -1,83 +0,0 @@
|
|||
// 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
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// SpotInfo
|
||||
|
||||
// A SpotInfo value describes a particular identifier spot in a given file;
|
||||
// It encodes three values: the SpotKind (declaration or use), a line or
|
||||
// snippet index "lori", and whether it's a line or index.
|
||||
//
|
||||
// The following encoding is used:
|
||||
//
|
||||
// bits 32 4 1 0
|
||||
// value [lori|kind|isIndex]
|
||||
//
|
||||
type SpotInfo uint32
|
||||
|
||||
// SpotKind describes whether an identifier is declared (and what kind of
|
||||
// declaration) or used.
|
||||
type SpotKind uint32
|
||||
|
||||
const (
|
||||
PackageClause SpotKind = iota
|
||||
ImportDecl
|
||||
ConstDecl
|
||||
TypeDecl
|
||||
VarDecl
|
||||
FuncDecl
|
||||
MethodDecl
|
||||
Use
|
||||
nKinds
|
||||
)
|
||||
|
||||
var (
|
||||
// These must match the SpotKind values above.
|
||||
name = []string{
|
||||
"Packages",
|
||||
"Imports",
|
||||
"Constants",
|
||||
"Types",
|
||||
"Variables",
|
||||
"Functions",
|
||||
"Methods",
|
||||
"Uses",
|
||||
"Unknown",
|
||||
}
|
||||
)
|
||||
|
||||
func (x SpotKind) Name() string { return name[x] }
|
||||
|
||||
func init() {
|
||||
// sanity check: if nKinds is too large, the SpotInfo
|
||||
// accessor functions may need to be updated
|
||||
if nKinds > 8 {
|
||||
panic("internal error: nKinds > 8")
|
||||
}
|
||||
}
|
||||
|
||||
// makeSpotInfo makes a SpotInfo.
|
||||
func makeSpotInfo(kind SpotKind, lori int, isIndex bool) SpotInfo {
|
||||
// encode lori: bits [4..32)
|
||||
x := SpotInfo(lori) << 4
|
||||
if int(x>>4) != lori {
|
||||
// lori value doesn't fit - since snippet indices are
|
||||
// most certainly always smaller then 1<<28, this can
|
||||
// only happen for line numbers; give it no line number (= 0)
|
||||
x = 0
|
||||
}
|
||||
// encode kind: bits [1..4)
|
||||
x |= SpotInfo(kind) << 1
|
||||
// encode isIndex: bit 0
|
||||
if isIndex {
|
||||
x |= 1
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
func (x SpotInfo) Kind() SpotKind { return SpotKind(x >> 1 & 7) }
|
||||
func (x SpotInfo) Lori() int { return int(x >> 4) }
|
||||
func (x SpotInfo) IsIndex() bool { return x&1 != 0 }
|
|
@ -1,88 +0,0 @@
|
|||
// Copyright 2011 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 util
|
||||
|
||||
import "time"
|
||||
|
||||
// A Throttle permits throttling of a goroutine by
|
||||
// calling the Throttle method repeatedly.
|
||||
//
|
||||
type Throttle struct {
|
||||
f float64 // f = (1-r)/r for 0 < r < 1
|
||||
dt time.Duration // minimum run time slice; >= 0
|
||||
tr time.Duration // accumulated time running
|
||||
ts time.Duration // accumulated time stopped
|
||||
tt time.Time // earliest throttle time (= time Throttle returned + tm)
|
||||
}
|
||||
|
||||
// NewThrottle creates a new Throttle with a throttle value r and
|
||||
// a minimum allocated run time slice of dt:
|
||||
//
|
||||
// r == 0: "empty" throttle; the goroutine is always sleeping
|
||||
// r == 1: full throttle; the goroutine is never sleeping
|
||||
//
|
||||
// A value of r == 0.6 throttles a goroutine such that it runs
|
||||
// approx. 60% of the time, and sleeps approx. 40% of the time.
|
||||
// Values of r < 0 or r > 1 are clamped down to values between 0 and 1.
|
||||
// Values of dt < 0 are set to 0.
|
||||
//
|
||||
func NewThrottle(r float64, dt time.Duration) *Throttle {
|
||||
var f float64
|
||||
switch {
|
||||
case r <= 0:
|
||||
f = -1 // indicates always sleep
|
||||
case r >= 1:
|
||||
f = 0 // assume r == 1 (never sleep)
|
||||
default:
|
||||
// 0 < r < 1
|
||||
f = (1 - r) / r
|
||||
}
|
||||
if dt < 0 {
|
||||
dt = 0
|
||||
}
|
||||
return &Throttle{f: f, dt: dt, tt: time.Now().Add(dt)}
|
||||
}
|
||||
|
||||
// Throttle calls time.Sleep such that over time the ratio tr/ts between
|
||||
// accumulated run (tr) and sleep times (ts) approximates the value 1/(1-r)
|
||||
// where r is the throttle value. Throttle returns immediately (w/o sleeping)
|
||||
// if less than tm ns have passed since the last call to Throttle.
|
||||
//
|
||||
func (p *Throttle) Throttle() {
|
||||
if p.f < 0 {
|
||||
select {} // always sleep
|
||||
}
|
||||
|
||||
t0 := time.Now()
|
||||
if t0.Before(p.tt) {
|
||||
return // keep running (minimum time slice not exhausted yet)
|
||||
}
|
||||
|
||||
// accumulate running time
|
||||
p.tr += t0.Sub(p.tt) + p.dt
|
||||
|
||||
// compute sleep time
|
||||
// Over time we want:
|
||||
//
|
||||
// tr/ts = r/(1-r)
|
||||
//
|
||||
// Thus:
|
||||
//
|
||||
// ts = tr*f with f = (1-r)/r
|
||||
//
|
||||
// After some incremental run time δr added to the total run time
|
||||
// tr, the incremental sleep-time δs to get to the same ratio again
|
||||
// after waking up from time.Sleep is:
|
||||
if δs := time.Duration(float64(p.tr)*p.f) - p.ts; δs > 0 {
|
||||
time.Sleep(δs)
|
||||
}
|
||||
|
||||
// accumulate (actual) sleep time
|
||||
t1 := time.Now()
|
||||
p.ts += t1.Sub(t0)
|
||||
|
||||
// set earliest next throttle time
|
||||
p.tt = t1.Add(p.dt)
|
||||
}
|
Загрузка…
Ссылка в новой задаче