@@ -81,34 +81,30 @@ func LanguageKindToScriptKind(languageID lsproto.LanguageKind) core.ScriptKind {
81
81
}
82
82
83
83
func DocumentURIToFileName (uri lsproto.DocumentUri ) string {
84
- parsed := core . Must ( url . Parse ( string (uri )))
85
- if parsed . Scheme == "file" {
84
+ if strings . HasPrefix ( string (uri ), "file://" ) {
85
+ parsed := core . Must ( url . Parse ( string ( uri )))
86
86
if parsed .Host != "" {
87
87
return "//" + parsed .Host + parsed .Path
88
88
}
89
89
return fixWindowsURIPath (parsed .Path )
90
90
}
91
91
92
- authority := parsed .Host
93
- if authority == "" {
94
- authority = "ts-nul-authority"
95
- }
96
- path := parsed .Path
97
- if path == "" {
98
- path = parsed .Opaque
99
- }
100
- if ! strings .HasPrefix (path , "/" ) {
101
- path = "/" + path
102
- }
103
- path = fixWindowsURIPath (path )
104
- if ! strings .HasPrefix (path , "/" ) {
105
- path = "/" + path
92
+ // Leave all other URIs escaped so we can round-trip them.
93
+
94
+ scheme , path , ok := strings .Cut (string (uri ), ":" )
95
+ if ! ok {
96
+ panic (fmt .Sprintf ("invalid URI: %s" , uri ))
106
97
}
107
- fragment := parsed .Fragment
108
- if fragment != "" {
109
- fragment = "#" + fragment
98
+
99
+ authority := "ts-nul-authority"
100
+ if rest , ok := strings .CutPrefix (path , "//" ); ok {
101
+ authority , path , ok = strings .Cut (rest , "/" )
102
+ if ! ok {
103
+ panic (fmt .Sprintf ("invalid URI: %s" , uri ))
104
+ }
110
105
}
111
- return fmt .Sprintf ("^/%s/%s%s%s" , parsed .Scheme , authority , path , fragment )
106
+
107
+ return "^/" + scheme + "/" + authority + "/" + path
112
108
}
113
109
114
110
func fixWindowsURIPath (path string ) string {
@@ -154,7 +150,18 @@ var extraEscapeReplacer = strings.NewReplacer(
154
150
155
151
func FileNameToDocumentURI (fileName string ) lsproto.DocumentUri {
156
152
if strings .HasPrefix (fileName , "^/" ) {
157
- return lsproto .DocumentUri (strings .Replace (fileName [2 :], "/ts-nul-authority/" , ":" , 1 ))
153
+ scheme , rest , ok := strings .Cut (fileName [2 :], "/" )
154
+ if ! ok {
155
+ panic ("invalid file name: " + fileName )
156
+ }
157
+ authority , path , ok := strings .Cut (rest , "/" )
158
+ if ! ok {
159
+ panic ("invalid file name: " + fileName )
160
+ }
161
+ if authority == "ts-nul-authority" {
162
+ return lsproto .DocumentUri (scheme + ":" + path )
163
+ }
164
+ return lsproto .DocumentUri (scheme + "://" + authority + "/" + path )
158
165
}
159
166
160
167
volume , fileName , _ := splitVolumePath (fileName )
0 commit comments