mirror of
https://github.com/golang/go
synced 2024-11-19 00:34:40 -07:00
e88138204b
We panic if the uri was not a valid file uri instead They always are a valid file URI, and we would fail miserably to cope if they were not anyway, and there are lots of places where we need to be able to get the filename and don't want to cope with an error that cannot occur. If we ever have not file uri's, you will have to check if it is a file before calling .Filename, which seems reasonable anyway. Change-Id: Ifb26a165bd43c2d310378314550b5749b09e2ebd Reviewed-on: https://go-review.googlesource.com/c/tools/+/181017 Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
51 lines
1.2 KiB
Go
51 lines
1.2 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) ([]byte, string, error) {
|
|
data, err := ioutil.ReadFile(h.identity.URI.Filename())
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
return data, hashContents(data), nil
|
|
}
|