-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathExportedFromLSP.swift
More file actions
88 lines (66 loc) · 2.55 KB
/
ExportedFromLSP.swift
File metadata and controls
88 lines (66 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import LanguageServerProtocol
/// Line starts at 0.
public typealias CursorPosition = LanguageServerProtocol.Position
public extension CursorPosition {
static let zero = CursorPosition(line: 0, character: 0)
static var outOfScope: CursorPosition { .init(line: -1, character: -1) }
var readableText: String {
return "[\(line + 1), \(character)]"
}
}
public struct CursorRange: Codable, Hashable, Sendable, Equatable, CustomStringConvertible {
public static let zero = CursorRange(start: .zero, end: .zero)
public var start: CursorPosition
public var end: CursorPosition
public init(start: Position, end: Position) {
self.start = start
self.end = end
}
public init(startPair: (Int, Int), endPair: (Int, Int)) {
start = CursorPosition(startPair)
end = CursorPosition(endPair)
}
public func contains(_ position: CursorPosition) -> Bool {
return position >= start && position <= end
}
public func contains(_ range: CursorRange) -> Bool {
return range.start >= start && range.end <= end
}
public func strictlyContains(_ range: CursorRange) -> Bool {
return range.start > start && range.end < end
}
public func intersects(_ other: LSPRange) -> Bool {
return contains(other.start) || contains(other.end)
}
public var isEmpty: Bool {
return start == end
}
public var isOneLine: Bool {
return start.line == end.line
}
/// The number of lines in the range.
public var lineCount: Int {
return end.line - start.line + 1
}
public static func == (lhs: CursorRange, rhs: CursorRange) -> Bool {
return lhs.start == rhs.start && lhs.end == rhs.end
}
public var description: String {
return "\(start.readableText) - \(end.readableText)"
}
public var isValid: Bool {
let startLine = start.line
let startCharacter = start.character
let endLine = end.line
let endCharacter = end.character
guard startLine >= 0 && startCharacter >= 0 && endLine >= 0 && endCharacter >= 0 else {return false}
guard startLine < endLine || (startLine == endLine && startCharacter <= endCharacter) else {return false}
return true
}
}
public extension CursorRange {
static var outOfScope: CursorRange { .init(start: .outOfScope, end: .outOfScope) }
static func cursor(_ position: CursorPosition) -> CursorRange {
return .init(start: position, end: position)
}
}