@@ -160,8 +160,7 @@ export const EditorUI = (props: EditorUIProps) => {
160
160
const [ isDiff , setIsDiff ] = useState ( props . isDiff || false )
161
161
const [ currentDiffFile , setCurrentDiffFile ] = useState ( props . currentDiffFile || '' )
162
162
const [ isPromptSuggestion , setIsPromptSuggestion ] = useState ( false )
163
- const [ widgetIds , setWidgetIds ] = useState < string [ ] > ( [ ] )
164
- const [ decoratorListCollection , setDecoratorListCollection ] = useState < any [ ] > ( [ ] )
163
+ const [ decoratorListCollection , setDecoratorListCollection ] = useState < Record < string , any > > ( { } )
165
164
const defaultEditorValue = `
166
165
\t\t\t\t\t\t\t ____ _____ __ __ ___ __ __ ___ ____ _____
167
166
\t\t\t\t\t\t\t| _ \\ | ____| | \\/ | |_ _| \\ \\/ / |_ _| | _ \\ | ____|
@@ -634,18 +633,18 @@ export const EditorUI = (props: EditorUIProps) => {
634
633
635
634
props . plugin . on ( 'fileManager' , 'currentFileChanged' , ( file : string ) => {
636
635
if ( file + '-ai' !== currentDiffFile ) {
637
- removeAllWidgets ( )
636
+ removeAllWidgetsAndDecorators ( )
638
637
}
639
638
} )
640
639
641
- function removeAllWidgets ( ) {
642
- if ( widgetIds . length === 0 ) return
643
- for ( const widgetId of widgetIds ) {
640
+ function removeAllWidgetsAndDecorators ( ) {
641
+ if ( Object . keys ( decoratorListCollection ) . length === 0 ) return
642
+ Object . keys ( decoratorListCollection ) . forEach ( ( widgetId ) => {
644
643
editorRef . current && editorRef . current . removeContentWidget ( {
645
644
getId : ( ) => widgetId
646
645
} )
647
- }
648
- setWidgetIds ( [ ] )
646
+ } )
647
+ setDecoratorListCollection ( { } )
649
648
}
650
649
651
650
function setReducerListener ( ) {
@@ -833,6 +832,16 @@ export const EditorUI = (props: EditorUIProps) => {
833
832
const funcRange = await props . plugin . call ( 'codeParser' , "getLineColumnOfNode" , { src : functionNode . src } )
834
833
const newLineCount = ( outputFunctionComments [ currentFunction . current ] || '' ) . split ( '\n' ) . length
835
834
835
+ setDecoratorListCollection ( decoratorListCollection => {
836
+ Object . keys ( decoratorListCollection ) . forEach ( ( widgetId ) => {
837
+ const decoratorList = decoratorListCollection [ widgetId ]
838
+ if ( decoratorList ) rejectHandler ( decoratorList , widgetId )
839
+ editorRef . current . removeContentWidget ( {
840
+ getId : ( ) => widgetId
841
+ } )
842
+ } )
843
+ return { }
844
+ } )
836
845
if ( functionNode . documentation ) {
837
846
const docsRange = await props . plugin . call ( 'codeParser' , "getLineColumnOfNode" , { src : functionNode . documentation . src } )
838
847
const docs = editor . getModel ( ) . getValueInRange ( new monacoRef . current . Range ( docsRange . start . line , docsRange . start . column , funcRange . start . line , 1000 ) )
@@ -863,7 +872,7 @@ export const EditorUI = (props: EditorUIProps) => {
863
872
const widgetId = `accept_decline_widget${ Math . random ( ) . toString ( 36 ) . substring ( 2 , 15 ) } `
864
873
865
874
setCurrentDiffFile ( uri )
866
- setWidgetIds ( widgetIds => [ ...widgetIds , widgetId ] )
875
+ setDecoratorListCollection ( decoratorListCollection => ( { ...decoratorListCollection , [ widgetId ] : decoratorList } ) )
867
876
addAcceptDeclineWidget ( widgetId , editorRef . current , { column : 0 , lineNumber : docsRange . start . line + 2 } , ( ) => acceptHandler ( decoratorList , widgetId ) , ( ) => rejectHandler ( decoratorList , widgetId ) )
868
877
} else {
869
878
editorRef . current . executeEdits ( 'newDocs' , [
@@ -883,7 +892,7 @@ export const EditorUI = (props: EditorUIProps) => {
883
892
const widgetId = `accept_decline_widget${ Math . random ( ) . toString ( 36 ) . substring ( 2 , 15 ) } `
884
893
885
894
setCurrentDiffFile ( uri )
886
- setWidgetIds ( widgetIds => [ ...widgetIds , widgetId ] )
895
+ setDecoratorListCollection ( decoratorListCollection => ( { ...decoratorListCollection , [ widgetId ] : decoratorList } ) )
887
896
addAcceptDeclineWidget ( widgetId , editorRef . current , { column : 0 , lineNumber : funcRange . start . line + 2 } , ( ) => acceptHandler ( decoratorList , widgetId ) , ( ) => rejectHandler ( decoratorList , widgetId ) )
888
897
}
889
898
}
@@ -1226,7 +1235,10 @@ export const EditorUI = (props: EditorUIProps) => {
1226
1235
] )
1227
1236
}
1228
1237
decoratorList . clear ( )
1229
- setWidgetIds ( widgetIds . filter ( ( id ) => id !== widgetId ) )
1238
+ setDecoratorListCollection ( decoratorListCollection => {
1239
+ const { [ widgetId ] : _ , ...rest } = decoratorListCollection
1240
+ return rest
1241
+ } )
1230
1242
}
1231
1243
1232
1244
function rejectHandler ( decoratorList , widgetId ) {
@@ -1243,33 +1255,36 @@ export const EditorUI = (props: EditorUIProps) => {
1243
1255
] )
1244
1256
}
1245
1257
decoratorList . clear ( )
1246
- setWidgetIds ( widgetIds . filter ( ( id ) => id !== widgetId ) )
1258
+ setDecoratorListCollection ( decoratorListCollection => {
1259
+ const { [ widgetId ] : _ , ...rest } = decoratorListCollection
1260
+ return rest
1261
+ } )
1247
1262
}
1248
1263
1249
1264
function acceptAllHandler ( ) {
1250
- decoratorListCollection . forEach ( ( decoratorList , index ) => {
1251
- const widgetId = `accept_decline_widget ${ index } `
1265
+ Object . keys ( decoratorListCollection ) . forEach ( ( widgetId ) => {
1266
+ const decoratorList = decoratorListCollection [ widgetId ]
1252
1267
1253
1268
acceptHandler ( decoratorList , widgetId )
1254
1269
editorRef . current . removeContentWidget ( {
1255
- getId : ( ) => `accept_decline_widget ${ index } `
1270
+ getId : ( ) => widgetId
1256
1271
} )
1257
1272
} )
1258
1273
}
1259
1274
1260
1275
function rejectAllHandler ( ) {
1261
- decoratorListCollection . forEach ( ( decoratorList , index ) => {
1262
- const widgetId = `accept_decline_widget ${ index } `
1276
+ Object . keys ( decoratorListCollection ) . forEach ( ( widgetId ) => {
1277
+ const decoratorList = decoratorListCollection [ widgetId ]
1263
1278
1264
1279
rejectHandler ( decoratorList , widgetId )
1265
1280
editorRef . current . removeContentWidget ( {
1266
- getId : ( ) => `accept_decline_widget ${ index } `
1281
+ getId : ( ) => widgetId
1267
1282
} )
1268
1283
} )
1269
1284
}
1270
1285
1271
1286
function showCustomDiff ( uri : string ) {
1272
- removeAllWidgets ( )
1287
+ removeAllWidgetsAndDecorators ( )
1273
1288
const lineChanges : monacoTypes . editor . ILineChange [ ] = diffEditorRef . current . getLineChanges ( )
1274
1289
let totalLineDifference = 0
1275
1290
@@ -1304,12 +1319,9 @@ export const EditorUI = (props: EditorUIProps) => {
1304
1319
marginClassName : 'modifiedChangesDecoration' ,
1305
1320
}
1306
1321
} ] )
1307
-
1308
- setDecoratorListCollection ( [ ...decoratorListCollection , decoratorList ] )
1309
-
1310
1322
const widgetId = `accept_decline_widget${ index } `
1311
1323
1312
- setWidgetIds ( [ ...widgetIds , widgetId ] )
1324
+ setDecoratorListCollection ( decoratorListCollection => ( { ...decoratorListCollection , [ widgetId ] : decoratorList } ) )
1313
1325
if ( index === 0 ) {
1314
1326
addAcceptDeclineWidget ( widgetId , editorRef . current , { column : 0 , lineNumber : modifiedStartLine + 1 } , ( ) => acceptHandler ( decoratorList , widgetId ) , ( ) => rejectHandler ( decoratorList , widgetId ) , acceptAllHandler , rejectAllHandler )
1315
1327
} else {
0 commit comments