-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #196 from TylerLeonhardt/workaround-unicode-charac…
…ters-uri-bug Workaround Unicode characters in URIs .NET bug
- Loading branch information
Showing
19 changed files
with
379 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/Protocol/Serialization/Converters/AbsoluteUriKeyConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
|
||
namespace OmniSharp.Extensions.LanguageServer.Protocol.Serialization.Converters | ||
{ | ||
class AbsoluteUriKeyConverter<TValue> : JsonConverter<Dictionary<Uri, TValue>> | ||
{ | ||
public override Dictionary<Uri, TValue> ReadJson( | ||
JsonReader reader, | ||
Type objectType, | ||
Dictionary<Uri, TValue> existingValue, | ||
bool hasExistingValue, | ||
JsonSerializer serializer) | ||
{ | ||
if (reader.TokenType != JsonToken.StartObject) | ||
{ | ||
throw new JsonException(); | ||
} | ||
|
||
var dictionary = new Dictionary<Uri, TValue>(); | ||
|
||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonToken.EndObject) | ||
{ | ||
return dictionary; | ||
} | ||
|
||
// Get the key. | ||
if (reader.TokenType != JsonToken.PropertyName) | ||
{ | ||
throw new JsonSerializationException($"The token type must be a property name. Given {reader.TokenType.ToString()}"); | ||
} | ||
|
||
// Get the stringified Uri. | ||
var key = new Uri((string)reader.Value, UriKind.RelativeOrAbsolute); | ||
if (!key.IsAbsoluteUri) | ||
{ | ||
throw new JsonSerializationException($"The Uri must be absolute. Given: {reader.Value}"); | ||
} | ||
|
||
// Get the value. | ||
reader.Read(); | ||
var value = serializer.Deserialize<TValue>(reader); | ||
|
||
// Add to dictionary. | ||
dictionary.Add(key, value); | ||
} | ||
|
||
throw new JsonException(); | ||
} | ||
|
||
public override void WriteJson( | ||
JsonWriter writer, | ||
Dictionary<Uri, TValue> value, | ||
JsonSerializer serializer) | ||
{ | ||
writer.WriteStartObject(); | ||
|
||
foreach (var kvp in value) | ||
{ | ||
var uri = kvp.Key; | ||
if (!uri.IsAbsoluteUri) | ||
{ | ||
throw new JsonSerializationException("The URI value must be an absolute Uri. Relative URI instances are not allowed."); | ||
} | ||
|
||
if (uri.IsFile) | ||
{ | ||
// First add the file scheme and :// | ||
var builder = new StringBuilder(uri.Scheme) | ||
.Append("://"); | ||
|
||
// UNC file paths use the Host | ||
if (uri.HostNameType != UriHostNameType.Basic) | ||
{ | ||
builder.Append(uri.Host); | ||
} | ||
|
||
// Paths that start with a drive letter don't have a slash in the PathAndQuery | ||
// but they need it in the final result. | ||
if (uri.PathAndQuery[0] != '/') | ||
{ | ||
builder.Append('/'); | ||
} | ||
|
||
// Lastly add the remaining parts of the URL | ||
builder.Append(uri.PathAndQuery); | ||
writer.WritePropertyName(builder.ToString()); | ||
} | ||
else | ||
{ | ||
writer.WritePropertyName(uri.AbsoluteUri); | ||
} | ||
|
||
serializer.Serialize(writer, kvp.Value); | ||
} | ||
|
||
writer.WriteEndObject(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
test/Lsp.Tests/Models/ApplyWorkspaceEditParamsTests_$NonStandardCharactersTest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"edit": { | ||
"changes": { | ||
"file:///abc/123/M%C3%B6rk%C3%B6.cs": [ | ||
{ | ||
"range": { | ||
"start": { | ||
"line": 1, | ||
"character": 1 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"character": 2 | ||
} | ||
}, | ||
"newText": "new text" | ||
}, | ||
{ | ||
"range": { | ||
"start": { | ||
"line": 3, | ||
"character": 3 | ||
}, | ||
"end": { | ||
"line": 4, | ||
"character": 4 | ||
} | ||
}, | ||
"newText": "new text2" | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.