2013-07-18 03:52:45 +04: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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-02-10 07:29:54 +03:00
|
|
|
"sync"
|
2013-07-18 07:14:09 +04:00
|
|
|
"time"
|
2013-07-18 03:52:45 +04:00
|
|
|
|
2014-11-10 00:50:40 +03:00
|
|
|
"golang.org/x/tools/godoc/analysis"
|
|
|
|
"golang.org/x/tools/godoc/util"
|
|
|
|
"golang.org/x/tools/godoc/vfs"
|
2013-07-18 03:52:45 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
// A Corpus holds all the state related to serving and indexing a
|
|
|
|
// collection of Go code.
|
|
|
|
//
|
|
|
|
// Construct a new Corpus with NewCorpus, then modify options,
|
|
|
|
// then call its Init method.
|
|
|
|
type Corpus struct {
|
|
|
|
fs vfs.FileSystem
|
|
|
|
|
2013-07-19 04:27:53 +04:00
|
|
|
// Verbose logging.
|
|
|
|
Verbose bool
|
|
|
|
|
2013-07-18 03:52:45 +04:00
|
|
|
// 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
|
|
|
|
|
2013-11-05 18:35:58 +04:00
|
|
|
// 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.
|
2013-07-18 03:52:45 +04:00
|
|
|
IndexThrottle float64
|
|
|
|
|
2013-11-06 22:31:30 +04:00
|
|
|
// 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
|
|
|
|
|
2013-11-21 20:55:42 +04:00
|
|
|
// 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
|
|
|
|
|
2013-07-18 03:52:45 +04:00
|
|
|
// MaxResults optionally specifies the maximum results for indexing.
|
|
|
|
MaxResults int
|
|
|
|
|
2013-10-30 22:34:32 +04:00
|
|
|
// SummarizePackage optionally specifies a function to
|
|
|
|
// summarize a package. It exists as an optimization to
|
|
|
|
// avoid reading files to parse package comments.
|
|
|
|
//
|
|
|
|
// If SummarizePackage returns false for ok, the caller
|
|
|
|
// ignores all return values and parses the files in the package
|
|
|
|
// as if SummarizePackage were nil.
|
|
|
|
//
|
|
|
|
// If showList is false, the package is hidden from the
|
|
|
|
// package listing.
|
|
|
|
SummarizePackage func(pkg string) (summary string, showList, ok bool)
|
|
|
|
|
2013-11-05 18:35:58 +04:00
|
|
|
// 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",
|
2014-09-10 17:02:54 +04:00
|
|
|
// "/src/io", etc.
|
2013-11-05 18:35:58 +04:00
|
|
|
// If nil, all directories are indexed if indexing is enabled.
|
|
|
|
IndexDirectory func(dir string) bool
|
|
|
|
|
2013-07-18 03:52:45 +04:00
|
|
|
// 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.
|
|
|
|
refreshMetadataSignal chan bool
|
|
|
|
|
|
|
|
// file system information
|
|
|
|
fsTree util.RWValue // *Directory tree of packages, updated with each sync (but sync code is removed now)
|
|
|
|
fsModified util.RWValue // timestamp of last call to invalidateIndex
|
|
|
|
docMetadata util.RWValue // mapping from paths to *Metadata
|
|
|
|
|
2013-07-18 07:14:09 +04:00
|
|
|
// SearchIndex is the search index in use.
|
|
|
|
searchIndex util.RWValue
|
2014-03-15 02:58:22 +04:00
|
|
|
|
|
|
|
// Analysis is the result of type and pointer analysis.
|
|
|
|
Analysis analysis.Result
|
2018-02-10 07:29:54 +03:00
|
|
|
|
|
|
|
// flag to check whether a corpus is initialized or not
|
|
|
|
initMu sync.RWMutex
|
|
|
|
initDone bool
|
2017-12-23 04:30:39 +03:00
|
|
|
|
|
|
|
// pkgAPIInfo contains the information about which package API
|
|
|
|
// features were added in which version of Go.
|
|
|
|
pkgAPIInfo apiVersions
|
2013-07-18 03:52:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCorpus returns a new Corpus from a filesystem.
|
2013-11-21 20:55:42 +04:00
|
|
|
// The returned corpus has all indexing enabled and MaxResults set to 1000.
|
|
|
|
// Change or set any options on Corpus before calling the Corpus.Init method.
|
2013-07-18 03:52:45 +04:00
|
|
|
func NewCorpus(fs vfs.FileSystem) *Corpus {
|
|
|
|
c := &Corpus{
|
2019-02-17 05:45:48 +03:00
|
|
|
fs: fs,
|
2013-07-18 03:52:45 +04:00
|
|
|
refreshMetadataSignal: make(chan bool, 1),
|
|
|
|
|
2013-11-21 20:55:42 +04:00
|
|
|
MaxResults: 1000,
|
|
|
|
IndexEnabled: true,
|
|
|
|
IndexDocs: true,
|
|
|
|
IndexGoCode: true,
|
|
|
|
IndexFullText: true,
|
2013-07-18 03:52:45 +04:00
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2013-07-18 07:14:09 +04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-07-18 03:52:45 +04:00
|
|
|
// Init initializes Corpus, once options on Corpus are set.
|
|
|
|
// It must be called before any subsequent method calls.
|
|
|
|
func (c *Corpus) Init() error {
|
|
|
|
if err := c.initFSTree(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.updateMetadata()
|
|
|
|
go c.refreshMetadataLoop()
|
2018-02-10 07:29:54 +03:00
|
|
|
|
|
|
|
c.initMu.Lock()
|
|
|
|
c.initDone = true
|
|
|
|
c.initMu.Unlock()
|
2013-07-18 03:52:45 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Corpus) initFSTree() error {
|
2019-08-27 16:43:50 +03:00
|
|
|
dir := c.newDirectory("/", -1)
|
2013-07-18 03:52:45 +04:00
|
|
|
if dir == nil {
|
|
|
|
return errors.New("godoc: corpus fstree is nil")
|
|
|
|
}
|
|
|
|
c.fsTree.Set(dir)
|
|
|
|
c.invalidateIndex()
|
|
|
|
return nil
|
|
|
|
}
|