Skip to content

Commit 3dcc43d

Browse files
committed
Remove widgets when file changes
1 parent 1f570b6 commit 3dcc43d

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

libs/remix-ui/editor/src/lib/remix-ui-editor.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ export const EditorUI = (props: EditorUIProps) => {
160160
const [isDiff, setIsDiff] = useState(props.isDiff || false)
161161
const [currentDiffFile, setCurrentDiffFile] = useState(props.currentDiffFile || '')
162162
const [isPromptSuggestion, setIsPromptSuggestion] = useState(false)
163+
const [widgetIds, setWidgetIds] = useState<string[]>([])
163164
const defaultEditorValue = `
164165
\t\t\t\t\t\t\t ____ _____ __ __ ___ __ __ ___ ____ _____
165166
\t\t\t\t\t\t\t| _ \\ | ____| | \\/ | |_ _| \\ \\/ / |_ _| | _ \\ | ____|
@@ -377,6 +378,7 @@ export const EditorUI = (props: EditorUIProps) => {
377378
} else if (file.language === 'python') {
378379
monacoRef.current.editor.setModelLanguage(file.model, 'remix-vyper')
379380
}
381+
380382
// @ts-ignore
381383
props.plugin.emit('addModel', editorRef.current.getModel().getValue(), 'remix-solidity', props.currentFile + '-ai', false)
382384
}, [props.currentFile, isDiff, currentDiffFile])
@@ -629,6 +631,22 @@ export const EditorUI = (props: EditorUIProps) => {
629631
}
630632
}
631633

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+
632650
function setReducerListener() {
633651
if (diffEditorRef.current && diffEditorRef.current.getModifiedEditor() && editorRef.current){
634652
reducerListener(props.plugin, dispatch, monacoRef.current, [diffEditorRef.current.getModifiedEditor(), editorRef.current], props.events)
@@ -1148,6 +1166,7 @@ export const EditorUI = (props: EditorUIProps) => {
11481166
}
11491167

11501168
function showCustomDiff (uri: string) {
1169+
removeAllWidgets()
11511170
const lineChanges: monacoTypes.editor.ILineChange[] = diffEditorRef.current.getLineChanges()
11521171
let totalLineDifference = 0
11531172
const decoratorListCollection = []
@@ -1198,6 +1217,7 @@ export const EditorUI = (props: EditorUIProps) => {
11981217
},
11991218
])
12001219
decoratorList.clear()
1220+
setWidgetIds(widgetIds.filter((id) => id !== `accept_decline_widget${index}`))
12011221
}
12021222

12031223
const rejectHandler = (decoratorList) => {
@@ -1212,6 +1232,7 @@ export const EditorUI = (props: EditorUIProps) => {
12121232
},
12131233
])
12141234
decoratorList.clear()
1235+
setWidgetIds(widgetIds.filter((id) => id !== `accept_decline_widget${index}`))
12151236
}
12161237

12171238
const acceptAllHandler = () => {
@@ -1232,10 +1253,13 @@ export const EditorUI = (props: EditorUIProps) => {
12321253
})
12331254
}
12341255

1256+
const widgetId = `accept_decline_widget${index}`
1257+
1258+
setWidgetIds([...widgetIds, widgetId])
12351259
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)
12371261
} 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))
12391263
}
12401264
totalLineDifference += linesCount
12411265
})

0 commit comments

Comments
 (0)