Skip to content

Commit 49581cf

Browse files
committed
Add ProgressToken struct.
1 parent e36e59c commit 49581cf

File tree

3 files changed

+159
-11
lines changed

3 files changed

+159
-11
lines changed

LanguageServer.VsCode/Contracts/Client/IWindow.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ public interface IWindow
4949
Task LogMessage(MessageType type, string message);
5050

5151
[JsonRpcMethod("window/workDoneProgress/create")]
52-
Task CreateWorkDoneProgress(string token);
53-
54-
[JsonRpcMethod("window/workDoneProgress/create")]
55-
Task CreateWorkDoneProgress(int token);
52+
Task CreateWorkDoneProgress(ProgressToken token);
5653

5754
/// <summary>
5855
/// Report any kind of progress including work done progress (usually used to report progress in the user interface using a progress bar)
@@ -65,11 +62,7 @@ public interface IWindow
6562
/// out of band and also for notification.
6663
/// </remarks>
6764
[JsonRpcMethod("$/progress", IsNotification = true)]
68-
void ReportWorkDoneProgress(string token, WorkDoneProgress value);
69-
70-
/// <inheritdoc cref="ReportWorkDoneProgress(string, WorkDoneProgress)"/>
71-
[JsonRpcMethod("$/progress", IsNotification = true)]
72-
void ReportWorkDoneProgress(int token, WorkDoneProgress value);
65+
void ReportWorkDoneProgress(ProgressToken token, WorkDoneProgress value);
7366

7467
}
7568
}

LanguageServer.VsCode/Contracts/WorkDoneProgress.cs

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Reflection;
34
using System.Text;
5+
using LanguageServer.VsCode.Contracts.Client;
46
using Newtonsoft.Json;
57
using Newtonsoft.Json.Converters;
68
using Newtonsoft.Json.Linq;
@@ -197,7 +199,160 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
197199
/// <inheritdoc />
198200
public override bool CanConvert(Type objectType)
199201
{
200-
throw new NotImplementedException();
202+
return typeof(WorkDoneProgress).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo());
203+
}
204+
}
205+
206+
///// <summary>
207+
///// Provides an optional token that a server can use to report work done progress.
208+
///// </summary>
209+
//public interface IWorkDoneProgressParams
210+
//{
211+
// /// <summary>
212+
// /// An optional token that a server can use to report work done progress.
213+
// /// </summary>
214+
// [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
215+
// ProgressToken WorkDoneToken { get; set; }
216+
//}
217+
218+
///// <summary>
219+
///// Provides an a parameter literal used to pass a partial result token.
220+
///// </summary>
221+
//public interface IPartialResultParams
222+
//{
223+
// /// <summary>
224+
// /// An optional token that a server can use to report partial results (e.g. streaming) to the client.
225+
// /// </summary>
226+
// [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
227+
// ProgressToken PartialResultToken { get; set; }
228+
//}
229+
230+
/// <summary>
231+
/// Represents a <see cref="string"/>, <see cref="int"/> or <see cref="long"/> value indicating
232+
/// a token used to report progress.
233+
/// </summary>
234+
/// <seealso cref="WorkDoneProgress"/>
235+
/// <seealso cref="IWindow.CreateWorkDoneProgress"/>
236+
[JsonConverter(typeof(ProgressTokenJsonConverter))]
237+
public struct ProgressToken : IEquatable<ProgressToken>
238+
{
239+
240+
public static readonly ProgressToken Empty = default;
241+
242+
public ProgressToken(string value)
243+
{
244+
Value = value;
245+
}
246+
247+
public ProgressToken(int value)
248+
{
249+
Value = value;
250+
}
251+
252+
public ProgressToken(long value)
253+
{
254+
if (value >= int.MinValue && value <= int.MaxValue)
255+
Value = (int) value;
256+
else
257+
Value = value;
258+
}
259+
260+
public ProgressToken(object value)
261+
{
262+
if (value is null || value is string || value is int)
263+
Value = value;
264+
var longValue = Convert.ToInt64(value);
265+
if (longValue >= int.MinValue && longValue <= int.MaxValue)
266+
Value = (int)longValue;
267+
else
268+
Value = longValue;
269+
}
270+
271+
public object Value { get; }
272+
273+
public static implicit operator ProgressToken(string rhs)
274+
{
275+
return new ProgressToken(rhs);
276+
}
277+
278+
public static implicit operator ProgressToken(int rhs)
279+
{
280+
return new ProgressToken(rhs);
281+
}
282+
283+
public static implicit operator ProgressToken(long rhs)
284+
{
285+
return new ProgressToken(rhs);
286+
}
287+
288+
public static explicit operator string(ProgressToken rhs)
289+
{
290+
return (string) rhs.Value;
291+
}
292+
293+
public static explicit operator int(ProgressToken rhs)
294+
{
295+
return (int)rhs.Value;
296+
}
297+
298+
public static explicit operator long(ProgressToken rhs)
299+
{
300+
return (long)rhs.Value;
301+
}
302+
303+
/// <inheritdoc />
304+
public bool Equals(ProgressToken other)
305+
{
306+
return Equals(Value, other.Value);
307+
}
308+
309+
/// <inheritdoc />
310+
public override bool Equals(object obj)
311+
{
312+
return obj is ProgressToken other && Equals(other);
313+
}
314+
315+
/// <inheritdoc />
316+
public override int GetHashCode()
317+
{
318+
#if BCL_FEATURE_HASHCODE
319+
return HashCode.Combine(Value);
320+
#else
321+
return Value != null ? Value.GetHashCode() : 0;
322+
#endif
323+
}
324+
325+
public static bool operator ==(ProgressToken left, ProgressToken right)
326+
{
327+
return left.Equals(right);
328+
}
329+
330+
public static bool operator !=(ProgressToken left, ProgressToken right)
331+
{
332+
return !left.Equals(right);
333+
}
334+
}
335+
336+
public class ProgressTokenJsonConverter : JsonConverter
337+
{
338+
public override bool CanConvert(Type objectType)
339+
{
340+
return objectType == typeof(ProgressToken);
341+
}
342+
343+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
344+
{
345+
if (objectType != typeof(ProgressToken))
346+
throw new NotSupportedException();
347+
return new ProgressToken(reader.Value);
348+
}
349+
350+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
351+
{
352+
if (value is ProgressToken token)
353+
writer.WriteValue(token.Value);
354+
else
355+
throw new NotSupportedException();
201356
}
202357
}
203358

LanguageServer.VsCode/LanguageServer.VsCode.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
</ItemGroup>
3838

3939
<PropertyGroup Condition=" $([System.Text.RegularExpressions.Regex]::IsMatch($(TargetFramework), `netstandard2.[1-9]$`)) ">
40-
<DefineConstants>$(DefineConstants);BCL_FEATURE_SPAN</DefineConstants>
40+
<DefineConstants>$(DefineConstants);BCL_FEATURE_SPAN;BCL_FEATURE_HASHCODE;LANG_FEATURE_INTERFACE_DEFAULT_METHODS</DefineConstants>
4141
</PropertyGroup>
4242

4343
</Project>

0 commit comments

Comments
 (0)