11import SwiftUI
22
33struct CodeBlock : View {
4- var suggestion : SuggestionPanelViewModel . Suggestion
4+ @ObservedObject var suggestion : SuggestionProvider
5+ @Environment ( \. colorScheme) var colorScheme
56
67 var body : some View {
78 VStack {
8- ForEach ( 0 ..< suggestion. code. endIndex, id: \. self) { index in
9+ let code = suggestion. highlightedCode ( colorScheme: colorScheme)
10+ ForEach ( 0 ..< code. endIndex, id: \. self) { index in
911 HStack ( alignment: . firstTextBaseline) {
1012 Text ( " \( index + suggestion. startLineIndex + 1 ) " )
1113 . multilineTextAlignment ( . trailing)
1214 . foregroundColor ( . secondary)
1315 . frame ( minWidth: 40 )
14- Text ( AttributedString ( suggestion . code [ index] ) )
16+ Text ( AttributedString ( code [ index] ) )
1517 . foregroundColor ( . white. opacity ( 0.1 ) )
1618 . frame ( maxWidth: . infinity, alignment: . leading)
1719 . multilineTextAlignment ( . leading)
@@ -26,17 +28,15 @@ struct CodeBlock: View {
2628}
2729
2830struct CodeBlockSuggestionPanel : View {
29- @ObservedObject var viewModel : SuggestionPanelViewModel
30- var suggestion : SuggestionPanelViewModel . Suggestion
31+ @ObservedObject var suggestion : SuggestionProvider
3132
3233 struct ToolBar : View {
33- @ObservedObject var viewModel : SuggestionPanelViewModel
34- var suggestion : SuggestionPanelViewModel . Suggestion
34+ @ObservedObject var suggestion : SuggestionProvider
3535
3636 var body : some View {
3737 HStack {
3838 Button ( action: {
39- viewModel . onPreviousButtonTapped ? ( )
39+ suggestion . selectPreviousSuggestion ( )
4040 } ) {
4141 Image ( systemName: " chevron.left " )
4242 } . buttonStyle ( . plain)
@@ -47,21 +47,21 @@ struct CodeBlockSuggestionPanel: View {
4747 . monospacedDigit ( )
4848
4949 Button ( action: {
50- viewModel . onNextButtonTapped ? ( )
50+ suggestion . selectNextSuggestion ( )
5151 } ) {
5252 Image ( systemName: " chevron.right " )
5353 } . buttonStyle ( . plain)
5454
5555 Spacer ( )
5656
5757 Button ( action: {
58- viewModel . onRejectButtonTapped ? ( )
58+ suggestion . rejectSuggestion ( )
5959 } ) {
6060 Text ( " Reject " )
6161 } . buttonStyle ( CommandButtonStyle ( color: . gray) )
6262
6363 Button ( action: {
64- viewModel . onAcceptButtonTapped ? ( )
64+ suggestion . acceptSuggestion ( )
6565 } ) {
6666 Text ( " Accept " )
6767 } . buttonStyle ( CommandButtonStyle ( color: . indigo) )
@@ -80,8 +80,116 @@ struct CodeBlockSuggestionPanel: View {
8080 }
8181 . background ( Color . contentBackground)
8282
83- ToolBar ( viewModel : viewModel , suggestion: suggestion)
83+ ToolBar ( suggestion: suggestion)
8484 }
8585 . xcodeStyleFrame ( )
8686 }
8787}
88+
89+ // MARK: - Previews
90+
91+ struct CodeBlockSuggestionPanel_Dark_Preview : PreviewProvider {
92+ static var previews : some View {
93+ CodeBlockSuggestionPanel ( suggestion: SuggestionProvider (
94+ code: """
95+ LazyVGrid(columns: [GridItem(.fixed(30)), GridItem(.flexible())]) {
96+ ForEach(0..<viewModel.suggestion.count, id: \\ .self) { index in // lkjaskldjalksjdlkasjdlkajslkdjas
97+ Text(viewModel.suggestion[index])
98+ .frame(maxWidth: .infinity, alignment: .leading)
99+ .multilineTextAlignment(.leading)
100+ }
101+ """ ,
102+ language: " swift " ,
103+ startLineIndex: 8 ,
104+ suggestionCount: 2 ,
105+ currentSuggestionIndex: 0
106+ ) )
107+ . preferredColorScheme ( . dark)
108+ . frame ( width: 450 , height: 400 )
109+ . background {
110+ HStack {
111+ Color . red
112+ Color . green
113+ Color . blue
114+ }
115+ }
116+ }
117+ }
118+
119+ struct CodeBlockSuggestionPanel_Bright_Preview : PreviewProvider {
120+ static var previews : some View {
121+ CodeBlockSuggestionPanel ( suggestion: SuggestionProvider (
122+ code: """
123+ LazyVGrid(columns: [GridItem(.fixed(30)), GridItem(.flexible())]) {
124+ ForEach(0..<viewModel.suggestion.count, id: \\ .self) { index in // lkjaskldjalksjdlkasjdlkajslkdjas
125+ Text(viewModel.suggestion[index])
126+ .frame(maxWidth: .infinity, alignment: .leading)
127+ .multilineTextAlignment(.leading)
128+ }
129+ """ ,
130+ language: " swift " ,
131+ startLineIndex: 8 ,
132+ suggestionCount: 2 ,
133+ currentSuggestionIndex: 0
134+ ) )
135+ . preferredColorScheme ( . light)
136+ . frame ( width: 450 , height: 400 )
137+ . background {
138+ HStack {
139+ Color . red
140+ Color . green
141+ Color . blue
142+ }
143+ }
144+ }
145+ }
146+
147+ struct CodeBlockSuggestionPanel_Dark_Objc_Preview : PreviewProvider {
148+ static var previews : some View {
149+ CodeBlockSuggestionPanel ( suggestion: SuggestionProvider (
150+ code: """
151+ - (void)addSubview:(UIView *)view {
152+ [self addSubview:view];
153+ }
154+ """ ,
155+ language: " objective-c " ,
156+ startLineIndex: 8 ,
157+ suggestionCount: 2 ,
158+ currentSuggestionIndex: 0
159+ ) )
160+ . preferredColorScheme ( . dark)
161+ . frame ( width: 450 , height: 400 )
162+ . background {
163+ HStack {
164+ Color . red
165+ Color . green
166+ Color . blue
167+ }
168+ }
169+ }
170+ }
171+
172+ struct CodeBlockSuggestionPanel_Bright_Objc_Preview : PreviewProvider {
173+ static var previews : some View {
174+ CodeBlockSuggestionPanel ( suggestion: SuggestionProvider (
175+ code: """
176+ - (void)addSubview:(UIView *)view {
177+ [self addSubview:view];
178+ }
179+ """ ,
180+ language: " objective-c " ,
181+ startLineIndex: 8 ,
182+ suggestionCount: 2 ,
183+ currentSuggestionIndex: 0
184+ ) )
185+ . preferredColorScheme ( . light)
186+ . frame ( width: 450 , height: 400 )
187+ . background {
188+ HStack {
189+ Color . red
190+ Color . green
191+ Color . blue
192+ }
193+ }
194+ }
195+ }
0 commit comments