mirror of
https://github.com/golang/go
synced 2024-11-18 23:34:45 -07:00
4d9ae51c24
On FileHandle Read now just returns the data hash and error This makes it more obvious that you should handle the error, rather than hiding it all in a struct. We also change the way we get and return content, the main source.File constructs now hold a FileHandle that then updates on invalidation Change-Id: I20be1b995355e948244342130eafec056df10081 Reviewed-on: https://go-review.googlesource.com/c/tools/+/180417 Reviewed-by: Rebecca Stambler <rstambler@golang.org>
68 lines
1.4 KiB
Go
68 lines
1.4 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"
|
|
"sync"
|
|
|
|
"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
|
|
handleMu sync.Mutex
|
|
handle source.FileHandle
|
|
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 a handle for the contents of the file.
|
|
func (f *fileBase) Handle(ctx context.Context) source.FileHandle {
|
|
f.handleMu.Lock()
|
|
defer f.handleMu.Unlock()
|
|
if f.handle == nil {
|
|
f.handle = f.view.Session().GetFile(f.URI())
|
|
}
|
|
return f.handle
|
|
}
|
|
|
|
func (f *fileBase) FileSet() *token.FileSet {
|
|
return f.view.Session().Cache().FileSet()
|
|
}
|