mirror of
https://github.com/golang/go
synced 2024-11-18 15:04:44 -07:00
34437f544f
This change separates the different behaviors of server.go by the categories defined in the spec. This allows us to differentiate more easily between the language features and the text synchronization code. I also renamed the "Symbols" function to "Symbol", which fits with the specification (https://microsoft.github.io/language-server-protocol/specification#workspace_symbol), and makes clearer the distinction between DocumentSymbols and Symbol. Change-Id: I926b8a772c478f6ae426352fb12dc4403f0e736a Reviewed-on: https://go-review.googlesource.com/c/tools/+/172637 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
97 lines
2.4 KiB
Go
97 lines
2.4 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"
|
|
"fmt"
|
|
|
|
"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.findView(ctx, uri)
|
|
spn := span.New(uri, span.Point{}, span.Point{})
|
|
return formatRange(ctx, view, spn)
|
|
}
|
|
|
|
func (s *Server) rangeFormatting(ctx context.Context, params *protocol.DocumentRangeFormattingParams) ([]protocol.TextEdit, error) {
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
view := s.findView(ctx, uri)
|
|
_, m, err := newColumnMap(ctx, view, uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
spn, err := m.RangeSpan(params.Range)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return formatRange(ctx, view, spn)
|
|
}
|
|
|
|
// 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, err := s.Range(m.Converter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if rng.Start == rng.End {
|
|
// If we have a single point, assume we want the whole file.
|
|
tok := f.GetToken(ctx)
|
|
if tok == nil {
|
|
return nil, fmt.Errorf("no file information for %s", f.URI())
|
|
}
|
|
rng.End = tok.Pos(tok.Size())
|
|
}
|
|
edits, err := source.Format(ctx, f, rng)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ToProtocolEdits(m, edits)
|
|
}
|
|
|
|
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
|
|
}
|