From 867e4c03fd02a052ad790b33f2fdc389cc750f48 Mon Sep 17 00:00:00 2001 From: Marcin Krzyzanowski Date: Wed, 13 Nov 2024 15:42:49 +0100 Subject: [PATCH] Add Range and LSPRange initializers for better interoperability (#24) Added initializers to the Range type for easy creation from Position and LSPRange. Enhanced LSPRange with an initializer that takes a Range. This improves the interoperability between these structures in the language server protocol. --- .../LanguageServerProtocol/BasicStructures.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Sources/LanguageServerProtocol/BasicStructures.swift b/Sources/LanguageServerProtocol/BasicStructures.swift index 17c5887..37f5740 100644 --- a/Sources/LanguageServerProtocol/BasicStructures.swift +++ b/Sources/LanguageServerProtocol/BasicStructures.swift @@ -33,6 +33,16 @@ extension Position: Comparable { } } +extension Range where Bound == Position { + public init(_ start: Position, _ end: Position) { + self.init(uncheckedBounds: (lower: start, upper: end)) + } + + public init(_ range: LSPRange) { + self.init(uncheckedBounds: (lower: range.start, upper: range.end)) + } +} + public struct LSPRange: Codable, Hashable, Sendable { public static let zero = LSPRange(start: .zero, end: .zero) @@ -49,6 +59,11 @@ public struct LSPRange: Codable, Hashable, Sendable { self.end = Position(endPair) } + public init(_ other: Range) { + self.start = other.lowerBound + self.end = other.upperBound + } + public func contains(_ position: Position) -> Bool { return position > start && position < end }