Skip to content

Commit 9776cee

Browse files
committed
Tweaks multiple modification prompt to code
1 parent a78effe commit 9776cee

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,46 @@ extension WindowBaseCommandHandler {
365365

366366
let codeLanguage = languageIdentifierFromFileURL(fileURL)
367367

368-
let snippets = editor.selections.map { selection in
368+
let selections: [CursorRange] = {
369+
var all = [CursorRange]()
370+
371+
// join the ranges if they overlaps in line
372+
373+
for selection in editor.selections {
374+
let range = CursorRange(start: selection.start, end: selection.end)
375+
376+
func intersect(_ lhs: CursorRange, _ rhs: CursorRange) -> Bool {
377+
lhs.start.line <= rhs.end.line && lhs.end.line >= rhs.start.line
378+
}
379+
380+
if let last = all.last, intersect(last, range) {
381+
all[all.count - 1] = CursorRange(
382+
start: .init(
383+
line: min(last.start.line, range.start.line),
384+
character: min(last.start.character, range.start.character)
385+
),
386+
end: .init(
387+
line: max(last.end.line, range.end.line),
388+
character: max(last.end.character, range.end.character)
389+
)
390+
)
391+
} else if !range.isEmpty {
392+
all.append(range)
393+
}
394+
}
395+
396+
return all
397+
}()
398+
399+
let snippets = selections.map { selection in
369400
guard selection.start != selection.end else {
370401
return PromptToCodeSnippet(
371402
startLineIndex: selection.start.line,
372403
originalCode: "",
373404
modifiedCode: "",
374405
description: "",
375406
error: "",
376-
attachedRange: .init(start: selection.start, end: selection.end)
407+
attachedRange: selection
377408
)
378409
}
379410
var selection = selection
@@ -399,7 +430,10 @@ extension WindowBaseCommandHandler {
399430
// indentation.
400431
selection.start = .init(line: selection.start.line, character: 0)
401432
}
402-
let selectedCode = editor.selectedCode(in: selection)
433+
let selectedCode = editor.selectedCode(in: .init(
434+
start: selection.start,
435+
end: selection.end
436+
))
403437
return PromptToCodeSnippet(
404438
startLineIndex: selection.start.line,
405439
originalCode: selectedCode,

Core/Sources/SuggestionWidget/FeatureReducers/PromptToCodeGroup.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public struct PromptToCodeGroup {
8080
return .none
8181

8282
case let .discardAcceptedPromptToCodeIfNotContinuous(id):
83-
state.promptToCodes.removeAll { $0.id == id && !$0.isContinuous }
83+
state.promptToCodes.removeAll { $0.id == id && $0.hasEnded }
8484
return .none
8585

8686
case let .updateActivePromptToCode(documentURL):

Core/Sources/SuggestionWidget/FeatureReducers/PromptToCodePanel.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public struct PromptToCodePanel {
3434
public var canRevert: Bool { !promptToCodeState.history.isEmpty }
3535

3636
public var generateDescriptionRequirement: Bool
37+
38+
public var hasEnded = false
3739

3840
public var snippetPanels: IdentifiedArrayOf<PromptToCodeSnippetPanel.State> {
3941
get {
@@ -223,6 +225,7 @@ public struct PromptToCodePanel {
223225
return .cancel(id: CancellationKey.modifyCode(state.id))
224226

225227
case .acceptButtonTapped:
228+
state.hasEnded = true
226229
return .run { _ in
227230
await commandHandler.acceptPromptToCode()
228231
activatePreviousActiveXcode()

0 commit comments

Comments
 (0)