@@ -343,6 +343,10 @@ export const EditorUI = (props: EditorUIProps) => {
343
343
defineAndSetTheme ( monacoRef . current )
344
344
} )
345
345
346
+ /**
347
+ * add widget ranges to disposedWidgets when decoratorListCollection changes,
348
+ * this is used to restore the widgets when the file is changed.
349
+ */
346
350
useEffect ( ( ) => {
347
351
if ( decoratorListCollection && currentFileRef . current && ( props . currentFile === currentFileRef . current ) ) {
348
352
const widgetsToDispose = { }
@@ -355,10 +359,15 @@ export const EditorUI = (props: EditorUIProps) => {
355
359
}
356
360
} , [ decoratorListCollection ] )
357
361
362
+ /**
363
+ * restore the widgets when the file is changed.
364
+ * currentFileRef.current is the previous file, props.currentFile is the new file.
365
+ */
358
366
useEffect ( ( ) => {
359
367
if ( currentFileRef . current ) {
360
368
if ( props . currentFile !== currentFileRef . current ) {
361
369
const restoredWidgets = disposedWidgets [ props . currentFile ]
370
+ // restore the widgets if they exist to the new file
362
371
if ( restoredWidgets ) {
363
372
Object . keys ( restoredWidgets ) . forEach ( ( widgetId ) => {
364
373
const ranges = restoredWidgets [ widgetId ]
@@ -370,8 +379,10 @@ export const EditorUI = (props: EditorUIProps) => {
370
379
addAcceptDeclineWidget ( widgetId , editorRef . current , { column : 0 , lineNumber : newEntryRange . startLineNumber + 1 } , ( ) => acceptHandler ( decoratorList , widgetId ) , ( ) => rejectHandler ( decoratorList , widgetId ) )
371
380
} , 150 )
372
381
} )
382
+ // set the current diff file, this is needed to avoid removeAllWidgets called more than once, because the currentFileChanged event is broken and fired more than once.
373
383
setCurrentDiffFile ( props . currentFile + '-ai' )
374
384
}
385
+ // remove widgets from the previous file, this is needed to avoid widgets from the previous file to be shown when the new file is loaded.
375
386
if ( disposedWidgets [ currentFileRef . current ] ) {
376
387
Object . keys ( disposedWidgets [ currentFileRef . current ] ) . forEach ( ( widgetId ) => {
377
388
editorRef . current . removeContentWidget ( {
@@ -389,12 +400,13 @@ export const EditorUI = (props: EditorUIProps) => {
389
400
props . plugin . call ( 'fileManager' , 'getUrlFromPath' , currentFileRef . current ) . then ( ( url ) => ( currentUrlRef . current = url . file ) )
390
401
391
402
const file = editorModelsState [ props . currentFile ]
392
- currentDiffFile && diffEditorRef && diffEditorRef . current && diffEditorRef . current . setModel ( {
393
- original : file . model ,
394
- modified : editorModelsState [ currentDiffFile ] . model
403
+
404
+ props . isDiff && diffEditorRef && diffEditorRef . current && diffEditorRef . current . setModel ( {
405
+ original : editorModelsState [ props . currentDiffFile ] . model ,
406
+ modified : file . model
395
407
} )
396
408
397
- currentDiffFile && diffEditorRef . current . getModifiedEditor ( ) . updateOptions ( { readOnly : editorModelsState [ props . currentFile ] . readOnly } )
409
+ props . isDiff && diffEditorRef . current . getModifiedEditor ( ) . updateOptions ( { readOnly : editorModelsState [ props . currentFile ] . readOnly } )
398
410
399
411
editorRef . current . setModel ( file . model )
400
412
editorRef . current . updateOptions ( {
@@ -417,12 +429,9 @@ export const EditorUI = (props: EditorUIProps) => {
417
429
} else if ( file . language === 'python' ) {
418
430
monacoRef . current . editor . setModelLanguage ( file . model , 'remix-vyper' )
419
431
}
432
+ } , [ props . currentFile , props . isDiff ] )
420
433
421
- // @ts -ignore
422
- props . plugin . emit ( 'addModel' , editorRef . current . getModel ( ) . getValue ( ) , 'remix-solidity' , props . currentFile + '-ai' , false )
423
- } , [ props . currentFile , isDiff , currentDiffFile ] )
424
-
425
- const inlineCompletionProvider = new RemixInLineCompletionProvider ( props )
434
+ const inlineCompletionProvider = new RemixInLineCompletionProvider ( props , monacoRef . current )
426
435
427
436
const convertToMonacoDecoration = ( decoration : lineText | sourceAnnotation | sourceMarker , typeOfDecoration : string ) => {
428
437
if ( typeOfDecoration === 'sourceAnnotationsPerFile' ) {
@@ -675,22 +684,6 @@ export const EditorUI = (props: EditorUIProps) => {
675
684
}
676
685
} )
677
686
678
- function removeAllWidgetsAndDecorators ( ) : Promise < void > {
679
- return new Promise ( resolve => {
680
- setDecoratorListCollection ( decoratorListCollection => {
681
- Object . keys ( decoratorListCollection ) . forEach ( ( widgetId ) => {
682
- const decoratorList = decoratorListCollection [ widgetId ]
683
- if ( decoratorList ) rejectHandler ( decoratorList , widgetId )
684
- editorRef . current . removeContentWidget ( {
685
- getId : ( ) => widgetId
686
- } )
687
- } )
688
- resolve ( )
689
- return { }
690
- } )
691
- } )
692
- }
693
-
694
687
function removeAllWidgets ( ) {
695
688
const widgetIds = Object . keys ( decoratorListCollection )
696
689
if ( widgetIds . length === 0 ) return
@@ -1217,8 +1210,6 @@ export const EditorUI = (props: EditorUIProps) => {
1217
1210
rejectBtn . classList . add ( ...[ 'btn' , 'border' , 'align-items-center' , 'px-1' , 'py-0' , 'bg-light' , 'text-dark' ] )
1218
1211
rejectBtn . style . fontSize = '0.8rem'
1219
1212
rejectBtn . textContent = 'Decline'
1220
-
1221
- // Add close button handler
1222
1213
rejectBtn . onclick = ( ) => {
1223
1214
rejectHandler && rejectHandler ( )
1224
1215
editor . removeContentWidget ( {
@@ -1258,7 +1249,6 @@ export const EditorUI = (props: EditorUIProps) => {
1258
1249
}
1259
1250
1260
1251
containerElement . appendChild ( innerContainer )
1261
- // Add input handler
1262
1252
return containerElement
1263
1253
} ,
1264
1254
@@ -1385,43 +1375,6 @@ export const EditorUI = (props: EditorUIProps) => {
1385
1375
return decoratorList
1386
1376
}
1387
1377
1388
- function showCustomDiff ( uri : string ) {
1389
- const lineChanges : monacoTypes . editor . ILineChange [ ] = diffEditorRef . current . getLineChanges ( )
1390
- let totalLineDifference = 0
1391
-
1392
- lineChanges . forEach ( ( lineChange , index ) => {
1393
- const line = editorModelsState [ uri ] . model . getValueInRange ( new monacoRef . current . Range ( lineChange . modifiedStartLineNumber , 0 , lineChange . modifiedEndLineNumber , 1000 ) )
1394
- const linesCount = line . split ( '\n' ) . length
1395
- const lineDifference = lineChange . originalEndLineNumber - lineChange . originalStartLineNumber
1396
- const modifiedStartLine = lineChange . originalStartLineNumber + totalLineDifference
1397
- const modifiedEndLine = lineChange . originalStartLineNumber - 1 + linesCount + totalLineDifference
1398
- const originalStartLine = lineChange . originalStartLineNumber + linesCount + totalLineDifference
1399
- const originalEndLine = lineChange . originalStartLineNumber + linesCount + lineDifference + totalLineDifference
1400
-
1401
- editorRef . current . executeEdits ( 'lineChange' + index , [
1402
- {
1403
- range : new monacoRef . current . Range ( modifiedStartLine , 0 , modifiedStartLine , 0 ) ,
1404
- text : line + '\n' ,
1405
- } ,
1406
- ] )
1407
-
1408
- const ranges = [
1409
- new monacoRef . current . Range ( modifiedStartLine , 0 , modifiedEndLine , 1000 ) ,
1410
- new monacoRef . current . Range ( originalStartLine , 0 , originalEndLine , 1000 )
1411
- ]
1412
- const widgetId = `accept_decline_widget${ index } `
1413
- const decoratorList = addDecoratorCollection ( widgetId , ranges )
1414
-
1415
- setDecoratorListCollection ( decoratorListCollection => ( { ...decoratorListCollection , [ widgetId ] : decoratorList } ) )
1416
- if ( index === 0 ) {
1417
- addAcceptDeclineWidget ( widgetId , editorRef . current , { column : 0 , lineNumber : modifiedStartLine + 1 } , ( ) => acceptHandler ( decoratorList , widgetId ) , ( ) => rejectHandler ( decoratorList , widgetId ) , acceptAllHandler , rejectAllHandler )
1418
- } else {
1419
- addAcceptDeclineWidget ( widgetId , editorRef . current , { column : 0 , lineNumber : modifiedStartLine + 1 } , ( ) => acceptHandler ( decoratorList , widgetId ) , ( ) => rejectHandler ( decoratorList , widgetId ) )
1420
- }
1421
- totalLineDifference += linesCount
1422
- } )
1423
- }
1424
-
1425
1378
return (
1426
1379
< div className = "w-100 h-100 d-flex flex-column-reverse" >
1427
1380
< DiffEditor
0 commit comments