Mayank2027 commited on
Commit
535bc87
·
verified ·
1 Parent(s): c9443aa

Update frontend/src/components/CodeEditor.tsx

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