1
0
mirror of https://github.com/golang/go synced 2024-10-01 09:28:37 -06:00
go/internal/lsp/cache/external.go
Rebecca Stambler e33b02e766 internal/lsp: use versioned URIs in rename and code actions
This change adds support for returning versions along with file URIs, so
that the client can know when to apply changes. The version is not yet
propagated along to the internal/lsp/cache package, so this change will
have no effect (VS Code ignores a version of 0 and still applies the
changes).

A few minor changes made in the rename code (to remove the view
parameter). Some minor staticcheck fixes.

Updates golang/go#35243

Change-Id: Icc26bd9d9e5703c699f555424b94034c97b01d6f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/206882
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-11-13 05:52:40 +00:00

67 lines
1.7 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"
"os"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/lsp/telemetry"
"golang.org/x/tools/internal/span"
"golang.org/x/tools/internal/telemetry/trace"
)
// ioLimit limits the number of parallel file reads per process.
var ioLimit = make(chan struct{}, 128)
// 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, kind source.FileKind) source.FileHandle {
identifier := "DOES NOT EXIST"
if fi, err := os.Stat(uri.Filename()); err == nil {
identifier = fi.ModTime().String()
}
return &nativeFileHandle{
fs: fs,
identity: source.FileIdentity{
URI: uri,
Identifier: identifier,
Kind: kind,
},
}
}
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) {
ctx, done := trace.StartSpan(ctx, "cache.nativeFileHandle.Read", telemetry.File.Of(h.identity.URI.Filename()))
_ = ctx
defer done()
ioLimit <- struct{}{}
defer func() { <-ioLimit }()
// TODO: this should fail if the version is not the same as the handle
data, err := ioutil.ReadFile(h.identity.URI.Filename())
if err != nil {
return nil, "", err
}
return data, hashContents(data), nil
}