|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"/> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1"/> |
| 6 | + <title>expr-eval + Monaco (minimal)</title> |
| 7 | + <style> |
| 8 | + html, body { |
| 9 | + height: 100%; |
| 10 | + margin: 0; |
| 11 | + padding: 0; |
| 12 | + } |
| 13 | + |
| 14 | + #editor { |
| 15 | + width: 100%; |
| 16 | + height: 95vh; |
| 17 | + } |
| 18 | + |
| 19 | + #status { |
| 20 | + height: 5vh; |
| 21 | + } |
| 22 | + </style> |
| 23 | + <script src="/dist/bundle.js"></script> |
| 24 | + <!-- Monaco loader from CDN --> |
| 25 | + <script src="https://cdn.jsdelivr.net/npm/monaco-editor@0.49.0/min/vs/loader.js"></script> |
| 26 | +</head> |
| 27 | +<body> |
| 28 | +<div id="editor"></div> |
| 29 | +<div id="status">No evaluations yet!</div> |
| 30 | + |
| 31 | +<script> |
| 32 | + // Configure Monaco AMD base path |
| 33 | + require.config({paths: {'vs': 'https://cdn.jsdelivr.net/npm/monaco-editor@0.49.0/min/vs'}}); |
| 34 | + |
| 35 | + // Start Monaco |
| 36 | + require(['vs/editor/editor.main'], function () { |
| 37 | + const languageId = 'expr-eval'; |
| 38 | + monaco.languages.register({id: languageId}); |
| 39 | + |
| 40 | + // Create editor with a sample model |
| 41 | + const initial = 'sum([1,2,3]) + max(2, 5)'; |
| 42 | + const model = monaco.editor.createModel(initial, languageId); |
| 43 | + const editor = monaco.editor.create(document.getElementById('editor'), { |
| 44 | + model, |
| 45 | + theme: 'vs-dark', |
| 46 | + automaticLayout: true, |
| 47 | + fontSize: 14, |
| 48 | + minimap: {enabled: false} |
| 49 | + }); |
| 50 | + |
| 51 | + // Access expr-eval UMD - because it is a umd bundle, it will be available as window.exprEval |
| 52 | + const {createLanguageService, Parser} = window.exprEval || {}; |
| 53 | + if (!createLanguageService) { |
| 54 | + console.error('expr-eval not found. Make sure /dist/bundle.js is built.'); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + const ls = createLanguageService(); |
| 59 | + |
| 60 | + // Minimal lsp text document backed by Monaco model |
| 61 | + function makeTextDocument(m) { |
| 62 | + return { |
| 63 | + uri: m.uri.toString(), |
| 64 | + getText: () => m.getValue(), |
| 65 | + positionAt: (offset) => { |
| 66 | + const p = m.getPositionAt(offset); |
| 67 | + return {line: p.lineNumber - 1, character: p.column - 1}; |
| 68 | + }, |
| 69 | + offsetAt: (pos) => m.getOffsetAt(new monaco.Position(pos.line + 1, pos.character + 1)) |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + function toLspPosition(mp) { |
| 74 | + return {line: mp.lineNumber - 1, character: mp.column - 1}; |
| 75 | + } |
| 76 | + |
| 77 | + function fromLspPosition(lp) { |
| 78 | + return new monaco.Position(lp.line + 1, lp.character + 1); |
| 79 | + } |
| 80 | + |
| 81 | + // Simple variables demo (appear in completions and hover) |
| 82 | + const demoVars = {x: 42, user: {name: 'Ada'}, flag: true}; |
| 83 | + |
| 84 | + // Completions provider |
| 85 | + monaco.languages.registerCompletionItemProvider(languageId, { |
| 86 | + provideCompletionItems: function (model, position) { |
| 87 | + const doc = makeTextDocument(model); |
| 88 | + const items = ls.getCompletions({ |
| 89 | + textDocument: doc, |
| 90 | + position: toLspPosition(position), |
| 91 | + variables: demoVars |
| 92 | + }) || []; |
| 93 | + |
| 94 | + const word = model.getWordUntilPosition(position); |
| 95 | + const range = new monaco.Range(position.lineNumber, word.startColumn, position.lineNumber, word.endColumn); |
| 96 | + |
| 97 | + function mapKind(k) { |
| 98 | + // Map LSP CompletionItemKind (numbers) to Monaco kinds |
| 99 | + const map = { |
| 100 | + 3: monaco.languages.CompletionItemKind.Function, // Function |
| 101 | + 6: monaco.languages.CompletionItemKind.Variable, // Variable |
| 102 | + 21: monaco.languages.CompletionItemKind.Constant, // Constant |
| 103 | + 14: monaco.languages.CompletionItemKind.Keyword // Keyword |
| 104 | + }; |
| 105 | + return map[k] || monaco.languages.CompletionItemKind.Text; |
| 106 | + } |
| 107 | + |
| 108 | + const suggestions = items.map(it => ({ |
| 109 | + label: it.label, |
| 110 | + kind: mapKind(it.kind), |
| 111 | + detail: it.detail, |
| 112 | + documentation: it.documentation, |
| 113 | + insertText: it.insertText || it.label, |
| 114 | + range |
| 115 | + })); |
| 116 | + |
| 117 | + return {suggestions}; |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + // Hover provider |
| 122 | + monaco.languages.registerHoverProvider(languageId, { |
| 123 | + provideHover: function (model, position) { |
| 124 | + const doc = makeTextDocument(model); |
| 125 | + const hover = ls.getHover({textDocument: doc, position: toLspPosition(position), variables: demoVars}); |
| 126 | + if (!hover || !hover.contents) return {contents: []}; |
| 127 | + |
| 128 | + let contents = []; |
| 129 | + if (typeof hover.contents === 'string') { |
| 130 | + contents = [{value: hover.contents}]; |
| 131 | + } else if (hover.contents && typeof hover.contents === 'object') { |
| 132 | + const kind = hover.contents.kind || 'plaintext'; |
| 133 | + const val = hover.contents.value || ''; |
| 134 | + contents = [{value: val}]; |
| 135 | + } |
| 136 | + |
| 137 | + let range = undefined; |
| 138 | + if (hover.range) { |
| 139 | + const start = fromLspPosition(hover.range.start); |
| 140 | + const end = fromLspPosition(hover.range.end); |
| 141 | + range = new monaco.Range(start.lineNumber, start.column, end.lineNumber, end.column); |
| 142 | + } |
| 143 | + return {contents, range}; |
| 144 | + } |
| 145 | + }); |
| 146 | + |
| 147 | + // Very basic syntax highlighting using decorations from service tokens |
| 148 | + function applyHighlighting() { |
| 149 | + const doc = makeTextDocument(model); |
| 150 | + const tokens = ls.getHighlighting(doc); |
| 151 | + const rangesByClass = new Map(); |
| 152 | + for (const t of tokens) { |
| 153 | + const start = model.getPositionAt(t.start); |
| 154 | + const end = model.getPositionAt(t.end); |
| 155 | + const range = new monaco.Range(start.lineNumber, start.column, end.lineNumber, end.column); |
| 156 | + const cls = 'tok-' + t.type; |
| 157 | + if (!rangesByClass.has(cls)) rangesByClass.set(cls, []); |
| 158 | + rangesByClass.get(cls).push({range, options: {inlineClassName: cls}}); |
| 159 | + } |
| 160 | + // Clear and set decorations per class (store IDs to update later if needed) |
| 161 | + window.__exprEvalDecos = window.__exprEvalDecos || {}; |
| 162 | + for (const [cls, decos] of rangesByClass.entries()) { |
| 163 | + const prev = window.__exprEvalDecos[cls] || []; |
| 164 | + window.__exprEvalDecos[cls] = editor.deltaDecorations(prev, decos); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + function evaluate() { |
| 169 | + const expression = model.getValue(); |
| 170 | + const status = document.getElementById('status'); |
| 171 | + try{ |
| 172 | + const parser = new Parser(); |
| 173 | + const evaluationResult = parser.evaluate(expression, demoVars); |
| 174 | + status.textContent = `Result: ${evaluationResult}`; |
| 175 | + } catch(error) { |
| 176 | + status.textContent = `Evaluation error: ${error.message}` |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + // Some minimal styles for tokens |
| 181 | + const style = document.createElement('style'); |
| 182 | + style.textContent = ` |
| 183 | + .tok-number { color: #b5cea8; } |
| 184 | + .tok-string { color: #ce9178; } |
| 185 | + .tok-keyword { color: #c586c0; } |
| 186 | + .tok-operator { color: #d4d4d4; } |
| 187 | + .tok-function { color: #4fc1ff; } |
| 188 | + .tok-punctuation { color: #d4d4d4; } |
| 189 | + .tok-name { color: #9cdcfe; } |
| 190 | + `; |
| 191 | + document.head.appendChild(style); |
| 192 | + |
| 193 | + applyHighlighting(); |
| 194 | + evaluate(); |
| 195 | + model.onDidChangeContent(() => { applyHighlighting(); evaluate(); }); |
| 196 | + }); |
| 197 | +</script> |
| 198 | +</body> |
| 199 | +</html> |
0 commit comments