fix code formatting
Browse files
frontend/src/components/CodeEditor.tsx
CHANGED
|
@@ -12,6 +12,8 @@ interface CodeEditorProps {
|
|
| 12 |
|
| 13 |
export default function CodeEditor({ code, language, onChange, readOnly = false }: CodeEditorProps) {
|
| 14 |
const editorRef = useRef<any>(null);
|
|
|
|
|
|
|
| 15 |
|
| 16 |
// Map our language names to Monaco language IDs
|
| 17 |
const getMonacoLanguage = (lang: string): string => {
|
|
@@ -30,8 +32,35 @@ export default function CodeEditor({ code, language, onChange, readOnly = false
|
|
| 30 |
editorRef.current = editor;
|
| 31 |
};
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
return (
|
| 34 |
-
<div className="h-full overflow-hidden">
|
| 35 |
<Editor
|
| 36 |
height="100%"
|
| 37 |
language={getMonacoLanguage(language)}
|
|
@@ -43,14 +72,26 @@ export default function CodeEditor({ code, language, onChange, readOnly = false
|
|
| 43 |
minimap: { enabled: true },
|
| 44 |
fontSize: 14,
|
| 45 |
fontFamily: "'SF Mono', 'JetBrains Mono', 'Menlo', 'Monaco', 'Courier New', monospace",
|
| 46 |
-
wordWrap: '
|
| 47 |
lineNumbers: 'on',
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
scrollBeyondLastLine: false,
|
| 49 |
automaticLayout: true,
|
| 50 |
tabSize: 2,
|
|
|
|
| 51 |
padding: { top: 16, bottom: 16 },
|
| 52 |
lineHeight: 22,
|
| 53 |
letterSpacing: 0.5,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
}}
|
| 55 |
onMount={handleEditorDidMount}
|
| 56 |
/>
|
|
|
|
| 12 |
|
| 13 |
export default function CodeEditor({ code, language, onChange, readOnly = false }: CodeEditorProps) {
|
| 14 |
const editorRef = useRef<any>(null);
|
| 15 |
+
const lastFormattedCodeRef = useRef<string>('');
|
| 16 |
+
const formatTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
| 17 |
|
| 18 |
// Map our language names to Monaco language IDs
|
| 19 |
const getMonacoLanguage = (lang: string): string => {
|
|
|
|
| 32 |
editorRef.current = editor;
|
| 33 |
};
|
| 34 |
|
| 35 |
+
// Format code intelligently - only when generation appears complete
|
| 36 |
+
useEffect(() => {
|
| 37 |
+
if (editorRef.current && code && code.length > 100) {
|
| 38 |
+
// Clear existing timeout
|
| 39 |
+
if (formatTimeoutRef.current) {
|
| 40 |
+
clearTimeout(formatTimeoutRef.current);
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
// Only format if code hasn't been formatted yet or if it's different
|
| 44 |
+
if (code !== lastFormattedCodeRef.current) {
|
| 45 |
+
// Wait 1 second after code stops changing before formatting
|
| 46 |
+
formatTimeoutRef.current = setTimeout(() => {
|
| 47 |
+
if (editorRef.current) {
|
| 48 |
+
editorRef.current.getAction('editor.action.formatDocument')?.run();
|
| 49 |
+
lastFormattedCodeRef.current = code;
|
| 50 |
+
}
|
| 51 |
+
}, 1000);
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
return () => {
|
| 56 |
+
if (formatTimeoutRef.current) {
|
| 57 |
+
clearTimeout(formatTimeoutRef.current);
|
| 58 |
+
}
|
| 59 |
+
};
|
| 60 |
+
}, [code]);
|
| 61 |
+
|
| 62 |
return (
|
| 63 |
+
<div className="h-full overflow-hidden bg-[#1e1e1e]">
|
| 64 |
<Editor
|
| 65 |
height="100%"
|
| 66 |
language={getMonacoLanguage(language)}
|
|
|
|
| 72 |
minimap: { enabled: true },
|
| 73 |
fontSize: 14,
|
| 74 |
fontFamily: "'SF Mono', 'JetBrains Mono', 'Menlo', 'Monaco', 'Courier New', monospace",
|
| 75 |
+
wordWrap: 'off',
|
| 76 |
lineNumbers: 'on',
|
| 77 |
+
lineNumbersMinChars: 3,
|
| 78 |
+
glyphMargin: false,
|
| 79 |
+
folding: true,
|
| 80 |
+
lineDecorationsWidth: 10,
|
| 81 |
scrollBeyondLastLine: false,
|
| 82 |
automaticLayout: true,
|
| 83 |
tabSize: 2,
|
| 84 |
+
insertSpaces: true,
|
| 85 |
padding: { top: 16, bottom: 16 },
|
| 86 |
lineHeight: 22,
|
| 87 |
letterSpacing: 0.5,
|
| 88 |
+
renderLineHighlight: 'line',
|
| 89 |
+
formatOnPaste: true,
|
| 90 |
+
formatOnType: false,
|
| 91 |
+
scrollbar: {
|
| 92 |
+
verticalScrollbarSize: 10,
|
| 93 |
+
horizontalScrollbarSize: 10,
|
| 94 |
+
},
|
| 95 |
}}
|
| 96 |
onMount={handleEditorDidMount}
|
| 97 |
/>
|