Skip to content

Commit 4b33310

Browse files
committed
Show documentation for a single function
1 parent f4ed286 commit 4b33310

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

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

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +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[]>([])
164-
const [decoratorListCollection, setDecoratorListCollection] = useState<any[]>([])
163+
const [decoratorListCollection, setDecoratorListCollection] = useState<Record<string, any>>({})
165164
const defaultEditorValue = `
166165
\t\t\t\t\t\t\t ____ _____ __ __ ___ __ __ ___ ____ _____
167166
\t\t\t\t\t\t\t| _ \\ | ____| | \\/ | |_ _| \\ \\/ / |_ _| | _ \\ | ____|
@@ -634,18 +633,18 @@ export const EditorUI = (props: EditorUIProps) => {
634633

635634
props.plugin.on('fileManager', 'currentFileChanged', (file: string) => {
636635
if (file + '-ai' !== currentDiffFile) {
637-
removeAllWidgets()
636+
removeAllWidgetsAndDecorators()
638637
}
639638
})
640639

641-
function removeAllWidgets() {
642-
if (widgetIds.length === 0) return
643-
for (const widgetId of widgetIds) {
640+
function removeAllWidgetsAndDecorators() {
641+
if (Object.keys(decoratorListCollection).length === 0) return
642+
Object.keys(decoratorListCollection).forEach((widgetId) => {
644643
editorRef.current && editorRef.current.removeContentWidget({
645644
getId: () => widgetId
646645
})
647-
}
648-
setWidgetIds([])
646+
})
647+
setDecoratorListCollection({})
649648
}
650649

651650
function setReducerListener() {
@@ -833,6 +832,16 @@ export const EditorUI = (props: EditorUIProps) => {
833832
const funcRange = await props.plugin.call('codeParser', "getLineColumnOfNode", { src: functionNode.src })
834833
const newLineCount = (outputFunctionComments[currentFunction.current] || '').split('\n').length
835834

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+
})
836845
if (functionNode.documentation) {
837846
const docsRange = await props.plugin.call('codeParser', "getLineColumnOfNode", { src: functionNode.documentation.src })
838847
const docs = editor.getModel().getValueInRange(new monacoRef.current.Range(docsRange.start.line, docsRange.start.column, funcRange.start.line, 1000))
@@ -863,7 +872,7 @@ export const EditorUI = (props: EditorUIProps) => {
863872
const widgetId = `accept_decline_widget${Math.random().toString(36).substring(2, 15)}`
864873

865874
setCurrentDiffFile(uri)
866-
setWidgetIds(widgetIds => [...widgetIds, widgetId])
875+
setDecoratorListCollection(decoratorListCollection => ({ ...decoratorListCollection, [widgetId]: decoratorList }))
867876
addAcceptDeclineWidget(widgetId, editorRef.current, { column: 0, lineNumber: docsRange.start.line + 2 }, () => acceptHandler(decoratorList, widgetId), () => rejectHandler(decoratorList, widgetId))
868877
} else {
869878
editorRef.current.executeEdits('newDocs', [
@@ -883,7 +892,7 @@ export const EditorUI = (props: EditorUIProps) => {
883892
const widgetId = `accept_decline_widget${Math.random().toString(36).substring(2, 15)}`
884893

885894
setCurrentDiffFile(uri)
886-
setWidgetIds(widgetIds => [...widgetIds, widgetId])
895+
setDecoratorListCollection(decoratorListCollection => ({ ...decoratorListCollection, [widgetId]: decoratorList }))
887896
addAcceptDeclineWidget(widgetId, editorRef.current, { column: 0, lineNumber: funcRange.start.line + 2 }, () => acceptHandler(decoratorList, widgetId), () => rejectHandler(decoratorList, widgetId))
888897
}
889898
}
@@ -1226,7 +1235,10 @@ export const EditorUI = (props: EditorUIProps) => {
12261235
])
12271236
}
12281237
decoratorList.clear()
1229-
setWidgetIds(widgetIds.filter((id) => id !== widgetId))
1238+
setDecoratorListCollection(decoratorListCollection => {
1239+
const { [widgetId]: _, ...rest } = decoratorListCollection
1240+
return rest
1241+
})
12301242
}
12311243

12321244
function rejectHandler(decoratorList, widgetId) {
@@ -1243,33 +1255,36 @@ export const EditorUI = (props: EditorUIProps) => {
12431255
])
12441256
}
12451257
decoratorList.clear()
1246-
setWidgetIds(widgetIds.filter((id) => id !== widgetId))
1258+
setDecoratorListCollection(decoratorListCollection => {
1259+
const { [widgetId]: _, ...rest } = decoratorListCollection
1260+
return rest
1261+
})
12471262
}
12481263

12491264
function acceptAllHandler() {
1250-
decoratorListCollection.forEach((decoratorList, index) => {
1251-
const widgetId = `accept_decline_widget${index}`
1265+
Object.keys(decoratorListCollection).forEach((widgetId) => {
1266+
const decoratorList = decoratorListCollection[widgetId]
12521267

12531268
acceptHandler(decoratorList, widgetId)
12541269
editorRef.current.removeContentWidget({
1255-
getId: () => `accept_decline_widget${index}`
1270+
getId: () => widgetId
12561271
})
12571272
})
12581273
}
12591274

12601275
function rejectAllHandler() {
1261-
decoratorListCollection.forEach((decoratorList, index) => {
1262-
const widgetId = `accept_decline_widget${index}`
1276+
Object.keys(decoratorListCollection).forEach((widgetId) => {
1277+
const decoratorList = decoratorListCollection[widgetId]
12631278

12641279
rejectHandler(decoratorList, widgetId)
12651280
editorRef.current.removeContentWidget({
1266-
getId: () => `accept_decline_widget${index}`
1281+
getId: () => widgetId
12671282
})
12681283
})
12691284
}
12701285

12711286
function showCustomDiff (uri: string) {
1272-
removeAllWidgets()
1287+
removeAllWidgetsAndDecorators()
12731288
const lineChanges: monacoTypes.editor.ILineChange[] = diffEditorRef.current.getLineChanges()
12741289
let totalLineDifference = 0
12751290

@@ -1304,12 +1319,9 @@ export const EditorUI = (props: EditorUIProps) => {
13041319
marginClassName: 'modifiedChangesDecoration',
13051320
}
13061321
}])
1307-
1308-
setDecoratorListCollection([...decoratorListCollection, decoratorList])
1309-
13101322
const widgetId = `accept_decline_widget${index}`
13111323

1312-
setWidgetIds([...widgetIds, widgetId])
1324+
setDecoratorListCollection(decoratorListCollection => ({ ...decoratorListCollection, [widgetId]: decoratorList }))
13131325
if (index === 0) {
13141326
addAcceptDeclineWidget(widgetId, editorRef.current, { column: 0, lineNumber: modifiedStartLine + 1 }, () => acceptHandler(decoratorList, widgetId), () => rejectHandler(decoratorList, widgetId), acceptAllHandler, rejectAllHandler)
13151327
} else {

0 commit comments

Comments
 (0)