Skip to content

Commit dfc6c18

Browse files
committed
Display reference kind in chat
1 parent a4bf72d commit dfc6c18

File tree

5 files changed

+140
-16
lines changed

5 files changed

+140
-16
lines changed

Core/Sources/ChatGPTChatTab/Chat.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,26 @@ public struct DisplayedChatMessage: Equatable {
1414
}
1515

1616
public struct Reference: Equatable {
17+
public typealias Kind = ChatMessage.Reference.Kind
18+
1719
public var title: String
1820
public var subtitle: String
1921
public var uri: String
2022
public var startLine: Int?
21-
22-
public init(title: String, subtitle: String, uri: String, startLine: Int?) {
23+
public var kind: Kind
24+
25+
public init(
26+
title: String,
27+
subtitle: String,
28+
uri: String,
29+
startLine: Int?,
30+
kind: Kind
31+
) {
2332
self.title = title
2433
self.subtitle = subtitle
2534
self.uri = uri
2635
self.startLine = startLine
36+
self.kind = kind
2737
}
2838
}
2939

@@ -304,7 +314,8 @@ struct Chat: ReducerProtocol {
304314
title: $0.title,
305315
subtitle: $0.subTitle,
306316
uri: $0.uri,
307-
startLine: $0.startLine
317+
startLine: $0.startLine,
318+
kind: $0.kind
308319
)
309320
}
310321
)

Core/Sources/ChatGPTChatTab/ChatPanel.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ struct ChatPanel_Preview: PreviewProvider {
440440
title: "Hello Hello Hello Hello",
441441
subtitle: "Hi Hi Hi Hi",
442442
uri: "https://google.com",
443-
startLine: nil
443+
startLine: nil,
444+
kind: .class
444445
),
445446
]
446447
),

Core/Sources/ChatGPTChatTab/Views/BotMessage.swift

