Skip to content

Commit c02aa52

Browse files
committed
internal/lsp: handle first change behavior on the server side
I'm not sure why this was being managed by the view, but delete the code that handles tracking a file's first change. It is only used to avoid spamming the user with error messages. Change-Id: Id95089478ffb7e189d38cbc147e3dde6a1c55c5e Reviewed-on: https://go-review.googlesource.com/c/tools/+/208274 Reviewed-by: Ian Cottrell <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 2189885 commit c02aa52

File tree

5 files changed

+39
-40
lines changed

5 files changed

+39
-40
lines changed

internal/lsp/cache/session.go

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ type overlay struct {
5151
// sameContentOnDisk is true if a file has been saved on disk,
5252
// and therefore does not need to be part of the overlay sent to go/packages.
5353
sameContentOnDisk bool
54-
55-
// unchanged is true if a file has not yet been edited.
56-
unchanged bool
5754
}
5855

5956
func (s *session) Options() source.Options {
@@ -342,7 +339,7 @@ func (s *session) GetFile(uri span.URI, kind source.FileKind) source.FileHandle
342339
return s.cache.GetFile(uri, kind)
343340
}
344341

345-
func (s *session) SetOverlay(uri span.URI, kind source.FileKind, version float64, data []byte) bool {
342+
func (s *session) SetOverlay(uri span.URI, kind source.FileKind, version float64, data []byte) {
346343
s.overlayMu.Lock()
347344
defer func() {
348345
s.overlayMu.Unlock()
@@ -351,22 +348,17 @@ func (s *session) SetOverlay(uri span.URI, kind source.FileKind, version float64
351348

352349
if data == nil {
353350
delete(s.overlays, uri)
354-
return false
351+
return
355352
}
356353

357-
o := s.overlays[uri]
358-
firstChange := o != nil && o.unchanged
359-
360354
s.overlays[uri] = &overlay{
361-
session: s,
362-
uri: uri,
363-
kind: kind,
364-
data: data,
365-
hash: hashContents(data),
366-
version: version,
367-
unchanged: o == nil,
355+
session: s,
356+
uri: uri,
357+
kind: kind,
358+
data: data,
359+
hash: hashContents(data),
360+
version: version,
368361
}
369-
return firstChange
370362
}
371363

372364
func (s *session) clearOverlay(uri span.URI) {
@@ -385,13 +377,12 @@ func (s *session) openOverlay(ctx context.Context, uri span.URI, kind source.Fil
385377
s.filesWatchMap.Notify(uri, source.Open)
386378
}()
387379
s.overlays[uri] = &overlay{
388-
session: s,
389-
uri: uri,
390-
kind: kind,
391-
data: data,
392-
hash: hashContents(data),
393-
unchanged: true,
394-
version: version,
380+
session: s,
381+
uri: uri,
382+
kind: kind,
383+
data: data,
384+
hash: hashContents(data),
385+
version: version,
395386
}
396387
// If the file is on disk, check if its content is the same as the overlay.
397388
if _, hash, err := s.cache.GetFile(uri, kind).Read(ctx); err == nil {

internal/lsp/cache/view.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,21 +324,21 @@ func (v *view) getSnapshot() *snapshot {
324324
}
325325

326326
// SetContent sets the overlay contents for a file.
327-
func (v *view) SetContent(ctx context.Context, uri span.URI, version float64, content []byte) (bool, error) {
327+
func (v *view) SetContent(ctx context.Context, uri span.URI, version float64, content []byte) {
328328
v.mu.Lock()
329329
defer v.mu.Unlock()
330330

331+
if v.Ignore(uri) {
332+
return
333+
}
334+
331335
// Cancel all still-running previous requests, since they would be
332336
// operating on stale data.
333337
v.cancel()
334338
v.backgroundCtx, v.cancel = context.WithCancel(v.baseCtx)
335339

336-
if v.Ignore(uri) {
337-
return false, nil
338-
}
339-
340340
kind := source.DetectLanguage("", uri.Filename())
341-
return v.session.SetOverlay(uri, kind, version, content), nil
341+
v.session.SetOverlay(uri, kind, version, content)
342342
}
343343

344344
// FindFile returns the file if the given URI is already a part of the view.

internal/lsp/server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"golang.org/x/tools/internal/jsonrpc2"
1515
"golang.org/x/tools/internal/lsp/protocol"
1616
"golang.org/x/tools/internal/lsp/source"
17+
"golang.org/x/tools/internal/span"
1718
)
1819

1920
// NewClientServer
@@ -83,6 +84,9 @@ type Server struct {
8384
undeliveredMu sync.Mutex
8485
undelivered map[source.FileIdentity][]source.Diagnostic
8586

87+
// changedFiles tracks files for which there has been a textDocument/didChange.
88+
changedFiles map[span.URI]struct{}
89+
8690
// folders is only valid between initialize and initialized, and holds the
8791
// set of folders to build views for when we are ready
8892
pendingFolders []protocol.WorkspaceFolder

internal/lsp/source/view.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ type View interface {
9696
FindFile(ctx context.Context, uri span.URI) File
9797

9898
// Called to set the effective contents of a file from this view.
99-
SetContent(ctx context.Context, uri span.URI, version float64, content []byte) (wasFirstChange bool, err error)
99+
SetContent(ctx context.Context, uri span.URI, version float64, content []byte)
100100

101101
// BackgroundContext returns a context used for all background processing
102102
// on behalf of this view.
@@ -172,7 +172,7 @@ type Session interface {
172172
IsOpen(uri span.URI) bool
173173

174174
// Called to set the effective contents of a file from this session.
175-
SetOverlay(uri span.URI, kind FileKind, version float64, data []byte) (wasFirstChange bool)
175+
SetOverlay(uri span.URI, kind FileKind, version float64, data []byte)
176176

177177
// DidChangeOutOfBand is called when a file under the root folder changes.
178178
// If the file was open in the editor, it returns true.

internal/lsp/text_synchronization.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,10 @@ func (s *Server) didChange(ctx context.Context, params *protocol.DidChangeTextDo
7171
if err != nil {
7272
return err
7373
}
74-
wasFirstChange, err := view.SetContent(ctx, uri, params.TextDocument.Version, []byte(text))
75-
if err != nil {
76-
return err
77-
}
78-
79-
// TODO: Ideally, we should be able to specify that a generated file should be opened as read-only.
74+
view.SetContent(ctx, uri, params.TextDocument.Version, []byte(text))
75+
// Ideally, we should be able to specify that a generated file should be opened as read-only.
8076
// Tell the user that they should not be editing a generated file.
81-
if source.IsGenerated(ctx, view, uri) && wasFirstChange {
77+
if s.wasFirstChange(uri) && source.IsGenerated(ctx, view, uri) {
8278
s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
8379
Message: fmt.Sprintf("Do not edit this file! %s is a generated file.", uri.Filename()),
8480
Type: protocol.Warning,
@@ -91,6 +87,14 @@ func (s *Server) didChange(ctx context.Context, params *protocol.DidChangeTextDo
9187
return nil
9288
}
9389

90+
func (s *Server) wasFirstChange(uri span.URI) bool {
91+
if s.changedFiles == nil {
92+
s.changedFiles = make(map[span.URI]struct{})
93+
}
94+
_, ok := s.changedFiles[uri]
95+
return ok
96+
}
97+
9498
func fullChange(changes []protocol.TextDocumentContentChangeEvent) (string, bool) {
9599
if len(changes) > 1 {
96100
return "", false
@@ -151,6 +155,6 @@ func (s *Server) didClose(ctx context.Context, params *protocol.DidCloseTextDocu
151155
if err != nil {
152156
return err
153157
}
154-
_, err = view.SetContent(ctx, uri, -1, nil)
155-
return err
158+
view.SetContent(ctx, uri, -1, nil)
159+
return nil
156160
}

0 commit comments

Comments
 (0)