Skip to content

Commit

Permalink
Add Range and LSPRange initializers for better interoperability (#24)
Browse files Browse the repository at this point in the history
Added initializers to the Range type for easy creation from Position and
LSPRange. Enhanced LSPRange with an initializer that takes a Range<Position>. This
improves the interoperability between these structures in the language server protocol.
  • Loading branch information
krzyzanowskim authored Nov 13, 2024
1 parent d514129 commit 867e4c0
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Sources/LanguageServerProtocol/BasicStructures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -49,6 +59,11 @@ public struct LSPRange: Codable, Hashable, Sendable {
self.end = Position(endPair)
}

public init(_ other: Range<Position>) {
self.start = other.lowerBound
self.end = other.upperBound
}

public func contains(_ position: Position) -> Bool {
return position > start && position < end
}
Expand Down

0 comments on commit 867e4c0

Please sign in to comment.