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-08-19 17:28:08 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/diff"
|
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{})
|
2019-06-27 14:29:03 -06:00
|
|
|
f, m, rng, err := spanToRange(ctx, view, spn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-21 15:00:02 -06:00
|
|
|
edits, err := source.Format(ctx, f, rng)
|
2019-03-15 11:19:43 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-16 15:05:40 -06:00
|
|
|
return source.ToProtocolEdits(m, edits)
|
2019-06-21 15:00:02 -06:00
|
|
|
}
|
|
|
|
|
2019-08-16 11:49:17 -06:00
|
|
|
func spanToRange(ctx context.Context, view source.View, spn span.Span) (source.GoFile, *protocol.ColumnMapper, span.Range, error) {
|
|
|
|
f, err := getGoFile(ctx, view, spn.URI())
|
2019-06-21 15:00:02 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, span.Range{}, err
|
|
|
|
}
|
2019-08-16 11:49:17 -06:00
|
|
|
m, err := getMapper(ctx, f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, span.Range{}, err
|
|
|
|
}
|
|
|
|
rng, err := spn.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.
|
2019-07-11 19:05:55 -06:00
|
|
|
tok, err := f.GetToken(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, span.Range{}, err
|
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-08-19 17:28:08 -06:00
|
|
|
func FromProtocolEdits(m *protocol.ColumnMapper, edits []protocol.TextEdit) ([]diff.TextEdit, error) {
|
2019-04-08 07:22:58 -06:00
|
|
|
if edits == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-08-19 17:28:08 -06:00
|
|
|
result := make([]diff.TextEdit, len(edits))
|
2019-04-08 07:22:58 -06:00
|
|
|
for i, edit := range edits {
|
|
|
|
spn, err := m.RangeSpan(edit.Range)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-19 17:28:08 -06:00
|
|
|
result[i] = diff.TextEdit{
|
2019-04-08 07:22:58 -06:00
|
|
|
Span: spn,
|
|
|
|
NewText: edit.NewText,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|