forked from sourcegraph/go-langserver
/
fs.go
289 lines (253 loc) · 8.31 KB
/
fs.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package langserver
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"sync"
"sync/atomic"
opentracing "github.com/opentracing/opentracing-go"
"github.com/sourcegraph/ctxvfs"
"github.com/sourcegraph/go-langserver/pkg/lsp"
"github.com/sourcegraph/jsonrpc2"
"github.com/sourcegraph/go-langserver/langserver/util"
)
// isFileSystemRequest returns if this is an LSP method whose sole
// purpose is modifying the contents of the overlay file system.
func isFileSystemRequest(method string) bool {
return method == "textDocument/didOpen" ||
method == "textDocument/didChange" ||
method == "textDocument/didClose" ||
method == "textDocument/didSave"
}
// handleFileSystemRequest handles textDocument/did* requests. The URI the
// request is for is returned. true is returned if a file was modified.
func (h *HandlerShared) handleFileSystemRequest(ctx context.Context, req *jsonrpc2.Request) (lsp.DocumentURI, bool, error) {
span := opentracing.SpanFromContext(ctx)
h.Mu.Lock()
overlay := h.overlay
h.Mu.Unlock()
do := func(uri lsp.DocumentURI, op func() error) (lsp.DocumentURI, bool, error) {
span.SetTag("uri", uri)
before, beforeErr := h.readFile(ctx, uri)
if beforeErr != nil && !os.IsNotExist(beforeErr) {
// There is no op that could succeed in this case. (Most
// commonly occurs when uri refers to a dir, not a file.)
return uri, false, beforeErr
}
err := op()
after, afterErr := h.readFile(ctx, uri)
if os.IsNotExist(beforeErr) && os.IsNotExist(afterErr) {
// File did not exist before or after so nothing has changed.
return uri, false, err
} else if afterErr != nil || beforeErr != nil {
// If an error prevented us from reading the file
// before or after then we assume the file changed to
// be conservative.
return uri, true, err
}
return uri, !bytes.Equal(before, after), err
}
switch req.Method {
case "textDocument/didOpen":
var params lsp.DidOpenTextDocumentParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return "", false, err
}
return do(params.TextDocument.URI, func() error {
overlay.didOpen(¶ms)
return nil
})
case "textDocument/didChange":
var params lsp.DidChangeTextDocumentParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return "", false, err
}
return do(params.TextDocument.URI, func() error {
return overlay.didChange(¶ms)
})
case "textDocument/didClose":
var params lsp.DidCloseTextDocumentParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return "", false, err
}
return do(params.TextDocument.URI, func() error {
overlay.didClose(¶ms)
return nil
})
case "textDocument/didSave":
var params lsp.DidSaveTextDocumentParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return "", false, err
}
// no-op
return params.TextDocument.URI, false, nil
default:
panic("unexpected file system request method: " + req.Method)
}
}
// overlay owns the overlay filesystem, as well as handling LSP filesystem
// requests.
type overlay struct {
mu sync.Mutex
m map[string][]byte
}
func newOverlay() *overlay {
return &overlay{m: make(map[string][]byte)}
}
// FS returns a vfs for the overlay.
func (h *overlay) FS() ctxvfs.FileSystem {
return ctxvfs.Sync(&h.mu, ctxvfs.Map(h.m))
}
func (h *overlay) didOpen(params *lsp.DidOpenTextDocumentParams) {
h.set(params.TextDocument.URI, []byte(params.TextDocument.Text))
}
func (h *overlay) didChange(params *lsp.DidChangeTextDocumentParams) error {
contents, found := h.get(params.TextDocument.URI)
if !found {
return fmt.Errorf("received textDocument/didChange for unknown file %q", params.TextDocument.URI)
}
contents, err := applyContentChanges(params.TextDocument.URI, contents, params.ContentChanges)
if err != nil {
return err
}
h.set(params.TextDocument.URI, contents)
return nil
}
// applyContentChanges updates `contents` based on `changes`
func applyContentChanges(uri lsp.DocumentURI, contents []byte, changes []lsp.TextDocumentContentChangeEvent) ([]byte, error) {
for _, change := range changes {
if change.Range == nil && change.RangeLength == 0 {
contents = []byte(change.Text) // new full content
continue
}
start, ok, why := offsetForPosition(contents, change.Range.Start)
if !ok {
return nil, fmt.Errorf("received textDocument/didChange for invalid position %q on %q: %s", change.Range.Start, uri, why)
}
var end int
if change.RangeLength != 0 {
end = start + int(change.RangeLength)
} else {
// RangeLength not specified, work it out from Range.End
end, ok, why = offsetForPosition(contents, change.Range.End)
if !ok {
return nil, fmt.Errorf("received textDocument/didChange for invalid position %q on %q: %s", change.Range.Start, uri, why)
}
}
if start < 0 || end > len(contents) || end < start {
return nil, fmt.Errorf("received textDocument/didChange for out of range position %q on %q", change.Range, uri)
}
// Try avoid doing too many allocations, so use bytes.Buffer
b := &bytes.Buffer{}
b.Grow(start + len(change.Text) + len(contents) - end)
b.Write(contents[:start])
b.WriteString(change.Text)
b.Write(contents[end:])
contents = b.Bytes()
}
return contents, nil
}
func (h *overlay) didClose(params *lsp.DidCloseTextDocumentParams) {
h.del(params.TextDocument.URI)
}
func uriToOverlayPath(uri lsp.DocumentURI) string {
if util.IsURI(uri) {
return strings.TrimPrefix(util.UriToPath(uri), "/")
}
return string(uri)
}
func (h *overlay) get(uri lsp.DocumentURI) (contents []byte, found bool) {
path := uriToOverlayPath(uri)
h.mu.Lock()
contents, found = h.m[path]
h.mu.Unlock()
return
}
func (h *overlay) set(uri lsp.DocumentURI, contents []byte) {
path := uriToOverlayPath(uri)
h.mu.Lock()
h.m[path] = contents
h.mu.Unlock()
}
func (h *overlay) del(uri lsp.DocumentURI) {
path := uriToOverlayPath(uri)
h.mu.Lock()
delete(h.m, path)
h.mu.Unlock()
}
func (h *HandlerShared) FilePath(uri lsp.DocumentURI) string {
path := util.UriToPath(uri)
if !strings.HasPrefix(path, "/") {
panic(fmt.Sprintf("bad uri %q (path %q MUST have leading slash; it can't be relative)", uri, path))
}
return path
}
func (h *HandlerShared) readFile(ctx context.Context, uri lsp.DocumentURI) ([]byte, error) {
if !util.IsURI(uri) {
return nil, &os.PathError{Op: "Open", Path: string(uri), Err: errors.New("unable to read out-of-workspace resource from virtual file system")}
}
h.Mu.Lock()
fs := h.FS
h.Mu.Unlock()
path := h.FilePath(uri)
contents, err := ctxvfs.ReadFile(ctx, fs, path)
if os.IsNotExist(err) {
if _, ok := err.(*os.PathError); !ok {
err = &os.PathError{Op: "Open", Path: path, Err: err}
}
}
return contents, err
}
// AtomicFS wraps a ctxvfs.NameSpace but is safe for concurrent calls to Bind
// while doing FS operations. It is optimized for "ReadMostly" use-case. IE
// Bind is a relatively rare call compared to actual FS operations.
type AtomicFS struct {
mu sync.Mutex // serialize calls to Bind (ie only used by writers)
v atomic.Value // stores the ctxvfs.NameSpace
}
// NewAtomicFS returns an AtomicFS with an empty wrapped ctxvfs.NameSpace
func NewAtomicFS() *AtomicFS {
fs := &AtomicFS{}
fs.v.Store(make(ctxvfs.NameSpace))
return fs
}
// Bind wraps ctxvfs.NameSpace.Bind
func (a *AtomicFS) Bind(old string, newfs ctxvfs.FileSystem, new string, mode ctxvfs.BindMode) {
// We do copy-on-write
a.mu.Lock()
defer a.mu.Unlock()
fs1 := a.v.Load().(ctxvfs.NameSpace)
fs2 := make(ctxvfs.NameSpace)
for k, v := range fs1 {
fs2[k] = v
}
fs2.Bind(old, newfs, new, mode)
a.v.Store(fs2)
}
func (*AtomicFS) String() string {
return "atomicfs"
}
// Open wraps ctxvfs.NameSpace.Open
func (a *AtomicFS) Open(ctx context.Context, path string) (ctxvfs.ReadSeekCloser, error) {
fs := a.v.Load().(ctxvfs.NameSpace)
return fs.Open(ctx, path)
}
// Stat wraps ctxvfs.NameSpace.Stat
func (a *AtomicFS) Stat(ctx context.Context, path string) (os.FileInfo, error) {
fs := a.v.Load().(ctxvfs.NameSpace)
return fs.Stat(ctx, path)
}
// Lstat wraps ctxvfs.NameSpace.Lstat
func (a *AtomicFS) Lstat(ctx context.Context, path string) (os.FileInfo, error) {
fs := a.v.Load().(ctxvfs.NameSpace)
return fs.Lstat(ctx, path)
}
// ReadDir wraps ctxvfs.NameSpace.ReadDir
func (a *AtomicFS) ReadDir(ctx context.Context, path string) ([]os.FileInfo, error) {
fs := a.v.Load().(ctxvfs.NameSpace)
return fs.ReadDir(ctx, path)
}