2018-12-05 15:00:36 -07:00
|
|
|
// 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 (
|
2019-03-05 15:30:44 -07:00
|
|
|
"context"
|
2018-12-05 15:00:36 -07:00
|
|
|
"go/token"
|
2019-03-27 07:25:30 -06:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2019-06-03 23:04:18 -06:00
|
|
|
"sync"
|
2018-12-05 15:00:36 -07:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2018-12-05 15:00:36 -07:00
|
|
|
)
|
|
|
|
|
2019-05-03 22:04:18 -06:00
|
|
|
// viewFile extends source.File with helper methods for the view package.
|
|
|
|
type viewFile interface {
|
|
|
|
source.File
|
2019-05-23 13:03:11 -06:00
|
|
|
|
2019-05-03 22:04:18 -06:00
|
|
|
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
|
2019-03-27 07:25:30 -06:00
|
|
|
|
2019-06-13 13:55:53 -06:00
|
|
|
view *view
|
|
|
|
|
2019-06-03 23:04:18 -06:00
|
|
|
handleMu sync.Mutex
|
|
|
|
handle source.FileHandle
|
2019-06-13 13:55:53 -06:00
|
|
|
|
|
|
|
token *token.File
|
2019-05-03 22:04:18 -06:00
|
|
|
}
|
|
|
|
|
2019-03-27 07:25:30 -06:00
|
|
|
func basename(filename string) string {
|
|
|
|
return strings.ToLower(filepath.Base(filename))
|
|
|
|
}
|
|
|
|
|
2019-05-03 22:04:18 -06:00
|
|
|
func (f *fileBase) URI() span.URI {
|
2019-03-27 07:25:30 -06:00
|
|
|
return f.uris[0]
|
2019-02-19 19:11:15 -07:00
|
|
|
}
|
|
|
|
|
2019-05-03 22:04:18 -06:00
|
|
|
func (f *fileBase) filename() string {
|
|
|
|
return f.fname
|
|
|
|
}
|
|
|
|
|
2019-04-17 16:21:47 -06:00
|
|
|
// View returns the view associated with the file.
|
2019-05-03 22:04:18 -06:00
|
|
|
func (f *fileBase) View() source.View {
|
2019-04-17 16:21:47 -06:00
|
|
|
return f.view
|
|
|
|
}
|
|
|
|
|
2019-06-03 23:04:18 -06:00
|
|
|
// 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
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
|
|
|
|
2019-05-17 08:51:19 -06:00
|
|
|
func (f *fileBase) FileSet() *token.FileSet {
|
|
|
|
return f.view.Session().Cache().FileSet()
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|