Lines changed: 97 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,20 @@ struct ReferenceList: View {
108108
chat.send(.referenceClicked(reference))
109109
}) {
110110
HStack(spacing: 8) {
111+
ReferenceIcon(kind: reference.kind)
112+
.layoutPriority(2)
111113
Text(reference.title)
112114
.truncationMode(.middle)
113115
.lineLimit(1)
116+
.layoutPriority(1)
114117
Text(reference.subtitle)
115118
.lineLimit(1)
116119
.truncationMode(.middle)
117-
.layoutPriority(/*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/)
118120
.foregroundStyle(.tertiary)
121+
.layoutPriority(/*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/)
119122
}
120-
.padding(.vertical, 6)
121-
.padding(.horizontal, 8)
123+
.padding(.vertical, 4)
124+
.padding(.horizontal, 4)
122125
.frame(maxWidth: .infinity, alignment: .leading)
123126
.overlay {
124127
RoundedRectangle(cornerRadius: 4)
@@ -134,6 +137,83 @@ struct ReferenceList: View {
134137
}
135138
}
136139

140+
struct ReferenceIcon: View {
141+
let kind: DisplayedChatMessage.Reference.Kind
142+
143+
var body: some View {
144+
RoundedRectangle(cornerRadius: 4)
145+
.fill({
146+
switch kind {
147+
case .class:
148+
Color.purple
149+
case .struct:
150+
Color.purple
151+
case .enum:
152+
Color.purple
153+
case .actor:
154+
Color.purple
155+
case .protocol:
156+
Color.purple
157+
case .extension:
158+
Color.indigo
159+
case .case:
160+
Color.green
161+
case .property:
162+
Color.teal
163+
case .typealias:
164+
Color.orange
165+
case .function:
166+
Color.teal
167+
case .method:
168+
Color.blue
169+
case .text:
170+
Color.gray
171+
case .webpage:
172+
Color.blue
173+
case .other:
174+
Color.gray
175+
}
176+
}())
177+
.frame(width: 22, height: 22)
178+
.overlay(alignment: .center) {
179+
Group {
180+
switch kind {
181+
case .class:
182+
Text("C")
183+
case .struct:
184+
Text("S")
185+
case .enum:
186+
Text("E")
187+
case .actor:
188+
Text("A")
189+
case .protocol:
190+
Text("Pr")
191+
case .extension:
192+
Text("Ex")
193+
case .case:
194+
Text("K")
195+
case .property:
196+
Text("P")
197+
case .typealias:
198+
Text("T")
199+
case .function:
200+
Text("𝑓")
201+
case .method:
202+
Text("M")
203+
case .text:
204+
Text("Tx")
205+
case .webpage:
206+
Text("Wb")
207+
case .other:
208+
Text("Ot")
209+
}
210+
}
211+
.font(.system(size: 12).monospaced())
212+
.foregroundColor(.white)
213+
}
214+
}
215+
}
216+
137217
#Preview("Bot Message") {
138218
BotMessage(
139219
id: "1",
@@ -147,7 +227,8 @@ struct ReferenceList: View {
147227
title: "ReferenceList",
148228
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
149229
uri: "https://google.com",
150-
startLine: nil
230+
startLine: nil,
231+
kind: .class
151232
), count: 20),
152233
chat: .init(initialState: .init(), reducer: Chat(service: .init()))
153234
)
@@ -161,37 +242,43 @@ struct ReferenceList: View {
161242
title: "ReferenceList",
162243
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
163244
uri: "https://google.com",
164-
startLine: nil
245+
startLine: nil,
246+
kind: .class
165247
),
166248
.init(
167249
title: "BotMessage.swift:100-102",
168250
subtitle: "/Core/Sources/ChatGPTChatTab/Views",
169251
uri: "https://google.com",
170-
startLine: nil
252+
startLine: nil,
253+
kind: .struct
171254
),
172255
.init(
173256
title: "ReferenceList",
174257
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
175258
uri: "https://google.com",
176-
startLine: nil
259+
startLine: nil,
260+
kind: .function
177261
),
178262
.init(
179263
title: "ReferenceList",
180264
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
181265
uri: "https://google.com",
182-
startLine: nil
266+
startLine: nil,
267+
kind: .case
183268
),
184269
.init(
185270
title: "ReferenceList",
186271
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
187272
uri: "https://google.com",
188-
startLine: nil
273+
startLine: nil,
274+
kind: .extension
189275
),
190276
.init(
191277
title: "ReferenceList",
192278
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
193279
uri: "https://google.com",
194-
startLine: nil
280+
startLine: nil,
281+
kind: .webpage
195282
),
196283
], chat: .init(initialState: .init(), reducer: Chat(service: .init())))
197284
}

Pro

Submodule Pro updated from 2100084 to fd1b2fe

Tool/Sources/OpenAIService/Models.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,48 @@ public struct ChatMessage: Equatable, Codable {
2828
}
2929

3030
public struct Reference: Codable, Equatable {
31+
public enum Kind: String, Codable {
32+
case `class`
33+
case `struct`
34+
case `enum`
35+
case `actor`
36+
case `protocol`
37+
case `extension`
38+
case `case`
39+
case property
40+
case `typealias`
41+
case function
42+
case method
43+
case text
44+
case webpage
45+
case other
46+
}
47+
3148
public var title: String
3249
public var subTitle: String
3350
public var uri: String
3451
public var content: String
3552
public var startLine: Int?
3653
public var endLine: Int?
54+
@FallbackDecoding<ReferenceKindFallback>
55+
public var kind: Kind
3756

3857
public init(
3958
title: String,
4059
subTitle: String,
4160
content: String,
4261
uri: String,
4362
startLine: Int?,
44-
endLine: Int?
63+
endLine: Int?,
64+
kind: Kind
4565
) {
4666
self.title = title
4767
self.subTitle = subTitle
4868
self.content = content
4969
self.uri = uri
5070
self.startLine = startLine
5171
self.endLine = endLine
72+
self.kind = kind
5273
}
5374
}
5475

@@ -112,3 +133,7 @@ public struct ChatMessage: Equatable, Codable {
112133
}
113134
}
114135

136+
public struct ReferenceKindFallback: FallbackValueProvider {
137+
public static var defaultValue: ChatMessage.Reference.Kind { .other }
138+
}
139+

0 commit comments

Comments
 (0)