forked from codemod-com/codemod
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[studio] feature: New ast tree (codemod-com#1089)
[studio] feature: New ast tree
- Loading branch information
Showing
16 changed files
with
382 additions
and
294 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
144 changes: 144 additions & 0 deletions
144
apps/frontend/app/(website)/studio/main/ASTViewer/AboristViewer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { cn } from "@/utils"; | ||
import type { Node } from "@babel/types"; | ||
import Text from "@studio/components/Text"; | ||
import { useScrollNodeIntoView } from "@studio/main/ASTViewer/useScrollNodeIntoView"; | ||
import { | ||
useSelectFirstTreeNodeForSnippet, | ||
useSnippetsStore, | ||
} from "@studio/store/snippets"; | ||
import { useRangesOnTarget } from "@studio/store/utils/useRangesOnTarget"; | ||
import React, { useCallback, useEffect, useRef, useState } from "react"; | ||
import { type NodeRendererProps, Tree } from "react-arborist"; | ||
import { flushSync } from "react-dom"; | ||
import useResizeObserver from "use-resize-observer"; | ||
|
||
type TreeNode = { | ||
id: string; | ||
actualNode: Node; | ||
label: string; | ||
children?: TreeNode[]; | ||
start: number; | ||
end: number; | ||
}; | ||
|
||
type EditorType = "before" | "after" | "output"; | ||
|
||
interface Props { | ||
type: EditorType; | ||
} | ||
|
||
const transformTreeData = (node: TreeNode): TreeNode => ({ | ||
...node, | ||
children: node.children?.length | ||
? node.children.map(transformTreeData) | ||
: undefined, | ||
}); | ||
|
||
export const ASTViewer: React.FC<Props> = ({ type }) => { | ||
const ASTTreeRef = useRef<HTMLDivElement>(null); | ||
const { width = 0, height = 0 } = useResizeObserver({ ref: ASTTreeRef }); | ||
const getFirstTreeNode = useSelectFirstTreeNodeForSnippet(); | ||
const [firstNode, setFirstNode] = useState<TreeNode | null>(null); | ||
const { getSelectedEditors } = useSnippetsStore(); | ||
const { | ||
[type]: { rootNode }, | ||
} = getSelectedEditors(); | ||
|
||
const setRangesOnTarget = useRangesOnTarget(); | ||
const scrollNodeIntoView = useScrollNodeIntoView(type); | ||
|
||
const handleNodeClick = useCallback( | ||
(node: TreeNode = rootNode) => { | ||
scrollNodeIntoView(node, ASTTreeRef); | ||
|
||
flushSync(() => { | ||
setFirstNode(node); | ||
setRangesOnTarget({ | ||
target: `${type.toUpperCase()}_INPUT`, | ||
ranges: [node], | ||
}); | ||
const setRange = getSelectedEditors().setSelection(type); | ||
setRange({ | ||
kind: "FIND_CLOSEST_PARENT", | ||
ranges: [node], | ||
}); | ||
}); | ||
}, | ||
[rootNode, scrollNodeIntoView, setRangesOnTarget, type, getSelectedEditors], | ||
); | ||
|
||
useEffect(() => { | ||
const firstTreeNode = getFirstTreeNode(type); | ||
if (firstTreeNode) { | ||
scrollNodeIntoView(firstTreeNode, ASTTreeRef); | ||
setFirstNode(firstTreeNode); | ||
} | ||
}, [scrollNodeIntoView, getFirstTreeNode, type]); | ||
|
||
const NodeComponent = React.memo<NodeRendererProps<TreeNode>>( | ||
({ node, style, dragHandle }) => { | ||
const isSelected = firstNode?.id === node.data.id; | ||
return ( | ||
<div style={style} ref={dragHandle}> | ||
<Text | ||
className="cursor-pointer whitespace-nowrap" | ||
color={isSelected ? "text-cyan-500" : undefined} | ||
> | ||
{!node.isLeaf && ( | ||
<strong | ||
onClick={() => node.toggle()} | ||
className={cn( | ||
node.isOpen ? "text-red-600 " : "text-green-500", | ||
)} | ||
> | ||
{node.isOpen ? "- " : "+ "} | ||
</strong> | ||
)} | ||
<span | ||
onClick={(e) => { | ||
e.stopPropagation(); | ||
handleNodeClick(node.data); | ||
}} | ||
className="inline-block" | ||
id={`${node.data.id}`} | ||
> | ||
{node.data.label} | ||
</span> | ||
</Text> | ||
</div> | ||
); | ||
}, | ||
); | ||
|
||
NodeComponent.displayName = "NodeComponent"; | ||
|
||
if (!rootNode) { | ||
return ( | ||
<div className="flex h-full flex-col w-full overflow-hidden p-2"> | ||
<Text> | ||
Please provide a snippet to render an Abstract Syntax Tree for it. | ||
</Text> | ||
</div> | ||
); | ||
} | ||
|
||
const transformedRootNode = transformTreeData(rootNode); | ||
|
||
return ( | ||
<div | ||
className="flex h-full flex-col w-full overflow-hidden p-2" | ||
ref={ASTTreeRef} | ||
> | ||
<Tree | ||
data={transformedRootNode.children || []} | ||
openByDefault={true} | ||
width={width} | ||
height={height} | ||
indent={12} | ||
rowHeight={22} | ||
> | ||
{NodeComponent} | ||
</Tree> | ||
</div> | ||
); | ||
}; |
40 changes: 40 additions & 0 deletions
40
apps/frontend/app/(website)/studio/main/ASTViewer/AstSectionBase.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { BoundResizePanel } from "@studio/components/ResizePanel/BoundResizePanel"; | ||
import ResizeHandle from "@studio/components/ResizePanel/ResizeHandler"; | ||
import { ASTViewer } from "@studio/main/ASTViewer/AboristViewer"; | ||
import type { | ||
PanelData, | ||
PanelsRefs, | ||
} from "@studio/main/PageBottomPane/utils/types"; | ||
import { isVisible } from "@studio/utils/visibility"; | ||
import React, { memo } from "react"; | ||
|
||
const AstSectionBase = ({ | ||
panels, | ||
panelRefs, | ||
}: { | ||
panels: PanelData[]; | ||
panelRefs: PanelsRefs; | ||
}) => { | ||
return panels.filter(isVisible).map((panel, i, { length }) => ( | ||
<React.Fragment key={panel.relatedAST}> | ||
<BoundResizePanel | ||
className="h-full" | ||
panelRefs={panelRefs} | ||
key={panel.relatedAST} | ||
defaultSize={100 / panels.length} | ||
panelRefIndex={panel.relatedAST} | ||
boundedIndex={ | ||
panel.boundIndex === panel.relatedAST ? panel.snippedIndex : undefined | ||
} | ||
{...panel} | ||
> | ||
<ASTViewer type={panel.type} /> | ||
</BoundResizePanel> | ||
{i !== length - 1 && isVisible(panels[i + 1]) && ( | ||
<ResizeHandle key={`resize-handle-${i}`} direction="horizontal" /> | ||
)} | ||
</React.Fragment> | ||
)); | ||
}; | ||
|
||
export const AstSection = memo(AstSectionBase); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
apps/frontend/app/(website)/studio/main/PageBottomPane/Components/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from "./Snippets/CodeSnippets"; | ||
export * from "./WarningTexts"; | ||
export * from "./SnippedHeader"; | ||
export * from "./side-components"; |
Oops, something went wrong.