| 1 | // Copyright 2023 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // The file package defines types used for working with LSP files. |
| 6 | package file |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | "fmt" |
| 11 | "time" |
| 12 | |
| 13 | "golang.org/x/tools/gopls/internal/protocol" |
| 14 | ) |
| 15 | |
| 16 | // An Identity identifies the name and contents of a file. |
| 17 | // |
| 18 | // TODO(rfindley): Identity may not carry its weight. Consider instead just |
| 19 | // exposing Handle.Hash, and using an ad-hoc key type where necessary. |
| 20 | // Or perhaps if mod/work parsing is moved outside of the memoize cache, |
| 21 | // a notion of Identity simply isn't needed. |
| 22 | type Identity struct { |
| 23 | URI protocol.DocumentURI |
| 24 | Hash Hash // digest of file contents |
| 25 | } |
| 26 | |
| 27 | func (id Identity) String() string { |
| 28 | return fmt.Sprintf("%s%s", id.URI, id.Hash) |
| 29 | } |
| 30 | |
| 31 | // A FileHandle represents the URI, content, hash, and optional |
| 32 | // version of a file tracked by the LSP session. |
| 33 | // |
| 34 | // File content may be provided by the file system (for Saved files) |
| 35 | // or from an overlay, for open files with unsaved edits. |
| 36 | // A FileHandle may record an attempt to read a non-existent file, |
| 37 | // in which case Content returns an error. |
| 38 | type Handle interface { |
| 39 | // URI is the URI for this file handle. |
| 40 | URI() protocol.DocumentURI |
| 41 | // Identity returns an Identity for the file, even if there was an error |
| 42 | // reading it. |
| 43 | Identity() Identity |
| 44 | // SameContentsOnDisk reports whether the file has the same content on disk: |
| 45 | // it is false for files open on an editor with unsaved edits. |
| 46 | SameContentsOnDisk() bool |
| 47 | // Version returns the file version, as defined by the LSP client. |
| 48 | // For on-disk file handles, Version returns 0. |
| 49 | Version() int32 |
| 50 | // Content returns the contents of a file. |
| 51 | // If the file is not available, returns a nil slice and an error. |
| 52 | Content() ([]byte, error) |
| 53 | // ModTime reports the modification time of a file. |
| 54 | // If the file is not available, returns the zero time and an error. |
| 55 | ModTime() (time.Time, error) |
| 56 | // String returns the file's path. |
| 57 | String() string |
| 58 | } |
| 59 | |
| 60 | // A Source maps URIs to Handles. |
| 61 | type Source interface { |
| 62 | // ReadFile returns the Handle for a given URI, either by reading the content |
| 63 | // of the file or by obtaining it from a cache. |
| 64 | // |
| 65 | // Invariant: ReadFile must only return an error in the case of context |
| 66 | // cancellation. If ctx.Err() is nil, the resulting error must also be nil. |
| 67 | ReadFile(ctx context.Context, uri protocol.DocumentURI) (Handle, error) |
| 68 | } |