fix code formatting
Browse files11 unmodified lines121314151617181912 unmodified lines3233343536373839404142434445464748495051525354555657585960616234636465665 unmodified lines72737446757677787980818283848586878889909192939495969711 unmodified lines
export default function CodeEditor({ code, language, onChange, readOnly = false }: CodeEditorProps) { const editorRef = useRef<any>(null); const lastFormattedCodeRef = useRef<string>(''); const formatTimeoutRef = useRef<NodeJS.Timeout | null>(null);
// Map our language names to Monaco language IDs const getMonacoLanguage = (lang: string): string => {12 unmodified lines editorRef.current = editor; };
// Format code intelligently - only when generation appears complete useEffect(() => { if (editorRef.current && code && code.length > 100) { // Clear existing timeout if (formatTimeoutRef.current) { clearTimeout(formatTimeoutRef.current); } // Only format if code hasn't been formatted yet or if it's different if (code !== lastFormattedCodeRef.current) { // Wait 1 second after code stops changing before formatting formatTimeoutRef.current = setTimeout(() => { if (editorRef.current) { editorRef.current.getAction('editor.action.formatDocument')?.run(); lastFormattedCodeRef.current = code; } }, 1000); } } return () => { if (formatTimeoutRef.current) { clearTimeout(formatTimeoutRef.current); } }; }, [code]);
return ( <div className="h-full overflow-hidden"> <div className="h-full overflow-hidden bg-[#1e1e1e]"> <Editor height="100%" language={getMonacoLanguage(language)}5 unmodified lines minimap: { enabled: true }, fontSize: 14, fontFamily: "'SF Mono', 'JetBrains Mono', 'Menlo', 'Monaco', 'Courier New', monospace", wordWrap: 'on', wordWrap: 'off', lineNumbers: 'on', lineNumbersMinChars: 3, glyphMargin: false, folding: true, lineDecorationsWidth: 10, scrollBeyondLastLine: false, automaticLayout: true, tabSize: 2, insertSpaces: true, padding: { top: 16, bottom: 16 }, lineHeight: 22, letterSpacing: 0.5, renderLineHighlight: 'line', formatOnPaste: true, formatOnType: false, scrollbar: { verticalScrollbarSize: 10, horizontalScrollbarSize: 10, }, }} onMount={handleEditorDidMount} />frontend/src/components/CodeEditor.tsx+43-2