chore: rewrite diagnostic — CSS class counts + cm-content view accessor

This commit is contained in:
claude
2026-06-08 19:35:02 +00:00
parent c65bb80512
commit f36dbd12e9
+26 -36
View File
@@ -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
}
}
}