Skip to content

Commit f82b88e

Browse files
committed
Move presentation logic to PresentInWindowSuggestionPresenter
1 parent 729feb1 commit f82b88e

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import XPCShared
99
struct WindowBaseCommandHandler: SuggestionCommandHanlder {
1010
nonisolated init() {}
1111

12+
let presenter = PresentInWindowSuggestionPresenter()
13+
1214
func presentSuggestions(editor: EditorContent) async throws -> UpdatedContent? {
1315
Task {
1416
do {
@@ -21,8 +23,8 @@ struct WindowBaseCommandHandler: SuggestionCommandHanlder {
2123
}
2224

2325
private func _presentSuggestions(editor: EditorContent) async throws {
24-
markAsProcessing(true)
25-
defer { markAsProcessing(false) }
26+
presenter.markAsProcessing(true)
27+
defer { presenter.markAsProcessing(false) }
2628
let fileURL = try await Environment.fetchCurrentFileURL()
2729
let (workspace, filespace) = try await Workspace
2830
.fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL)
@@ -48,9 +50,9 @@ struct WindowBaseCommandHandler: SuggestionCommandHanlder {
4850
)
4951

5052
if let suggestion = filespace.presentingSuggestion {
51-
presentSuggestion(suggestion, lines: editor.lines, fileURL: fileURL)
53+
presenter.presentSuggestion(suggestion, lines: editor.lines, fileURL: fileURL)
5254
} else {
53-
discardSuggestion(fileURL: fileURL)
55+
presenter.discardSuggestion(fileURL: fileURL)
5456
}
5557
}
5658

@@ -62,8 +64,8 @@ struct WindowBaseCommandHandler: SuggestionCommandHanlder {
6264
}
6365

6466
private func _presentNextSuggestion(editor: EditorContent) async throws {
65-
markAsProcessing(true)
66-
defer { markAsProcessing(false) }
67+
presenter.markAsProcessing(true)
68+
defer { presenter.markAsProcessing(false) }
6769
let fileURL = try await Environment.fetchCurrentFileURL()
6870
let (workspace, filespace) = try await Workspace
6971
.fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL)
@@ -74,9 +76,9 @@ struct WindowBaseCommandHandler: SuggestionCommandHanlder {
7476
)
7577

7678
if let suggestion = filespace.presentingSuggestion {
77-
presentSuggestion(suggestion, lines: editor.lines, fileURL: fileURL)
79+
presenter.presentSuggestion(suggestion, lines: editor.lines, fileURL: fileURL)
7880
} else {
79-
discardSuggestion(fileURL: fileURL)
81+
presenter.discardSuggestion(fileURL: fileURL)
8082
}
8183
}
8284

@@ -88,8 +90,8 @@ struct WindowBaseCommandHandler: SuggestionCommandHanlder {
8890
}
8991

9092
private func _presentPreviousSuggestion(editor: EditorContent) async throws {
91-
markAsProcessing(true)
92-
defer { markAsProcessing(false) }
93+
presenter.markAsProcessing(true)
94+
defer { presenter.markAsProcessing(false) }
9395
let fileURL = try await Environment.fetchCurrentFileURL()
9496
let (workspace, filespace) = try await Workspace
9597
.fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL)
@@ -100,9 +102,9 @@ struct WindowBaseCommandHandler: SuggestionCommandHanlder {
100102
)
101103

102104
if let suggestion = filespace.presentingSuggestion {
103-
presentSuggestion(suggestion, lines: editor.lines, fileURL: fileURL)
105+
presenter.presentSuggestion(suggestion, lines: editor.lines, fileURL: fileURL)
104106
} else {
105-
discardSuggestion(fileURL: fileURL)
107+
presenter.discardSuggestion(fileURL: fileURL)
106108
}
107109
}
108110

@@ -114,20 +116,20 @@ struct WindowBaseCommandHandler: SuggestionCommandHanlder {
114116
}
115117

116118
private func _rejectSuggestion(editor: EditorContent) async throws {
117-
markAsProcessing(true)
118-
defer { markAsProcessing(false) }
119+
presenter.markAsProcessing(true)
120+
defer { presenter.markAsProcessing(false) }
119121
let fileURL = try await Environment.fetchCurrentFileURL()
120122
let (workspace, _) = try await Workspace.fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL)
121123
workspace.rejectSuggestion(forFileAt: fileURL)
122-
discardSuggestion(fileURL: fileURL)
124+
presenter.discardSuggestion(fileURL: fileURL)
123125
}
124126

125127
func acceptSuggestion(editor: EditorContent) async throws -> UpdatedContent? {
126-
markAsProcessing(true)
127-
defer { markAsProcessing(false) }
128+
presenter.markAsProcessing(true)
129+
defer { presenter.markAsProcessing(false) }
128130
Task {
129131
let fileURL = try await Environment.fetchCurrentFileURL()
130-
discardSuggestion(fileURL: fileURL)
132+
presenter.discardSuggestion(fileURL: fileURL)
131133
}
132134
return try await CommentBaseCommandHandler().acceptSuggestion(editor: editor)
133135
}
@@ -140,29 +142,4 @@ struct WindowBaseCommandHandler: SuggestionCommandHanlder {
140142
func generateRealtimeSuggestions(editor: EditorContent) async throws -> UpdatedContent? {
141143
try await presentSuggestions(editor: editor)
142144
}
143-
144-
func presentSuggestion(_ suggestion: CopilotCompletion, lines: [String], fileURL: URL) {
145-
Task { @MainActor in
146-
let controller = GraphicalUserInterfaceController.shared.suggestionWidget
147-
controller.suggestCode(
148-
suggestion.text,
149-
startLineIndex: suggestion.position.line,
150-
fileURL: fileURL
151-
)
152-
}
153-
}
154-
155-
func discardSuggestion(fileURL: URL) {
156-
Task { @MainActor in
157-
let controller = GraphicalUserInterfaceController.shared.suggestionWidget
158-
controller.discardSuggestion(fileURL: fileURL)
159-
}
160-
}
161-
162-
func markAsProcessing(_ isProcessing: Bool) {
163-
Task { @MainActor in
164-
let controller = GraphicalUserInterfaceController.shared.suggestionWidget
165-
controller.markAsProcessing(isProcessing)
166-
}
167-
}
168145
}
Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
import CopilotModel
2+
import Foundation
3+
14
struct PresentInWindowSuggestionPresenter {
2-
5+
func presentSuggestion(_ suggestion: CopilotCompletion, lines: [String], fileURL: URL) {
6+
Task { @MainActor in
7+
let controller = GraphicalUserInterfaceController.shared.suggestionWidget
8+
controller.suggestCode(
9+
suggestion.text,
10+
startLineIndex: suggestion.position.line,
11+
fileURL: fileURL
12+
)
13+
}
14+
}
15+
16+
func discardSuggestion(fileURL: URL) {
17+
Task { @MainActor in
18+
let controller = GraphicalUserInterfaceController.shared.suggestionWidget
19+
controller.discardSuggestion(fileURL: fileURL)
20+
}
21+
}
22+
23+
func markAsProcessing(_ isProcessing: Bool) {
24+
Task { @MainActor in
25+
let controller = GraphicalUserInterfaceController.shared.suggestionWidget
26+
controller.markAsProcessing(isProcessing)
27+
}
28+
}
329
}

0 commit comments

Comments
 (0)