Files
sourcekit-lsp/Sources/LanguageServerProtocol/TextDocumentContentChangeEvent.swift
Ben Langmuir d2cfe7e847 Upgrade CustomCodable to handle optional values using encodeIfPresent
The LSP spec requires us to omit keys for nil values rather than using
the JSON `null` constant in most places. This change to CustomCodable
allows us to do it automatically using CustomCodable, removing one of
the limitations of the property wrapper.

The idea for the adding overloads of `encode` and `decode` came from
https://forums.swift.org/t/pre-pitch-codable-customization-using-propertywrappers/30244/
2019-12-04 08:49:19 -08:00

33 lines
1.2 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// A change to a text document.
///
/// If `range` and `rangeLength` are unspecified, the whole document content is replaced.
///
/// The `range.end` and `rangeLength` are potentially redundant. Based on https://github.com/Microsoft/language-server-protocol/issues/9, servers should be lenient and accept either.
public struct TextDocumentContentChangeEvent: Codable, Hashable {
@CustomCodable<PositionRange?>
public var range: Range<Position>?
public var rangeLength: Int?
public var text: String
public init(range: Range<Position>? = nil, rangeLength: Int? = nil, text: String) {
self._range = CustomCodable(wrappedValue: range)
self.rangeLength = rangeLength
self.text = text
}
}