Skip to content

Commit

Permalink
Add InlayHintLabel structures
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed May 21, 2022
1 parent 3c280ff commit d66d80d
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Sources/LanguageServerProtocol/SupportTypes/InlayHint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,58 @@ public struct InlayHintKind: RawRepresentable, Codable, Hashable {
/// Swift, since Swift already has explicit parameter labels.
public static let parameter: InlayHintKind = InlayHintKind(rawValue: 2)
}

/// A hint's label, either being a single string or a composition of parts.
public enum InlayHintLabel: Codable, Hashable {
case parts([InlayHintLabelPart])
case string(String)

public init(from decoder: Decoder) throws {
if let parts = try? [InlayHintLabelPart](from: decoder) {
self = .parts(parts)
} else if let string = try? String(from: decoder) {
self = .string(string)
} else {
let context = DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Expected [InlayHintLabelPart] or String")
throw DecodingError.dataCorrupted(context)
}
}

public func encode(to encoder: Encoder) throws {
switch self {
case let .parts(parts):
try parts.encode(to: encoder)
case let .string(string):
try string.encode(to: encoder)
}
}
}

/// A part of an interactive or composite inlay hint label.
public struct InlayHintLabelPart: Codable, Hashable {
/// The value of this label part.
public let value: String

/// The tooltip to show when the part is hovered.
public let tooltip: MarkupContent?

/// An optional source code location representing this part.
/// Used by the editor for hover and code navigation, e.g.
/// by making the part a clickable link to the given position.
public let location: Location?

/// An optional command for this label part.
public let command: Command?

public init(
value: String,
tooltip: MarkupContent? = nil,
location: Location? = nil,
command: Command? = nil
) {
self.value = value
self.tooltip = tooltip
self.location = location
self.command = command
}
}

0 comments on commit d66d80d

Please sign in to comment.