-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathDocumentUri.cs
469 lines (418 loc) · 17.9 KB
/
DocumentUri.cs
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Serialization.Converters;
namespace OmniSharp.Extensions.LanguageServer.Protocol
{
/// <summary>
/// Uniform Resource Identifier (URI) http://tools.ietf.org/html/rfc3986.
/// This class is a simple parser which creates the basic component parts
/// (http://tools.ietf.org/html/rfc3986#section-3) with minimal validation
/// and encoding.
///
/// ```txt
/// foo://example.com:8042/over/there?name=ferret#nose
/// \_/ \______________/\_________/ \_________/ \__/
/// | | | | |
/// scheme authority path query fragment
/// | _____________________|__
/// / \ / \
/// urn:example:animal:ferret:nose
/// ```
/// </summary>
/// <summary>
/// This class describes a document uri as defined by https://microsoft.github.io/language-server-protocol/specifications/specification-current/#uri
/// </summary>
/// <remarks>This exists because of some non-standard serialization in vscode around uris and .NET's behavior when deserializing those uris</remarks>
[JsonConverter(typeof(DocumentUriConverter))]
public partial class DocumentUri : IEquatable<DocumentUri>
{
/// <summary>
/// scheme is the "http' part of 'http://www.msft.com/some/path?query#fragment".
/// The part before the first colon.
/// </summary>
public string Scheme { get; }
/// <summary>
/// authority is the "www.msft.com' part of 'http://www.msft.com/some/path?query#fragment".
/// The part between the first double slashes and the next slash.
/// </summary>
public string Authority { get; }
/// <summary>
/// path is the "/some/path' part of 'http://www.msft.com/some/path?query#fragment".
/// </summary>
public string Path { get; }
/// <summary>
/// query is the "query' part of 'http://www.msft.com/some/path?query#fragment".
/// </summary>
public string Query { get; }
/// <summary>
/// fragment is the "fragment' part of 'http://www.msft.com/some/path?query#fragment".
/// </summary>
public string Fragment { get; }
/// <summary>
/// Convert the uri to a <see cref="Uri"/>
/// </summary>
/// <returns></returns>
/// <remarks>This will produce a uri where asian and cyrillic characters will be encoded</remarks>
public Uri ToUri()
{
if (Authority.IndexOf(":") > -1)
{
var parts = Authority.Split(':');
var host = parts[0];
var port = int.Parse(parts[1]);
return new UriBuilder() {
Scheme = Scheme,
Host = host,
Port = port,
Path = Path,
Fragment = Fragment,
Query = Query
}.Uri;
}
return new UriBuilder() {
Scheme = Scheme,
Host = Authority,
Path = Path,
Fragment = Fragment,
Query = Query
}.Uri;
}
// ---- printing/externalize ---------------------------
/// <summary>
/// Creates a string representation for this URI. It's guaranteed that calling
/// `URI.parse` with the result of this function creates an URI which is equal
/// to this URI.
///
/// * The result shall *not* be used for display purposes but for externalization or transport.
/// * The result will be encoded using the percentage encoding and encoding happens mostly
/// ignore the scheme-specific encoding rules.
///
/// @param skipEncoding Do not encode the result, default is `false`
/// </summary>
public override string ToString()
{
return _asFormatted(this, false);
}
public string ToUnencodedString()
{
return _asFormatted(this, true);
}
/// <summary>
/// Gets the file system path prefixed with / for unix platforms
/// </summary>
/// <returns></returns>
/// <remarks>This will not a uri encode asian and cyrillic characters</remarks>
public string GetFileSystemPath() => UriToFsPath(this, false);
/// <inheritdoc />
public bool Equals(DocumentUri other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
// It's possible mac can have case insensitive file systems... we can always come back and change this.
var comparison = RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
? StringComparison.Ordinal
: StringComparison.OrdinalIgnoreCase;
return string.Equals(Path, other.Path, comparison) &&
string.Equals(Scheme, other.Scheme, comparison) &&
string.Equals(Authority, other.Authority, comparison) &&
string.Equals(Query, other.Query, comparison) &&
string.Equals(Fragment, other.Fragment, comparison);
}
/// <inheritdoc />
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj.GetType() == GetType() && Equals((DocumentUri) obj);
}
/// <inheritdoc />
public override int GetHashCode()
{
// It's possible mac can have case insensitive file systems... we can always come back and change this.
var comparer = RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
? StringComparer.Ordinal
: StringComparer.OrdinalIgnoreCase;
unchecked
{
var hashCode = comparer.GetHashCode(Path);
hashCode = (hashCode * 397) ^ comparer.GetHashCode(Scheme);
hashCode = (hashCode * 397) ^ comparer.GetHashCode(Authority);
hashCode = (hashCode * 397) ^ comparer.GetHashCode(Query);
hashCode = (hashCode * 397) ^ comparer.GetHashCode(Fragment);
return hashCode;
}
}
/// <summary>
/// Deconstruct the document uri into it's different parts
/// </summary>
/// <param name="scheme"></param>
/// <param name="authority"></param>
/// <param name="path"></param>
/// <param name="query"></param>
/// <param name="fragment"></param>
/// <returns></returns>
public void Deconstruct(out string scheme, out string authority, out string path, out string query,
out string fragment)
{
scheme = Scheme;
authority = Authority;
path = Path;
query = Query;
fragment = Fragment;
}
/// <summary>
/// Check if two uris are equal
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator ==(DocumentUri left, DocumentUri right) => Equals(left, right);
/// <summary>
/// Check if two uris are not equal
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator !=(DocumentUri left, DocumentUri right) => !Equals(left, right);
/// <summary>
/// Convert this uri into a <see cref="string"/>.
/// </summary>
/// <param name="uri"></param>
/// <remarks>This is explicit because to string gives the schema string with file:// but if you want the file system you use <see cref="GetFileSystemPath()"/></remarks>
/// <returns></returns>
public static explicit operator string(DocumentUri uri) => uri.ToString();
/// <summary>
/// Convert this into a <see cref="Uri"/>.
/// </summary>
/// <param name="uri"></param>
/// <remarks>The uri class has issues with higher level utf8 characters such as asian and cyrillic characters</remarks>
/// <returns></returns>
public static explicit operator Uri(DocumentUri uri) => uri.ToUri();
/// <summary>
/// Automatically convert a string to a uri for both filesystem paths or uris in a string
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static implicit operator DocumentUri(string url) => From(url);
/// <summary>
/// Automatically convert a uri to a document uri
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public static implicit operator DocumentUri(Uri uri) => From(uri);
/// <summary>
/// Create a new document uri from the given <see cref="Uri"/>
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public static DocumentUri From(Uri uri)
{
if (uri.OriginalString.IndexOf(EncodeTable[CharCode.Colon], StringComparison.OrdinalIgnoreCase) > -1)
return From(uri.OriginalString);
if (uri.Scheme == Uri.UriSchemeFile)
{
return From(uri.LocalPath);
}
return From(uri.ToString());
}
/// <summary>
/// Create a new document uri from a string
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static DocumentUri From(string url)
{
if (string.IsNullOrWhiteSpace (url))
{
throw new UriFormatException("Given uri is null or empty");
}
if (url.StartsWith(@"\\") || (url.StartsWith("/")) || WindowsPath.IsMatch(url))
{
return File(url);
}
return Parse(url);
}
/// <summary>
/// Get the local file-system path for the specified document URI.
/// </summary>
/// <param name="textDocumentIdentifierParams">
/// The text document params object
/// </param>
/// <returns>
/// The file-system path, or <c>null</c> if the URI does not represent a file-system path.
/// </returns>
public static string GetFileSystemPath(ITextDocumentIdentifierParams textDocumentIdentifierParams) =>
GetFileSystemPath(textDocumentIdentifierParams.TextDocument.Uri);
/// <summary>
/// Get the local file-system path for the specified document URI.
/// </summary>
/// <param name="textDocumentIdentifier">
/// The text document identifier
/// </param>
/// <returns>
/// The file-system path, or <c>null</c> if the URI does not represent a file-system path.
/// </returns>
public static string GetFileSystemPath(TextDocumentIdentifier textDocumentIdentifier) =>
GetFileSystemPath(textDocumentIdentifier.Uri);
/// <summary>
/// Get the local file-system path for the specified document URI.
/// </summary>
/// <param name="documentUri">
/// The LSP document URI.
/// </param>
/// <returns>
/// The file-system path, or <c>null</c> if the URI does not represent a file-system path.
/// </returns>
public static string GetFileSystemPath(DocumentUri documentUri)
{
if (documentUri == null)
throw new ArgumentNullException(nameof(documentUri));
if (documentUri.Scheme != Uri.UriSchemeFile)
return null;
return documentUri.GetFileSystemPath();
}
/// <summary>
/// Convert a file-system path to an LSP document URI.
/// </summary>
/// <param name="fileSystemPath">
/// The file-system path.
/// </param>
/// <returns>
/// The LSP document URI.
/// </returns>
public static DocumentUri FromFileSystemPath(string fileSystemPath) => File(fileSystemPath);
private sealed class DocumentUriEqualityComparer : IEqualityComparer<DocumentUri>
{
public bool Equals(DocumentUri x, DocumentUri y)
{
if (ReferenceEquals(x, y)) return true;
if (ReferenceEquals(x, null)) return false;
if (ReferenceEquals(y, null)) return false;
return x.GetType() == y.GetType() && x.Equals(y);
}
public int GetHashCode(DocumentUri obj) => obj.GetHashCode();
}
/// <summary>
/// A default comparer that can be used for equality
/// </summary>
public static IEqualityComparer<DocumentUri> Comparer { get; } = new DocumentUriEqualityComparer();
// for a while we allowed uris *without* schemes and this is the migration
// for them, e.g. an uri without scheme and without strict-mode warns and falls
// back to the file-scheme. that should cause the least carnage and still be a
// clear warning
// implements a bit of https://tools.ietf.org/html/rfc3986#section-5
// reserved characters: https://tools.ietf.org/html/rfc3986#section-2.2
// --- decode
/// <summary>
/// @internal
/// </summary>
public DocumentUri(string scheme, string authority, string path, string query, string fragment,
bool? strict = null)
{
Scheme = SchemeFix(scheme, strict);
Authority = authority ?? Empty;
Path = ReferenceResolution(Scheme, path ?? Empty);
Query = query ?? Empty;
Fragment = fragment ?? Empty;
_validateUri(this, strict);
}
// ---- parse & validate ------------------------
/// <summary>
/// Creates a new URI from a string, e.g. `http://www.msft.com/some/path`,
/// `file:///usr/home`, or `scheme:with/path`.
///
/// @param value A string which represents an URI (see `URI#toString`).
/// </summary>
public static DocumentUri Parse(string value, bool strict = false)
{
if (string.IsNullOrWhiteSpace (value))
{
throw new UriFormatException("Given uri is null or empty");
}
var match = Regexp.Match(value);
if (!match.Success)
{
return new DocumentUri(Empty, Empty, Empty, Empty, Empty);
}
return new DocumentUri(
match.Groups[2].Value ?? Empty,
PercentDecode(match.Groups[4].Value ?? Empty),
PercentDecode(match.Groups[5].Value ?? Empty),
PercentDecode(match.Groups[7].Value ?? Empty),
PercentDecode(match.Groups[9].Value ?? Empty),
strict
);
}
/// <summary>
/// Creates a new URI from a file system path, e.g. `c:\my\files`,
/// `/usr/home`, or `\\server\share\some\path`.
///
/// The *difference* between `URI#parse` and `URI#file` is that the latter treats the argument
/// as path, not as stringified-uri. E.g. `URI.file(path)` is **not the same as**
/// `URI.parse("file://" + path)` because the path might contain characters that are
/// interpreted (# and ?). See the following sample:
///
/// @param path A file system path (see `URI#fsPath`)
/// </summary>
public static DocumentUri File(string path)
{
if (string.IsNullOrWhiteSpace (path))
{
throw new UriFormatException("Given path is null or empty");
}
var authority = Empty;
// normalize to fwd-slashes on windows,
// on other systems bwd-slashes are valid
// filename character, eg /f\oo/ba\r.txt
if (path[0] != Slash)
{
path = path.Replace('\\', Slash);
}
// check for authority as used in UNC shares
// or use the path as given
if (path[0] == Slash && path[1] == Slash)
{
var idx = path.IndexOf(Slash, 2);
if (idx == -1)
{
authority = path.Substring(2);
path = StrSlash;
}
else
{
authority = path.Substring(2, idx - 2);
path = path.Substring(idx);
if (string.IsNullOrWhiteSpace(path)) path = StrSlash;
}
}
if (path.IndexOf("%3A", StringComparison.OrdinalIgnoreCase) > -1)
{
path = path.Replace("%3a", ":").Replace("%3A", ":");
}
return new DocumentUri("file", authority, path, Empty, Empty, null);
}
public DocumentUri With(DocumentUriComponents components)
{
return new DocumentUri(
components.Scheme ?? Scheme,
components.Authority ?? Authority,
components.Path ?? Path,
components.Query ?? Query,
components.Fragment ?? Fragment
);
}
public static DocumentUri From(DocumentUriComponents components)
{
return new DocumentUri(
components.Scheme ?? string.Empty,
components.Authority ?? string.Empty,
components.Path ?? string.Empty,
components.Query ?? string.Empty,
components.Fragment ?? string.Empty
);
}
}
}