Skip to content

Commit a9a2170

Browse files
committed
Update selectionRange type from AX
1 parent f0e92f2 commit a9a2170

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

Core/Sources/AXExtension/AXUIElement.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public extension AXUIElement {
1111
var value: String {
1212
(try? copyValue(key: kAXValueAttribute)) ?? ""
1313
}
14-
14+
1515
var doubleValue: Double {
1616
(try? copyValue(key: kAXValueAttribute)) ?? 0.0
1717
}
@@ -23,17 +23,17 @@ public extension AXUIElement {
2323
var description: String {
2424
(try? copyValue(key: kAXDescriptionAttribute)) ?? ""
2525
}
26-
26+
2727
var isSourceEditor: Bool {
2828
description == "Source Editor"
2929
}
3030

31-
var selectedTextRange: Range<Int>? {
31+
var selectedTextRange: ClosedRange<Int>? {
3232
guard let value: AXValue = try? copyValue(key: kAXSelectedTextRangeAttribute)
3333
else { return nil }
3434
var range: CFRange = .init(location: 0, length: 0)
3535
if AXValueGetValue(value, .cfRange, &range) {
36-
return Range(.init(location: range.location, length: range.length))
36+
return range.location...(range.location + range.length)
3737
}
3838
return nil
3939
}

Core/Sources/CopilotModel/ExportedFromLSP.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
11
import LanguageServerProtocol
22

33
public typealias CursorPosition = LanguageServerProtocol.Position
4-
public typealias CursorRange = LanguageServerProtocol.LSPRange
54

65
public extension CursorPosition {
6+
static let zero = CursorPosition(line: 0, character: 0)
77
static var outOfScope: CursorPosition { .init(line: -1, character: -1) }
88
}
99

10+
public struct CursorRange: Codable, Hashable, Sendable {
11+
static let zero = CursorRange(start: .zero, end: .zero)
12+
13+
public var start: CursorPosition
14+
public var end: CursorPosition
15+
16+
public init(start: Position, end: Position) {
17+
self.start = start
18+
self.end = end
19+
}
20+
21+
public init(startPair: (Int, Int), endPair: (Int, Int)) {
22+
self.start = Position(startPair)
23+
self.end = Position(endPair)
24+
}
25+
26+
public func contains(_ position: Position) -> Bool {
27+
return position > start && position < end
28+
}
29+
30+
public func intersects(_ other: LSPRange) -> Bool {
31+
return contains(other.start) || contains(other.end)
32+
}
33+
34+
public var isEmpty: Bool {
35+
return start == end
36+
}
37+
}
38+
1039
public extension CursorRange {
1140
static var outOfScope: CursorRange { .init(start: .outOfScope, end: .outOfScope) }
1241
static func cursor(_ position: CursorPosition) -> CursorRange {

0 commit comments

Comments
 (0)