1
0
mirror of https://github.com/golang/go synced 2024-11-18 23:14:43 -07:00
go/internal/lsp/format.go
Rebecca Stambler 8deeabbe2e internal/lsp: support range formatting
Refactor code a bit to support range formatting as well document
formatting. Also, separate view from server to clean up.

Change-Id: Ica397c7a0fb92a7708ea247c2d5de83e5528d8d4
Reviewed-on: https://go-review.googlesource.com/138275
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-10-01 16:29:50 +00:00

78 lines
2.2 KiB
Go

package lsp
import (
"fmt"
"go/format"
"strings"
"golang.org/x/tools/internal/lsp/protocol"
)
// format formats a document with a given range.
func (s *server) format(uri protocol.DocumentURI, rng *protocol.Range) ([]protocol.TextEdit, error) {
data, err := s.readActiveFile(uri)
if err != nil {
return nil, err
}
if rng != nil {
start, err := positionToOffset(data, int(rng.Start.Line), int(rng.Start.Character))
if err != nil {
return nil, err
}
end, err := positionToOffset(data, int(rng.End.Line), int(rng.End.Character))
if err != nil {
return nil, err
}
data = data[start:end]
// format.Source will fail if the substring is not a balanced expression tree.
// TODO(rstambler): parse the file and use astutil.PathEnclosingInterval to
// find the largest ast.Node n contained within start:end, and format the
// region n.Pos-n.End instead.
}
// format.Source changes slightly from one release to another, so the version
// of Go used to build the LSP server will determine how it formats code.
// This should be acceptable for all users, who likely be prompted to rebuild
// the LSP server on each Go release.
fmted, err := format.Source([]byte(data))
if err != nil {
return nil, err
}
if rng == nil {
// Get the ending line and column numbers for the original file.
line := strings.Count(data, "\n")
col := len(data) - strings.LastIndex(data, "\n") - 1
if col < 0 {
col = 0
}
rng = &protocol.Range{
Start: protocol.Position{0, 0},
End: protocol.Position{float64(line), float64(col)},
}
}
// TODO(rstambler): Compute text edits instead of replacing whole file.
return []protocol.TextEdit{
{
Range: *rng,
NewText: string(fmted),
},
}, nil
}
// positionToOffset converts a 0-based line and column number in a file
// to a byte offset value.
func positionToOffset(contents string, line, col int) (int, error) {
start := 0
for i := 0; i < int(line); i++ {
if start >= len(contents) {
return 0, fmt.Errorf("file contains %v lines, not %v lines", i, line)
}
index := strings.IndexByte(contents[start:], '\n')
if index == -1 {
return 0, fmt.Errorf("file contains %v lines, not %v lines", i, line)
}
start += (index + 1)
}
offset := start + int(col)
return offset, nil
}