Skip to content

Commit d5c6f90

Browse files
committed
Document new useEffects and remove unused code changes
1 parent 46cb8c2 commit d5c6f90

File tree

1 file changed

+18
-65
lines changed

1 file changed

+18
-65
lines changed

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

Lines changed: 18 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@ export const EditorUI = (props: EditorUIProps) => {
343343
defineAndSetTheme(monacoRef.current)
344344
})
345345

346+
/**
347+
* add widget ranges to disposedWidgets when decoratorListCollection changes,
348+
* this is used to restore the widgets when the file is changed.
349+
*/
346350
useEffect(() => {
347351
if (decoratorListCollection && currentFileRef.current && (props.currentFile === currentFileRef.current)) {
348352
const widgetsToDispose = {}
@@ -355,10 +359,15 @@ export const EditorUI = (props: EditorUIProps) => {
355359
}
356360
}, [decoratorListCollection])
357361

362+
/**
363+
* restore the widgets when the file is changed.
364+
* currentFileRef.current is the previous file, props.currentFile is the new file.
365+
*/
358366
useEffect(() => {
359367
if (currentFileRef.current) {
360368
if (props.currentFile !== currentFileRef.current) {
361369
const restoredWidgets = disposedWidgets[props.currentFile]
370+
// restore the widgets if they exist to the new file
362371
if (restoredWidgets) {
363372
Object.keys(restoredWidgets).forEach((widgetId) => {
364373
const ranges = restoredWidgets[widgetId]
@@ -370,8 +379,10 @@ export const EditorUI = (props: EditorUIProps) => {
370379
addAcceptDeclineWidget(widgetId, editorRef.current, { column: 0, lineNumber: newEntryRange.startLineNumber + 1 }, () => acceptHandler(decoratorList, widgetId), () => rejectHandler(decoratorList, widgetId))
371380
}, 150)
372381
})
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.
373383
setCurrentDiffFile(props.currentFile + '-ai')
374384
}
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.
375386
if (disposedWidgets[currentFileRef.current]) {
376387
Object.keys(disposedWidgets[currentFileRef.current]).forEach((widgetId) => {
377388
editorRef.current.removeContentWidget({
@@ -389,12 +400,13 @@ export const EditorUI = (props: EditorUIProps) => {
389400
props.plugin.call('fileManager', 'getUrlFromPath', currentFileRef.current).then((url) => (currentUrlRef.current = url.file))
390401

391402
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
395407
})
396408

397-
currentDiffFile && diffEditorRef.current.getModifiedEditor().updateOptions({ readOnly: editorModelsState[props.currentFile].readOnly })
409+
props.isDiff && diffEditorRef.current.getModifiedEditor().updateOptions({ readOnly: editorModelsState[props.currentFile].readOnly })
398410

399411
editorRef.current.setModel(file.model)
400412
editorRef.current.updateOptions({
@@ -417,12 +429,9 @@ export const EditorUI = (props: EditorUIProps) => {
417429
} else if (file.language === 'python') {
418430
monacoRef.current.editor.setModelLanguage(file.model, 'remix-vyper')
419431
}
432+
}, [props.currentFile, props.isDiff])
420433

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)
426435

427436
const convertToMonacoDecoration = (decoration: lineText | sourceAnnotation | sourceMarker, typeOfDecoration: string) => {
428437
if (typeOfDecoration === 'sourceAnnotationsPerFile') {
@@ -675,22 +684,6 @@ export const EditorUI = (props: EditorUIProps) => {
675684
}
676685
})
677686

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-
694687
function removeAllWidgets() {
695688
const widgetIds = Object.keys(decoratorListCollection)
696689
if (widgetIds.length === 0) return
@@ -1217,8 +1210,6 @@ export const EditorUI = (props: EditorUIProps) => {
12171210
rejectBtn.classList.add(...['btn', 'border', 'align-items-center', 'px-1', 'py-0', 'bg-light', 'text-dark'])
12181211
rejectBtn.style.fontSize = '0.8rem'
12191212
rejectBtn.textContent = 'Decline'
1220-
1221-
// Add close button handler
12221213
rejectBtn.onclick = () => {
12231214
rejectHandler && rejectHandler()
12241215
editor.removeContentWidget({
@@ -1258,7 +1249,6 @@ export const EditorUI = (props: EditorUIProps) => {
12581249
}
12591250

12601251
containerElement.appendChild(innerContainer)
1261-
// Add input handler
12621252
return containerElement
12631253
},
12641254

@@ -1385,43 +1375,6 @@ export const EditorUI = (props: EditorUIProps) => {
13851375
return decoratorList
13861376
}
13871377

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-
14251378
return (
14261379
<div className="w-100 h-100 d-flex flex-column-reverse">
14271380
<DiffEditor

0 commit comments

Comments
 (0)