Skip to content

Commit 60483e7

Browse files
Added Container.From. do not serialize out the PrivateHandlerId if it's empty. StringOrMarkupContent implict conversion should be nullable.
1 parent 1c5e7dc commit 60483e7

File tree

6 files changed

+111
-17
lines changed

6 files changed

+111
-17
lines changed

src/Dap.Protocol/Models/Container.cs

+33
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,39 @@
66

77
namespace OmniSharp.Extensions.DebugAdapter.Protocol.Models
88
{
9+
public static class Container
10+
{
11+
[return: NotNullIfNotNull("items")]
12+
public static Container<T>? From<T>(IEnumerable<T>? items) => items switch {
13+
not null => new Container<T>(items),
14+
_ => null
15+
};
16+
17+
[return: NotNullIfNotNull("items")]
18+
public static Container<T>? From<T>(params T[] items) => items switch {
19+
not null => new Container<T>(items),
20+
_ => null
21+
};
22+
23+
[return: NotNullIfNotNull("items")]
24+
public static Container<T>? From<T>(List<T>? items) => items switch {
25+
not null => new Container<T>(items),
26+
_ => null
27+
};
28+
29+
[return: NotNullIfNotNull("items")]
30+
public static Container<T>? From<T>(in ImmutableArray<T>? items) => items switch {
31+
not null => new Container<T>(items),
32+
_ => null
33+
};
34+
35+
[return: NotNullIfNotNull("items")]
36+
public static Container<T>? From<T>(ImmutableList<T>? items) => items switch {
37+
not null => new Container<T>(items),
38+
_ => null
39+
};
40+
}
41+
942
public class Container<T> : ContainerBase<T>
1043
{
1144
public Container() : this(Enumerable.Empty<T>())

src/Protocol/Models/Container.cs

+33
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,39 @@
66

77
namespace OmniSharp.Extensions.LanguageServer.Protocol.Models
88
{
9+
public static class Container
10+
{
11+
[return: NotNullIfNotNull("items")]
12+
public static Container<T>? From<T>(IEnumerable<T>? items) => items switch {
13+
not null => new Container<T>(items),
14+
_ => null
15+
};
16+
17+
[return: NotNullIfNotNull("items")]
18+
public static Container<T>? From<T>(params T[] items) => items switch {
19+
not null => new Container<T>(items),
20+
_ => null
21+
};
22+
23+
[return: NotNullIfNotNull("items")]
24+
public static Container<T>? From<T>(List<T>? items) => items switch {
25+
not null => new Container<T>(items),
26+
_ => null
27+
};
28+
29+
[return: NotNullIfNotNull("items")]
30+
public static Container<T>? From<T>(in ImmutableArray<T>? items) => items switch {
31+
not null => new Container<T>(items),
32+
_ => null
33+
};
34+
35+
[return: NotNullIfNotNull("items")]
36+
public static Container<T>? From<T>(ImmutableList<T>? items) => items switch {
37+
not null => new Container<T>(items),
38+
_ => null
39+
};
40+
}
41+
942
public class Container<T> : ContainerBase<T>
1043
{
1144
public Container() : this(Enumerable.Empty<T>())

src/Protocol/Models/StringOrMarkupContent.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace OmniSharp.Extensions.LanguageServer.Protocol.Models
66
{
7-
[JsonConverter(typeof(StringOrMarkupContentConverter))]
7+
[JsonConverter(typeof( StringOrMarkupContentConverter))]
88
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")]
99
public record StringOrMarkupContent
1010
{
@@ -17,9 +17,9 @@ public record StringOrMarkupContent
1717
public MarkupContent? MarkupContent { get; }
1818
public bool HasMarkupContent => String == null;
1919

20-
public static implicit operator StringOrMarkupContent(string value) => new StringOrMarkupContent(value);
20+
public static implicit operator StringOrMarkupContent?(string? value) => value is null ? null : new StringOrMarkupContent(value);
2121

22-
public static implicit operator StringOrMarkupContent(MarkupContent markupContent) => new StringOrMarkupContent(markupContent);
22+
public static implicit operator StringOrMarkupContent?(MarkupContent? markupContent) => markupContent is null ? null : new StringOrMarkupContent(markupContent);
2323

2424
private string DebuggerDisplay => $"{( HasString ? String : HasMarkupContent ? MarkupContent!.ToString() : string.Empty )}";
2525

src/Server/Pipelines/ResolveCommandPipeline.cs

+33-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class ResolveCommandPipeline<TRequest, TResponse> : IPipelineBehavior<TRe
2121
public ResolveCommandPipeline(IRequestContext context, ILogger<ResolveCommandPipeline<TRequest, TResponse>> logger)
2222
{
2323
_logger = logger;
24-
_descriptor = (context.Descriptor as ILspHandlerDescriptor)!;
24+
_descriptor = ( context.Descriptor as ILspHandlerDescriptor )!;
2525
}
2626

2727
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
@@ -40,15 +40,42 @@ public async Task<TResponse> Handle(TRequest request, CancellationToken cancella
4040
);
4141
foreach (var item in canBeResolvedItems)
4242
{
43-
item.SetRawData(item.Data ?? new JObject());
44-
if (item.Data is JObject o)
45-
{
46-
o[Constants.PrivateHandlerId] = id;
47-
}
43+
UpdatePrivateHandlerId(item, id);
4844
}
4945
}
5046

47+
// Only pin the handler type, if we know the source handler (codelens) is also the resolver.
48+
if (response is ICanBeResolved canBeResolvedItem)
49+
{
50+
var id = _descriptor.Handler is ICanBeIdentifiedHandler resolved ? resolved.Id : Guid.Empty;
51+
_logger.LogTrace(
52+
"Updating Resolve items with wrapped data for {Method}:{Handler}",
53+
_descriptor.Method,
54+
_descriptor.ImplementationType.FullName
55+
);
56+
UpdatePrivateHandlerId(canBeResolvedItem, id);
57+
}
58+
5159
return response;
60+
61+
void UpdatePrivateHandlerId(ICanBeResolved item, Guid id)
62+
{
63+
item.SetRawData(item.Data ?? new JObject());
64+
if (item.Data is JObject o)
65+
{
66+
if (id == Guid.Empty)
67+
{
68+
if (o.ContainsKey(Constants.PrivateHandlerId))
69+
{
70+
o.Remove(Constants.PrivateHandlerId);
71+
}
72+
73+
return;
74+
}
75+
76+
o[Constants.PrivateHandlerId] = id;
77+
}
78+
}
5279
}
5380
}
5481
}

test/Lsp.Tests/CompletionItemKindTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FluentAssertions;
2+
using Lsp.Tests.Integration.Fixtures;
23
using OmniSharp.Extensions.LanguageServer.Protocol;
34
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
45
using OmniSharp.Extensions.LanguageServer.Protocol.Models;

test/Lsp.Tests/Matchers/ResolveCommandMatcherTests.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace Lsp.Tests.Matchers
2323
public class ResolveCommandMatcherTests : AutoTestBase
2424
{
2525
private readonly Guid _trueId = Guid.NewGuid();
26-
private readonly Guid _falseId = Guid.NewGuid();
26+
private readonly Guid _falseId = Guid.Empty;
2727

2828
public ResolveCommandMatcherTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
2929
{
@@ -247,7 +247,7 @@ public async Task Should_Update_CompletionItems_With_HandlerType()
247247
0,
248248
TextDocumentNames.Completion,
249249
"Key",
250-
(resolveHandler as IJsonRpcHandler)!,
250+
( resolveHandler as IJsonRpcHandler )!,
251251
resolveHandler.GetType(),
252252
typeof(CompletionParams),
253253
null,
@@ -298,7 +298,7 @@ public async Task Should_Update_CodeLensContainer_With_HandlerType()
298298
0,
299299
TextDocumentNames.CodeLens,
300300
"Key",
301-
(resolveHandler as IJsonRpcHandler)!,
301+
( resolveHandler as IJsonRpcHandler )!,
302302
resolveHandler.GetType(),
303303
typeof(CodeLensParams),
304304
null,
@@ -344,12 +344,12 @@ public async Task Should_Update_CodeLens_Removing_HandlerType()
344344
typeof(ICanBeIdentifiedHandler)
345345
}, new object[0]
346346
);
347-
( resolveHandler as ICanBeIdentifiedHandler )?.Id.Returns(_trueId);
347+
( resolveHandler as ICanBeIdentifiedHandler )?.Id.Returns(_falseId);
348348
var descriptor = new LspHandlerDescriptor(
349349
0,
350350
TextDocumentNames.CodeLensResolve,
351351
"Key",
352-
(resolveHandler as IJsonRpcHandler)!,
352+
( resolveHandler as IJsonRpcHandler )!,
353353
resolveHandler.GetType(),
354354
typeof(CodeLens),
355355
null,
@@ -368,15 +368,15 @@ public async Task Should_Update_CodeLens_Removing_HandlerType()
368368
var item = new CodeLens {
369369
Data = JObject.FromObject(new { hello = "world" })
370370
};
371-
item.Data[Constants.PrivateHandlerId] = Guid.Empty;
372371

373372
// When
374373
var response = await handlerMatcher.Handle(item, CancellationToken.None, () => Task.FromResult(item));
375374

376375
// Then
377376
response.Should().BeEquivalentTo(item, x => x.UsingStructuralRecordEquality());
378-
item.Data?[Constants.PrivateHandlerId].Value<Guid>().Should().BeEmpty();
379-
item.Data?["hello"].Value<string>().Should().Be("world");
377+
var data = item.Data.Should().BeOfType<JObject>().Subject;
378+
data.Should().NotContainKey(Constants.PrivateHandlerId);
379+
data["hello"].Value<string>().Should().Be("world");
380380
}
381381
}
382382
}

0 commit comments

Comments
 (0)