forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeSuggestion.swift
More file actions
114 lines (98 loc) · 3.74 KB
/
Copy pathCodeSuggestion.swift
File metadata and controls
114 lines (98 loc) · 3.74 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
import CodableWrappers
import Foundation
public struct CodeSuggestion: Codable, Equatable, Identifiable {
public struct Description: Codable, Equatable {
public enum Kind: Codable, Equatable {
case warning
case action
}
public var kind: Kind
public var content: String
public init(kind: Kind, content: String) {
self.kind = kind
self.content = content
}
}
public enum EffectiveRange: Codable, Equatable {
case replacingRange
case line
case full
case ignored
}
public init(
id: String,
text: String,
position: CursorPosition,
range: CursorRange,
effectiveRange: EffectiveRange = .line,
replacingLines: [String] = [],
descriptions: [Description] = [],
middlewareComments: [String] = [],
metadata: [MetadataKey: String] = [:]
) {
self.text = text
self.position = position
self.id = id
self.range = range
self.effectiveRange = effectiveRange
self.replacingLines = replacingLines
self.descriptions = descriptions
self.middlewareComments = middlewareComments
self.metadata = metadata
}
public static func == (lhs: CodeSuggestion, rhs: CodeSuggestion) -> Bool {
return lhs.text == rhs.text
&& lhs.position == rhs.position
&& lhs.id == rhs.id
&& lhs.range == rhs.range
&& lhs.descriptions == rhs.descriptions
&& lhs.middlewareComments == rhs.middlewareComments
}
/// The new code to be inserted and the original code on the first line.
public var text: String
/// The position of the cursor before generating the completion.
public var position: CursorPosition
/// An id.
public var id: String
/// The range of the original code that should be replaced.
public var range: CursorRange
/// The range of the suggestion that has an effect.
public var effectiveRange: EffectiveRange
/// Descriptions about this code suggestion
@FallbackDecoding<EmptyArray> public var replacingLines: [String]
/// Descriptions about this code suggestion
@FallbackDecoding<EmptyArray> public var descriptions: [Description]
/// A place to store comments inserted by middleware for debugging use.
@FallbackDecoding<EmptyArray> public var middlewareComments: [String]
/// A place to store extra data.
@FallbackDecoding<EmptyDictionary> public var metadata: [MetadataKey: String]
public struct MetadataKey: ExpressibleByStringLiteral, Hashable, Codable {
public let rawValue: String
public static var group: MetadataKey { "group" }
public static func custom(_ key: String) -> MetadataKey { .init(rawValue: key) }
public init(rawValue: String) {
self.rawValue = rawValue
}
public init(stringLiteral value: StringLiteralType) {
self.rawValue = value
}
public enum CodingKeys: CodingKey {
case rawValue
}
public func encode(to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(rawValue)
}
public init(from decoder: any Decoder) throws {
let container = try decoder.singleValueContainer()
self.rawValue = try container.decode(String.self)
}
}
public subscript(metadata key: MetadataKey) -> String? {
get { metadata[key] }
set { metadata[key] = newValue }
}
public var isActionOnly: Bool {
text.isEmpty && range.isEmpty && descriptions.contains(where: { $0.kind == .action })
}
}