Don't rely on the file root typeDef + refactor mcdoc props

This commit is contained in:
Misode
2024-10-25 20:00:58 +02:00
parent 35de8bc538
commit b9e72bc4da
2 changed files with 69 additions and 65 deletions

View File

@@ -1,11 +1,12 @@
import type { DocAndNode, Range } from '@spyglassmc/core'
import type { JsonNode } from '@spyglassmc/json'
import { JsonFileNode } from '@spyglassmc/json'
import type { AttributeValue, McdocType } from '@spyglassmc/mcdoc'
import { useCallback, useErrorBoundary, useMemo } from 'preact/hooks'
import { useLocale } from '../../contexts/index.js'
import { disectFilePath, useLocale, useVersion } from '../../contexts/index.js'
import { useDocAndNode, useSpyglass } from '../../contexts/Spyglass.jsx'
import type { McdocContext } from './McdocRenderer.jsx'
import { McdocRoot } from './McdocRenderer.jsx'
import { McdocRoot, simplifyType } from './McdocRenderer.jsx'
type TreePanelProps = {
docAndNode: DocAndNode,
@@ -13,6 +14,7 @@ type TreePanelProps = {
}
export function Tree({ docAndNode: original, onError }: TreePanelProps) {
const { lang } = useLocale()
const { version } = useVersion()
const { service } = useSpyglass()
if (lang === 'none') return <></>
@@ -58,7 +60,46 @@ export function Tree({ docAndNode: original, onError }: TreePanelProps) {
return service.getCheckerContext(docAndNode.doc, errors)
}, [docAndNode, service])
const type = useMemo(() => {
if (!ctx) {
return undefined
}
const path = original.doc.uri
.replace(/^file:\/\/\/project\//, '')
.replace(/\.json$/, '')
const res = disectFilePath(path, version)
if (!res) {
return undefined
}
return simplifyType(getRootType(res.type), ctx)
}, [original.doc.uri, version, ctx])
return <div class="tree node-root" data-cy="tree">
{ctx && <McdocRoot node={fileChild.children[0]} makeEdit={makeEdit} ctx={ctx} />}
{(ctx && type) && <McdocRoot type={type} node={fileChild.children[0]} makeEdit={makeEdit} ctx={ctx} />}
</div>
}
function getRootType(id: string): McdocType {
if (id === 'pack_mcmeta') {
return { kind: 'reference', path: '::java::pack::Pack' }
}
if (id.startsWith('tag/')) {
const attribute: AttributeValue = {
kind: 'tree',
values: {
registry: { kind: 'literal', value: { kind: 'string', value: id.slice(4) } },
tags: { kind: 'literal', value: { kind: 'string', value: 'allowed' } },
},
}
return {
kind: 'concrete',
child: { kind: 'reference', path: '::java::data::tag::Tag' },
typeArgs: [{ kind: 'string', attributes: [{ name: 'id', value: attribute }] }],
}
}
return {
kind: 'dispatcher',
registry: 'minecraft:resource',
parallelIndices: [{ kind: 'static', value: id }],
}
}