add format button

This commit is contained in:
Kurt Hutten IrevDev
2022-11-28 19:43:20 +11:00
parent ade1e9fb82
commit b671db1e81
2 changed files with 34 additions and 14 deletions

View File

@ -1,10 +1,11 @@
import { useRef, useState, useEffect } from 'react' import { useRef, useState, useEffect, useMemo } from 'react'
import { Canvas } from '@react-three/fiber' import { Canvas } from '@react-three/fiber'
import { Allotment } from 'allotment' import { Allotment } from 'allotment'
import { OrbitControls, OrthographicCamera } from '@react-three/drei' import { OrbitControls, OrthographicCamera } from '@react-three/drei'
import { lexer } from './lang/tokeniser' import { lexer } from './lang/tokeniser'
import { abstractSyntaxTree } from './lang/abstractSyntaxTree' import { abstractSyntaxTree } from './lang/abstractSyntaxTree'
import { executor } from './lang/executor' import { executor } from './lang/executor'
import { recast } from './lang/recast'
import { BufferGeometry } from 'three' import { BufferGeometry } from 'three'
import CodeMirror from '@uiw/react-codemirror' import CodeMirror from '@uiw/react-codemirror'
import { javascript } from '@codemirror/lang-javascript' import { javascript } from '@codemirror/lang-javascript'
@ -38,6 +39,8 @@ function App() {
code, code,
setCode, setCode,
setAst, setAst,
formatCode,
ast,
} = useStore((s) => ({ } = useStore((s) => ({
editorView: s.editorView, editorView: s.editorView,
setEditorView: s.setEditorView, setEditorView: s.setEditorView,
@ -51,7 +54,8 @@ function App() {
setCode: s.setCode, setCode: s.setCode,
ast: s.ast, ast: s.ast,
setAst: s.setAst, setAst: s.setAst,
lastGuiMode: s.lastGuiMode lastGuiMode: s.lastGuiMode,
formatCode: s.formatCode,
})) }))
// const onChange = React.useCallback((value: string, viewUpdate: ViewUpdate) => { // const onChange = React.useCallback((value: string, viewUpdate: ViewUpdate) => {
const onChange = (value: string, viewUpdate: ViewUpdate) => { const onChange = (value: string, viewUpdate: ViewUpdate) => {
@ -116,11 +120,18 @@ function App() {
addLog(e) addLog(e)
} }
}, [code]) }, [code])
const shouldFormat = useMemo(() => {
if(!ast) return false
const recastedCode = recast(ast)
return recastedCode !== code
}, [code, ast])
return ( return (
<div className="h-screen"> <div className="h-screen">
<Allotment> <Allotment>
<Logs /> <Logs />
<div className="bg-red h-full overflow-auto"> <div className="h-full flex flex-col items-start">
<button disabled={!shouldFormat} onClick={formatCode} className={`${!shouldFormat && "text-gray-300"}`}>format</button>
<div className="bg-red h-full w-full overflow-auto">
<CodeMirror <CodeMirror
className="h-full" className="h-full"
value={code} value={code}
@ -130,6 +141,7 @@ function App() {
onCreateEditor={(_editorView) => setEditorView(_editorView)} onCreateEditor={(_editorView) => setEditorView(_editorView)}
/> />
</div> </div>
</div>
<div className="h-full"> <div className="h-full">
<Toolbar /> <Toolbar />
<div className="border h-full border-gray-300 relative"> <div className="border h-full border-gray-300 relative">

View File

@ -1,7 +1,8 @@
import create from 'zustand' import create from 'zustand'
import { addLineHighlight, EditorView } from './editor/highlightextension' import { addLineHighlight, EditorView } from './editor/highlightextension'
import { Program } from './lang/abstractSyntaxTree' import { Program, abstractSyntaxTree } from './lang/abstractSyntaxTree'
import { recast } from './lang/recast' import { recast } from './lang/recast'
import { lexer } from './lang/tokeniser'
export type Range = [number, number] export type Range = [number, number]
@ -42,6 +43,7 @@ interface StoreState {
updateAst: (ast: Program) => void updateAst: (ast: Program) => void
code: string code: string
setCode: (code: string) => void setCode: (code: string) => void
formatCode: () => void
} }
export const useStore = create<StoreState>()((set, get) => ({ export const useStore = create<StoreState>()((set, get) => ({
@ -66,7 +68,7 @@ export const useStore = create<StoreState>()((set, get) => ({
setGuiMode: (guiMode) => { setGuiMode: (guiMode) => {
const lastGuiMode = get().guiMode const lastGuiMode = get().guiMode
set({ guiMode }) set({ guiMode })
if(guiMode.mode !== 'codeError') { if (guiMode.mode !== 'codeError') {
// don't set lastGuiMode to and error state // don't set lastGuiMode to and error state
// as the point fo lastGuiMode is to restore the last healthy state // as the point fo lastGuiMode is to restore the last healthy state
// todo maybe rename to lastHealthyGuiMode and remove this comment // todo maybe rename to lastHealthyGuiMode and remove this comment
@ -98,5 +100,11 @@ export const useStore = create<StoreState>()((set, get) => ({
code: '', code: '',
setCode: (code) => { setCode: (code) => {
set({ code }) set({ code })
} },
formatCode: () => {
const code = get().code
const ast = abstractSyntaxTree(lexer(code))
const newCode = recast(ast)
set({ code: newCode, ast })
},
})) }))