|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
| 3 | +using System.Reflection; |
3 | 4 | using System.Text;
|
| 5 | +using LanguageServer.VsCode.Contracts.Client; |
4 | 6 | using Newtonsoft.Json;
|
5 | 7 | using Newtonsoft.Json.Converters;
|
6 | 8 | using Newtonsoft.Json.Linq;
|
@@ -197,7 +199,160 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
|
197 | 199 | /// <inheritdoc />
|
198 | 200 | public override bool CanConvert(Type objectType)
|
199 | 201 | {
|
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(); |
201 | 356 | }
|
202 | 357 | }
|
203 | 358 |
|
|
0 commit comments