Skip to content

Commit 651b639

Browse files
committed
Support retrieval in context aware prompt to code
1 parent 63af3b4 commit 651b639

File tree

10 files changed

+33
-12
lines changed

10 files changed

+33
-12
lines changed

Core/Sources/PromptToCodeService/OpenAIPromptToCodeService.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public final class OpenAIPromptToCodeService: PromptToCodeServiceType {
3333

3434
let editor: EditorInformation = XcodeInspector.shared.focusedEditorContent ?? .init(
3535
editorContent: .init(
36-
content: source.allCode,
37-
lines: [],
36+
content: source.content,
37+
lines: source.lines,
3838
selections: [source.range],
3939
cursorPosition: .outOfScope,
4040
lineAnnotations: []

Core/Sources/PromptToCodeService/PromptToCodeServiceType.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,23 @@ public struct PromptToCodeSource {
1919
public var language: CodeLanguage
2020
public var documentURL: URL
2121
public var projectRootURL: URL
22-
public var allCode: String
22+
public var content: String
23+
public var lines: [String]
2324
public var range: CursorRange
2425

2526
public init(
2627
language: CodeLanguage,
2728
documentURL: URL,
2829
projectRootURL: URL,
29-
allCode: String,
30+
content: String,
31+
lines: [String],
3032
range: CursorRange
3133
) {
3234
self.language = language
3335
self.documentURL = documentURL
3436
self.projectRootURL = projectRootURL
35-
self.allCode = allCode
37+
self.content = content
38+
self.lines = lines
3639
self.range = range
3740
}
3841
}
@@ -74,7 +77,8 @@ extension ContextAwarePromptToCodeService: PromptToCodeServiceType {
7477
language: source.language,
7578
documentURL: source.documentURL,
7679
projectRootURL: source.projectRootURL,
77-
allCode: source.allCode,
80+
content: source.content,
81+
lines: source.lines,
7882
range: source.range
7983
),
8084
isDetached: isDetached,
@@ -86,7 +90,7 @@ extension ContextAwarePromptToCodeService: PromptToCodeServiceType {
8690

8791
public struct PromptToCodeServiceFactoryDependencyKey: DependencyKey {
8892
public static let liveValue: () -> PromptToCodeServiceType = {
89-
OpenAIPromptToCodeService()
93+
ContextAwarePromptToCodeService()
9094
}
9195

9296
public static let previewValue: () -> PromptToCodeServiceType = {

Core/Sources/Service/GUI/ChatTabFactory.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ enum ChatTabFactory {
9292
language: .plaintext,
9393
documentURL: .init(fileURLWithPath: "/"),
9494
projectRootURL: .init(fileURLWithPath: "/"),
95-
allCode: prompt,
95+
content: prompt,
96+
lines: prompt.breakLines(),
9697
range: .outOfScope
9798
),
9899
isDetached: true,

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,8 @@ extension WindowBaseCommandHandler {
424424
usesTabsForIndentation: filespace.codeMetadata.usesTabsForIndentation ?? false,
425425
documentURL: fileURL,
426426
projectRootURL: workspace.projectRootURL,
427-
allCode: editor.content,
427+
allCode: editor.content,
428+
allLines: editor.lines,
428429
isContinuous: isContinuous,
429430
commandName: name,
430431
defaultPrompt: newPrompt ?? "",

Core/Sources/SuggestionWidget/FeatureReducers/PromptToCode.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public struct PromptToCode: ReducerProtocol {
5757
public var projectRootURL: URL
5858
public var documentURL: URL
5959
public var allCode: String
60+
public var allLines: [String]
6061
public var extraSystemPrompt: String?
6162
public var generateDescriptionRequirement: Bool?
6263
public var commandName: String?
@@ -76,6 +77,7 @@ public struct PromptToCode: ReducerProtocol {
7677
projectRootURL: URL,
7778
documentURL: URL,
7879
allCode: String,
80+
allLines: [String],
7981
commandName: String? = nil,
8082
description: String = "",
8183
isResponding: Bool = false,
@@ -101,6 +103,7 @@ public struct PromptToCode: ReducerProtocol {
101103
self.projectRootURL = projectRootURL
102104
self.documentURL = documentURL
103105
self.allCode = allCode
106+
self.allLines = allLines
104107
self.extraSystemPrompt = extraSystemPrompt
105108
self.generateDescriptionRequirement = generateDescriptionRequirement
106109
self.isAttachedToSelectionRange = isAttachedToSelectionRange
@@ -165,7 +168,8 @@ public struct PromptToCode: ReducerProtocol {
165168
language: copiedState.language,
166169
documentURL: copiedState.documentURL,
167170
projectRootURL: copiedState.projectRootURL,
168-
allCode: copiedState.allCode,
171+
content: copiedState.allCode,
172+
lines: copiedState.allLines,
169173
range: copiedState.selectionRange ?? .outOfScope
170174
),
171175
isDetached: !copiedState.isAttachedToSelectionRange,

Core/Sources/SuggestionWidget/FeatureReducers/PromptToCodeGroup.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public struct PromptToCodeGroup: ReducerProtocol {
3131
public var documentURL: URL
3232
public var projectRootURL: URL
3333
public var allCode: String
34+
public var allLines: [String]
3435
public var isContinuous: Bool
3536
public var commandName: String?
3637
public var defaultPrompt: String
@@ -46,6 +47,7 @@ public struct PromptToCodeGroup: ReducerProtocol {
4647
documentURL: URL,
4748
projectRootURL: URL,
4849
allCode: String,
50+
allLines: [String],
4951
isContinuous: Bool,
5052
commandName: String?,
5153
defaultPrompt: String,
@@ -60,6 +62,7 @@ public struct PromptToCodeGroup: ReducerProtocol {
6062
self.documentURL = documentURL
6163
self.projectRootURL = projectRootURL
6264
self.allCode = allCode
65+
self.allLines = allLines
6366
self.isContinuous = isContinuous
6467
self.commandName = commandName
6568
self.defaultPrompt = defaultPrompt
@@ -100,7 +103,8 @@ public struct PromptToCodeGroup: ReducerProtocol {
100103
usesTabsForIndentation: s.usesTabsForIndentation,
101104
projectRootURL: s.projectRootURL,
102105
documentURL: s.documentURL,
103-
allCode: s.allCode,
106+
allCode: s.allCode,
107+
allLines: s.allLines,
104108
commandName: s.commandName,
105109
isContinuous: s.isContinuous,
106110
selectionRange: s.selectionRange,

Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ struct PromptToCodePanel_Preview: PreviewProvider {
430430
projectRootURL: URL(fileURLWithPath: "path/to/file.txt"),
431431
documentURL: URL(fileURLWithPath: "path/to/file.txt"),
432432
allCode: "",
433+
allLines: [],
433434
commandName: "Generate Code",
434435
description: "Hello world",
435436
isResponding: false,
@@ -460,6 +461,7 @@ struct PromptToCodePanel_Error_Detached_Preview: PreviewProvider {
460461
projectRootURL: URL(fileURLWithPath: "path/to/file.txt"),
461462
documentURL: URL(fileURLWithPath: "path/to/file.txt"),
462463
allCode: "",
464+
allLines: [],
463465
commandName: "Generate Code",
464466
description: "Hello world",
465467
isResponding: false,

Pro

Submodule Pro updated from ce6f163 to 01fc14a

Tool/Sources/SuggestionModel/EditorInformation.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public struct EditorInformation {
8585
}
8686

8787
public static func lines(in code: [String], containing range: CursorRange) -> [String] {
88+
guard !code.isEmpty else { return [] }
8889
let startIndex = min(max(0, range.start.line), code.endIndex - 1)
8990
let endIndex = min(max(startIndex, range.end.line), code.endIndex - 1)
9091
let selectedLines = code[startIndex...endIndex]

Tool/Sources/SuggestionModel/ExportedFromLSP.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public struct CursorRange: Codable, Hashable, Sendable, Equatable, CustomStringC
4848
return start == end
4949
}
5050

51+
public var isOneLine: Bool {
52+
return start.line == end.line
53+
}
54+
5155
public static func == (lhs: CursorRange, rhs: CursorRange) -> Bool {
5256
return lhs.start == rhs.start && lhs.end == rhs.end
5357
}

0 commit comments

Comments
 (0)