Mayank2027 commited on
Commit
6b7fbaf
·
verified ·
1 Parent(s): 17d50b3

Create frontend/src/components/CodeEditor.tsx

Browse files
frontend/src/components/CodeEditor.tsx ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React, { useRef } from 'react';
2
+ import Editor, { OnMount } from '@monaco-editor/react';
3
+ import * as monaco from 'monaco-editor';
4
+
5
+ const CodeEditor: React.FC<{ filePath: string | null; projectPath: string }> = ({ filePath, projectPath }) => {
6
+ const editorRef = useRef<monaco.editor.IStandaloneCodeEditor | null>(null);
7
+
8
+ const handleEditorDidMount: OnMount = (editor, monaco) => {
9
+ editorRef.current = editor;
10
+
11
+ monaco.languages.registerInlineCompletionsProvider('*', {
12
+ provideInlineCompletions: async (model, position, context, token) => {
13
+ const prefix = model.getValueInRange({
14
+ startLineNumber: 1,
15
+ startColumn: 1,
16
+ endLineNumber: position.lineNumber,
17
+ endColumn: position.column,
18
+ });
19
+ const suffix = model.getValueInRange({
20
+ startLineNumber: position.lineNumber,
21
+ startColumn: position.column,
22
+ endLineNumber: model.getLineCount(),
23
+ endColumn: model.getLineMaxColumn(model.getLineCount()),
24
+ });
25
+
26
+ try {
27
+ const resp = await fetch('/api/autocomplete', {
28
+ method: 'POST',
29
+ headers: { 'Content-Type': 'application/json' },
30
+ body: JSON.stringify({
31
+ prefix,
32
+ suffix,
33
+ filepath: filePath || '',
34
+ language: model.getLanguageId(),
35
+ max_tokens: 64,
36
+ model: 'groq-llama-3.1-8b-instant',
37
+ }),
38
+ });
39
+ if (!resp.ok) return { items: [] };
40
+ const data = await resp.json();
41
+ const completions = data.completions || [];
42
+ return {
43
+ items: completions.map((c: any) => ({
44
+ insertText: c.text,
45
+ range: {
46
+ startLineNumber: position.lineNumber,
47
+ startColumn: position.column,
48
+ endLineNumber: position.lineNumber,
49
+ endColumn: position.column,
50
+ },
51
+ })),
52
+ };
53
+ } catch (e) {
54
+ return { items: [] };
55
+ }
56
+ },
57
+ freeInlineCompletions: () => {},
58
+ });
59
+ };
60
+
61
+ if (!filePath) return (
62
+ <div style={{ flex: 1, display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--text-secondary)', fontSize: 14 }}>
63
+ ← Select a file from the explorer to start editing
64
+ </div>
65
+ );
66
+
67
+ return (
68
+ <Editor
69
+ height="100%"
70
+ defaultLanguage="typescript"
71
+ theme="vs-dark"
72
+ path={filePath}
73
+ onMount={handleEditorDidMount}
74
+ loading={<div style={{ color: 'var(--text-secondary)', padding: 20 }}>Loading editor...</div>}
75
+ options={{
76
+ fontSize: 13,
77
+ minimap: { enabled: true },
78
+ smoothScrolling: true,
79
+ cursorBlinking: 'smooth',
80
+ inlineSuggest: { enabled: true },
81
+ }}
82
+ />
83
+ );
84
+ };
85
+
86
+ export default CodeEditor;