-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathuri.go
150 lines (126 loc) · 3.91 KB
/
uri.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
//
// Copyright 2024 Cristian Maglie. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package lsp
import (
"fmt"
"net/url"
"path/filepath"
"regexp"
"strings"
"unicode"
"github.com/arduino/go-paths-helper"
"go.bug.st/json"
)
type URI string
// DocumentURI Many of the interfaces contain fields that correspond to the URI of a document.
// For clarity, the type of such a field is declared as a DocumentUri. Over the wire, it will
// still be transferred as a string, but this guarantees that the contents of that string
// can be parsed as a valid URI.
type DocumentURI struct {
url url.URL
}
// NilURI is the empty DocumentURI
var NilURI = DocumentURI{}
// for example, `"/c:"` or `"/A:"`
var expDriveWithLeadingSlashID = regexp.MustCompile("^/[a-zA-Z]:")
// for example, `"C:"` or `"A:"`
var expUppercaseDriveID = regexp.MustCompile("^[A-Z]:")
// AsPath convert the DocumentURI to a paths.Path
func (uri DocumentURI) AsPath() *paths.Path {
return paths.New(uri.unbox()).Canonical()
}
// unbox convert the DocumentURI to a file path string
func (uri DocumentURI) unbox() string {
path := uri.url.Path
if expDriveWithLeadingSlashID.MatchString(path) {
return path[1:]
}
return path
}
// Converts `"C:"` to `"c:"` to be compatible with VS Code URI's drive letter casing
// https://github.com/Microsoft/vscode/issues/68325#issuecomment-462239992
func lowercaseDriveSegment(pathSegment string) string {
if expUppercaseDriveID.MatchString(pathSegment) {
chars := []rune(pathSegment)
chars[0] = unicode.ToLower(chars[0])
return string(chars)
}
return pathSegment
}
func (uri DocumentURI) String() string {
return uri.url.String()
}
// Ext returns the extension of the file pointed by the URI
func (uri DocumentURI) Ext() string {
return filepath.Ext(uri.unbox())
}
// NewDocumentURIFromPath create a DocumentURI from the given Path object
func NewDocumentURIFromPath(path *paths.Path) DocumentURI {
return NewDocumentURI(path.String())
}
var toSlash = filepath.ToSlash
// NewDocumentURI create a DocumentURI from the given string path
func NewDocumentURI(path string) DocumentURI {
// transform path into URI
path = toSlash(path)
if len(path) == 0 || path[0] != '/' {
path = "/" + path
}
segments := strings.Split(path, "/")
encodedSegments := make([]string, len(segments))
for i, segment := range segments {
if len(segment) == 0 {
encodedSegments[i] = segment
} else {
segment = lowercaseDriveSegment(segment)
segment = url.QueryEscape(segment)
// Spaces must be turned into `%20`. Otherwise, `url.QueryEscape`` encodes them to `+`.
encodedSegments[i] = strings.ReplaceAll(segment, "+", "%20")
}
}
urlPath := strings.Join(encodedSegments, "/")
uri, err := NewDocumentURIFromURL("file://" + urlPath)
if err != nil {
panic(err)
}
return uri
}
// NewDocumentURIFromURL converts an URL into a DocumentURI
func NewDocumentURIFromURL(inURL string) (DocumentURI, error) {
uri, err := url.Parse(inURL)
if err != nil {
return NilURI, err
}
return DocumentURI{url: *uri}, nil
}
// UnmarshalJSON implements json.Unmarshaller interface
func (uri *DocumentURI) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("expected JSON string for DocumentURI: %s", err)
}
newDocURI, err := NewDocumentURIFromURL(s)
if err != nil {
return fmt.Errorf("parsing DocumentURI: %s", err)
}
*uri = newDocURI
return nil
}
func (uri *DocumentURI) UnmarshalText(text []byte) error {
newDocURI, err := NewDocumentURIFromURL(string(text))
if err != nil {
return fmt.Errorf("parsing DocumentURI: %s", err)
}
*uri = newDocURI
return nil
}
// MarshalJSON implements json.Marshaller interface
func (uri DocumentURI) MarshalJSON() ([]byte, error) {
return json.Marshal(uri.url.String())
}
func (uri DocumentURI) MarshalText() (text []byte, err error) {
return []byte(uri.String()), nil
}