Skip to content

Commit badeaa6

Browse files
committed
Show custom diff after switching files
1 parent 9891fca commit badeaa6

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

libs/remix-ui/editor/src/lib/providers/hoverProvider.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export class RemixHoverProvider implements monacoTypes.languages.HoverProvider {
2424
}
2525

2626
async provideHover (model: monacoTypes.editor.ITextModel, position: monacoTypes.Position): Promise<monacoTypes.languages.Hover> {
27-
console.log('position: ', position)
2827
for (const action of this.triggerRangeActions) {
2928
if (action.range.startLineNumber <= position.lineNumber &&
3029
action.range.endLineNumber >= position.lineNumber &&

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

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { RemixCodeActionProvider } from './providers/codeActionProvider'
2424
import './remix-ui-editor.css'
2525
import { circomLanguageConfig, circomTokensProvider } from './syntaxes/circom'
2626
import { noirLanguageConfig, noirTokensProvider } from './syntaxes/noir'
27-
import { IPosition } from 'monaco-editor'
27+
import { IPosition, IRange } from 'monaco-editor'
2828
import { RemixInLineCompletionProvider } from './providers/inlineCompletionProvider'
2929
const _paq = (window._paq = window._paq || [])
3030

@@ -159,8 +159,8 @@ export const EditorUI = (props: EditorUIProps) => {
159159
const [isSplit, setIsSplit] = useState(true)
160160
const [isDiff, setIsDiff] = useState(props.isDiff || false)
161161
const [currentDiffFile, setCurrentDiffFile] = useState(props.currentDiffFile || '')
162-
const [isPromptSuggestion, setIsPromptSuggestion] = useState(false)
163-
const [decoratorListCollection, setDecoratorListCollection] = useState<Record<string, any>>({})
162+
const [decoratorListCollection, setDecoratorListCollection] = useState<Record<string, monacoTypes.editor.IEditorDecorationsCollection>>({})
163+
const [disposedWidgets, setDisposedWidgets] = useState<Record<string, Record<string, monacoTypes.IRange[]>>>({})
164164
const defaultEditorValue = `
165165
\t\t\t\t\t\t\t ____ _____ __ __ ___ __ __ ___ ____ _____
166166
\t\t\t\t\t\t\t| _ \\ | ____| | \\/ | |_ _| \\ \\/ / |_ _| | _ \\ | ____|
@@ -344,6 +344,72 @@ export const EditorUI = (props: EditorUIProps) => {
344344
defineAndSetTheme(monacoRef.current)
345345
})
346346

347+
useEffect(() => {
348+
if (decoratorListCollection && currentFileRef.current && (props.currentFile === currentFileRef.current)) {
349+
const widgetsToDispose = {}
350+
Object.keys(decoratorListCollection).forEach((widgetId) => {
351+
const ranges = decoratorListCollection[widgetId].getRanges()
352+
353+
widgetsToDispose[widgetId] = ranges
354+
})
355+
setDisposedWidgets({ ...disposedWidgets, [currentFileRef.current]: widgetsToDispose })
356+
}
357+
}, [decoratorListCollection])
358+
359+
useEffect(() => {
360+
if (currentFileRef.current) {
361+
if (props.currentFile !== currentFileRef.current) {
362+
const restoredWidgets = disposedWidgets[props.currentFile]
363+
if (restoredWidgets) {
364+
Object.keys(restoredWidgets).forEach((widgetId) => {
365+
const ranges = restoredWidgets[widgetId]
366+
let decoratorList: monacoTypes.editor.IEditorDecorationsCollection
367+
368+
if (ranges.length === 1) {
369+
decoratorList = editorRef.current.createDecorationsCollection([{
370+
range: ranges[0],
371+
options: {
372+
isWholeLine: true,
373+
className: 'newChangesDecoration',
374+
marginClassName: 'newChangesDecoration',
375+
}
376+
}])
377+
} else {
378+
decoratorList = editorRef.current.createDecorationsCollection([{
379+
range: ranges[0],
380+
options: {
381+
isWholeLine: true,
382+
className: 'newChangesDecoration',
383+
marginClassName: 'newChangesDecoration',
384+
}
385+
}, {
386+
range: ranges[1],
387+
options: {
388+
isWholeLine: true,
389+
className: 'modifiedChangesDecoration',
390+
marginClassName: 'modifiedChangesDecoration',
391+
}
392+
}])
393+
}
394+
setTimeout(() => {
395+
const newEntryRange = decoratorList.getRange(0)
396+
397+
addAcceptDeclineWidget(widgetId, editorRef.current, { column: 0, lineNumber: newEntryRange.startLineNumber + 1 }, () => acceptHandler(decoratorList, widgetId), () => rejectHandler(decoratorList, widgetId))
398+
}, 150)
399+
})
400+
setCurrentDiffFile(props.currentFile + '-ai')
401+
}
402+
if (disposedWidgets[currentFileRef.current]) {
403+
Object.keys(disposedWidgets[currentFileRef.current]).forEach((widgetId) => {
404+
editorRef.current.removeContentWidget({
405+
getId: () => widgetId
406+
})
407+
})
408+
}
409+
}
410+
}
411+
}, [props.currentFile])
412+
347413
useEffect(() => {
348414
if (!(editorRef.current || diffEditorRef.current ) || !props.currentFile) return
349415
currentFileRef.current = props.currentFile

0 commit comments

Comments
 (0)