Skip to content

Commit

Permalink
internal/lsp: update column mapper with content on incremental changes
Browse files Browse the repository at this point in the history
Fixes golang/go#32114

Change-Id: If2ffade3d8d1e026e3b0aa7f2c9db4dc46d7c8b2
Reviewed-on: https://go-review.googlesource.com/c/tools/+/178157
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
  • Loading branch information
stamblerre committed May 20, 2019
1 parent d88f798 commit 9558fe4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
5 changes: 2 additions & 3 deletions internal/lsp/protocol/span.go
Expand Up @@ -23,14 +23,13 @@ func NewURI(uri span.URI) string {
return string(uri)
}

func NewColumnMapper(uri span.URI, fn string, fset *token.FileSet, f *token.File, content []byte) *ColumnMapper {
func NewColumnMapper(uri span.URI, filename string, fset *token.FileSet, f *token.File, content []byte) *ColumnMapper {
var converter *span.TokenConverter
if f == nil {
converter = span.NewContentConverter(fn, content)
converter = span.NewContentConverter(filename, content)
} else {
converter = span.NewTokenConverter(fset, f)
}

return &ColumnMapper{
URI: uri,
Converter: converter,
Expand Down
4 changes: 0 additions & 4 deletions internal/lsp/source/diagnostics.go
Expand Up @@ -88,10 +88,6 @@ func Diagnostics(ctx context.Context, v View, uri span.URI) (map[span.URI][]Diag
}
// Updates to the diagnostics for this package may need to be propagated.
for _, f := range gof.GetActiveReverseDeps(ctx) {
if f == nil {
v.Session().Logger().Errorf(ctx, "nil file in reverse active dependencies for %s", f.URI())
continue
}
pkg := f.GetPackage(ctx)
if pkg == nil {
continue
Expand Down
12 changes: 10 additions & 2 deletions internal/lsp/text_synchronization.go
Expand Up @@ -65,11 +65,16 @@ func (s *Server) applyChanges(ctx context.Context, params *protocol.DidChangeTex

uri := span.NewURI(params.TextDocument.URI)
view := s.session.ViewOf(uri)
file, m, err := getSourceFile(ctx, view, uri)
f, m, err := getSourceFile(ctx, view, uri)
if err != nil {
return "", jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "file not found")
}
content := file.GetContent(ctx)
fset := f.GetFileSet(ctx)
filename, err := f.URI().Filename()
if err != nil {
return "", jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "no filename for %s", uri)
}
content := f.GetContent(ctx)
for _, change := range params.ContentChanges {
spn, err := m.RangeSpan(*change.Range)
if err != nil {
Expand All @@ -87,6 +92,9 @@ func (s *Server) applyChanges(ctx context.Context, params *protocol.DidChangeTex
buf.WriteString(change.Text)
buf.Write(content[end:])
content = buf.Bytes()

// Update column mapper along with the content.
m = protocol.NewColumnMapper(f.URI(), filename, fset, nil, content)
}
return string(content), nil
}
Expand Down
6 changes: 2 additions & 4 deletions internal/lsp/util.go
Expand Up @@ -51,13 +51,11 @@ func getSourceFile(ctx context.Context, v source.View, uri span.URI) (source.Fil
if err != nil {
return nil, nil, err
}

fname, err := f.URI().Filename()
filename, err := f.URI().Filename()
if err != nil {
return nil, nil, err
}

m := protocol.NewColumnMapper(f.URI(), fname, f.GetFileSet(ctx), f.GetToken(ctx), f.GetContent(ctx))
m := protocol.NewColumnMapper(f.URI(), filename, f.GetFileSet(ctx), f.GetToken(ctx), f.GetContent(ctx))

return f, m, nil
}
Expand Down

0 comments on commit 9558fe4

Please sign in to comment.