/
documentcontents.go
215 lines (185 loc) · 5.12 KB
/
documentcontents.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package proxy
import (
"fmt"
"strings"
"sync"
lsp "github.com/a-h/protocol"
"go.uber.org/zap"
)
// newDocumentContents creates a document content processing tool.
func newDocumentContents(log *zap.Logger) *DocumentContents {
return &DocumentContents{
m: new(sync.Mutex),
uriToContents: make(map[string]*Document),
log: log,
}
}
type DocumentContents struct {
m *sync.Mutex
uriToContents map[string]*Document
log *zap.Logger
}
// Set the contents of a document.
func (dc *DocumentContents) Set(uri string, d *Document) {
dc.m.Lock()
defer dc.m.Unlock()
dc.uriToContents[uri] = d
}
// Get the contents of a document.
func (dc *DocumentContents) Get(uri string) (d *Document, ok bool) {
dc.m.Lock()
defer dc.m.Unlock()
d, ok = dc.uriToContents[uri]
return
}
// Delete a document from memory.
func (dc *DocumentContents) Delete(uri string) {
dc.m.Lock()
defer dc.m.Unlock()
delete(dc.uriToContents, uri)
}
func (dc *DocumentContents) URIs() (uris []string) {
dc.m.Lock()
defer dc.m.Unlock()
uris = make([]string, len(dc.uriToContents))
var i int
for k := range dc.uriToContents {
uris[i] = k
i++
}
return uris
}
// Apply changes to the document from the client, and return a list of change requests to send back to the client.
func (dc *DocumentContents) Apply(uri string, changes []lsp.TextDocumentContentChangeEvent) (d *Document, err error) {
dc.m.Lock()
defer dc.m.Unlock()
var ok bool
d, ok = dc.uriToContents[uri]
if !ok {
err = fmt.Errorf("document not found")
return
}
for _, change := range changes {
d.Apply(change.Range, change.Text)
}
return
}
func NewDocument(log *zap.Logger, s string) *Document {
return &Document{
Log: log,
Lines: strings.Split(s, "\n"),
}
}
type Document struct {
Log *zap.Logger
Lines []string
}
func (d *Document) LineLengths() (lens []int) {
lens = make([]int, len(d.Lines))
for i, l := range d.Lines {
lens[i] = len(l)
}
return
}
func (d *Document) Len() (line, col int) {
line = len(d.Lines)
col = len(d.Lines[len(d.Lines)-1])
return
}
func (d *Document) Overwrite(fromLine, fromCol, toLine, toCol int, lines []string) {
suffix := d.Lines[toLine][toCol:]
toLen := d.LineLengths()[toLine]
d.Delete(fromLine, fromCol, toLine, toLen)
lines[len(lines)-1] = lines[len(lines)-1] + suffix
d.Insert(fromLine, fromCol, lines)
}
func (d *Document) Insert(line, col int, lines []string) {
prefix := d.Lines[line][:col]
suffix := d.Lines[line][col:]
lines[0] = prefix + lines[0]
d.Lines[line] = lines[0]
if len(lines) > 1 {
d.InsertLines(line+1, lines[1:])
}
d.Lines[line+len(lines)-1] = lines[len(lines)-1] + suffix
}
func (d *Document) InsertLines(i int, withLines []string) {
d.Lines = append(d.Lines[:i], append(withLines, d.Lines[i:]...)...)
}
func (d *Document) Delete(fromLine, fromCol, toLine, toCol int) {
prefix := d.Lines[fromLine][:fromCol]
suffix := d.Lines[toLine][toCol:]
// Delete intermediate lines.
deleteFrom := fromLine
deleteTo := fromLine + (toLine - fromLine)
d.DeleteLines(deleteFrom, deleteTo)
// Merge the contents of the final line.
d.Lines[fromLine] = prefix + suffix
}
func (d *Document) DeleteLines(i, j int) {
d.Lines = append(d.Lines[:i], d.Lines[j:]...)
}
func (d *Document) String() string {
return strings.Join(d.Lines, "\n")
}
func (d *Document) Replace(with string) {
d.Lines = strings.Split(with, "\n")
}
func (d *Document) Apply(r *lsp.Range, with string) {
withLines := strings.Split(with, "\n")
d.normalize(r)
if d.isWholeDocument(r) {
d.Lines = withLines
return
}
if d.isInsert(r, with) {
d.Insert(int(r.Start.Line), int(r.Start.Character), withLines)
return
}
if d.isDelete(r, with) {
d.Delete(int(r.Start.Line), int(r.Start.Character), int(r.End.Line), int(r.End.Character))
return
}
if d.isOverwrite(r, with) {
d.Overwrite(int(r.Start.Line), int(r.Start.Character), int(r.End.Line), int(r.End.Character), withLines)
}
}
func (d *Document) normalize(r *lsp.Range) {
if r == nil {
return
}
lens := d.LineLengths()
if r.Start.Line >= uint32(len(lens)) {
r.Start.Line = uint32(len(lens) - 1)
r.Start.Character = uint32(lens[r.Start.Line])
}
if r.Start.Character > uint32(lens[r.Start.Line]) {
r.Start.Character = uint32(lens[r.Start.Line])
}
if r.End.Line >= uint32(len(lens)) {
r.End.Line = uint32(len(lens) - 1)
r.End.Character = uint32(lens[r.End.Line])
}
if r.End.Character > uint32(lens[r.End.Line]) {
r.End.Character = uint32(lens[r.End.Line])
}
}
func (d *Document) isOverwrite(r *lsp.Range, with string) bool {
return (r.End.Line != r.Start.Line || r.Start.Character != r.End.Character) && with != ""
}
func (d *Document) isInsert(r *lsp.Range, with string) bool {
return r.End.Line == r.Start.Line && r.Start.Character == r.End.Character && with != ""
}
func (d *Document) isDelete(r *lsp.Range, with string) bool {
return (r.End.Line != r.Start.Line || r.Start.Character != r.End.Character) && with == ""
}
func (d *Document) isWholeDocument(r *lsp.Range) bool {
if r == nil {
return true
}
if r.Start.Line != 0 || r.Start.Character != 0 {
return false
}
l, c := d.Len()
return r.End.Line == uint32(l) || r.End.Character == uint32(c)
}