@@ -160,6 +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 [ ] > ( [ ] )
163
164
const defaultEditorValue = `
164
165
\t\t\t\t\t\t\t ____ _____ __ __ ___ __ __ ___ ____ _____
165
166
\t\t\t\t\t\t\t| _ \\ | ____| | \\/ | |_ _| \\ \\/ / |_ _| | _ \\ | ____|
@@ -377,6 +378,7 @@ export const EditorUI = (props: EditorUIProps) => {
377
378
} else if ( file . language === 'python' ) {
378
379
monacoRef . current . editor . setModelLanguage ( file . model , 'remix-vyper' )
379
380
}
381
+
380
382
// @ts -ignore
381
383
props . plugin . emit ( 'addModel' , editorRef . current . getModel ( ) . getValue ( ) , 'remix-solidity' , props . currentFile + '-ai' , false )
382
384
} , [ props . currentFile , isDiff , currentDiffFile ] )
@@ -629,6 +631,22 @@ export const EditorUI = (props: EditorUIProps) => {
629
631
}
630
632
}
631
633
634
+ props . plugin . on ( 'fileManager' , 'currentFileChanged' , ( file : string ) => {
635
+ if ( file + '-ai' !== currentDiffFile ) {
636
+ removeAllWidgets ( )
637
+ }
638
+ } )
639
+
640
+ function removeAllWidgets ( ) {
641
+ if ( widgetIds . length === 0 ) return
642
+ for ( const widgetId of widgetIds ) {
643
+ editorRef . current && editorRef . current . removeContentWidget ( {
644
+ getId : ( ) => widgetId
645
+ } )
646
+ }
647
+ setWidgetIds ( [ ] )
648
+ }
649
+
632
650
function setReducerListener ( ) {
633
651
if ( diffEditorRef . current && diffEditorRef . current . getModifiedEditor ( ) && editorRef . current ) {
634
652
reducerListener ( props . plugin , dispatch , monacoRef . current , [ diffEditorRef . current . getModifiedEditor ( ) , editorRef . current ] , props . events )
@@ -1148,6 +1166,7 @@ export const EditorUI = (props: EditorUIProps) => {
1148
1166
}
1149
1167
1150
1168
function showCustomDiff ( uri : string ) {
1169
+ removeAllWidgets ( )
1151
1170
const lineChanges : monacoTypes . editor . ILineChange [ ] = diffEditorRef . current . getLineChanges ( )
1152
1171
let totalLineDifference = 0
1153
1172
const decoratorListCollection = [ ]
@@ -1198,6 +1217,7 @@ export const EditorUI = (props: EditorUIProps) => {
1198
1217
} ,
1199
1218
] )
1200
1219
decoratorList . clear ( )
1220
+ setWidgetIds ( widgetIds . filter ( ( id ) => id !== `accept_decline_widget${ index } ` ) )
1201
1221
}
1202
1222
1203
1223
const rejectHandler = ( decoratorList ) => {
@@ -1212,6 +1232,7 @@ export const EditorUI = (props: EditorUIProps) => {
1212
1232
} ,
1213
1233
] )
1214
1234
decoratorList . clear ( )
1235
+ setWidgetIds ( widgetIds . filter ( ( id ) => id !== `accept_decline_widget${ index } ` ) )
1215
1236
}
1216
1237
1217
1238
const acceptAllHandler = ( ) => {
@@ -1232,10 +1253,13 @@ export const EditorUI = (props: EditorUIProps) => {
1232
1253
} )
1233
1254
}
1234
1255
1256
+ const widgetId = `accept_decline_widget${ index } `
1257
+
1258
+ setWidgetIds ( [ ...widgetIds , widgetId ] )
1235
1259
if ( index === 0 ) {
1236
- addAcceptDeclineWidget ( `accept_decline_widget ${ index } ` , editorRef . current , { column : 0 , lineNumber : modifiedStartLine + 1 } , ( ) => acceptHandler ( decoratorList ) , ( ) => rejectHandler ( decoratorList ) , acceptAllHandler , rejectAllHandler )
1260
+ addAcceptDeclineWidget ( widgetId , editorRef . current , { column : 0 , lineNumber : modifiedStartLine + 1 } , ( ) => acceptHandler ( decoratorList ) , ( ) => rejectHandler ( decoratorList ) , acceptAllHandler , rejectAllHandler )
1237
1261
} else {
1238
- addAcceptDeclineWidget ( `accept_decline_widget ${ index } ` , editorRef . current , { column : 0 , lineNumber : modifiedStartLine + 1 } , ( ) => acceptHandler ( decoratorList ) , ( ) => rejectHandler ( decoratorList ) )
1262
+ addAcceptDeclineWidget ( widgetId , editorRef . current , { column : 0 , lineNumber : modifiedStartLine + 1 } , ( ) => acceptHandler ( decoratorList ) , ( ) => rejectHandler ( decoratorList ) )
1239
1263
}
1240
1264
totalLineDifference += linesCount
1241
1265
} )
0 commit comments