Skip to content

Commit dfcf570

Browse files
committed
internal/lsp: stop requiring file kind when fetching a file
This change consolidates the FileKind into only the FileHandle. Previously, it had been set in multiple places, which required users to pass in a FileKind when fetching a file. This resulted in confusion, particularly in places when users did not have access to the file kind. Change-Id: I9e07d7320c46a21d453ffe108d1431a615706a71 Reviewed-on: https://go-review.googlesource.com/c/tools/+/213459 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
1 parent 4877332 commit dfcf570

File tree

10 files changed

+147
-171
lines changed

10 files changed

+147
-171
lines changed

internal/lsp/cache/builtin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (v *view) buildBuiltinPackage(ctx context.Context) error {
4444
pkg := pkgs[0]
4545
files := make(map[string]*ast.File)
4646
for _, filename := range pkg.GoFiles {
47-
fh := v.session.GetFile(span.FileURI(filename), source.Go)
47+
fh := v.session.GetFile(span.FileURI(filename))
4848
ph := v.session.cache.ParseGoHandle(fh, source.ParseFull)
4949
v.builtin.files = append(v.builtin.files, ph)
5050
file, _, _, err := ph.Parse(ctx)

internal/lsp/cache/cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ type fileData struct {
5656
err error
5757
}
5858

59-
func (c *cache) GetFile(uri span.URI, kind source.FileKind) source.FileHandle {
60-
underlying := c.fs.GetFile(uri, kind)
59+
func (c *cache) GetFile(uri span.URI) source.FileHandle {
60+
underlying := c.fs.GetFile(uri)
6161
key := fileKey{
6262
identity: underlying.Identity(),
6363
}

internal/lsp/cache/external.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ type nativeFileHandle struct {
2727
identity source.FileIdentity
2828
}
2929

30-
func (fs *nativeFileSystem) GetFile(uri span.URI, kind source.FileKind) source.FileHandle {
30+
func (fs *nativeFileSystem) GetFile(uri span.URI) source.FileHandle {
3131
identifier := "DOES NOT EXIST"
3232
if fi, err := os.Stat(uri.Filename()); err == nil {
3333
identifier = fi.ModTime().String()
3434
}
35+
kind := source.DetectLanguage("", uri.Filename())
3536
return &nativeFileHandle{
3637
fs: fs,
3738
identity: source.FileIdentity{

internal/lsp/cache/file.go

Lines changed: 0 additions & 49 deletions
This file was deleted.

internal/lsp/cache/overlay.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (o *overlay) Read(ctx context.Context) ([]byte, string, error) {
4141
return o.text, o.hash, nil
4242
}
4343

44-
func (s *session) updateOverlay(ctx context.Context, c source.FileModification) error {
44+
func (s *session) updateOverlay(ctx context.Context, c source.FileModification) (source.FileKind, error) {
4545
s.overlayMu.Lock()
4646
defer s.overlayMu.Unlock()
4747

@@ -54,18 +54,18 @@ func (s *session) updateOverlay(ctx context.Context, c source.FileModification)
5454
kind = source.DetectLanguage(c.LanguageID, c.URI.Filename())
5555
default:
5656
if !ok {
57-
return errors.Errorf("updateOverlay: modifying unopened overlay %v", c.URI)
57+
return -1, errors.Errorf("updateOverlay: modifying unopened overlay %v", c.URI)
5858
}
5959
kind = o.kind
6060
}
6161
if kind == source.UnknownKind {
62-
return errors.Errorf("updateOverlay: unknown file kind for %s", c.URI)
62+
return -1, errors.Errorf("updateOverlay: unknown file kind for %s", c.URI)
6363
}
6464

6565
// Closing a file just deletes its overlay.
6666
if c.Action == source.Close {
6767
delete(s.overlays, c.URI)
68-
return nil
68+
return kind, nil
6969
}
7070

7171
// If the file is on disk, check if its content is the same as the overlay.
@@ -77,15 +77,15 @@ func (s *session) updateOverlay(ctx context.Context, c source.FileModification)
7777
var sameContentOnDisk bool
7878
switch c.Action {
7979
case source.Open:
80-
_, h, err := s.cache.GetFile(c.URI, kind).Read(ctx)
80+
_, h, err := s.cache.GetFile(c.URI).Read(ctx)
8181
sameContentOnDisk = (err == nil && h == hash)
8282
case source.Save:
8383
// Make sure the version and content (if present) is the same.
8484
if o.version != c.Version {
85-
return errors.Errorf("updateOverlay: saving %s at version %v, currently at %v", c.URI, c.Version, o.version)
85+
return -1, errors.Errorf("updateOverlay: saving %s at version %v, currently at %v", c.URI, c.Version, o.version)
8686
}
8787
if c.Text != nil && o.hash != hash {
88-
return errors.Errorf("updateOverlay: overlay %s changed on save", c.URI)
88+
return -1, errors.Errorf("updateOverlay: overlay %s changed on save", c.URI)
8989
}
9090
sameContentOnDisk = true
9191
}
@@ -98,7 +98,7 @@ func (s *session) updateOverlay(ctx context.Context, c source.FileModification)
9898
hash: hash,
9999
sameContentOnDisk: sameContentOnDisk,
100100
}
101-
return nil
101+
return kind, nil
102102
}
103103

104104
func (s *session) readOverlay(uri span.URI) *overlay {

internal/lsp/cache/session.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ func (s *session) DidModifyFile(ctx context.Context, c source.FileModification)
256256
ctx = telemetry.URI.With(ctx, c.URI)
257257

258258
// Perform the session-specific updates.
259-
if err := s.updateOverlay(ctx, c); err != nil {
259+
kind, err := s.updateOverlay(ctx, c)
260+
if err != nil {
260261
return nil, err
261262
}
262263

@@ -265,12 +266,11 @@ func (s *session) DidModifyFile(ctx context.Context, c source.FileModification)
265266
if view.Ignore(c.URI) {
266267
return nil, errors.Errorf("ignored file %v", c.URI)
267268
}
268-
// Set the content for the file, only for didChange and didClose events.
269-
f, err := view.getFileLocked(ctx, c.URI)
270-
if err != nil {
269+
// Make sure to add the file to the view.
270+
if _, err := view.getFileLocked(ctx, c.URI); err != nil {
271271
return nil, err
272272
}
273-
snapshots = append(snapshots, view.invalidateContent(ctx, c.URI, f.kind, c.Action))
273+
snapshots = append(snapshots, view.invalidateContent(ctx, c.URI, kind, c.Action))
274274
}
275275
return snapshots, nil
276276
}
@@ -283,23 +283,25 @@ func (s *session) IsOpen(uri span.URI) bool {
283283
return open
284284
}
285285

286-
func (s *session) GetFile(uri span.URI, kind source.FileKind) source.FileHandle {
286+
func (s *session) GetFile(uri span.URI) source.FileHandle {
287287
if overlay := s.readOverlay(uri); overlay != nil {
288288
return overlay
289289
}
290290
// Fall back to the cache-level file system.
291-
return s.cache.GetFile(uri, kind)
291+
return s.cache.GetFile(uri)
292292
}
293293

294294
func (s *session) DidChangeOutOfBand(ctx context.Context, uri span.URI, action source.FileAction) bool {
295295
view, err := s.viewOf(uri)
296296
if err != nil {
297297
return false
298298
}
299-
f, err := view.getFileLocked(ctx, uri)
300-
if err != nil {
299+
// Make sure that the file is part of the view.
300+
if _, err := view.getFileLocked(ctx, uri); err != nil {
301301
return false
302302
}
303-
view.invalidateContent(ctx, f.URI(), f.kind, action)
303+
// TODO(golang/go#31553): Remove this when this issue has been resolved.
304+
kind := source.DetectLanguage("", uri.Filename())
305+
view.invalidateContent(ctx, uri, kind, action)
304306
return true
305307
}

internal/lsp/cache/snapshot.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"context"
99
"io"
1010
"os"
11+
"path/filepath"
12+
"strings"
1113
"sync"
1214

1315
"golang.org/x/tools/go/analysis"
@@ -92,7 +94,7 @@ func (s *snapshot) ModFiles(ctx context.Context) (source.FileHandle, source.File
9294
return nil, nil, err
9395
}
9496
// Go directly to disk to get the correct FileHandle, since we just copied the file without invalidating the snapshot.
95-
tempfh := s.view.Session().Cache().GetFile(span.FileURI(s.view.modfiles.temp), source.Mod)
97+
tempfh := s.view.Session().Cache().GetFile(span.FileURI(s.view.modfiles.temp))
9698
if tempfh == nil {
9799
return nil, nil, errors.Errorf("temporary go.mod filehandle is nil")
98100
}
@@ -533,9 +535,7 @@ func (s *snapshot) GetFile(ctx context.Context, uri span.URI) (source.FileHandle
533535
s.view.mu.Lock()
534536
defer s.view.mu.Unlock()
535537

536-
// TODO(rstambler): Should there be a version that provides a kind explicitly?
537-
kind := source.DetectLanguage("", uri.Filename())
538-
f, err := s.view.getFile(ctx, uri, kind)
538+
f, err := s.view.getFile(ctx, uri)
539539
if err != nil {
540540
return nil, err
541541
}
@@ -547,7 +547,7 @@ func (s *snapshot) getFileHandle(ctx context.Context, f *fileBase) source.FileHa
547547
defer s.mu.Unlock()
548548

549549
if _, ok := s.files[f.URI()]; !ok {
550-
s.files[f.URI()] = s.view.session.GetFile(f.URI(), f.kind)
550+
s.files[f.URI()] = s.view.session.GetFile(f.URI())
551551
}
552552
return s.files[f.URI()]
553553
}
@@ -633,7 +633,7 @@ func (s *snapshot) clone(ctx context.Context, withoutURI span.URI, withoutFileKi
633633
result.files[k] = v
634634
}
635635
// Handle the invalidated file; it may have new contents or not exist.
636-
currentFH := s.view.session.GetFile(withoutURI, withoutFileKind)
636+
currentFH := s.view.session.GetFile(withoutURI)
637637
if _, _, err := currentFH.Read(ctx); os.IsNotExist(err) {
638638
delete(result.files, withoutURI)
639639
} else {
@@ -680,6 +680,10 @@ func (s *snapshot) clone(ctx context.Context, withoutURI span.URI, withoutFileKi
680680
return result
681681
}
682682

683+
func dir(filename string) string {
684+
return strings.ToLower(filepath.Dir(filename))
685+
}
686+
683687
func (s *snapshot) ID() uint64 {
684688
return s.id
685689
}

0 commit comments

Comments
 (0)