Skip to content

Commit cbf25db

Browse files
committed
Support detaching prompt to code from selection range
1 parent e9a1f72 commit cbf25db

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

Core/Sources/PromptToCodeService/PromptToCodeService.swift

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public final class PromptToCodeService: ObservableObject {
3939
@Published public var isResponding: Bool = false
4040
@Published public var description: String = ""
4141
@Published public var isContinuous = false
42+
@Published public var selectionRange: CursorRange?
4243
public var canRevert: Bool { history != .empty }
43-
public var selectionRange: CursorRange
4444
public var language: CodeLanguage
4545
public var indentSize: Int
4646
public var usesTabsForIndentation: Bool
@@ -52,7 +52,7 @@ public final class PromptToCodeService: ObservableObject {
5252

5353
public init(
5454
code: String,
55-
selectionRange: CursorRange,
55+
selectionRange: CursorRange?,
5656
language: CodeLanguage,
5757
identSize: Int,
5858
usesTabsForIndentation: Bool,
@@ -122,16 +122,6 @@ public final class PromptToCodeService: ObservableObject {
122122
self.description = description
123123
}
124124

125-
public func generateCompletion() -> CodeSuggestion {
126-
.init(
127-
text: code,
128-
position: selectionRange.start,
129-
uuid: UUID().uuidString,
130-
range: selectionRange,
131-
displayText: code
132-
)
133-
}
134-
135125
public func stopResponding() {
136126
runningAPI?.stopResponding()
137127
isResponding = false

Core/Sources/Service/GUI/PromptToCodeProvider+Service.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extension PromptToCodeProvider {
2525
service.$isContinuous.sink(receiveValue: set(\.isContinuous)).store(in: &cancellables)
2626
service.$history.map { $0 != .empty }
2727
.sink(receiveValue: set(\.canRevert)).store(in: &cancellables)
28+
service.$selectionRange.sink(receiveValue: set(\.attachedToRange)).store(in: &cancellables)
2829

2930
onCancelTapped = { [cancellables] in
3031
_ = cancellables
@@ -59,7 +60,7 @@ extension PromptToCodeProvider {
5960
let handler = PseudoCommandHandler()
6061
await handler.acceptSuggestion()
6162
if let app = ActiveApplicationMonitor.shared.previousApp,
62-
app.isXcode,
63+
app.isXcode,
6364
!(self?.isContinuous ?? false)
6465
{
6566
try await Task.sleep(nanoseconds: 200_000_000)
@@ -71,6 +72,14 @@ extension PromptToCodeProvider {
7172
onContinuousToggleClick = {
7273
service.isContinuous.toggle()
7374
}
75+
76+
onToggleAttachOrDetachToCode = {
77+
if service.selectionRange != nil {
78+
service.selectionRange = nil
79+
} else {
80+
// reset to selected or focused range.
81+
}
82+
}
7483
}
7584

7685
func set<T>(_ keyPath: WritableKeyPath<PromptToCodeProvider, T>) -> (T) -> Void {

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,16 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
167167
let dataSource = Service.shared.guiController.widgetDataSource
168168

169169
if let service = await dataSource.promptToCodes[fileURL]?.promptToCodeService {
170+
let rangeStart = service.selectionRange?.start ?? editor.cursorPosition
171+
170172
let suggestion = CodeSuggestion(
171173
text: service.code,
172-
position: service.selectionRange.start,
174+
position: rangeStart,
173175
uuid: UUID().uuidString,
174-
range: service.selectionRange,
176+
range: service.selectionRange ?? .init(
177+
start: editor.cursorPosition,
178+
end: editor.cursorPosition
179+
),
175180
displayText: service.code
176181
)
177182

@@ -184,7 +189,7 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
184189

185190
if service.isContinuous {
186191
service.selectionRange = .init(
187-
start: service.selectionRange.start,
192+
start: rangeStart,
188193
end: cursorPosition
189194
)
190195
presenter.presentPromptToCode(fileURL: fileURL)
@@ -195,7 +200,7 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
195200

196201
return .init(
197202
content: String(lines.joined(separator: "")),
198-
newSelection: .init(start: service.selectionRange.start, end: cursorPosition),
203+
newSelection: .init(start: rangeStart, end: cursorPosition),
199204
modifications: extraInfo.modifications
200205
)
201206
} else if let acceptedSuggestion = workspace.acceptSuggestion(

Core/Sources/SuggestionWidget/Providers/PromptToCodeProvider.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public final class PromptToCodeProvider: ObservableObject {
2424
public var onAcceptSuggestionTapped: () -> Void
2525
public var onRequirementSent: (String) -> Void
2626
public var onContinuousToggleClick: () -> Void
27+
public var onToggleAttachOrDetachToCode: () -> Void
2728

2829
public init(
2930
code: String = "",
@@ -41,7 +42,8 @@ public final class PromptToCodeProvider: ObservableObject {
4142
onCancelTapped: @escaping () -> Void = {},
4243
onAcceptSuggestionTapped: @escaping () -> Void = {},
4344
onRequirementSent: @escaping (String) -> Void = { _ in },
44-
onContinuousToggleClick: @escaping () -> Void = {}
45+
onContinuousToggleClick: @escaping () -> Void = {},
46+
onToggleAttachOrDetachToCode: @escaping () -> Void = {}
4547
) {
4648
self.code = code
4749
self.language = language
@@ -59,6 +61,7 @@ public final class PromptToCodeProvider: ObservableObject {
5961
self.onAcceptSuggestionTapped = onAcceptSuggestionTapped
6062
self.onRequirementSent = onRequirementSent
6163
self.onContinuousToggleClick = onContinuousToggleClick
64+
self.onToggleAttachOrDetachToCode = onToggleAttachOrDetachToCode
6265
}
6366

6467
func revert() {
@@ -84,5 +87,7 @@ public final class PromptToCodeProvider: ObservableObject {
8487
func acceptSuggestion() { onAcceptSuggestionTapped() }
8588

8689
func toggleContinuous() { onContinuousToggleClick() }
90+
91+
func toggleAttachOrDetachToCode() { onToggleAttachOrDetachToCode() }
8792
}
8893

Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extension PromptToCodePanel {
3131
var body: some View {
3232
HStack {
3333
Button(action: {
34-
provider.acceptSuggestion()
34+
provider.toggleAttachOrDetachToCode()
3535
}) {
3636
let attachedToRange = provider.attachedToRange
3737
let isAttached = attachedToRange != nil

0 commit comments

Comments
 (0)