* Add support for forcing kcl input create variable * Command palette padding tweak * Make traverse function work for ExpressionStatements * Add utilities for getting earliest safe index in AST * Fix the insertIndex logic to not be based on the selection anymore * Add workflow to create a named constant * Fix bug with nameEndInDigits matcher * Tweak command config * Add a three-dot menu to feature tree pane to create parameters * Add E2E test for create parameter flow * Remove edit flow oops * Fix tsc error * Fix E2E test * Update named constant position in edit flow test * Add tags into consideration for safe insert index Per @Irev-dev's helpful feedback, with unit tests! * Fix tsc by removing a generic type * Remove unused imports * Fix lints * A snapshot a day keeps the bugs away! 📷🐛 * Add utilities for working with variable declarations * Add "edit parameter" user flow * Add edit flow config * WIP working on de-bloating useCalculateKclExpreesion * Add the ability to specify a `displayName` for an arg * Add utility to type check on SourceRanges * Review step design tweak fixes * Refactor useCalculateKclExpression to take a sourceRange * Make option arg validation work for objects and arrays Using an admittedly dumb stringification approach * Make edit flow never move the constant to be edited * Add E2E test section * Fix lints * Remove lying comment, tiny CSS tweak * A snapshot a day keeps the bugs away! 📷🐛 --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import { getIdentifiersInProgram } from './getIndentifiersInProgram'
|
|
import { Program, Expr } from 'lang/wasm'
|
|
import { Node } from '@rust/kcl-lib/bindings/Node'
|
|
import { getTagDeclaratorsInProgram } from './getTagDeclaratorsInProgram'
|
|
|
|
/**
|
|
* Given a target expression, return the body index of the last-used variable
|
|
* or tag declaration within the provided program.
|
|
*/
|
|
export function getSafeInsertIndex(
|
|
targetExpr: Node<Program | Expr>,
|
|
program: Node<Program>
|
|
) {
|
|
const identifiers = getIdentifiersInProgram(targetExpr)
|
|
const safeIdentifierIndex = identifiers.reduce((acc, curr) => {
|
|
const bodyIndex = program.body.findIndex(
|
|
(a) =>
|
|
a.type === 'VariableDeclaration' && a.declaration.id?.name === curr.name
|
|
)
|
|
return Math.max(acc, bodyIndex + 1)
|
|
}, 0)
|
|
|
|
const tagDeclarators = getTagDeclaratorsInProgram(program)
|
|
const safeTagIndex = tagDeclarators.reduce((acc, curr) => {
|
|
return identifiers.findIndex((a) => a.name === curr.tag.value) === -1
|
|
? acc
|
|
: Math.max(acc, curr.bodyIndex + 1)
|
|
}, 0)
|
|
|
|
return Math.max(safeIdentifierIndex, safeTagIndex)
|
|
}
|