|
1 | | - |
2 | 1 | let editor; |
3 | 2 | let completionTippy; |
4 | | -let currentCompletion = ''; |
| 3 | +let currentCompletion = ""; |
5 | 4 |
|
6 | 5 | function updateEditor(delta) { |
7 | | - if (delta.action === 'insert' && (delta.lines[0] === '.' || delta.lines[0] === ' ')) { |
8 | | - showCompletionSuggestion(); |
9 | | - } |
| 6 | + if ( |
| 7 | + delta.action === "insert" && |
| 8 | + (delta.lines[0] === "." || delta.lines[0] === " ") |
| 9 | + ) { |
| 10 | + showCompletionSuggestion(); |
| 11 | + } |
10 | 12 |
|
11 | | - // Recover from errors TODO(avh): only do this if there's an error |
12 | | - createModule().then((Module) => { |
13 | | - // Keep your existing Module setup |
14 | | - Module.print = window.customPrint; |
15 | | - Module.printErr = window.customPrint; |
16 | | - window.Module = Module; |
17 | | - console.log("updateEditor() - Module ready"); |
18 | | - }); |
| 13 | + // Recover from errors TODO(avh): only do this if there's an error |
| 14 | + createModule().then((Module) => { |
| 15 | + // Keep your existing Module setup |
| 16 | + Module.print = window.customPrint; |
| 17 | + Module.printErr = window.customPrint; |
| 18 | + window.Module = Module; |
| 19 | + console.log("updateEditor() - Module ready"); |
| 20 | + }); |
19 | 21 |
|
20 | | - if (window.Module && window.Module.executeKernel) { |
21 | | - console.log("Executing kernel"); |
22 | | - window.terminal.clear(); |
23 | | - window.Module.executeKernel(editor.getValue()); |
24 | | - } else { |
25 | | - console.log("updateEditor() - Module not ready"); |
26 | | - } |
| 22 | + if (window.Module && window.Module.executeKernel) { |
| 23 | + console.log("Executing kernel"); |
| 24 | + window.terminal.clear(); |
| 25 | + const wgSize = [256, 1, 1]; |
| 26 | + const gridSize = [256, 1, 1]; |
| 27 | + window.Module.executeKernel(editor.getValue(), wgSize, gridSize); |
| 28 | + } else { |
| 29 | + console.log("updateEditor() - Module not ready"); |
| 30 | + } |
27 | 31 | } |
28 | 32 |
|
29 | 33 | function initEditor(initial_content) { |
30 | | - editor = ace.edit("editor"); |
31 | | - editor.setTheme("ace/theme/monokai"); |
32 | | - editor.session.setMode("ace/mode/javascript"); |
33 | | - editor.setOptions({ |
34 | | - fontSize: "14px", |
35 | | - showPrintMargin: false, |
36 | | - // disable showing errors in gutter, Ace's WGSL parser is out of date |
37 | | - showGutter: false, |
38 | | - highlightActiveLine: true, |
39 | | - wrap: true, |
40 | | - }); |
41 | | - editor.setKeyboardHandler("ace/keyboard/vim"); |
42 | | - editor.setValue(initial_content); |
43 | | - window.addEventListener('resize', function() { |
44 | | - editor.resize(); |
45 | | - }); |
46 | | - // document.getElementById('language').addEventListener('change', function(e) { |
47 | | - // let mode = "ace/mode/" + e.target.value; |
48 | | - // editor.session.setMode(mode); |
49 | | - // }); |
| 34 | + editor = ace.edit("editor"); |
| 35 | + editor.setTheme("ace/theme/monokai"); |
| 36 | + editor.session.setMode("ace/mode/javascript"); |
| 37 | + editor.setOptions({ |
| 38 | + fontSize: "16px", |
| 39 | + showPrintMargin: false, // disable showing errors in gutter, Ace's WGSL parser is out of date |
| 40 | + showGutter: false, |
| 41 | + highlightActiveLine: true, |
| 42 | + wrap: true, |
| 43 | + }); |
| 44 | + editor.setKeyboardHandler("ace/keyboard/vim"); |
| 45 | + editor.setValue(initial_content); |
| 46 | + window.addEventListener("resize", function () { |
| 47 | + editor.resize(); |
| 48 | + }); |
50 | 49 |
|
51 | | - editor.session.on('change', updateEditor); |
| 50 | + editor.session.on("change", updateEditor); |
52 | 51 |
|
53 | | - completionTippy = tippy(document.getElementById('editor'), { |
54 | | - content: 'Loading...', |
55 | | - trigger: 'manual', |
56 | | - placement: 'top-start', |
57 | | - arrow: true, |
58 | | - interactive: true |
59 | | - }); |
| 52 | + completionTippy = tippy(document.getElementById("editor"), { |
| 53 | + content: "Loading...", |
| 54 | + trigger: "manual", |
| 55 | + placement: "top-start", |
| 56 | + arrow: true, |
| 57 | + interactive: true, |
| 58 | + }); |
60 | 59 |
|
61 | | - // Override the default tab behavior |
62 | | - editor.commands.addCommand({ |
63 | | - name: 'insertCompletion', |
64 | | - bindKey: {win: 'Tab', mac: 'Tab'}, |
65 | | - exec: function(editor) { |
66 | | - if (currentCompletion) { |
67 | | - editor.insert(currentCompletion); |
68 | | - currentCompletion = ''; |
69 | | - completionTippy.hide(); |
70 | | - } else { |
71 | | - editor.indent(); |
72 | | - } |
73 | | - } |
74 | | - }); |
| 60 | + // Override the default tab behavior |
| 61 | + editor.commands.addCommand({ |
| 62 | + name: "insertCompletion", |
| 63 | + bindKey: { win: "Tab", mac: "Tab" }, |
| 64 | + exec: function (editor) { |
| 65 | + if (currentCompletion) { |
| 66 | + editor.insert(currentCompletion); |
| 67 | + currentCompletion = ""; |
| 68 | + completionTippy.hide(); |
| 69 | + } else { |
| 70 | + editor.indent(); |
| 71 | + } |
| 72 | + }, |
| 73 | + }); |
75 | 74 | } |
76 | 75 |
|
77 | 76 | async function showCompletionSuggestion() { |
78 | | - const cursorPosition = editor.getCursorPosition(); |
79 | | - const screenPosition = editor.renderer.textToScreenCoordinates(cursorPosition.row, cursorPosition.column); |
| 77 | + const cursorPosition = editor.getCursorPosition(); |
| 78 | + const screenPosition = editor.renderer.textToScreenCoordinates( |
| 79 | + cursorPosition.row, |
| 80 | + cursorPosition.column, |
| 81 | + ); |
| 82 | + |
| 83 | + completionTippy.setContent("Loading..."); |
| 84 | + completionTippy.setProps({ |
| 85 | + getReferenceClientRect: () => ({ |
| 86 | + width: 0, |
| 87 | + height: 0, |
| 88 | + top: screenPosition.pageY, |
| 89 | + bottom: screenPosition.pageY, |
| 90 | + left: screenPosition.pageX, |
| 91 | + right: screenPosition.pageX, |
| 92 | + }), |
| 93 | + }); |
| 94 | + completionTippy.show(); |
80 | 95 |
|
81 | | - completionTippy.setContent('Loading...'); |
82 | | - completionTippy.setProps({ |
83 | | - getReferenceClientRect: () => ({ |
84 | | - width: 0, |
85 | | - height: 0, |
86 | | - top: screenPosition.pageY, |
87 | | - bottom: screenPosition.pageY, |
88 | | - left: screenPosition.pageX, |
89 | | - right: screenPosition.pageX, |
90 | | - }) |
| 96 | + try { |
| 97 | + const response = await fetch("/complete", { |
| 98 | + method: "POST", |
| 99 | + headers: { |
| 100 | + "Content-Type": "application/json", |
| 101 | + }, |
| 102 | + body: JSON.stringify({ |
| 103 | + code: editor.getValue(), |
| 104 | + row: cursorPosition.row, |
| 105 | + column: cursorPosition.column, |
| 106 | + }), |
91 | 107 | }); |
92 | | - completionTippy.show(); |
93 | 108 |
|
94 | | - try { |
95 | | - const response = await fetch('/complete', { |
96 | | - method: 'POST', |
97 | | - headers: { |
98 | | - 'Content-Type': 'application/json', |
99 | | - }, |
100 | | - body: JSON.stringify({ |
101 | | - code: editor.getValue(), |
102 | | - row: cursorPosition.row, |
103 | | - column: cursorPosition.column |
104 | | - }), |
105 | | - }); |
106 | | - |
107 | | - if (!response.ok) { |
108 | | - throw new Error(`HTTP error! status: ${response.status}`); |
109 | | - } |
110 | | - |
111 | | - const data = await response.json(); |
112 | | - currentCompletion = data.completion; |
113 | | - completionTippy.setContent(`${currentCompletion} (Press Tab to insert)`); |
114 | | - } catch (error) { |
115 | | - console.error('Error:', error); |
116 | | - completionTippy.setContent('Error fetching completion'); |
117 | | - currentCompletion = ''; |
| 109 | + if (!response.ok) { |
| 110 | + throw new Error(`HTTP error! status: ${response.status}`); |
118 | 111 | } |
119 | 112 |
|
120 | | - setTimeout(() => { |
121 | | - if (currentCompletion) { |
122 | | - completionTippy.hide(); |
123 | | - currentCompletion = ''; |
124 | | - } |
125 | | - }, 5000); |
| 113 | + const data = await response.json(); |
| 114 | + currentCompletion = data.completion; |
| 115 | + completionTippy.setContent(`${currentCompletion} (Press Tab to insert)`); |
| 116 | + } catch (error) { |
| 117 | + console.error("Error:", error); |
| 118 | + completionTippy.setContent("Error fetching completion"); |
| 119 | + currentCompletion = ""; |
| 120 | + } |
| 121 | + |
| 122 | + setTimeout(() => { |
| 123 | + if (currentCompletion) { |
| 124 | + completionTippy.hide(); |
| 125 | + currentCompletion = ""; |
| 126 | + } |
| 127 | + }, 5000); |
126 | 128 | } |
0 commit comments