2023-10-16 08:54:38 +11:00
|
|
|
import {
|
|
|
|
SetVarNameModal,
|
|
|
|
createSetVarNameModal,
|
|
|
|
} from 'components/SetVarNameModal'
|
2024-11-16 16:49:44 -05:00
|
|
|
import { editorManager, kclManager, codeManager } from 'lib/singletons'
|
|
|
|
import { reportRejection, trap, err } from 'lib/trap'
|
2023-09-09 01:38:36 -04:00
|
|
|
import { moveValueIntoNewVariable } from 'lang/modifyAst'
|
|
|
|
import { isNodeSafeToReplace } from 'lang/queryAst'
|
|
|
|
import { useEffect, useState } from 'react'
|
2023-10-11 13:36:54 +11:00
|
|
|
import { useModelingContext } from './useModelingContext'
|
2024-11-16 16:49:44 -05:00
|
|
|
import { PathToNode, SourceRange, recast } from 'lang/wasm'
|
2024-05-24 20:54:42 +10:00
|
|
|
import { useKclContext } from 'lang/KclProvider'
|
2024-09-09 18:17:45 -04:00
|
|
|
import { toSync } from 'lib/utils'
|
2023-09-09 01:38:36 -04:00
|
|
|
|
2024-05-24 20:54:42 +10:00
|
|
|
export const getVarNameModal = createSetVarNameModal(SetVarNameModal)
|
2023-09-09 01:38:36 -04:00
|
|
|
|
2024-05-24 20:54:42 +10:00
|
|
|
export function useConvertToVariable(range?: SourceRange) {
|
|
|
|
const { ast } = useKclContext()
|
2023-10-11 13:36:54 +11:00
|
|
|
const { context } = useModelingContext()
|
2023-09-09 01:38:36 -04:00
|
|
|
const [enable, setEnabled] = useState(false)
|
2024-04-19 14:24:40 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
editorManager.convertToVariableEnabled = enable
|
|
|
|
}, [enable])
|
|
|
|
|
2023-09-09 01:38:36 -04:00
|
|
|
useEffect(() => {
|
2024-12-09 16:43:58 -05:00
|
|
|
// Return early if there are no selection ranges for whatever reason
|
|
|
|
if (!context.selectionRanges) return
|
2024-09-06 17:52:52 -04:00
|
|
|
const parsed = ast
|
2024-06-24 11:45:40 -04:00
|
|
|
|
|
|
|
const meta = isNodeSafeToReplace(
|
|
|
|
parsed,
|
2024-11-21 15:04:30 +11:00
|
|
|
range ||
|
|
|
|
context.selectionRanges.graphSelections?.[0]?.codeRef?.range ||
|
|
|
|
[]
|
2023-09-09 01:38:36 -04:00
|
|
|
)
|
2024-06-24 11:45:40 -04:00
|
|
|
if (trap(meta)) return
|
|
|
|
|
|
|
|
const { isSafe, value } = meta
|
2023-09-09 01:38:36 -04:00
|
|
|
const canReplace = isSafe && value.type !== 'Identifier'
|
2023-10-11 13:36:54 +11:00
|
|
|
const isOnlyOneSelection =
|
2024-11-21 15:04:30 +11:00
|
|
|
!!range || context.selectionRanges.graphSelections.length === 1
|
2023-09-09 01:38:36 -04:00
|
|
|
|
2024-05-24 20:54:42 +10:00
|
|
|
setEnabled(canReplace && isOnlyOneSelection)
|
2023-10-11 13:36:54 +11:00
|
|
|
}, [context.selectionRanges])
|
2023-09-09 01:38:36 -04:00
|
|
|
|
2024-05-24 20:54:42 +10:00
|
|
|
const handleClick = async (
|
|
|
|
valueName?: string
|
|
|
|
): Promise<PathToNode | undefined> => {
|
2023-09-09 01:38:36 -04:00
|
|
|
try {
|
2024-05-24 20:54:42 +10:00
|
|
|
const { variableName } = await getVarNameModal({
|
|
|
|
valueName: valueName || 'var',
|
2023-10-16 08:54:38 +11:00
|
|
|
})
|
2023-09-09 01:38:36 -04:00
|
|
|
|
2024-05-24 20:54:42 +10:00
|
|
|
const { modifiedAst: _modifiedAst, pathToReplacedNode } =
|
|
|
|
moveValueIntoNewVariable(
|
|
|
|
ast,
|
|
|
|
kclManager.programMemory,
|
2024-11-21 15:04:30 +11:00
|
|
|
range || context.selectionRanges.graphSelections[0]?.codeRef?.range,
|
2024-05-24 20:54:42 +10:00
|
|
|
variableName
|
|
|
|
)
|
2023-09-09 01:38:36 -04:00
|
|
|
|
2024-05-24 20:54:42 +10:00
|
|
|
await kclManager.updateAst(_modifiedAst, true)
|
2024-11-16 16:49:44 -05:00
|
|
|
|
|
|
|
const newCode = recast(_modifiedAst)
|
|
|
|
if (err(newCode)) return
|
|
|
|
codeManager.updateCodeEditor(newCode)
|
|
|
|
|
2024-05-24 20:54:42 +10:00
|
|
|
return pathToReplacedNode
|
2023-09-09 01:38:36 -04:00
|
|
|
} catch (e) {
|
2023-09-25 17:28:03 +10:00
|
|
|
console.log('error', e)
|
2023-09-09 01:38:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-09 18:17:45 -04:00
|
|
|
editorManager.convertToVariableCallback = toSync(handleClick, reportRejection)
|
2024-04-19 14:24:40 -07:00
|
|
|
|
2023-09-09 01:38:36 -04:00
|
|
|
return { enable, handleClick }
|
|
|
|
}
|