|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using Newtonsoft.Json; |
| 5 | + |
| 6 | +namespace LanguageServer.VsCode.Contracts |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// A `MarkupContent` literal represents a string value which content is interpreted base on its |
| 10 | + /// kind flag. Currently the protocol supports <c>plaintext</c> and <c>markdown</c> as markup kinds. |
| 11 | + /// If the kind is `markdown` then the value can contain fenced code blocks like in GitHub issues. |
| 12 | + /// See <a href="https://help.github.com/articles/creating-and-highlighting-code-blocks/#syntax-highlighting">https://help.github.com/articles/creating-and-highlighting-code-blocks/#syntax-highlighting</a>. |
| 13 | + /// </summary> |
| 14 | + [JsonObject] |
| 15 | + public class MarkupContent |
| 16 | + { |
| 17 | + |
| 18 | + /// <exception cref="ArgumentNullException"><paramref name="kind"/> is <c>null</c>.</exception> |
| 19 | + public MarkupContent(MarkupKind kind, string value) |
| 20 | + { |
| 21 | + Kind = kind ?? throw new ArgumentNullException(nameof(kind)); |
| 22 | + Value = value; |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary>The type of the Markup.</summary> |
| 26 | + [JsonProperty] |
| 27 | + public MarkupKind Kind { get; } |
| 28 | + |
| 29 | + /// <summary>The content itself.</summary> |
| 30 | + [JsonProperty] |
| 31 | + public string Value { get; } |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Describes the content type that a client supports in various |
| 37 | + /// result literals like <see cref="Hover"/>, <see cref="ParameterInformation"/> or <see cref="CompletionItem"/>. |
| 38 | + /// </summary> |
| 39 | + /// <remarks> |
| 40 | + /// Please note that <see cref="MarkupKind"/> must not start with a `$`. This kinds |
| 41 | + /// are reserved for internal usage. |
| 42 | + /// </remarks> |
| 43 | + [JsonConverter(typeof(MarkupKindJsonConverter))] |
| 44 | + public sealed class MarkupKind : IEquatable<MarkupKind> |
| 45 | + { |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Plain text is supported as a content format. |
| 49 | + /// </summary> |
| 50 | + public static MarkupKind PlainText{ get; } = new MarkupKind("plaintext"); |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Markdown is supported as a content format. |
| 54 | + /// </summary> |
| 55 | + public static MarkupKind Markdown { get; } = new MarkupKind("markdown"); |
| 56 | + |
| 57 | + public MarkupKind(string value) |
| 58 | + { |
| 59 | + Value = value; |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Gets the string representation of the markup kind. |
| 64 | + /// </summary> |
| 65 | + public string Value { get; } |
| 66 | + |
| 67 | + /// <inheritdoc /> |
| 68 | + public override string ToString() |
| 69 | + { |
| 70 | + return Value; |
| 71 | + } |
| 72 | + |
| 73 | + /// <inheritdoc /> |
| 74 | + public bool Equals(MarkupKind other) |
| 75 | + { |
| 76 | + if (ReferenceEquals(null, other)) return false; |
| 77 | + if (ReferenceEquals(this, other)) return true; |
| 78 | + return string.Equals(Value, other.Value); |
| 79 | + } |
| 80 | + |
| 81 | + /// <inheritdoc /> |
| 82 | + public override bool Equals(object obj) |
| 83 | + { |
| 84 | + if (ReferenceEquals(null, obj)) return false; |
| 85 | + if (ReferenceEquals(this, obj)) return true; |
| 86 | + return obj is MarkupKind && Equals((MarkupKind)obj); |
| 87 | + } |
| 88 | + |
| 89 | + /// <inheritdoc /> |
| 90 | + public override int GetHashCode() |
| 91 | + { |
| 92 | + return Value.GetHashCode(); |
| 93 | + } |
| 94 | + |
| 95 | + public static bool operator ==(MarkupKind left, MarkupKind right) |
| 96 | + { |
| 97 | + return Equals(left, right); |
| 98 | + } |
| 99 | + |
| 100 | + public static bool operator !=(MarkupKind left, MarkupKind right) |
| 101 | + { |
| 102 | + return !Equals(left, right); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Used to convert <see cref="MarkupKind"/> into JSON. |
| 108 | + /// </summary> |
| 109 | + public class MarkupKindJsonConverter : JsonConverter |
| 110 | + { |
| 111 | + |
| 112 | + /// <inheritdoc /> |
| 113 | + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
| 114 | + { |
| 115 | + var v = (MarkupKind)value; |
| 116 | + writer.WriteValue(v); |
| 117 | + } |
| 118 | + |
| 119 | + /// <inheritdoc /> |
| 120 | + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) |
| 121 | + { |
| 122 | + var v = reader.ReadAsString(); |
| 123 | + if (v == null) return null; |
| 124 | + if (v == "plaintext") return MarkupKind.PlainText; |
| 125 | + if (v == "markdown") return MarkupKind.Markdown; |
| 126 | + return new MarkupKind(v); |
| 127 | + } |
| 128 | + |
| 129 | + /// <inheritdoc /> |
| 130 | + public override bool CanConvert(Type objectType) |
| 131 | + { |
| 132 | + return objectType == typeof(MarkupKind); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments