Skip to content

Commit d99330e

Browse files
committed
Support continuous mode in accepting content
1 parent 635c70e commit d99330e

File tree

2 files changed

+63
-62
lines changed

2 files changed

+63
-62
lines changed

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -140,70 +140,64 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
140140
let fileURL = try await Environment.fetchCurrentFileURL()
141141
let (workspace, _) = try await Workspace.fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL)
142142

143-
let result: (
144-
suggestion: CopilotCompletion,
145-
cleanup: () -> Void,
146-
startPosition: CursorPosition?
147-
)? = {
148-
if let service = WidgetDataSource.shared.promptToCodes[fileURL]?.promptToCodeService {
149-
return (
150-
CopilotCompletion(
151-
text: service.code,
152-
position: service.selectionRange.start,
153-
uuid: UUID().uuidString,
154-
range: service.selectionRange,
155-
displayText: service.code
156-
),
157-
{
158-
WidgetDataSource.shared.removePromptToCode(for: fileURL)
159-
presenter.closePromptToCode(fileURL: fileURL)
160-
},
161-
service.selectionRange.start
162-
)
163-
}
164-
165-
if let acceptedSuggestion = workspace.acceptSuggestion(
166-
forFileAt: fileURL,
167-
editor: editor
168-
) {
169-
return (
170-
acceptedSuggestion,
171-
{
172-
presenter.discardSuggestion(fileURL: fileURL)
173-
},
174-
nil
175-
)
176-
}
177-
178-
return nil
179-
}()
180-
181-
guard let result else { return nil }
182-
183143
let injector = SuggestionInjector()
184144
var lines = editor.lines
185145
var cursorPosition = editor.cursorPosition
186146
var extraInfo = SuggestionInjector.ExtraInfo()
187147

188-
injector.acceptSuggestion(
189-
intoContentWithoutSuggestion: &lines,
190-
cursorPosition: &cursorPosition,
191-
completion: result.suggestion,
192-
extraInfo: &extraInfo
193-
)
148+
if let service = WidgetDataSource.shared.promptToCodes[fileURL]?.promptToCodeService {
149+
let suggestion = CopilotCompletion(
150+
text: service.code,
151+
position: service.selectionRange.start,
152+
uuid: UUID().uuidString,
153+
range: service.selectionRange,
154+
displayText: service.code
155+
)
194156

195-
result.cleanup()
157+
injector.acceptSuggestion(
158+
intoContentWithoutSuggestion: &lines,
159+
cursorPosition: &cursorPosition,
160+
completion: suggestion,
161+
extraInfo: &extraInfo
162+
)
196163

197-
return .init(
198-
content: String(lines.joined(separator: "")),
199-
newSelection: {
200-
if let startPosition = result.startPosition {
201-
return .init(start: startPosition, end: cursorPosition)
202-
}
203-
return .cursor(cursorPosition)
204-
}(),
205-
modifications: extraInfo.modifications
206-
)
164+
if service.isContinuous {
165+
service.selectionRange = .init(
166+
start: service.selectionRange.start,
167+
end: cursorPosition
168+
)
169+
presenter.presentPromptToCode(fileURL: fileURL)
170+
} else {
171+
WidgetDataSource.shared.removePromptToCode(for: fileURL)
172+
presenter.closePromptToCode(fileURL: fileURL)
173+
}
174+
175+
return .init(
176+
content: String(lines.joined(separator: "")),
177+
newSelection: .init(start: service.selectionRange.start, end: cursorPosition),
178+
modifications: extraInfo.modifications
179+
)
180+
} else if let acceptedSuggestion = workspace.acceptSuggestion(
181+
forFileAt: fileURL,
182+
editor: editor
183+
) {
184+
injector.acceptSuggestion(
185+
intoContentWithoutSuggestion: &lines,
186+
cursorPosition: &cursorPosition,
187+
completion: acceptedSuggestion,
188+
extraInfo: &extraInfo
189+
)
190+
191+
presenter.discardSuggestion(fileURL: fileURL)
192+
193+
return .init(
194+
content: String(lines.joined(separator: "")),
195+
newSelection: .cursor(cursorPosition),
196+
modifications: extraInfo.modifications
197+
)
198+
}
199+
200+
return nil
207201
}
208202

209203
func presentRealtimeSuggestions(editor: EditorContent) async throws -> UpdatedContent? {
@@ -348,13 +342,14 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
348342
defer { presenter.markAsProcessing(false) }
349343
let fileURL = try await Environment.fetchCurrentFileURL()
350344
let codeLanguage = languageIdentifierFromFileURL(fileURL)
351-
345+
352346
let (code, selection) = {
353347
guard var selection = editor.selections.last,
354348
selection.start != selection.end
355349
else { return ("", .cursor(editor.cursorPosition)) }
356350
if selection.start.line != selection.end.line {
357-
// when there are multiple lines start from char 0 so that it can keep the indentation.
351+
// when there are multiple lines start from char 0 so that it can keep the
352+
// indentation.
358353
selection.start = .init(line: selection.start.line, character: 0)
359354
}
360355
return (

Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,15 @@ struct PromptToCodePanel: View {
7575
.buttonStyle(.plain)
7676
} else {
7777
HStack {
78-
Toggle("Continuous Mode", isOn: $provider.isContinuous)
79-
.toggleStyle(.checkbox)
80-
78+
Toggle(
79+
"Continuous Mode",
80+
isOn: .init(
81+
get: { provider.isContinuous },
82+
set: { _ in provider.toggleContinuous() }
83+
)
84+
)
85+
.toggleStyle(.checkbox)
86+
8187
Button(action: {
8288
provider.cancel()
8389
}) {

0 commit comments

Comments
 (0)