1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:44:42 -07:00

internal/lsp: separate out getMapper function

This is a super minimal change that will simplify the diffs for when I
actually delete the getMapper function.

Change-Id: I16984b344c87b3645fd451668b6ea747c5be12ab
Reviewed-on: https://go-review.googlesource.com/c/tools/+/190557
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Rebecca Stambler 2019-08-16 13:49:17 -04:00
parent caa95bb40b
commit 6889da9d54
15 changed files with 126 additions and 52 deletions

View File

@ -21,7 +21,11 @@ import (
func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) { func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) {
uri := span.NewURI(params.TextDocument.URI) uri := span.NewURI(params.TextDocument.URI)
view := s.session.ViewOf(uri) view := s.session.ViewOf(uri)
f, m, err := getSourceFile(ctx, view, uri) f, err := view.GetFile(ctx, uri)
if err != nil {
return nil, err
}
m, err := getMapper(ctx, f)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -229,7 +233,11 @@ func quickFixes(ctx context.Context, view source.View, gof source.GoFile) ([]pro
return nil, err return nil, err
} }
for _, ca := range diag.SuggestedFixes { for _, ca := range diag.SuggestedFixes {
_, m, err := getGoFile(ctx, view, diag.URI) f, err := view.GetFile(ctx, diag.URI)
if err != nil {
return nil, err
}
m, err := getMapper(ctx, f)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -19,7 +19,11 @@ import (
func (s *Server) completion(ctx context.Context, params *protocol.CompletionParams) (*protocol.CompletionList, error) { func (s *Server) completion(ctx context.Context, params *protocol.CompletionParams) (*protocol.CompletionList, error) {
uri := span.NewURI(params.TextDocument.URI) uri := span.NewURI(params.TextDocument.URI)
view := s.session.ViewOf(uri) view := s.session.ViewOf(uri)
f, m, err := getGoFile(ctx, view, uri) f, err := getGoFile(ctx, view, uri)
if err != nil {
return nil, err
}
m, err := getMapper(ctx, f)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -15,7 +15,11 @@ import (
func (s *Server) definition(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]protocol.Location, error) { func (s *Server) definition(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]protocol.Location, error) {
uri := span.NewURI(params.TextDocument.URI) uri := span.NewURI(params.TextDocument.URI)
view := s.session.ViewOf(uri) view := s.session.ViewOf(uri)
f, m, err := getGoFile(ctx, view, uri) f, err := getGoFile(ctx, view, uri)
if err != nil {
return nil, err
}
m, err := getMapper(ctx, f)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -35,7 +39,11 @@ func (s *Server) definition(ctx context.Context, params *protocol.TextDocumentPo
if err != nil { if err != nil {
return nil, err return nil, err
} }
_, decM, err := getSourceFile(ctx, view, decSpan.URI()) decFile, err := getGoFile(ctx, view, decSpan.URI())
if err != nil {
return nil, err
}
decM, err := getMapper(ctx, decFile)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -49,7 +57,11 @@ func (s *Server) definition(ctx context.Context, params *protocol.TextDocumentPo
func (s *Server) typeDefinition(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]protocol.Location, error) { func (s *Server) typeDefinition(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]protocol.Location, error) {
uri := span.NewURI(params.TextDocument.URI) uri := span.NewURI(params.TextDocument.URI)
view := s.session.ViewOf(uri) view := s.session.ViewOf(uri)
f, m, err := getGoFile(ctx, view, uri) f, err := getGoFile(ctx, view, uri)
if err != nil {
return nil, err
}
m, err := getMapper(ctx, f)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -69,7 +81,11 @@ func (s *Server) typeDefinition(ctx context.Context, params *protocol.TextDocume
if err != nil { if err != nil {
return nil, err return nil, err
} }
_, identM, err := getSourceFile(ctx, view, identSpan.URI()) identFile, err := getGoFile(ctx, view, identSpan.URI())
if err != nil {
return nil, err
}
identM, err := getMapper(ctx, identFile)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -37,7 +37,7 @@ func (s *Server) Diagnostics(ctx context.Context, view source.View, uri span.URI
defer s.undeliveredMu.Unlock() defer s.undeliveredMu.Unlock()
for uri, diagnostics := range reports { for uri, diagnostics := range reports {
if err := s.publishDiagnostics(ctx, view, uri, diagnostics); err != nil { if err := s.publishDiagnostics(ctx, uri, diagnostics); err != nil {
if s.undelivered == nil { if s.undelivered == nil {
s.undelivered = make(map[span.URI][]source.Diagnostic) s.undelivered = make(map[span.URI][]source.Diagnostic)
} }
@ -51,7 +51,7 @@ func (s *Server) Diagnostics(ctx context.Context, view source.View, uri span.URI
// Anytime we compute diagnostics, make sure to also send along any // Anytime we compute diagnostics, make sure to also send along any
// undelivered ones (only for remaining URIs). // undelivered ones (only for remaining URIs).
for uri, diagnostics := range s.undelivered { for uri, diagnostics := range s.undelivered {
if err := s.publishDiagnostics(ctx, view, uri, diagnostics); err != nil { if err := s.publishDiagnostics(ctx, uri, diagnostics); err != nil {
log.Error(ctx, "failed to deliver diagnostic for (will not retry)", err, telemetry.File) log.Error(ctx, "failed to deliver diagnostic for (will not retry)", err, telemetry.File)
} }
// If we fail to deliver the same diagnostics twice, just give up. // If we fail to deliver the same diagnostics twice, just give up.
@ -59,7 +59,7 @@ func (s *Server) Diagnostics(ctx context.Context, view source.View, uri span.URI
} }
} }
func (s *Server) publishDiagnostics(ctx context.Context, view source.View, uri span.URI, diagnostics []source.Diagnostic) error { func (s *Server) publishDiagnostics(ctx context.Context, uri span.URI, diagnostics []source.Diagnostic) error {
protocolDiagnostics, err := toProtocolDiagnostics(ctx, diagnostics) protocolDiagnostics, err := toProtocolDiagnostics(ctx, diagnostics)
if err != nil { if err != nil {
return err return err

View File

@ -27,12 +27,16 @@ func (s *Server) formatting(ctx context.Context, params *protocol.DocumentFormat
return ToProtocolEdits(m, edits) return ToProtocolEdits(m, edits)
} }
func spanToRange(ctx context.Context, view source.View, s span.Span) (source.GoFile, *protocol.ColumnMapper, span.Range, error) { func spanToRange(ctx context.Context, view source.View, spn span.Span) (source.GoFile, *protocol.ColumnMapper, span.Range, error) {
f, m, err := getGoFile(ctx, view, s.URI()) f, err := getGoFile(ctx, view, spn.URI())
if err != nil { if err != nil {
return nil, nil, span.Range{}, err return nil, nil, span.Range{}, err
} }
rng, err := s.Range(m.Converter) m, err := getMapper(ctx, f)
if err != nil {
return nil, nil, span.Range{}, err
}
rng, err := spn.Range(m.Converter)
if err != nil { if err != nil {
return nil, nil, span.Range{}, err return nil, nil, span.Range{}, err
} }

View File

@ -17,7 +17,11 @@ import (
func (s *Server) documentHighlight(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]protocol.DocumentHighlight, error) { func (s *Server) documentHighlight(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]protocol.DocumentHighlight, error) {
uri := span.NewURI(params.TextDocument.URI) uri := span.NewURI(params.TextDocument.URI)
view := s.session.ViewOf(uri) view := s.session.ViewOf(uri)
f, m, err := getGoFile(ctx, view, uri) f, err := getGoFile(ctx, view, uri)
if err != nil {
return nil, err
}
m, err := getMapper(ctx, f)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -34,7 +34,11 @@ const (
func (s *Server) hover(ctx context.Context, params *protocol.TextDocumentPositionParams) (*protocol.Hover, error) { func (s *Server) hover(ctx context.Context, params *protocol.TextDocumentPositionParams) (*protocol.Hover, error) {
uri := span.NewURI(params.TextDocument.URI) uri := span.NewURI(params.TextDocument.URI)
view := s.session.ViewOf(uri) view := s.session.ViewOf(uri)
f, m, err := getGoFile(ctx, view, uri) f, err := getGoFile(ctx, view, uri)
if err != nil {
return nil, err
}
m, err := getMapper(ctx, f)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -23,7 +23,11 @@ import (
func (s *Server) documentLink(ctx context.Context, params *protocol.DocumentLinkParams) ([]protocol.DocumentLink, error) { func (s *Server) documentLink(ctx context.Context, params *protocol.DocumentLinkParams) ([]protocol.DocumentLink, error) {
uri := span.NewURI(params.TextDocument.URI) uri := span.NewURI(params.TextDocument.URI)
view := s.session.ViewOf(uri) view := s.session.ViewOf(uri)
f, m, err := getGoFile(ctx, view, uri) f, err := getGoFile(ctx, view, uri)
if err != nil {
return nil, err
}
m, err := getMapper(ctx, f)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -275,9 +275,13 @@ func (r *runner) Format(t *testing.T, data tests.Formats) {
} }
continue continue
} }
_, m, err := getSourceFile(r.ctx, r.server.session.ViewOf(uri), uri) f, err := getGoFile(r.ctx, r.server.session.ViewOf(uri), uri)
if err != nil { if err != nil {
t.Error(err) t.Fatal(err)
}
m, err := getMapper(r.ctx, f)
if err != nil {
t.Fatal(err)
} }
sedits, err := FromProtocolEdits(m, edits) sedits, err := FromProtocolEdits(m, edits)
if err != nil { if err != nil {
@ -312,9 +316,13 @@ func (r *runner) Import(t *testing.T, data tests.Imports) {
} }
continue continue
} }
_, m, err := getSourceFile(r.ctx, r.server.session.ViewOf(uri), uri) f, err := getGoFile(r.ctx, r.server.session.ViewOf(uri), uri)
if err != nil { if err != nil {
t.Error(err) t.Fatal(err)
}
m, err := getMapper(r.ctx, f)
if err != nil {
t.Fatal(err)
} }
var edits []protocol.TextEdit var edits []protocol.TextEdit
for _, a := range actions { for _, a := range actions {
@ -501,9 +509,13 @@ func (r *runner) Rename(t *testing.T, data tests.Renames) {
var res []string var res []string
for uri, edits := range *workspaceEdits.Changes { for uri, edits := range *workspaceEdits.Changes {
spnURI := span.URI(uri) spnURI := span.URI(uri)
_, m, err := getSourceFile(r.ctx, r.server.session.ViewOf(span.URI(spnURI)), spnURI) f, err := getGoFile(r.ctx, r.server.session.ViewOf(spnURI), spnURI)
if err != nil { if err != nil {
t.Error(err) t.Fatal(err)
}
m, err := getMapper(r.ctx, f)
if err != nil {
t.Fatal(err)
} }
sedits, err := FromProtocolEdits(m, edits) sedits, err := FromProtocolEdits(m, edits)

View File

@ -17,7 +17,11 @@ import (
func (s *Server) references(ctx context.Context, params *protocol.ReferenceParams) ([]protocol.Location, error) { func (s *Server) references(ctx context.Context, params *protocol.ReferenceParams) ([]protocol.Location, error) {
uri := span.NewURI(params.TextDocument.URI) uri := span.NewURI(params.TextDocument.URI)
view := s.session.ViewOf(uri) view := s.session.ViewOf(uri)
f, m, err := getGoFile(ctx, view, uri) f, err := getGoFile(ctx, view, uri)
if err != nil {
return nil, err
}
m, err := getMapper(ctx, f)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -63,7 +67,11 @@ func (s *Server) references(ctx context.Context, params *protocol.ReferenceParam
} }
seen[refSpan] = true seen[refSpan] = true
_, refM, err := getSourceFile(ctx, view, refSpan.URI()) refFile, err := getGoFile(ctx, view, refSpan.URI())
if err != nil {
return nil, err
}
refM, err := getMapper(ctx, refFile)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -15,7 +15,11 @@ import (
func (s *Server) rename(ctx context.Context, params *protocol.RenameParams) (*protocol.WorkspaceEdit, error) { func (s *Server) rename(ctx context.Context, params *protocol.RenameParams) (*protocol.WorkspaceEdit, error) {
uri := span.NewURI(params.TextDocument.URI) uri := span.NewURI(params.TextDocument.URI)
view := s.session.ViewOf(uri) view := s.session.ViewOf(uri)
f, m, err := getGoFile(ctx, view, uri) f, err := getGoFile(ctx, view, uri)
if err != nil {
return nil, err
}
m, err := getMapper(ctx, f)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -37,7 +41,11 @@ func (s *Server) rename(ctx context.Context, params *protocol.RenameParams) (*pr
} }
changes := make(map[string][]protocol.TextEdit) changes := make(map[string][]protocol.TextEdit)
for uri, textEdits := range edits { for uri, textEdits := range edits {
_, m, err := getGoFile(ctx, view, uri) f, err := getGoFile(ctx, view, uri)
if err != nil {
return nil, err
}
m, err := getMapper(ctx, f)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -17,7 +17,11 @@ import (
func (s *Server) signatureHelp(ctx context.Context, params *protocol.TextDocumentPositionParams) (*protocol.SignatureHelp, error) { func (s *Server) signatureHelp(ctx context.Context, params *protocol.TextDocumentPositionParams) (*protocol.SignatureHelp, error) {
uri := span.NewURI(params.TextDocument.URI) uri := span.NewURI(params.TextDocument.URI)
view := s.session.ViewOf(uri) view := s.session.ViewOf(uri)
f, m, err := getGoFile(ctx, view, uri) f, err := getGoFile(ctx, view, uri)
if err != nil {
return nil, err
}
m, err := getMapper(ctx, f)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -18,7 +18,11 @@ func (s *Server) documentSymbol(ctx context.Context, params *protocol.DocumentSy
defer done() defer done()
uri := span.NewURI(params.TextDocument.URI) uri := span.NewURI(params.TextDocument.URI)
view := s.session.ViewOf(uri) view := s.session.ViewOf(uri)
f, m, err := getGoFile(ctx, view, uri) f, err := getGoFile(ctx, view, uri)
if err != nil {
return nil, err
}
m, err := getMapper(ctx, f)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -149,7 +149,7 @@ func (s *Server) didClose(ctx context.Context, params *protocol.DidCloseTextDocu
clear := []span.URI{uri} // by default, clear the closed URI clear := []span.URI{uri} // by default, clear the closed URI
defer func() { defer func() {
for _, uri := range clear { for _, uri := range clear {
if err := s.publishDiagnostics(ctx, view, uri, []source.Diagnostic{}); err != nil { if err := s.publishDiagnostics(ctx, uri, []source.Diagnostic{}); err != nil {
log.Error(ctx, "failed to clear diagnostics", err, telemetry.File) log.Error(ctx, "failed to clear diagnostics", err, telemetry.File)
} }
} }

View File

@ -13,32 +13,26 @@ import (
errors "golang.org/x/xerrors" errors "golang.org/x/xerrors"
) )
func getSourceFile(ctx context.Context, v source.View, uri span.URI) (source.File, *protocol.ColumnMapper, error) { func getGoFile(ctx context.Context, view source.View, uri span.URI) (source.GoFile, error) {
f, err := v.GetFile(ctx, uri) f, err := view.GetFile(ctx, uri)
if err != nil { if err != nil {
return nil, nil, err return nil, err
}
data, _, err := f.Handle(ctx).Read(ctx)
if err != nil {
return nil, nil, err
}
tok, err := f.GetToken(ctx)
if err != nil {
return nil, nil, err
}
m := protocol.NewColumnMapper(f.URI(), f.URI().Filename(), f.FileSet(), tok, data)
return f, m, nil
}
func getGoFile(ctx context.Context, v source.View, uri span.URI) (source.GoFile, *protocol.ColumnMapper, error) {
f, m, err := getSourceFile(ctx, v, uri)
if err != nil {
return nil, nil, err
} }
gof, ok := f.(source.GoFile) gof, ok := f.(source.GoFile)
if !ok { if !ok {
return nil, nil, errors.Errorf("not a Go file %v", f.URI()) return nil, errors.Errorf("%s is not a Go file", uri)
} }
return gof, m, nil return gof, nil
}
func getMapper(ctx context.Context, f source.File) (*protocol.ColumnMapper, error) {
data, _, err := f.Handle(ctx).Read(ctx)
if err != nil {
return nil, err
}
tok, err := f.GetToken(ctx)
if err != nil {
return nil, err
}
return protocol.NewColumnMapper(f.URI(), f.URI().Filename(), f.FileSet(), tok, data), nil
} }