function getBackwardOffsetData()

in src/yaml/yamlUtils.ts [322:387]


function getBackwardOffsetData(text: string, offset: number, initialNumberOfSpaces: number) {
    let isDirectChildOfStates = false
    let isGrandChildOfStates = false
    let isWithinCatchRetryState = false;
    let hasCatchPropSibling = false;
    let hasRetryPropSibling = false;
    let beginLineOffset = offset;
    let levelsDown = 0;

    for (let i = offset; i >= 0; i--) {
        if (text[i] === '\n') {
            const lineText = text.slice(i + 1, beginLineOffset)
            const numberOfPrecedingSpaces = getNumberOfLeftSpaces(lineText)
            const trimmedLine = lineText.trim()
            beginLineOffset = i

            // Ignore empty lines
            if (trimmedLine.length === 0) {
                continue
            }

            // If number of spaces lower than that of the cursor
            // it is a parent property or a sibling of parent property
            if (numberOfPrecedingSpaces < initialNumberOfSpaces) {
                if (trimmedLine.startsWith(STATES_PROP_STRING)) {
                    isDirectChildOfStates = levelsDown === 0;
                    isGrandChildOfStates = levelsDown === 1;
                    break
                // If the indentation is one level lower increase the number in levelsDown variable
                // and start checking if the offset is a "grandchild" of states.
                } else if (levelsDown === 0) {
                    levelsDown++
                    continue
                // When the levelDown is 1 it means there is no point of searching anymore.
                // We know that the offset is neither child nor grandchild of states. We have all the needed information.
                } else {
                    break
                }

            // If number of spaces is higher than that of the cursor it means it is a child
            // of the property or of its siblings
            } else if (numberOfPrecedingSpaces > initialNumberOfSpaces) {
                continue
            } else if (levelsDown > 0) {
                continue
            }

            hasCatchPropSibling = trimmedLine.startsWith(CATCH_PROP_STRING) || hasCatchPropSibling
            hasRetryPropSibling = trimmedLine.startsWith(RETRY_PROP_STRING) || hasRetryPropSibling

            const isCatchRetryState = CATCH_RETRY_STATE_REGEX.test(trimmedLine)

            if (isCatchRetryState) {
                isWithinCatchRetryState = true
            }
        }
    }

    return {
        isDirectChildOfStates,
        isGrandChildOfStates,
        isWithinCatchRetryState,
        hasCatchPropSibling,
        hasRetryPropSibling
    }
}