/
util.go
42 lines (33 loc) · 890 Bytes
/
util.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
package store
import (
"net/url"
"path/filepath"
"runtime"
"strings"
"go.lsp.dev/uri"
)
func uriToPath(docuri uri.URI) (string, error) {
parsed, err := url.Parse(docuri.Filename())
if err != nil {
return "", err
}
if runtime.GOOS == "windows" {
// In Windows "file:///c:/tmp/foo.md" is parsed to "/c:/tmp/foo.md".
// Strip the first character to get a valid path.
if strings.Contains(parsed.Path[1:], ":") {
// url.Parse() behaves differently with "file:///c:/..." and "file://c:/..."
return parsed.Path[1:], nil
}
// if the windows drive is not included in Path it will be in Host
return parsed.Host + "/" + parsed.Path[1:], nil
}
return parsed.Path, nil
}
func canonical(path string) (string, error) {
path = filepath.Clean(path)
resolvedPath, err := filepath.EvalSymlinks(path)
if err != nil {
return path, err
}
return resolvedPath, nil
}