Skip to content

Commit

Permalink
LSP Protocol: Support Uri in SumType
Browse files Browse the repository at this point in the history
  • Loading branch information
mhutch committed Aug 20, 2024
1 parent 2e3dc5f commit e89c402
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task CreatingDirectoryWatchRequestsDirectoryWatch()

var watcher = GetSingleFileWatcher(dynamicCapabilitiesRpcTarget);

Assert.Equal(tempDirectory.Path, watcher.GlobPattern.Second.BaseUri.First.LocalPath);
Assert.Equal(tempDirectory.Path, watcher.GlobPattern.Second.BaseUri.Second.LocalPath);
Assert.Equal("**/*", watcher.GlobPattern.Second.Pattern);

// Get rid of the registration and it should be gone again
Expand Down Expand Up @@ -98,7 +98,7 @@ public async Task CreatingFileWatchRequestsFileWatch()

var watcher = GetSingleFileWatcher(dynamicCapabilitiesRpcTarget);

Assert.Equal("Z:\\", watcher.GlobPattern.Second.BaseUri.First.LocalPath);
Assert.Equal("Z:\\", watcher.GlobPattern.Second.BaseUri.Second.LocalPath);
Assert.Equal("SingleFile.txt", watcher.GlobPattern.Second.Pattern);

// Get rid of the registration and it should be gone again
Expand Down
11 changes: 10 additions & 1 deletion src/LanguageServer/Protocol/Protocol/Converters/SumConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public SumTypeInfoCache(Type sumTypeType)

if (parameterTypeInfo.IsPrimitive ||
parameterTypeInfo == typeof(string) ||
parameterTypeInfo == typeof(Uri) ||
typeof(IStringEnum).IsAssignableFrom(parameterTypeInfo))
{
primitiveUnionTypeInfosSet ??= new List<UnionTypeInfo>();
Expand Down Expand Up @@ -249,7 +250,7 @@ public override T Read(ref Utf8JsonReader reader, Type objectType, JsonSerialize
}
}

throw new JsonException(LanguageServerProtocolResources.NoSumTypeMatch);
throw new JsonException($"No sum type match for {objectType}");
}

/// <inheritdoc/>
Expand All @@ -259,6 +260,13 @@ public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions

var sumValue = value.Value;

// behavior from DocumentUriConverter
if (sumValue is Uri)
{
writer.WriteStringValue(sumValue.ToString());
return;
}

if (sumValue != null)
{
JsonSerializer.Serialize(writer, sumValue, options);
Expand Down Expand Up @@ -288,6 +296,7 @@ private static bool IsTokenCompatibleWithType(ref Utf8JsonReader reader, SumConv
break;
case JsonTokenType.String:
isCompatible = unionTypeInfo.Type == typeof(string) ||
unionTypeInfo.Type == typeof(Uri) ||
typeof(IStringEnum).IsAssignableFrom(unionTypeInfo.Type);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class RelativePattern
/// </summary>
[JsonPropertyName("baseUri")]
[JsonRequired]
public SumType<Uri, WorkspaceFolder> BaseUri { get; init; }
public SumType<WorkspaceFolder, Uri> BaseUri { get; init; }

/// <summary>
/// The actual glob pattern. See <see href="https://microsoft.github.io/language-server-protocol/specifications/specification-current/#pattern">Glob Pattern</see> for more detail.
Expand Down

0 comments on commit e89c402

Please sign in to comment.