forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorInformation.swift
More file actions
133 lines (121 loc) · 4.5 KB
/
EditorInformation.swift
File metadata and controls
133 lines (121 loc) · 4.5 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import Foundation
import Parsing
public struct EditorInformation {
public struct LineAnnotation {
public var type: String
public var line: Int
public var message: String
}
public struct SourceEditorContent {
/// The content of the source editor.
public var content: String
/// The content of the source editor in lines.
public var lines: [String]
/// The selection ranges of the source editor.
public var selections: [CursorRange]
/// The cursor position of the source editor.
public var cursorPosition: CursorPosition
/// Line annotations of the source editor.
public var lineAnnotations: [LineAnnotation]
public var selectedContent: String {
if let range = selections.first {
let startIndex = min(
max(0, range.start.line),
lines.endIndex - 1
)
let endIndex = min(
max(startIndex, range.end.line),
lines.endIndex - 1
)
let selectedContent = lines[startIndex...endIndex]
return selectedContent.joined()
}
return ""
}
public init(
content: String,
lines: [String],
selections: [CursorRange],
cursorPosition: CursorPosition,
lineAnnotations: [String]
) {
self.content = content
self.lines = lines
self.selections = selections
self.cursorPosition = cursorPosition
self.lineAnnotations = lineAnnotations.map(EditorInformation.parseLineAnnotation)
}
}
public let editorContent: SourceEditorContent?
public let selectedContent: String
public let selectedLines: [String]
public let documentURL: URL
public let projectURL: URL
public let relativePath: String
public let language: CodeLanguage
public init(
editorContent: SourceEditorContent?,
selectedContent: String,
selectedLines: [String],
documentURL: URL,
projectURL: URL,
relativePath: String,
language: CodeLanguage
) {
self.editorContent = editorContent
self.selectedContent = selectedContent
self.selectedLines = selectedLines
self.documentURL = documentURL
self.projectURL = projectURL
self.relativePath = relativePath
self.language = language
}
public func code(in range: CursorRange) -> String {
return EditorInformation.code(in: editorContent?.lines ?? [], inside: range).code
}
public static func lines(in code: [String], containing range: CursorRange) -> [String] {
let startIndex = min(max(0, range.start.line), code.endIndex - 1)
let endIndex = min(max(startIndex, range.end.line), code.endIndex - 1)
let selectedLines = code[startIndex...endIndex]
return Array(selectedLines)
}
public static func code(
in code: [String],
inside range: CursorRange
) -> (code: String, lines: [String]) {
let rangeLines = lines(in: code, containing: range)
var content = rangeLines
if !content.isEmpty {
content[content.endIndex - 1] = String(
content[content.endIndex - 1].dropLast(
content[content.endIndex - 1].count - range.end.character
)
)
content[0] = String(content[0].dropFirst(range.start.character))
}
return (content.joined(), rangeLines)
}
/// Error Line 25: FileName.swift:25 Cannot convert Type
static func parseLineAnnotation(_ annotation: String) -> LineAnnotation {
let lineAnnotationParser = Parse(input: Substring.self) {
PrefixUpTo(":")
":"
PrefixUpTo(":")
":"
Int.parser()
Prefix(while: { _ in true })
}.map { (prefix: Substring, _: Substring, line: Int, message: Substring) in
let type = String(prefix.split(separator: " ").first ?? prefix)
return LineAnnotation(
type: type.trimmingCharacters(in: .whitespacesAndNewlines),
line: line,
message: message.trimmingCharacters(in: .whitespacesAndNewlines)
)
}
do {
return try lineAnnotationParser.parse(annotation[...])
} catch {
return .init(type: "", line: 0, message: annotation)
}
}
}