From f36dbd12e9bae0558db95dbdbeafbea94973f145 Mon Sep 17 00:00:00 2001 From: claude Date: Mon, 8 Jun 2026 19:35:02 +0000 Subject: [PATCH] =?UTF-8?q?chore:=20rewrite=20diagnostic=20=E2=80=94=20CSS?= =?UTF-8?q?=20class=20counts=20+=20cm-content=20view=20accessor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- diagnostic-typst.js | 62 +++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/diagnostic-typst.js b/diagnostic-typst.js index 02409b55bb..517eb32932 100644 --- a/diagnostic-typst.js +++ b/diagnostic-typst.js @@ -1,53 +1,43 @@ /** * Typst syntax highlighting diagnostics. - * Paste this entire file into the browser dev tools console - * while a Typst file is open in the editor. + * Paste into browser dev tools console with a Typst file open. */ -// 1. Find the active CodeMirror editor view (try several accessor patterns) -const view = [...document.querySelectorAll('.cm-editor')] - .map(el => el.cmView?.view ?? el.cmView ?? el['__cm']) - .find(v => v?.state?.doc) +// ── Part 1: CSS token counts (no view needed) ──────────────────────────── +// If all are 0, the language mode is not being applied at all. +console.log('=== Token CSS class counts ===') +;['heading','comment','keyword','string','number', + 'variableName','function','emphasis','strong'].forEach(t => { + const n = document.querySelectorAll('.tok-' + t).length + console.log(` .tok-${t}: ${n}`) +}) -if (!view) { - console.error('No CodeMirror view found — make sure a file is open in the editor') +// ── Part 2: Try to get the parse tree ──────────────────────────────────── +// CodeMirror 6 stores DocView on .cm-content; DocView.view = EditorView +const content = document.querySelector('.cm-content') +const view = content?.cmView?.view + +if (!view?.state) { + console.warn('Could not find EditorView — parse tree unavailable') + console.log('Keys on .cm-content:', Object.keys(content ?? {}).join(', ')) } else { - // 2. Show the language currently active - const lang = view.state.facet( - window.CM?.language?.language ?? view.state.facet.constructor - ) - console.log('Language:', view.state.languageDataAt('commentTokens', 0)) + console.log('\n=== Parse tree (top 600 chars) ===') + console.log(view.state.tree.toString().slice(0, 600)) - // 3. Print the top of the parse tree (first 800 chars) - const tree = view.state.tree - console.log('Parse tree (top):\n' + tree.toString().slice(0, 800)) - - // 4. Walk the first 20 leaf nodes and show their type + text - console.log('\nFirst 20 leaf nodes:') - let count = 0 - tree.iterate({ - enter(node) { - if (node.firstChild) return // skip non-leaf - if (count++ >= 20) return false - const text = view.state.doc.sliceString(node.from, node.to) - console.log(` [${node.name}] ${JSON.stringify(text.slice(0, 40))}`) - } - }) - - // 5. Check whether the Typst heading tokens exist at line 15-ish + // First heading line const doc = view.state.doc - for (let ln = 1; ln <= Math.min(doc.lines, 20); ln++) { + for (let ln = 1; ln <= Math.min(doc.lines, 25); ln++) { const line = doc.line(ln) if (line.text.trimStart().startsWith('=')) { - console.log(`\nLine ${ln} starts with '=': "${line.text}"`) - tree.iterate({ - from: line.from, - to: line.to, + console.log(`\n=== Nodes on heading line ${ln}: "${line.text}" ===`) + view.state.tree.iterate({ + from: line.from, to: line.to, enter(node) { const t = doc.sliceString(node.from, node.to) - console.log(` node: ${node.name} text: ${JSON.stringify(t.slice(0, 60))}`) + console.log(` ${node.name}: ${JSON.stringify(t.slice(0, 50))}`) } }) + break } } }