1
0
mirror of https://github.com/golang/go synced 2024-11-18 23:34:45 -07:00
go/internal/lsp/cache/external.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

57 lines
1.3 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 (
"context"
"io/ioutil"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
)
// nativeFileSystem implements FileSystem reading from the normal os file system.
type nativeFileSystem struct{}
// nativeFileHandle implements FileHandle for nativeFileSystem
type nativeFileHandle struct {
fs *nativeFileSystem
identity source.FileIdentity
}
func (fs *nativeFileSystem) GetFile(uri span.URI) source.FileHandle {
return &nativeFileHandle{
fs: fs,
identity: source.FileIdentity{
URI: uri,
// TODO: decide what the version string is for a native file system
// could be the mtime?
Version: "",
},
}
}
func (h *nativeFileHandle) FileSystem() source.FileSystem {
return h.fs
}
func (h *nativeFileHandle) Identity() source.FileIdentity {
return h.identity
}
func (h *nativeFileHandle) Read(ctx context.Context) *source.FileContent {
r := &source.FileContent{}
filename, err := h.identity.URI.Filename()
if err != nil {
r.Error = err
return r
}
r.Data, r.Error = ioutil.ReadFile(filename)
if r.Error != nil {
r.Hash = hashContents(r.Data)
}
return r
}