1
0
mirror of https://github.com/golang/go synced 2024-10-01 05:38:32 -06:00
go/internal/lsp/format.go
Rebecca Stambler b667c4c58e internal/lsp: cache the *ast.File and *token.File on the package
This change removes the need for the ast and token fields on the *goFile
object. We switch to using source.ParseGoHandles on the package, which
means that we can easily access both the AST and token via the package,
which is already cached.

Change-Id: I5f78bbe09362f4d95eb15556617bdbd809a7a55d
Reviewed-on: https://go-review.googlesource.com/c/tools/+/185878
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-07-16 19:44:59 +00:00

85 lines
2.1 KiB
Go

// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lsp
import (
"context"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
)
func (s *Server) formatting(ctx context.Context, params *protocol.DocumentFormattingParams) ([]protocol.TextEdit, error) {
uri := span.NewURI(params.TextDocument.URI)
view := s.session.ViewOf(uri)
spn := span.New(uri, span.Point{}, span.Point{})
f, m, rng, err := spanToRange(ctx, view, spn)
if err != nil {
return nil, err
}
edits, err := source.Format(ctx, f, rng)
if err != nil {
return nil, err
}
return ToProtocolEdits(m, edits)
}
func spanToRange(ctx context.Context, view source.View, s span.Span) (source.GoFile, *protocol.ColumnMapper, span.Range, error) {
f, m, err := getGoFile(ctx, view, s.URI())
if err != nil {
return nil, nil, span.Range{}, err
}
rng, err := s.Range(m.Converter)
if err != nil {
return nil, nil, span.Range{}, err
}
if rng.Start == rng.End {
// If we have a single point, assume we want the whole file.
tok, err := f.GetToken(ctx)
if err != nil {
return nil, nil, span.Range{}, err
}
rng.End = tok.Pos(tok.Size())
}
return f, m, rng, nil
}
func ToProtocolEdits(m *protocol.ColumnMapper, edits []source.TextEdit) ([]protocol.TextEdit, error) {
if edits == nil {
return nil, nil
}
result := make([]protocol.TextEdit, len(edits))
for i, edit := range edits {
rng, err := m.Range(edit.Span)
if err != nil {
return nil, err
}
result[i] = protocol.TextEdit{
Range: rng,
NewText: edit.NewText,
}
}
return result, nil
}
func FromProtocolEdits(m *protocol.ColumnMapper, edits []protocol.TextEdit) ([]source.TextEdit, error) {
if edits == nil {
return nil, nil
}
result := make([]source.TextEdit, len(edits))
for i, edit := range edits {
spn, err := m.RangeSpan(edit.Range)
if err != nil {
return nil, err
}
result[i] = source.TextEdit{
Span: spn,
NewText: edit.NewText,
}
}
return result, nil
}