Skip to content

Commit 35ec1ac

Browse files
committed
Show widget options in right position
1 parent 4b33310 commit 35ec1ac

File tree

1 file changed

+55
-24
lines changed

1 file changed

+55
-24
lines changed

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

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -633,18 +633,38 @@ export const EditorUI = (props: EditorUIProps) => {
633633

634634
props.plugin.on('fileManager', 'currentFileChanged', (file: string) => {
635635
if (file + '-ai' !== currentDiffFile) {
636-
removeAllWidgetsAndDecorators()
636+
removeAllWidgets()
637637
}
638638
})
639639

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 {}
645652
})
646653
})
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+
})
648668
}
649669

650670
function setReducerListener() {
@@ -825,26 +845,16 @@ export const EditorUI = (props: EditorUIProps) => {
825845
run: async () => {
826846
if (functionNode) {
827847
const uri = currentFileRef.current + '-ai'
828-
const content = editorModelsState[currentFileRef.current].model.getValue()
848+
const content = editorRef.current.getModel().getValue()
829849
const query = intl.formatMessage({ id: 'editor.generateDocumentationByAI' }, { content, currentFunction: currentFunction.current })
830850
const output = await props.plugin.call('remixAI', 'code_explaining', query)
831851
const outputFunctionComments = extractFunctionComments(output, 1, false)
832852
const funcRange = await props.plugin.call('codeParser', "getLineColumnOfNode", { src: functionNode.src })
833853
const newLineCount = (outputFunctionComments[currentFunction.current] || '').split('\n').length
834854

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-
})
845855
if (functionNode.documentation) {
846856
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))
848858
const oldLineCount = (docs || '').split('\n').length - 1
849859

850860
editorRef.current.executeEdits('docsChange', [
@@ -872,8 +882,19 @@ export const EditorUI = (props: EditorUIProps) => {
872882
const widgetId = `accept_decline_widget${Math.random().toString(36).substring(2, 15)}`
873883

874884
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))
877898
} else {
878899
editorRef.current.executeEdits('newDocs', [
879900
{
@@ -892,8 +913,19 @@ export const EditorUI = (props: EditorUIProps) => {
892913
const widgetId = `accept_decline_widget${Math.random().toString(36).substring(2, 15)}`
893914

894915
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))
897929
}
898930
}
899931
_paq.push(['trackEvent', 'ai', 'remixAI', 'generateDocumentation'])
@@ -1284,7 +1316,6 @@ export const EditorUI = (props: EditorUIProps) => {
12841316
}
12851317

12861318
function showCustomDiff (uri: string) {
1287-
removeAllWidgetsAndDecorators()
12881319
const lineChanges: monacoTypes.editor.ILineChange[] = diffEditorRef.current.getLineChanges()
12891320
let totalLineDifference = 0
12901321

0 commit comments

Comments
 (0)