2020-02-02 10:53:30 -07:00
|
|
|
// Copyright 2020 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 fake
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-02-27 15:20:53 -07:00
|
|
|
"sort"
|
2020-02-02 10:53:30 -07:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Pos represents a 0-indexed position in a text buffer.
|
|
|
|
type Pos struct {
|
|
|
|
Line, Column int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Pos) toProtocolPosition() protocol.Position {
|
|
|
|
return protocol.Position{
|
|
|
|
Line: float64(p.Line),
|
|
|
|
Character: float64(p.Column),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fromProtocolPosition(pos protocol.Position) Pos {
|
|
|
|
return Pos{
|
|
|
|
Line: int(pos.Line),
|
|
|
|
Column: int(pos.Character),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edit represents a single (contiguous) buffer edit.
|
|
|
|
type Edit struct {
|
|
|
|
Start, End Pos
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
2020-02-06 17:50:37 -07:00
|
|
|
// NewEdit creates an edit replacing all content between
|
|
|
|
// (startLine, startColumn) and (endLine, endColumn) with text.
|
|
|
|
func NewEdit(startLine, startColumn, endLine, endColumn int, text string) Edit {
|
|
|
|
return Edit{
|
|
|
|
Start: Pos{Line: startLine, Column: startColumn},
|
|
|
|
End: Pos{Line: endLine, Column: endColumn},
|
|
|
|
Text: text,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 10:53:30 -07:00
|
|
|
func (e Edit) toProtocolChangeEvent() protocol.TextDocumentContentChangeEvent {
|
|
|
|
return protocol.TextDocumentContentChangeEvent{
|
|
|
|
Range: &protocol.Range{
|
|
|
|
Start: e.Start.toProtocolPosition(),
|
|
|
|
End: e.End.toProtocolPosition(),
|
|
|
|
},
|
|
|
|
Text: e.Text,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fromProtocolTextEdit(textEdit protocol.TextEdit) Edit {
|
|
|
|
return Edit{
|
|
|
|
Start: fromProtocolPosition(textEdit.Range.Start),
|
|
|
|
End: fromProtocolPosition(textEdit.Range.End),
|
|
|
|
Text: textEdit.NewText,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 14:46:55 -07:00
|
|
|
// inText reports whether p is a valid position in the text buffer.
|
|
|
|
func inText(p Pos, content []string) bool {
|
|
|
|
if p.Line < 0 || p.Line >= len(content) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Note the strict right bound: the column indexes character _separators_,
|
|
|
|
// not characters.
|
2020-02-27 15:20:53 -07:00
|
|
|
if p.Column < 0 || p.Column > len([]rune(content[p.Line])) {
|
2020-02-06 14:46:55 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-02-02 10:53:30 -07:00
|
|
|
// editContent implements a simplistic, inefficient algorithm for applying text
|
|
|
|
// edits to our buffer representation. It returns an error if the edit is
|
|
|
|
// invalid for the current content.
|
2020-02-27 15:20:53 -07:00
|
|
|
func editContent(content []string, edits []Edit) ([]string, error) {
|
|
|
|
newEdits := make([]Edit, len(edits))
|
|
|
|
copy(newEdits, edits)
|
|
|
|
sort.Slice(newEdits, func(i, j int) bool {
|
|
|
|
if newEdits[i].Start.Line < newEdits[j].Start.Line {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if newEdits[i].Start.Line > newEdits[j].Start.Line {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return newEdits[i].Start.Column < newEdits[j].Start.Column
|
|
|
|
})
|
|
|
|
|
|
|
|
// Validate edits.
|
|
|
|
for _, edit := range newEdits {
|
|
|
|
if edit.End.Line < edit.Start.Line || (edit.End.Line == edit.Start.Line && edit.End.Column < edit.Start.Column) {
|
|
|
|
return nil, fmt.Errorf("invalid edit: end %v before start %v", edit.End, edit.Start)
|
|
|
|
}
|
|
|
|
if !inText(edit.Start, content) {
|
|
|
|
return nil, fmt.Errorf("start position %v is out of bounds", edit.Start)
|
|
|
|
}
|
|
|
|
if !inText(edit.End, content) {
|
|
|
|
return nil, fmt.Errorf("end position %v is out of bounds", edit.End)
|
|
|
|
}
|
2020-02-02 10:53:30 -07:00
|
|
|
}
|
2020-02-27 15:20:53 -07:00
|
|
|
|
|
|
|
var (
|
|
|
|
b strings.Builder
|
|
|
|
line, column int
|
|
|
|
)
|
|
|
|
advance := func(toLine, toColumn int) {
|
|
|
|
for ; line < toLine; line++ {
|
|
|
|
b.WriteString(string([]rune(content[line])[column:]) + "\n")
|
|
|
|
column = 0
|
|
|
|
}
|
|
|
|
b.WriteString(string([]rune(content[line])[column:toColumn]))
|
|
|
|
column = toColumn
|
2020-02-02 10:53:30 -07:00
|
|
|
}
|
2020-02-27 15:20:53 -07:00
|
|
|
for _, edit := range newEdits {
|
|
|
|
advance(edit.Start.Line, edit.Start.Column)
|
|
|
|
b.WriteString(edit.Text)
|
|
|
|
line = edit.End.Line
|
|
|
|
column = edit.End.Column
|
2020-02-02 10:53:30 -07:00
|
|
|
}
|
2020-02-27 15:20:53 -07:00
|
|
|
advance(len(content)-1, len([]rune(content[len(content)-1])))
|
|
|
|
return strings.Split(b.String(), "\n"), nil
|
2020-02-02 10:53:30 -07:00
|
|
|
}
|