2019-04-17 13:37:20 -06:00
|
|
|
// 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.
|
|
|
|
|
2018-12-05 15:00:36 -07:00
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-13 17:31:41 -06:00
|
|
|
"fmt"
|
2018-12-05 15:00:36 -07:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2018-12-05 15:00:36 -07:00
|
|
|
)
|
|
|
|
|
2019-04-17 13:37:20 -06:00
|
|
|
func (s *Server) formatting(ctx context.Context, params *protocol.DocumentFormattingParams) ([]protocol.TextEdit, error) {
|
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
2019-05-15 10:24:49 -06:00
|
|
|
view := s.session.ViewOf(uri)
|
2019-04-17 13:37:20 -06:00
|
|
|
spn := span.New(uri, span.Point{}, span.Point{})
|
|
|
|
return formatRange(ctx, view, spn)
|
|
|
|
}
|
|
|
|
|
2018-12-05 15:00:36 -07:00
|
|
|
// formatRange formats a document with a given range.
|
2019-06-21 15:00:02 -06:00
|
|
|
func formatRange(ctx context.Context, view source.View, s span.Span) ([]protocol.TextEdit, error) {
|
|
|
|
f, m, rng, err := spanToRange(ctx, view, s)
|
|
|
|
edits, err := source.Format(ctx, f, rng)
|
2019-03-15 11:19:43 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-21 15:00:02 -06:00
|
|
|
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
|
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
rng, err := s.Range(m.Converter)
|
2019-02-04 15:44:35 -07:00
|
|
|
if err != nil {
|
2019-06-21 15:00:02 -06:00
|
|
|
return nil, nil, span.Range{}, err
|
2019-02-04 15:44:35 -07:00
|
|
|
}
|
2019-02-19 19:11:15 -07:00
|
|
|
if rng.Start == rng.End {
|
2019-03-13 17:31:41 -06:00
|
|
|
// If we have a single point, assume we want the whole file.
|
|
|
|
tok := f.GetToken(ctx)
|
|
|
|
if tok == nil {
|
2019-06-21 15:00:02 -06:00
|
|
|
return nil, nil, span.Range{}, fmt.Errorf("no file information for %s", f.URI())
|
2019-03-13 17:31:41 -06:00
|
|
|
}
|
|
|
|
rng.End = tok.Pos(tok.Size())
|
2018-12-18 14:18:03 -07:00
|
|
|
}
|
2019-06-21 15:00:02 -06:00
|
|
|
return f, m, rng, nil
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
|
|
|
|
2019-04-08 07:22:58 -06:00
|
|
|
func ToProtocolEdits(m *protocol.ColumnMapper, edits []source.TextEdit) ([]protocol.TextEdit, error) {
|
2018-12-05 15:00:36 -07:00
|
|
|
if edits == nil {
|
2019-03-15 11:19:43 -06:00
|
|
|
return nil, nil
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
|
|
|
result := make([]protocol.TextEdit, len(edits))
|
|
|
|
for i, edit := range edits {
|
2019-03-15 11:19:43 -06:00
|
|
|
rng, err := m.Range(edit.Span)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-05 15:00:36 -07:00
|
|
|
result[i] = protocol.TextEdit{
|
2019-03-15 11:19:43 -06:00
|
|
|
Range: rng,
|
2018-12-05 15:00:36 -07:00
|
|
|
NewText: edit.NewText,
|
|
|
|
}
|
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
return result, nil
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
2019-02-19 19:11:15 -07:00
|
|
|
|
2019-04-08 07:22:58 -06:00
|
|
|
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
|
|
|
|
}
|