go/internal/lsp/format.go
Ian Cottrell dbad8e90c9 internal/lsp: convert to the new location library
This rationalises all the position handling and conversion code out.
Fixes golang/go#29149

Change-Id: I2814f3e8ba769924bc70f35df9e5bf4d97d064de
Reviewed-on: https://go-review.googlesource.com/c/tools/+/166884
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-03-13 19:34:21 +00:00

51 lines
1.3 KiB
Go

package lsp
import (
"context"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
)
// formatRange formats a document with a given range.
func formatRange(ctx context.Context, v source.View, s span.Span) ([]protocol.TextEdit, error) {
f, m, err := newColumnMap(ctx, v, s.URI)
if err != nil {
return nil, err
}
rng := s.Range(m.Converter)
if rng.Start == rng.End {
// if we have a single point, then assume the rest of the file
rng.End = f.GetToken(ctx).Pos(f.GetToken(ctx).Size())
}
edits, err := source.Format(ctx, f, rng)
if err != nil {
return nil, err
}
return toProtocolEdits(m, edits), nil
}
func toProtocolEdits(m *protocol.ColumnMapper, edits []source.TextEdit) []protocol.TextEdit {
if edits == nil {
return nil
}
result := make([]protocol.TextEdit, len(edits))
for i, edit := range edits {
result[i] = protocol.TextEdit{
Range: m.Range(edit.Span),
NewText: edit.NewText,
}
}
return result
}
func newColumnMap(ctx context.Context, v source.View, uri span.URI) (source.File, *protocol.ColumnMapper, error) {
f, err := v.GetFile(ctx, uri)
if err != nil {
return nil, nil, err
}
m := protocol.NewColumnMapper(f.URI(), f.GetFileSet(ctx), f.GetToken(ctx), f.GetContent(ctx))
return f, m, nil
}