forked from sourcegraph/lsp-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uris.go
195 lines (166 loc) · 5.11 KB
/
uris.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
package main
import (
"fmt"
"log"
"net/url"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"github.com/sourcegraph/go-langserver/pkg/lsp"
"github.com/pkg/errors"
)
func (p *cloneProxy) cloneWorkspaceToCache(globs []string) error {
fs := &remoteFS{conn: p.client, traceID: p.sessionID.String()}
err := fs.Clone(p.ctx, p.workspaceCacheDir(), globs)
if err != nil {
return errors.Wrap(err, "failed to clone workspace to local cache")
}
log.Printf("Cloned workspace to %s", p.workspaceCacheDir())
return nil
}
func (p *cloneProxy) cleanWorkspaceCache() error {
log.Printf("Removing workspace cache from %s", p.workspaceCacheDir())
return os.RemoveAll(p.workspaceCacheDir())
}
func (p *cloneProxy) removeWorkspaceCache(workspaceName string) error {
path := filepath.Join(*cacheDir, p.sessionID.String(), workspaceName)
log.Printf("Removing workspace cache from %s", path)
return os.RemoveAll(path)
}
func (p *cloneProxy) workspaceCacheDir() string {
return filepath.Join(*cacheDir, p.sessionID.String())
}
func clientToServerURI(uri lsp.DocumentURI, sysCacheDir string) lsp.DocumentURI {
// sysCacheDir needs to be converted from a local path to a URI path
cacheDir := filepath.ToSlash(sysCacheDir)
parsedURI, err := url.Parse(string(uri))
if err != nil {
log.Println(fmt.Sprintf("clientToServerURI: err when trying to parse uri %s", uri), err)
return uri
}
if !probablyFileURI(parsedURI) {
return uri
}
// We assume that any path provided by the client to the server
// is a project path that is relative to '/'
parsedURI.Path = path.Join(cacheDir, parsedURI.Path)
return lsp.DocumentURI(parsedURI.String())
}
func serverToClientURI(uri lsp.DocumentURI, sysCacheDir string) lsp.DocumentURI {
// sysCacheDir needs to be converted from a local path to a URI path
cacheDir := filepath.ToSlash(sysCacheDir)
parsedURI, err := url.Parse(string(uri))
if err != nil {
log.Println(fmt.Sprintf("serverToClientURI: err when trying to parse uri %s", uri), err)
return uri
}
if !probablyFileURI(parsedURI) {
return uri
}
// Only rewrite uris that point to a location in the workspace cache. If it does
// point to a cache location, then we assume that the path points to a location in the
// project.
if pathHasPrefix(parsedURI.Path, cacheDir) {
parsedURI.Path = path.Join("/", pathTrimPrefix(parsedURI.Path, cacheDir))
}
return lsp.DocumentURI(parsedURI.String())
}
func probablyFileURI(candidate *url.URL) bool {
if !(candidate.Scheme == "" || candidate.Scheme == "file") {
return false
}
if candidate.Path == "" {
return false
}
return true
}
func pathHasPrefix(s, prefix string) bool {
return rawHasPrefix(s, prefix, "/")
}
func filepathHasPrefix(s, prefix string) bool {
return rawHasPrefix(s, prefix, string(os.PathSeparator))
}
// adapted from sourcegraph/go-langserver/util.go
func rawHasPrefix(s, prefix, pathSep string) bool {
var prefixSlash string
if prefix != "" && !strings.HasSuffix(prefix, pathSep) {
prefixSlash = prefix + pathSep
}
return s == prefix || strings.HasPrefix(s, prefixSlash)
}
func pathTrimPrefix(s, prefix string) string {
return rawTrimPrefix(s, prefix, "/")
}
func filepathTrimPrefix(s, prefix string) string {
return rawTrimPrefix(s, prefix, string(os.PathSeparator))
}
// adapted from sourcegraph/go-langserver/util.go
func rawTrimPrefix(s, prefix, pathSep string) string {
if s == prefix {
return ""
}
if !strings.HasSuffix(prefix, pathSep) {
prefix += pathSep
}
return strings.TrimPrefix(s, prefix)
}
// WalkURIFields walks the LSP params/result object for fields
// containing document URIs.
//
// If update is non-nil, it updates all document URIs in an LSP
// params/result with the value of f(existingURI). Callers can use
// this to rewrite paths in the params/result.
//
// TODO(sqs): does not support WorkspaceEdit (with a field whose
// TypeScript type is {[uri: string]: TextEdit[]}.
func WalkURIFields(o interface{}, update func(lsp.DocumentURI) lsp.DocumentURI) {
var walk func(o interface{})
walk = func(o interface{}) {
switch o := o.(type) {
case map[string]interface{}:
for k, v := range o { // Location, TextDocumentIdentifier, TextDocumentItem, etc.
// Handling "rootPath" and "rootUri" special cases the initialize method.
if k == "uri" || k == "rootPath" || k == "rootUri" {
s, ok := v.(string)
if !ok {
s2, ok2 := v.(lsp.DocumentURI)
s = string(s2)
ok = ok2
}
if ok {
if update != nil {
o[k] = update(lsp.DocumentURI(s))
}
continue
}
}
walk(v)
}
case []interface{}: // Location[]
for _, v := range o {
walk(v)
}
default: // structs with a "URI" field
rv := reflect.ValueOf(o)
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
if rv.Kind() == reflect.Struct {
if fv := rv.FieldByName("URI"); fv.Kind() == reflect.String {
if update != nil {
fv.SetString(string(update(lsp.DocumentURI(fv.String()))))
}
}
for i := 0; i < rv.NumField(); i++ {
fv := rv.Field(i)
if fv.Kind() == reflect.Ptr || fv.Kind() == reflect.Struct || fv.Kind() == reflect.Array {
walk(fv.Interface())
}
}
}
}
}
walk(o)
}