Skip to content

Commit 212c3fe

Browse files
committed
dirty start
1 parent ce90db1 commit 212c3fe

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

ui/src/components/CanvasContextMenu.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import React, { useContext } from "react";
99
import CodeIcon from "@mui/icons-material/Code";
1010
import PostAddIcon from "@mui/icons-material/PostAdd";
1111
import NoteIcon from "@mui/icons-material/Note";
12+
import AutoFixHighIcon from "@mui/icons-material/AutoFixHigh";
1213
import FormatListNumberedIcon from "@mui/icons-material/FormatListNumbered";
1314

1415
const paneMenuStyle = (left, top) => {
@@ -40,6 +41,11 @@ export function CanvasContextMenu(props) {
4041
store,
4142
(state) => state.flipShowLineNumbers
4243
);
44+
const autoCompletion = useStore(store, (state) => state.autoCompletion);
45+
const flipAutoCompletion = useStore(
46+
store,
47+
(state) => state.flipAutoCompletion
48+
);
4349
const isGuest = useStore(store, (state) => state.role === "GUEST");
4450
return (
4551
<Box sx={paneMenuStyle(props.x, props.y)}>
@@ -76,6 +82,15 @@ export function CanvasContextMenu(props) {
7682
{showLineNumbers ? "Hide " : "Show "} Line Numbers
7783
</ListItemText>
7884
</MenuItem>
85+
<MenuItem onClick={flipAutoCompletion} sx={ItemStyle}>
86+
<ListItemIcon sx={{ color: "inherit" }}>
87+
<AutoFixHighIcon />
88+
</ListItemIcon>
89+
<ListItemText>
90+
{autoCompletion ? "Disable " : "Enable "} Auto Completion (Provided
91+
by Codeium)
92+
</ListItemText>
93+
</MenuItem>
7994
</MenuList>
8095
</Box>
8196
);

ui/src/components/MyMonaco.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { RepoContext } from "../lib/store";
88
import { MonacoBinding } from "y-monaco";
99
import { useReactFlow } from "reactflow";
1010
import { Annotation } from "../lib/parser";
11+
import { MonacoCompletionProvider } from "../lib/monacoCompletionProvider";
1112

1213
const theme: monaco.editor.IStandaloneThemeData = {
1314
base: "vs",
@@ -26,6 +27,9 @@ monaco.languages.setLanguageConfiguration("julia", {
2627
decreaseIndentPattern: /^\s*(end|else|elseif|catch|finally)\b.*$/,
2728
},
2829
});
30+
31+
console.log("monaco", monaco);
32+
console.log("languages", monaco.languages.registerInlineCompletionsProvider);
2933
function construct_indent(pos, indent) {
3034
return [
3135
{
@@ -402,6 +406,7 @@ export const MyMonaco = memo<MyMonacoProps>(function MyMonaco({
402406
const showAnnotations = useStore(store, (state) => state.showAnnotations);
403407
const scopedVars = useStore(store, (state) => state.scopedVars);
404408
const updateView = useStore(store, (state) => state.updateView);
409+
const setMonaco = useStore(store, (state) => state.setMonaco);
405410

406411
const value = getPod(id)?.content || "";
407412
let lang = getPod(id)?.lang || "javascript";
@@ -441,6 +446,7 @@ export const MyMonaco = memo<MyMonacoProps>(function MyMonaco({
441446
monaco
442447
) {
443448
setEditor(editor);
449+
setMonaco(monaco);
444450
// console.log(Math.min(1000, editor.getContentHeight()));
445451
const updateHeight = () => {
446452
// max height: 400
@@ -479,6 +485,7 @@ export const MyMonaco = memo<MyMonacoProps>(function MyMonaco({
479485
// });
480486

481487
// bind it to the ytext with pod id
488+
// if (monaco.languages.registerInlineCompletionsProvider)
482489
const ytext = ydoc.getText("monaco-" + id);
483490
const monacoBinding = new MonacoBinding(
484491
ytext,

ui/src/lib/store/repoStateSlice.tsx

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createStore, StateCreator, StoreApi } from "zustand";
22
import { devtools } from "zustand/middleware";
33
import produce from "immer";
44
import { createContext } from "react";
5+
import { MonacoCompletionProvider } from "../monacoCompletionProvider";
56

67
import {
78
normalize,
@@ -22,6 +23,8 @@ import { addAwarenessStyle } from "../styles";
2223
import { Annotation } from "../parser";
2324
import { MyState, Pod } from ".";
2425

26+
declare type Monaco = typeof import("monaco-editor");
27+
2528
let serverURL;
2629
if (window.location.protocol === "http:") {
2730
serverURL = `ws://${window.location.host}/socket`;
@@ -46,6 +49,7 @@ export interface RepoStateSlice {
4649
clients: Map<string, any>;
4750
user: any;
4851
ydoc: Doc;
52+
monaco: Monaco | null;
4953
collaborators: any[];
5054
addCollaborator: (client: ApolloClient<object>, email: string) => any;
5155
deleteCollaborator: (client: ApolloClient<object>, id: string) => any;
@@ -74,6 +78,12 @@ export interface RepoStateSlice {
7478
yjsConnecting: boolean;
7579
connectYjs: () => void;
7680
disconnectYjs: () => void;
81+
setMonaco: (monaco: Monaco) => void;
82+
autoCompletion: boolean;
83+
unregisterCompletionHandler: null | (() => void);
84+
registerCompletion: () => void;
85+
unregisterCompletion: () => void;
86+
flipAutoCompletion: () => void;
7787
}
7888

7989
export const createRepoStateSlice: StateCreator<
@@ -96,7 +106,9 @@ export const createRepoStateSlice: StateCreator<
96106
currentEditor: null,
97107
//TODO: all presence information are now saved in clients map for future usage. create a modern UI to show those information from clients (e.g., online users)
98108
clients: new Map(),
99-
109+
monaco: null,
110+
autoCompletion: false,
111+
unregisterCompletionHandler: null,
100112
loadError: null,
101113
role: "GUEST",
102114
collaborators: [],
@@ -283,6 +295,64 @@ export const createRepoStateSlice: StateCreator<
283295
state.ydoc.destroy();
284296
})
285297
),
298+
setMonaco: (monaco) => {
299+
set(
300+
produce((state) => {
301+
if (state.monaco === null) {
302+
state.monaco = monaco;
303+
console.log("set monaco as", monaco);
304+
if (state.autoCompletion) {
305+
state.registerCompletion();
306+
}
307+
}
308+
})
309+
);
310+
},
311+
registerCompletion: () => {
312+
set(
313+
produce((state) => {
314+
if (state.monaco && !state.unregisterCompletionHandler) {
315+
const completionProvider = new MonacoCompletionProvider();
316+
const { dispose } =
317+
state.monaco.languages.registerCompletionItemProvider(
318+
"codeium",
319+
completionProvider
320+
);
321+
console.log("register completion", dispose);
322+
state.unregisterCompletionHandler = dispose;
323+
state.monaco.editor.registerCommand(
324+
"codeium.acceptCompletion",
325+
(accessor, args) => {
326+
console.log("acceptCompletion", args);
327+
}
328+
);
329+
}
330+
})
331+
);
332+
},
333+
334+
unregisterCompletion: () =>
335+
set(
336+
produce((state) => {
337+
if (state.unregisterCompletionHandler) {
338+
console.log("unregister", state.unregisterCompletionHandler);
339+
state.unregisterCompletionHandler();
340+
state.unregisterCompletionHandler = null;
341+
}
342+
})
343+
),
344+
345+
flipAutoCompletion: () =>
346+
set(
347+
produce((state) => {
348+
state.autoCompletion = !state.autoCompletion;
349+
if (state.autoCompletion) {
350+
state.registerCompletion();
351+
} else {
352+
state.unregisterCompletion();
353+
}
354+
})
355+
),
286356
});
287357

288358
function loadRepo(set, get) {

0 commit comments

Comments
 (0)