Skip to content

Commit f652d0a

Browse files
committed
Merge branch 'feature/present-errors-in-widget' into develop
2 parents 7e982b7 + cfa7323 commit f652d0a

7 files changed

Lines changed: 217 additions & 117 deletions

File tree

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
1717
do {
1818
try await _presentSuggestions(editor: editor)
1919
} catch {
20+
presenter.presentError(error)
2021
Logger.service.error(error)
2122
}
2223
}
@@ -63,7 +64,11 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
6364

6465
func presentNextSuggestion(editor: EditorContent) async throws -> UpdatedContent? {
6566
Task {
66-
try await _presentNextSuggestion(editor: editor)
67+
do {
68+
try await _presentNextSuggestion(editor: editor)
69+
} catch {
70+
presenter.presentError(error)
71+
}
6772
}
6873
return nil
6974
}
@@ -92,7 +97,11 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
9297

9398
func presentPreviousSuggestion(editor: EditorContent) async throws -> UpdatedContent? {
9499
Task {
95-
try await _presentPreviousSuggestion(editor: editor)
100+
do {
101+
try await _presentPreviousSuggestion(editor: editor)
102+
} catch {
103+
presenter.presentError(error)
104+
}
96105
}
97106
return nil
98107
}
@@ -121,7 +130,11 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
121130

122131
func rejectSuggestion(editor: EditorContent) async throws -> UpdatedContent? {
123132
Task {
124-
try await _rejectSuggestion(editor: editor)
133+
do {
134+
try await _rejectSuggestion(editor: editor)
135+
} catch {
136+
presenter.presentError(error)
137+
}
125138
}
126139
return nil
127140
}
@@ -147,6 +160,7 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
147160
}
148161
return result
149162
} catch {
163+
presenter.presentError(error)
150164
throw error
151165
}
152166
}

Core/Sources/Service/SuggestionPresenter/PresentInWindowSuggestionPresenter.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,11 @@ struct PresentInWindowSuggestionPresenter {
3636
controller.markAsProcessing(isProcessing)
3737
}
3838
}
39+
40+
func presentError(_ error: Error) {
41+
Task { @MainActor in
42+
let controller = GraphicalUserInterfaceController.shared.suggestionWidget
43+
controller.presentError(error.localizedDescription)
44+
}
45+
}
3946
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import SwiftUI
2+
3+
struct CodeBlock: View {
4+
var suggestion: SuggestionPanelViewModel.Suggestion
5+
6+
var body: some View {
7+
VStack {
8+
ForEach(0..<suggestion.code.endIndex, id: \.self) { index in
9+
HStack(alignment: .firstTextBaseline) {
10+
Text("\(index + suggestion.startLineIndex + 1)")
11+
.multilineTextAlignment(.trailing)
12+
.foregroundColor(.secondary)
13+
.frame(minWidth: 40)
14+
Text(AttributedString(suggestion.code[index]))
15+
.foregroundColor(.white.opacity(0.1))
16+
.frame(maxWidth: .infinity, alignment: .leading)
17+
.multilineTextAlignment(.leading)
18+
.lineSpacing(4)
19+
}
20+
}
21+
}
22+
.foregroundColor(.white)
23+
.font(.system(size: 12, design: .monospaced))
24+
.padding()
25+
}
26+
}
27+
28+
struct CodeBlockSuggestionPanel: View {
29+
@ObservedObject var viewModel: SuggestionPanelViewModel
30+
var suggestion: SuggestionPanelViewModel.Suggestion
31+
32+
struct ToolBar: View {
33+
@ObservedObject var viewModel: SuggestionPanelViewModel
34+
var suggestion: SuggestionPanelViewModel.Suggestion
35+
36+
var body: some View {
37+
HStack {
38+
Button(action: {
39+
viewModel.onPreviousButtonTapped?()
40+
}) {
41+
Image(systemName: "chevron.left")
42+
}.buttonStyle(.plain)
43+
44+
Text(
45+
"\(suggestion.currentSuggestionIndex + 1) / \(suggestion.suggestionCount)"
46+
)
47+
.monospacedDigit()
48+
49+
Button(action: {
50+
viewModel.onNextButtonTapped?()
51+
}) {
52+
Image(systemName: "chevron.right")
53+
}.buttonStyle(.plain)
54+
55+
Spacer()
56+
57+
Button(action: {
58+
viewModel.onRejectButtonTapped?()
59+
}) {
60+
Text("Reject")
61+
}.buttonStyle(CommandButtonStyle(color: .gray))
62+
63+
Button(action: {
64+
viewModel.onAcceptButtonTapped?()
65+
}) {
66+
Text("Accept")
67+
}.buttonStyle(CommandButtonStyle(color: .indigo))
68+
}
69+
.padding()
70+
.foregroundColor(.secondary)
71+
.background(.regularMaterial)
72+
}
73+
}
74+
75+
var body: some View {
76+
VStack(spacing: 0) {
77+
ScrollView {
78+
CodeBlock(suggestion: suggestion)
79+
.frame(maxWidth: .infinity)
80+
}
81+
.background(Color(nsColor: {
82+
switch viewModel.colorScheme {
83+
case .dark:
84+
return #colorLiteral(red: 0.1580096483, green: 0.1730263829, blue: 0.2026666105, alpha: 1)
85+
case .light:
86+
return .white
87+
@unknown default:
88+
return .white
89+
}
90+
}()))
91+
92+
ToolBar(viewModel: viewModel, suggestion: suggestion)
93+
}
94+
}
95+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import SwiftUI
2+
3+
struct ErrorPanel: View {
4+
@ObservedObject var viewModel: SuggestionPanelViewModel
5+
var description: String
6+
7+
var body: some View {
8+
ZStack(alignment: .topTrailing) {
9+
Text(description)
10+
.multilineTextAlignment(.leading)
11+
.frame(maxWidth: .infinity, alignment: .leading)
12+
.foregroundColor(.white)
13+
.padding()
14+
.background(Color.red)
15+
16+
// close button
17+
Button(action: {
18+
viewModel.isPanelDisplayed = false
19+
viewModel.content = .empty
20+
}) {
21+
Image(systemName: "xmark")
22+
.padding([.leading, .bottom], 16)
23+
.padding([.top, .trailing], 8)
24+
.foregroundColor(.white)
25+
}
26+
.buttonStyle(.plain)
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)