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.
 
 
 

98 lines
2.6 KiB

import * as monaco from 'monaco-editor';
import { CubicalT, Environment, Value } from '../typings/cubical';
import * as haskell from '../typings/cubical';
import language from './language';
import toast from './toast';
import { CubicalEditor, MODEL_ENVS } from './editor';
let initCode: string[] = [
"{-# PRIMITIVE Type #-}",
"",
"data Nat : Type where",
" zero : Nat",
" succ : Nat -> Nat",
"",
"test : Nat",
"test = succ zero"
]
declare const cubical: CubicalT;
const LANGUAGE: string = "cubical";
monaco.languages.register({
id: LANGUAGE
});
monaco.languages.registerHoverProvider(LANGUAGE, {
provideHover: async (model: monaco.editor.ITextModel, position: monaco.Position, token: monaco.CancellationToken) => {
const word = model.getWordAtPosition(position);
if (!word) return;
try {
const env: Environment | null = MODEL_ENVS[model.id];
if (!env) return null;
const ty: Value = await cubical.exports.getTypeByNameJs(word.word, env);
return {
contents: [{
value: await cubical.exports.zonkAndShowTypeJs(ty)
}]
}
} catch (e) {
return null;
}
}
});
monaco.languages.registerDefinitionProvider(LANGUAGE, {
provideDefinition: async (model: monaco.editor.ITextModel, position: monaco.Position, token: monaco.CancellationToken) => {
const word = model.getWordAtPosition(position);
if (!word) return [];
try {
const env: Environment | null = MODEL_ENVS[model.id];
if (!env) return [];
const range: haskell.Range | null = await cubical.exports.findDefinitionJs(word.word, env);
if (range) {
return [{
range: {
startColumn: range[0].posnColm,
endColumn: range[1].posnColm,
startLineNumber: range[0].posnLine,
endLineNumber: range[1].posnLine,
},
uri: model.uri,
}];
} else {
return [];
}
} catch (e) {
console.log(e);
return [];
}
}
})
monaco.languages.setMonarchTokensProvider(LANGUAGE, language);
document.addEventListener('DOMContentLoaded', async () => {
let editors: CubicalEditor[] = [];
let n = 0;
document.querySelectorAll("pre.editor-container").forEach(async (theEl) => {
let el = theEl as HTMLPreElement;
let attr = el.getAttribute("src");
let text : string;
if (attr) {
text = await fetch(attr).then(x => x.text());
} else {
text = el.innerText;
}
el.innerText = '';
let editor = new CubicalEditor(el, text, monaco.Uri.parse(attr ?? `editor://${n++}`));
await editor.load();
})
});