/
workspace_store.go
169 lines (138 loc) · 4.17 KB
/
workspace_store.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
package langserver
import (
"fmt"
"strings"
"sync"
"github.com/thought-machine/please/tools/build_langserver/lsp"
)
type workspaceStore struct {
rootURI lsp.DocumentURI
mu sync.Mutex
documents map[lsp.DocumentURI]*document
}
type document struct {
// text content of the document from the last time when saved
text []string
// test content of the document while in editing(not been saved)
textInEdit []string
version int
}
func newWorkspaceStore(rootURI lsp.DocumentURI) *workspaceStore {
return &workspaceStore{
rootURI: rootURI,
documents: make(map[lsp.DocumentURI]*document),
}
}
// Store method is generally used to correspond to "textDocument/didOpen",
// this stores the initial state of the document when opened
func (ws *workspaceStore) Store(uri lsp.DocumentURI, content string, version int) {
text := SplitLines(content, true)
ws.documents[uri] = &document{
text: text,
textInEdit: text,
version: version,
}
}
// Update method corresponds to "textDocument/didSave".
// This updates the existing uri document in the store
func (ws *workspaceStore) Update(uri lsp.DocumentURI, content string) error {
text := SplitLines(content, true)
if _, ok := ws.documents[uri]; !ok {
return fmt.Errorf("document %s did not open", uri)
}
ws.documents[uri].text = text
ws.documents[uri].textInEdit = text
return nil
}
// Close method corresponds to "textDocument/didClose"
// This removes the document stored in workspaceStore
func (ws *workspaceStore) Close(uri lsp.DocumentURI) error {
if _, ok := ws.documents[uri]; !ok {
return fmt.Errorf("document %s did not open", uri)
}
ws.mu.Lock()
defer ws.mu.Unlock()
delete(ws.documents, uri)
return nil
}
// TrackEdit tracks the changes of the content for the targeting uri, and update the corresponding
func (ws *workspaceStore) TrackEdit(uri lsp.DocumentURI, contentChanges []lsp.TextDocumentContentChangeEvent, version int) error {
doc, ok := ws.documents[uri]
if !ok {
log.Error("document '%s' is not opened, edit did not apply", uri)
return nil
}
ws.mu.Lock()
defer ws.mu.Unlock()
for _, change := range contentChanges {
newText, err := ws.applyChange(doc.textInEdit, change)
if err != nil {
return err
}
doc.textInEdit = newText
}
doc.version = version
return nil
}
func (ws *workspaceStore) applyChange(text []string, change lsp.TextDocumentContentChangeEvent) ([]string, error) {
if change.Range == nil && change.RangeLength == 0 {
return text, nil // new full content
}
changeText := change.Text
startLine := change.Range.Start.Line
endLine := change.Range.End.Line
startCol := change.Range.Start.Character
endCol := change.Range.End.Character
var newText string
for i, line := range text {
if i < startLine || i > endLine {
newText += line
continue
}
if i == startLine {
newText += line[:startCol] + changeText
}
if i == endLine {
// Apparently, when you delete a whole line, intellij plugin sometimes sends the range like so:
// {startline: deletedline_index, startcol: 0}, {endline: nextline, endcol: len_of_deleted_line}...
if len(line)-1 < endCol && (len(text) != 1 && len(text[i-1])-1 == endCol) {
newText += line
} else {
newText += line[endCol:]
}
}
}
return SplitLines(newText, true), nil
}
// SplitLines splits a content with \n characters, and returns a slice of string
// if keepEnds is true, all lines will keep it's original splited character
func SplitLines(content string, keepEnds bool) []string {
splited := strings.Split(content, "\n")
if !keepEnds {
return splited
}
for i := range splited {
// Do not add endline character on the last empty line
if (i == len(splited)-1 && splited[i] == "") || len(splited) <= 1 {
continue
}
splited[i] += "\n"
}
return splited
}
// JoinLines concatenate a slice of string, removes the trailing "\n" if hadEnds is true
func JoinLines(text []string, hasEnds bool) string {
if !hasEnds {
concat := strings.Join(text, "\n")
return concat
}
newText := make([]string, len(text))
for i := range text {
if i == len(text)-1 && text[i] == "" {
newText[i] = text[i]
continue
}
newText[i] = strings.TrimSuffix(text[i], "\n")
}
return strings.Join(newText, "\n")
}