1
0
mirror of https://github.com/golang/go synced 2024-10-01 08:18:32 -06:00
go/internal/lsp/cache/file.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

90 lines
1.9 KiB
Go

// Copyright 2018 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 (
"context"
"go/token"
"path/filepath"
"strings"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
)
// viewFile extends source.File with helper methods for the view package.
type viewFile interface {
source.File
filename() string
addURI(uri span.URI) int
}
// fileBase holds the common functionality for all files.
// It is intended to be embedded in the file implementations
type fileBase struct {
uris []span.URI
fname string
view *view
fc *source.FileContent
token *token.File
}
func basename(filename string) string {
return strings.ToLower(filepath.Base(filename))
}
func (f *fileBase) URI() span.URI {
return f.uris[0]
}
func (f *fileBase) filename() string {
return f.fname
}
// View returns the view associated with the file.
func (f *fileBase) View() source.View {
return f.view
}
// Content returns the contents of the file, reading it from file system if needed.
func (f *fileBase) Content(ctx context.Context) *source.FileContent {
f.view.mu.Lock()
defer f.view.mu.Unlock()
f.read(ctx)
return f.fc
}
func (f *fileBase) FileSet() *token.FileSet {
return f.view.Session().Cache().FileSet()
}
// read is the internal part of GetContent. It assumes that the caller is
// holding the mutex of the file's view.
func (f *fileBase) read(ctx context.Context) {
if err := ctx.Err(); err != nil {
f.fc = &source.FileContent{Error: err}
return
}
if f.fc != nil {
if len(f.view.contentChanges) == 0 {
return
}
f.view.mcache.mu.Lock()
err := f.view.applyContentChanges(ctx)
f.view.mcache.mu.Unlock()
if err != nil {
f.fc = &source.FileContent{Error: err}
return
}
}
// We don't know the content yet, so read it.
f.fc = f.view.Session().GetFile(f.URI()).Read(ctx)
}