less prototype, less bad code implementation of CCHM type theory
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.9 KiB

  1. import * as monaco from 'monaco-editor';
  2. import { CubicalT, Environment, Value } from '../typings/cubical';
  3. import * as haskell from '../typings/cubical';
  4. let editorNum: number = 0;
  5. let cubical: CubicalT;
  6. export const MODEL_ENVS: Record<string, Environment> = {}
  7. async function reloadModelEnv(model: monaco.editor.ITextModel, environ: Environment): Promise<void> {
  8. console.log(`reloading environment for model ${model.id}`)
  9. let code, typed;
  10. try {
  11. code = await cubical.exports.parseFromStringJs(model.getValue());
  12. typed = await cubical.exports.typeCheckProgram(environ, code);
  13. MODEL_ENVS[model.id] = typed;
  14. } catch (e) {
  15. if (typeof (e) === 'string')
  16. console.log(e);
  17. }
  18. }
  19. export class CubicalEditor {
  20. private model: monaco.editor.ITextModel;
  21. private editor: monaco.editor.IStandaloneCodeEditor;
  22. private environment: Environment | undefined;
  23. private uri: monaco.Uri;
  24. private element: HTMLElement;
  25. private didChangeLocked: boolean;
  26. constructor(el: HTMLElement, code: string, uri: monaco.Uri) {
  27. this.element = el;
  28. this.uri = uri;
  29. this.model = monaco.editor.createModel(code, 'cubical', this.uri);
  30. this.editor = monaco.editor.create(el, {
  31. model: this.model,
  32. tabSize: 2,
  33. insertSpaces: true,
  34. })
  35. this.didChangeLocked = false;
  36. }
  37. async load() {
  38. cubical = await haskell.waitForLoad;
  39. this.environment = await cubical.exports.newEnvironment();
  40. this.model.onDidChangeContent(async (e) => {
  41. if (this.didChangeLocked) {
  42. console.log("going too fast");
  43. return;
  44. }
  45. this.didChangeLocked = true;
  46. console.log('doing the work')
  47. // reloadModelEnv(this.model!, this.environment!);
  48. setTimeout(() => {
  49. this.didChangeLocked = false;
  50. }, 1000);
  51. });
  52. this.editor.layout();
  53. reloadModelEnv(this.model, this.environment);
  54. window.addEventListener('resize', () => this.editor.layout());
  55. }
  56. }