11import Combine
2+ import CommandHandler
3+ import Dependencies
24import Perception
35import SharedUIComponents
46import SuggestionBasic
57import SwiftUI
68import 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+
832struct 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];
0 commit comments