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.

97 lines
2.6 KiB

  1. import * as monaco from 'monaco-editor';
  2. import { CubicalT, Environment, Value } from '../typings/cubical';
  3. import * as haskell from '../typings/cubical';
  4. import language from './language';
  5. import toast from './toast';
  6. import { CubicalEditor, MODEL_ENVS } from './editor';
  7. let initCode: string[] = [
  8. "{-# PRIMITIVE Type #-}",
  9. "",
  10. "data Nat : Type where",
  11. " zero : Nat",
  12. " succ : Nat -> Nat",
  13. "",
  14. "test : Nat",
  15. "test = succ zero"
  16. ]
  17. declare const cubical: CubicalT;
  18. const LANGUAGE: string = "cubical";
  19. monaco.languages.register({
  20. id: LANGUAGE
  21. });
  22. monaco.languages.registerHoverProvider(LANGUAGE, {
  23. provideHover: async (model: monaco.editor.ITextModel, position: monaco.Position, token: monaco.CancellationToken) => {
  24. const word = model.getWordAtPosition(position);
  25. if (!word) return;
  26. try {
  27. const env: Environment | null = MODEL_ENVS[model.id];
  28. if (!env) return null;
  29. const ty: Value = await cubical.exports.getTypeByNameJs(word.word, env);
  30. return {
  31. contents: [{
  32. value: await cubical.exports.zonkAndShowTypeJs(ty)
  33. }]
  34. }
  35. } catch (e) {
  36. return null;
  37. }
  38. }
  39. });
  40. monaco.languages.registerDefinitionProvider(LANGUAGE, {
  41. provideDefinition: async (model: monaco.editor.ITextModel, position: monaco.Position, token: monaco.CancellationToken) => {
  42. const word = model.getWordAtPosition(position);
  43. if (!word) return [];
  44. try {
  45. const env: Environment | null = MODEL_ENVS[model.id];
  46. if (!env) return [];
  47. const range: haskell.Range | null = await cubical.exports.findDefinitionJs(word.word, env);
  48. if (range) {
  49. return [{
  50. range: {
  51. startColumn: range[0].posnColm,
  52. endColumn: range[1].posnColm,
  53. startLineNumber: range[0].posnLine,
  54. endLineNumber: range[1].posnLine,
  55. },
  56. uri: model.uri,
  57. }];
  58. } else {
  59. return [];
  60. }
  61. } catch (e) {
  62. console.log(e);
  63. return [];
  64. }
  65. }
  66. })
  67. monaco.languages.setMonarchTokensProvider(LANGUAGE, language);
  68. document.addEventListener('DOMContentLoaded', async () => {
  69. let editors: CubicalEditor[] = [];
  70. let n = 0;
  71. document.querySelectorAll("pre.editor-container").forEach(async (theEl) => {
  72. let el = theEl as HTMLPreElement;
  73. let attr = el.getAttribute("src");
  74. let text : string;
  75. if (attr) {
  76. text = await fetch(attr).then(x => x.text());
  77. } else {
  78. text = el.innerText;
  79. }
  80. el.innerText = '';
  81. let editor = new CubicalEditor(el, text, monaco.Uri.parse(attr ?? `editor://${n++}`));
  82. await editor.load();
  83. })
  84. });