Skip to content

Commit 8960cb5

Browse files
committed
Support accepting promptToCode result
1 parent 51a2f1e commit 8960cb5

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

Core/Sources/Service/SuggestionCommandHandler/PseudoCommandHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ struct PseudoCommandHandler {
8181
.presentErrorMessage("Unable to get file content.")
8282
return
8383
}
84-
let handler = CommentBaseCommandHandler()
84+
let handler = WindowBaseCommandHandler()
8585
do {
8686
guard let result = try await handler.acceptSuggestion(editor: .init(
8787
content: content,

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,61 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
136136
func acceptSuggestion(editor: EditorContent) async throws -> UpdatedContent? {
137137
presenter.markAsProcessing(true)
138138
defer { presenter.markAsProcessing(false) }
139+
140+
let fileURL = try await Environment.fetchCurrentFileURL()
141+
let (workspace, _) = try await Workspace.fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL)
139142

140-
do {
141-
let result = try await CommentBaseCommandHandler().acceptSuggestion(editor: editor)
142-
Task {
143-
let fileURL = try await Environment.fetchCurrentFileURL()
144-
presenter.discardSuggestion(fileURL: fileURL)
143+
let result: (suggestion: CopilotCompletion, cleanup: () -> Void)? = {
144+
if let service = WidgetDataSource.shared.promptToCodes[fileURL]?.promptToCodeService {
145+
return (CopilotCompletion(
146+
text: service.code,
147+
position: service.selectionRange.start,
148+
uuid: UUID().uuidString,
149+
range: service.selectionRange,
150+
displayText: service.code
151+
), {
152+
WidgetDataSource.shared.removePromptToCode(for: fileURL)
153+
presenter.closePromptToCode(fileURL: fileURL)
154+
})
145155
}
146-
return result
147-
} catch {
148-
presenter.presentError(error)
149-
throw error
150-
}
156+
157+
if let acceptedSuggestion = workspace.acceptSuggestion(
158+
forFileAt: fileURL,
159+
editor: editor
160+
) {
161+
return (acceptedSuggestion, {
162+
presenter.discardSuggestion(fileURL: fileURL)
163+
})
164+
}
165+
166+
return nil
167+
}()
168+
169+
guard let result else { return nil }
170+
171+
let injector = SuggestionInjector()
172+
var lines = editor.lines
173+
var cursorPosition = editor.cursorPosition
174+
var extraInfo = SuggestionInjector.ExtraInfo()
175+
injector.rejectCurrentSuggestions(
176+
from: &lines,
177+
cursorPosition: &cursorPosition,
178+
extraInfo: &extraInfo
179+
)
180+
injector.acceptSuggestion(
181+
intoContentWithoutSuggestion: &lines,
182+
cursorPosition: &cursorPosition,
183+
completion: result.suggestion,
184+
extraInfo: &extraInfo
185+
)
186+
187+
result.cleanup()
188+
189+
return .init(
190+
content: String(lines.joined(separator: "")),
191+
newCursor: cursorPosition,
192+
modifications: extraInfo.modifications
193+
)
151194
}
152195

153196
func presentRealtimeSuggestions(editor: EditorContent) async throws -> UpdatedContent? {

0 commit comments

Comments
 (0)