import React, { useRef } from 'react'; import Editor, { OnMount } from '@monaco-editor/react'; import * as monaco from 'monaco-editor'; const CodeEditor: React.FC<{ filePath: string | null; projectPath: string }> = ({ filePath, projectPath }) => { const editorRef = useRef(null); const handleEditorDidMount: OnMount = (editor, monaco) => { editorRef.current = editor; monaco.languages.registerInlineCompletionsProvider('*', { provideInlineCompletions: async (model, position, context, token) => { const prefix = model.getValueInRange({ startLineNumber: 1, startColumn: 1, endLineNumber: position.lineNumber, endColumn: position.column, }); const suffix = model.getValueInRange({ startLineNumber: position.lineNumber, startColumn: position.column, endLineNumber: model.getLineCount(), endColumn: model.getLineMaxColumn(model.getLineCount()), }); try { const resp = await fetch('/api/autocomplete', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ prefix, suffix, filepath: filePath || '', language: model.getLanguageId(), max_tokens: 64, model: 'groq-llama-3.1-8b-instant', }), }); if (!resp.ok) return { items: [] }; const data = await resp.json(); const completions = data.completions || []; return { items: completions.map((c: any) => ({ insertText: c.text, range: { startLineNumber: position.lineNumber, startColumn: position.column, endLineNumber: position.lineNumber, endColumn: position.column, }, })), }; } catch (e) { return { items: [] }; } }, freeInlineCompletions: () => {}, }); }; if (!filePath) return (
← Select a file from the explorer to start editing
); return ( Loading editor...} options={{ fontSize: 13, minimap: { enabled: true }, smoothScrolling: true, cursorBlinking: 'smooth', inlineSuggest: { enabled: true }, }} /> ); }; export default CodeEditor;