1
0
mirror of https://github.com/golang/go synced 2024-11-19 02:14:43 -07:00
go/internal/lsp/cache/cache.go
Ian Cottrell 2c3de6a5ae internal/lsp: change file system to allow lazy reads
We split aquiring a "handle" from reading a files contents so that we can do the
former eagerly and the latter lazily.
We also "version" the handles so that the same file at different versions is a
different handle.

Change-Id: I06cc346d4b4c77d784aa454702c54689f2f177e0
Reviewed-on: https://go-review.googlesource.com/c/tools/+/179917
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-06-06 01:09:34 +00:00

70 lines
1.6 KiB
Go

// Copyright 2019 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 cache
import (
"crypto/sha1"
"fmt"
"go/token"
"strconv"
"sync/atomic"
"golang.org/x/tools/internal/lsp/debug"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/lsp/xlog"
"golang.org/x/tools/internal/span"
)
func New() source.Cache {
index := atomic.AddInt64(&cacheIndex, 1)
c := &cache{
fs: &nativeFileSystem{},
id: strconv.FormatInt(index, 10),
fset: token.NewFileSet(),
}
debug.AddCache(debugCache{c})
return c
}
type cache struct {
fs source.FileSystem
id string
fset *token.FileSet
}
func (c *cache) GetFile(uri span.URI) source.FileHandle {
return c.fs.GetFile(uri)
}
func (c *cache) NewSession(log xlog.Logger) source.Session {
index := atomic.AddInt64(&sessionIndex, 1)
s := &session{
cache: c,
id: strconv.FormatInt(index, 10),
log: log,
overlays: make(map[span.URI]*overlay),
filesWatchMap: NewWatchMap(),
}
debug.AddSession(debugSession{s})
return s
}
func (c *cache) FileSet() *token.FileSet {
return c.fset
}
func hashContents(contents []byte) string {
// TODO: consider whether sha1 is the best choice here
// This hash is used for internal identity detection only
return fmt.Sprintf("%x", sha1.Sum(contents))
}
var cacheIndex, sessionIndex, viewIndex int64
type debugCache struct{ *cache }
func (c *cache) ID() string { return c.id }
func (c debugCache) FileSet() *token.FileSet { return c.fset }