@@ -633,18 +633,38 @@ export const EditorUI = (props: EditorUIProps) => {
633
633
634
634
props . plugin . on ( 'fileManager' , 'currentFileChanged' , ( file : string ) => {
635
635
if ( file + '-ai' !== currentDiffFile ) {
636
- removeAllWidgetsAndDecorators ( )
636
+ removeAllWidgets ( )
637
637
}
638
638
} )
639
639
640
- function removeAllWidgetsAndDecorators ( ) {
641
- if ( Object . keys ( decoratorListCollection ) . length === 0 ) return
642
- Object . keys ( decoratorListCollection ) . forEach ( ( widgetId ) => {
643
- editorRef . current && editorRef . current . removeContentWidget ( {
644
- getId : ( ) => widgetId
640
+ function removeAllWidgetsAndDecorators ( ) : Promise < void > {
641
+ return new Promise ( resolve => {
642
+ setDecoratorListCollection ( decoratorListCollection => {
643
+ Object . keys ( decoratorListCollection ) . forEach ( ( widgetId ) => {
644
+ const decoratorList = decoratorListCollection [ widgetId ]
645
+ if ( decoratorList ) rejectHandler ( decoratorList , widgetId )
646
+ editorRef . current . removeContentWidget ( {
647
+ getId : ( ) => widgetId
648
+ } )
649
+ } )
650
+ resolve ( )
651
+ return { }
645
652
} )
646
653
} )
647
- setDecoratorListCollection ( { } )
654
+ }
655
+
656
+ function removeAllWidgets ( ) {
657
+ const widgetIds = Object . keys ( decoratorListCollection )
658
+ if ( widgetIds . length === 0 ) return
659
+ if ( document . getElementById ( widgetIds [ 0 ] ) === null ) return
660
+ setDecoratorListCollection ( decoratorListCollection => {
661
+ Object . keys ( decoratorListCollection ) . forEach ( ( widgetId ) => {
662
+ editorRef . current . removeContentWidget ( {
663
+ getId : ( ) => widgetId
664
+ } )
665
+ } )
666
+ return decoratorListCollection
667
+ } )
648
668
}
649
669
650
670
function setReducerListener ( ) {
@@ -825,26 +845,16 @@ export const EditorUI = (props: EditorUIProps) => {
825
845
run : async ( ) => {
826
846
if ( functionNode ) {
827
847
const uri = currentFileRef . current + '-ai'
828
- const content = editorModelsState [ currentFileRef . current ] . model . getValue ( )
848
+ const content = editorRef . current . getModel ( ) . getValue ( )
829
849
const query = intl . formatMessage ( { id : 'editor.generateDocumentationByAI' } , { content, currentFunction : currentFunction . current } )
830
850
const output = await props . plugin . call ( 'remixAI' , 'code_explaining' , query )
831
851
const outputFunctionComments = extractFunctionComments ( output , 1 , false )
832
852
const funcRange = await props . plugin . call ( 'codeParser' , "getLineColumnOfNode" , { src : functionNode . src } )
833
853
const newLineCount = ( outputFunctionComments [ currentFunction . current ] || '' ) . split ( '\n' ) . length
834
854
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
- } )
845
855
if ( functionNode . documentation ) {
846
856
const docsRange = await props . plugin . call ( 'codeParser' , "getLineColumnOfNode" , { src : functionNode . documentation . src } )
847
- const docs = editor . getModel ( ) . getValueInRange ( new monacoRef . current . Range ( docsRange . start . line , docsRange . start . column , funcRange . start . line , 1000 ) )
857
+ const docs = editorRef . current . getModel ( ) . getValueInRange ( new monacoRef . current . Range ( docsRange . start . line , docsRange . start . column , funcRange . start . line , 1000 ) )
848
858
const oldLineCount = ( docs || '' ) . split ( '\n' ) . length - 1
849
859
850
860
editorRef . current . executeEdits ( 'docsChange' , [
@@ -872,8 +882,19 @@ export const EditorUI = (props: EditorUIProps) => {
872
882
const widgetId = `accept_decline_widget${ Math . random ( ) . toString ( 36 ) . substring ( 2 , 15 ) } `
873
883
874
884
setCurrentDiffFile ( uri )
875
- setDecoratorListCollection ( decoratorListCollection => ( { ...decoratorListCollection , [ widgetId ] : decoratorList } ) )
876
- addAcceptDeclineWidget ( widgetId , editorRef . current , { column : 0 , lineNumber : docsRange . start . line + 2 } , ( ) => acceptHandler ( decoratorList , widgetId ) , ( ) => rejectHandler ( decoratorList , widgetId ) )
885
+ setDecoratorListCollection ( decoratorListCollection => {
886
+ Object . keys ( decoratorListCollection ) . forEach ( ( widgetId ) => {
887
+ const decoratorList = decoratorListCollection [ widgetId ]
888
+ if ( decoratorList ) rejectHandler ( decoratorList , widgetId )
889
+ editorRef . current . removeContentWidget ( {
890
+ getId : ( ) => widgetId
891
+ } )
892
+ } )
893
+ return { [ widgetId ] : decoratorList }
894
+ } )
895
+ const newEntryRange = decoratorList . getRange ( 0 )
896
+
897
+ addAcceptDeclineWidget ( widgetId , editorRef . current , { column : 0 , lineNumber : newEntryRange . startLineNumber + 1 } , ( ) => acceptHandler ( decoratorList , widgetId ) , ( ) => rejectHandler ( decoratorList , widgetId ) )
877
898
} else {
878
899
editorRef . current . executeEdits ( 'newDocs' , [
879
900
{
@@ -892,8 +913,19 @@ export const EditorUI = (props: EditorUIProps) => {
892
913
const widgetId = `accept_decline_widget${ Math . random ( ) . toString ( 36 ) . substring ( 2 , 15 ) } `
893
914
894
915
setCurrentDiffFile ( uri )
895
- setDecoratorListCollection ( decoratorListCollection => ( { ...decoratorListCollection , [ widgetId ] : decoratorList } ) )
896
- addAcceptDeclineWidget ( widgetId , editorRef . current , { column : 0 , lineNumber : funcRange . start . line + 2 } , ( ) => acceptHandler ( decoratorList , widgetId ) , ( ) => rejectHandler ( decoratorList , widgetId ) )
916
+ setDecoratorListCollection ( decoratorListCollection => {
917
+ Object . keys ( decoratorListCollection ) . forEach ( ( widgetId ) => {
918
+ const decoratorList = decoratorListCollection [ widgetId ]
919
+ if ( decoratorList ) rejectHandler ( decoratorList , widgetId )
920
+ editorRef . current . removeContentWidget ( {
921
+ getId : ( ) => widgetId
922
+ } )
923
+ } )
924
+ return { [ widgetId ] : decoratorList }
925
+ } )
926
+ const newEntryRange = decoratorList . getRange ( 0 )
927
+
928
+ addAcceptDeclineWidget ( widgetId , editorRef . current , { column : 0 , lineNumber : newEntryRange . startLineNumber + 1 } , ( ) => acceptHandler ( decoratorList , widgetId ) , ( ) => rejectHandler ( decoratorList , widgetId ) )
897
929
}
898
930
}
899
931
_paq . push ( [ 'trackEvent' , 'ai' , 'remixAI' , 'generateDocumentation' ] )
@@ -1284,7 +1316,6 @@ export const EditorUI = (props: EditorUIProps) => {
1284
1316
}
1285
1317
1286
1318
function showCustomDiff ( uri : string ) {
1287
- removeAllWidgetsAndDecorators ( )
1288
1319
const lineChanges : monacoTypes . editor . ILineChange [ ] = diffEditorRef . current . getLineChanges ( )
1289
1320
let totalLineDifference = 0
1290
1321
0 commit comments