Skip to content

Commit e9440ea

Browse files
committed
Remove CodeSuggestionProvider in favor of PresentingCodeSuggestion and CommandHandler
1 parent 00760a7 commit e9440ea

File tree

9 files changed

+99
-123
lines changed

9 files changed

+99
-123
lines changed

Core/Sources/Service/GUI/WidgetDataSource.swift

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import SuggestionWidget
1414
final class WidgetDataSource {}
1515

1616
extension WidgetDataSource: SuggestionWidgetDataSource {
17-
func suggestionForFile(at url: URL) async -> CodeSuggestionProvider? {
17+
func suggestionForFile(at url: URL) async -> PresentingCodeSuggestion? {
1818
for workspace in Service.shared.workspacePool.workspaces.values {
1919
if let filespace = workspace.filespaces[url],
2020
let suggestion = filespace.presentingSuggestion
@@ -24,40 +24,7 @@ extension WidgetDataSource: SuggestionWidgetDataSource {
2424
language: filespace.language.rawValue,
2525
startLineIndex: suggestion.position.line,
2626
suggestionCount: filespace.suggestions.count,
27-
currentSuggestionIndex: filespace.suggestionIndex,
28-
onSelectPreviousSuggestionTapped: {
29-
Task {
30-
let handler = PseudoCommandHandler()
31-
await handler.presentPreviousSuggestion()
32-
}
33-
},
34-
onSelectNextSuggestionTapped: {
35-
Task {
36-
let handler = PseudoCommandHandler()
37-
await handler.presentNextSuggestion()
38-
}
39-
},
40-
onRejectSuggestionTapped: {
41-
Task {
42-
let handler = PseudoCommandHandler()
43-
await handler.rejectSuggestions()
44-
NSWorkspace.activatePreviousActiveXcode()
45-
}
46-
},
47-
onAcceptSuggestionTapped: {
48-
Task {
49-
let handler = PseudoCommandHandler()
50-
await handler.acceptSuggestion()
51-
NSWorkspace.activatePreviousActiveXcode()
52-
}
53-
},
54-
onDismissSuggestionTapped: {
55-
Task {
56-
let handler = PseudoCommandHandler()
57-
await handler.dismissSuggestion()
58-
NSWorkspace.activatePreviousActiveXcode()
59-
}
60-
}
27+
currentSuggestionIndex: filespace.suggestionIndex
6128
)
6229
}
6330
}

Core/Sources/SuggestionWidget/FeatureReducers/PanelFeature.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public struct PanelFeature {
2525

2626
public enum Action: Equatable {
2727
case presentSuggestion
28-
case presentSuggestionProvider(CodeSuggestionProvider, displayContent: Bool)
28+
case presentSuggestionProvider(PresentingCodeSuggestion, displayContent: Bool)
2929
case presentError(String)
3030
case presentPromptToCode(PromptToCodeGroup.PromptToCodeInitialState)
3131
case displayPanelContent
@@ -136,7 +136,7 @@ public struct PanelFeature {
136136
}
137137
}
138138

139-
func fetchSuggestionProvider(fileURL: URL) async -> CodeSuggestionProvider? {
139+
func fetchSuggestionProvider(fileURL: URL) async -> PresentingCodeSuggestion? {
140140
guard let provider = await suggestionWidgetControllerDependency
141141
.suggestionWidgetDataSource?
142142
.suggestionForFile(at: fileURL) else { return nil }

Core/Sources/SuggestionWidget/FeatureReducers/SharedPanelFeature.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import SwiftUI
66
public struct SharedPanelFeature {
77
public struct Content: Equatable {
88
public var promptToCodeGroup = PromptToCodeGroup.State()
9-
var suggestion: CodeSuggestionProvider?
9+
var suggestion: PresentingCodeSuggestion?
1010
public var promptToCode: PromptToCode.State? { promptToCodeGroup.activePromptToCode }
1111
var error: String?
1212
}

Core/Sources/SuggestionWidget/FeatureReducers/SuggestionPanelFeature.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import SwiftUI
66
public struct SuggestionPanelFeature {
77
@ObservableState
88
public struct State: Equatable {
9-
var content: CodeSuggestionProvider?
9+
var content: PresentingCodeSuggestion?
1010
var colorScheme: ColorScheme = .light
1111
var alignTopToAnchor = false
1212
var isPanelDisplayed: Bool = false

Core/Sources/SuggestionWidget/Providers/CodeSuggestionProvider.swift

Lines changed: 0 additions & 60 deletions
This file was deleted.

Core/Sources/SuggestionWidget/SharedPanelView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ struct SharedPanelView: View {
101101
}
102102

103103
@ViewBuilder
104-
func suggestion(_ suggestion: CodeSuggestionProvider) -> some View {
104+
func suggestion(_ suggestion: PresentingCodeSuggestion) -> some View {
105105
switch suggestionPresentationMode {
106106
case .nearbyTextCursor:
107107
EmptyView()

Core/Sources/SuggestionWidget/SuggestionPanelContent/CodeBlockSuggestionPanel.swift

Lines changed: 88 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
11
import Combine
2+
import CommandHandler
3+
import Dependencies
24
import Perception
35
import SharedUIComponents
46
import SuggestionBasic
57
import SwiftUI
68
import XcodeInspector
79

10+
public struct PresentingCodeSuggestion: Equatable {
11+
public var code: String
12+
public var language: String
13+
public var startLineIndex: Int
14+
public var suggestionCount: Int
15+
public var currentSuggestionIndex: Int
16+
17+
public init(
18+
code: String,
19+
language: String,
20+
startLineIndex: Int,
21+
suggestionCount: Int,
22+
currentSuggestionIndex: Int
23+
) {
24+
self.code = code
25+
self.language = language
26+
self.startLineIndex = startLineIndex
27+
self.suggestionCount = suggestionCount
28+
self.currentSuggestionIndex = currentSuggestionIndex
29+
}
30+
}
31+
832
struct CodeBlockSuggestionPanel: View {
9-
let suggestion: CodeSuggestionProvider
10-
@Environment(TextCursorTracker.self) var cursorPositionTracker
33+
let suggestion: PresentingCodeSuggestion
34+
@Environment(TextCursorTracker.self) var textCursorTracker
1135
@Environment(\.colorScheme) var colorScheme
1236
@AppStorage(\.suggestionCodeFont) var codeFont
1337
@AppStorage(\.suggestionDisplayCompactMode) var suggestionDisplayCompactMode
@@ -20,13 +44,17 @@ struct CodeBlockSuggestionPanel: View {
2044
@AppStorage(\.codeBackgroundColorDark) var codeBackgroundColorDark
2145

2246
struct ToolBar: View {
23-
let suggestion: CodeSuggestionProvider
47+
@Dependency(\.commandHandler) var commandHandler
48+
let suggestion: PresentingCodeSuggestion
2449

2550
var body: some View {
2651
WithPerceptionTracking {
2752
HStack {
2853
Button(action: {
29-
suggestion.selectPreviousSuggestion()
54+
Task {
55+
await commandHandler.presentPreviousSuggestion()
56+
NSWorkspace.activatePreviousActiveXcode()
57+
}
3058
}) {
3159
Image(systemName: "chevron.left")
3260
}.buttonStyle(.plain)
@@ -37,27 +65,39 @@ struct CodeBlockSuggestionPanel: View {
3765
.monospacedDigit()
3866

3967
Button(action: {
40-
suggestion.selectNextSuggestion()
68+
Task {
69+
await commandHandler.presentNextSuggestion()
70+
NSWorkspace.activatePreviousActiveXcode()
71+
}
4172
}) {
4273
Image(systemName: "chevron.right")
4374
}.buttonStyle(.plain)
4475

4576
Spacer()
4677

4778
Button(action: {
48-
suggestion.dismissSuggestion()
79+
Task {
80+
await commandHandler.dismissSuggestion()
81+
NSWorkspace.activatePreviousActiveXcode()
82+
}
4983
}) {
5084
Text("Dismiss").foregroundStyle(.tertiary).padding(.trailing, 4)
5185
}.buttonStyle(.plain)
5286

5387
Button(action: {
54-
suggestion.rejectSuggestion()
88+
Task {
89+
await commandHandler.rejectSuggestions()
90+
NSWorkspace.activatePreviousActiveXcode()
91+
}
5592
}) {
5693
Text("Reject")
5794
}.buttonStyle(CommandButtonStyle(color: .gray))
5895

5996
Button(action: {
60-
suggestion.acceptSuggestion()
97+
Task {
98+
await commandHandler.acceptSuggestion()
99+
NSWorkspace.activatePreviousActiveXcode()
100+
}
61101
}) {
62102
Text("Accept")
63103
}.buttonStyle(CommandButtonStyle(color: .accentColor))
@@ -70,13 +110,17 @@ struct CodeBlockSuggestionPanel: View {
70110
}
71111

72112
struct CompactToolBar: View {
73-
let suggestion: CodeSuggestionProvider
113+
@Dependency(\.commandHandler) var commandHandler
114+
let suggestion: PresentingCodeSuggestion
74115

75116
var body: some View {
76117
WithPerceptionTracking {
77118
HStack {
78119
Button(action: {
79-
suggestion.selectPreviousSuggestion()
120+
Task {
121+
await commandHandler.presentPreviousSuggestion()
122+
NSWorkspace.activatePreviousActiveXcode()
123+
}
80124
}) {
81125
Image(systemName: "chevron.left")
82126
}.buttonStyle(.plain)
@@ -87,15 +131,21 @@ struct CodeBlockSuggestionPanel: View {
87131
.monospacedDigit()
88132

89133
Button(action: {
90-
suggestion.selectNextSuggestion()
134+
Task {
135+
await commandHandler.presentNextSuggestion()
136+
NSWorkspace.activatePreviousActiveXcode()
137+
}
91138
}) {
92139
Image(systemName: "chevron.right")
93140
}.buttonStyle(.plain)
94141

95142
Spacer()
96143

97144
Button(action: {
98-
suggestion.dismissSuggestion()
145+
Task {
146+
await commandHandler.dismissSuggestion()
147+
NSWorkspace.activatePreviousActiveXcode()
148+
}
99149
}) {
100150
Image(systemName: "xmark")
101151
}.buttonStyle(.plain)
@@ -113,6 +163,11 @@ struct CodeBlockSuggestionPanel: View {
113163
VStack(spacing: 0) {
114164
CustomScrollView {
115165
WithPerceptionTracking {
166+
let diffResult = Self.diff(
167+
suggestion: suggestion,
168+
textCursorTracker: textCursorTracker
169+
)
170+
116171
AsyncCodeBlock(
117172
code: suggestion.code,
118173
language: suggestion.language,
@@ -134,10 +189,7 @@ struct CodeBlockSuggestionPanel: View {
134189
}
135190
return nil
136191
}(),
137-
dimmedCharacterCount: suggestion.startLineIndex
138-
== cursorPositionTracker.cursorPosition.line
139-
? cursorPositionTracker.cursorPosition.character
140-
: 0
192+
dimmedCharacterCount: 0
141193
)
142194
.frame(maxWidth: .infinity)
143195
.background({ () -> Color in
@@ -169,12 +221,29 @@ struct CodeBlockSuggestionPanel: View {
169221
}())
170222
}
171223
}
224+
225+
struct DiffResult {
226+
var dimmedRanges: [Range<String.Index>]
227+
var mutatedRanges: [Range<String.Index>]
228+
var deletedRanges: [Range<String.Index>]
229+
}
230+
231+
@MainActor
232+
static func diff(
233+
suggestion: PresentingCodeSuggestion,
234+
textCursorTracker: TextCursorTracker
235+
) -> DiffResult {
236+
let typedContentCount = suggestion.startLineIndex == textCursorTracker.cursorPosition.line
237+
? textCursorTracker.cursorPosition.character
238+
: 0
239+
return .init(dimmedRanges: [], mutatedRanges: [], deletedRanges: [])
240+
}
172241
}
173242

174243
// MARK: - Previews
175244

176245
#Preview("Code Block Suggestion Panel") {
177-
CodeBlockSuggestionPanel(suggestion: CodeSuggestionProvider(
246+
CodeBlockSuggestionPanel(suggestion: PresentingCodeSuggestion(
178247
code: """
179248
LazyVGrid(columns: [GridItem(.fixed(30)), GridItem(.flexible())]) {
180249
ForEach(0..<viewModel.suggestion.count, id: \\.self) { index in // lkjaskldjalksjdlkasjdlkajslkdjas
@@ -202,7 +271,7 @@ struct CodeBlockSuggestionPanel: View {
202271
}
203272

204273
#Preview("Code Block Suggestion Panel Compact Mode") {
205-
CodeBlockSuggestionPanel(suggestion: CodeSuggestionProvider(
274+
CodeBlockSuggestionPanel(suggestion: PresentingCodeSuggestion(
206275
code: """
207276
LazyVGrid(columns: [GridItem(.fixed(30)), GridItem(.flexible())]) {
208277
ForEach(0..<viewModel.suggestion.count, id: \\.self) { index in // lkjaskldjalksjdlkasjdlkajslkdjas
@@ -231,7 +300,7 @@ struct CodeBlockSuggestionPanel: View {
231300
}
232301

233302
#Preview("Code Block Suggestion Panel Highlight ObjC") {
234-
CodeBlockSuggestionPanel(suggestion: CodeSuggestionProvider(
303+
CodeBlockSuggestionPanel(suggestion: PresentingCodeSuggestion(
235304
code: """
236305
- (void)addSubview:(UIView *)view {
237306
[self addSubview:view];

Core/Sources/SuggestionWidget/SuggestionWidgetDataSource.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import Foundation
22

33
public protocol SuggestionWidgetDataSource {
4-
func suggestionForFile(at url: URL) async -> CodeSuggestionProvider?
4+
func suggestionForFile(at url: URL) async -> PresentingCodeSuggestion?
55
}
66

77
struct MockWidgetDataSource: SuggestionWidgetDataSource {
8-
func suggestionForFile(at url: URL) async -> CodeSuggestionProvider? {
9-
return CodeSuggestionProvider(
8+
func suggestionForFile(at url: URL) async -> PresentingCodeSuggestion? {
9+
return PresentingCodeSuggestion(
1010
code: """
1111
func test() {
1212
let x = 1

0 commit comments

Comments
 (0)