import { useModelingContext } from 'hooks/useModelingContext' import { kclManager } from 'lang/KclSinglton' import { getNodeFromPath, getNodePathFromSourceRange } from 'lang/queryAst' import { useEffect, useRef, useState } from 'react' import { useStore } from 'useStore' export function AstExplorer() { const setHighlightRange = useStore((s) => s.setHighlightRange) const { context } = useModelingContext() const pathToNode = getNodePathFromSourceRange( // TODO maybe need to have callback to make sure it stays in sync kclManager.ast, context.selectionRanges.codeBasedSelections?.[0]?.range ) const node = getNodeFromPath(kclManager.ast, pathToNode).node const [filterKeys, setFilterKeys] = useState(['start', 'end']) return (
filter out keys:
{['start', 'end', 'type'].map((key) => { return ( ) })}
{ setHighlightRange([0, 0]) }} >
          
        
) } function DisplayBody({ body, filterKeys, node, }: { body: { start: number; end: number; [key: string]: any }[] filterKeys: string[] node: any }) { return ( <> {body.map((b, index) => { return (
) })} ) } function DisplayObj({ obj, filterKeys, node, }: { obj: { start: number; end: number; [key: string]: any } filterKeys: string[] node: any }) { const setHighlightRange = useStore((s) => s.setHighlightRange) const { send } = useModelingContext() const ref = useRef(null) const [hasCursor, setHasCursor] = useState(false) const [isCollapsed, setIsCollapsed] = useState(false) useEffect(() => { if ( node?.start === obj?.start && node?.end === obj?.end && node.type === obj?.type ) { ref?.current?.scrollIntoView?.({ behavior: 'smooth', block: 'center' }) setHasCursor(true) } else { setHasCursor(false) } }, [node.start, node.end, node.type]) return (
 {
        setHighlightRange([obj?.start || 0, obj.end])
        e.stopPropagation()
      }}
      onMouseMove={(e) => {
        e.stopPropagation()
        setHighlightRange([obj?.start || 0, obj.end])
      }}
      onClick={(e) => {
        send({
          type: 'Set selection',
          data: {
            selectionType: 'singleCodeCursor',
            selection: {
              type: 'default',
              range: [obj?.start || 0, obj.end || 0],
            },
          },
        })
        e.stopPropagation()
      }}
    >
      {isCollapsed ? (
        
      ) : (
        
          {/*  */}
          
    {Object.entries(obj).map(([key, value]) => { if (filterKeys.includes(key)) { return null } else if (Array.isArray(value)) { return (
  • {`${key}: [`} {']'}
  • ) } else if ( typeof value === 'object' && value !== null && value?.end ) { return (
  • {key}:
  • ) } else if ( typeof value === 'string' || typeof value === 'number' ) { return (
  • {key}: {value}
  • ) } return null })}
)}
) }