2023-10-16 08:54:38 +11:00
|
|
|
import {
|
|
|
|
SetVarNameModal,
|
|
|
|
createSetVarNameModal,
|
|
|
|
} from 'components/SetVarNameModal'
|
2024-04-19 14:24:40 -07:00
|
|
|
import { editorManager, kclManager } from 'lib/singletons'
|
2024-06-24 11:45:40 -04:00
|
|
|
import { trap } 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-05-24 20:54:42 +10:00
|
|
|
import { PathToNode, SourceRange, parse, recast } from 'lang/wasm'
|
|
|
|
import { useKclContext } from 'lang/KclProvider'
|
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-06-24 11:45:40 -04:00
|
|
|
const parsed = parse(recast(ast))
|
|
|
|
if (trap(parsed)) return
|
|
|
|
|
|
|
|
const meta = isNodeSafeToReplace(
|
|
|
|
parsed,
|
2024-05-24 20:54:42 +10:00
|
|
|
range || context.selectionRanges.codeBasedSelections?.[0]?.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-05-24 20:54:42 +10:00
|
|
|
!!range || context.selectionRanges.codeBasedSelections.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,
|
|
|
|
range || context.selectionRanges.codeBasedSelections[0].range,
|
|
|
|
variableName
|
|
|
|
)
|
2023-09-09 01:38:36 -04:00
|
|
|
|
2024-05-24 20:54:42 +10:00
|
|
|
await kclManager.updateAst(_modifiedAst, true)
|
|
|
|
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-04-19 14:24:40 -07:00
|
|
|
editorManager.convertToVariableCallback = handleClick
|
|
|
|
|
2023-09-09 01:38:36 -04:00
|
|
|
return { enable, handleClick }
|
|
|
|
}
|