function processLineWithColon()

in src/yaml/yamlUtils.ts [118:230]


function processLineWithColon(document: TextDocument, cursorPosition: Position, currentLine: string, currentLineEnd: number): ProcessYamlDocForCompletionOutput {
    const docText = document.getText()
    const docTextLength = docText.length

    let modifiedDocText: string = docText
    let shouldPrependSpace: boolean = false

    const tempPositionForCompletions: Position = { ...cursorPosition }
    const startPositionForInsertion: Position = { ...cursorPosition }
    const endPositionForInsertion: Position = { ...cursorPosition }

    const charNum = cursorPosition.character
    const lineOffsets = getLineOffsets(docText)

    let preText: string
    let postText: string
    let insertedText: string

    // Current line has a colon, determine if cursor position is right or left of it
    const colonIndex = currentLine.indexOf(':')

    // Cursor is left of the colon.
    if (charNum <= colonIndex) {
        // Start position is first non-space character, ending position is left of colon.
        const preColonTextTrimmed = currentLine.substring(0, colonIndex).trim()

        // Only whitespace before the colon.
        if (preColonTextTrimmed.length === 0) {
            // Insert placeholder quotes and place cursor inside of them.
            preText = docText.substring(0, lineOffsets[cursorPosition.line] + colonIndex)
            insertedText = "''"
            postText = docText.substr(lineOffsets[cursorPosition.line] + colonIndex)

            startPositionForInsertion.character = colonIndex
            endPositionForInsertion.character = colonIndex
            tempPositionForCompletions.character = colonIndex + 1

        // Only hyphen before the colon.
        } else if (preColonTextTrimmed.length === 1 && preColonTextTrimmed.charAt(0) === '-') {
            // Insert placeholder quotes and place cursor inside of them.
            preText = docText.substring(0, lineOffsets[cursorPosition.line] + colonIndex)
            insertedText = " ''"
            postText = docText.substr(lineOffsets[cursorPosition.line] + colonIndex)

            startPositionForInsertion.character = colonIndex
            endPositionForInsertion.character = colonIndex

            // Move cursor to be inside the quotes
            tempPositionForCompletions.character = colonIndex + 2
            shouldPrependSpace = currentLine.charAt(Math.max(colonIndex - 1, 0)) !== ' '
        } else {
            // Set start of insertion range to be where the non-whitespace characters start.
            startPositionForInsertion.character = currentLine.indexOf(preColonTextTrimmed)
            // Set end of insertion range to be immediately before the colon.
            endPositionForInsertion.character = colonIndex

            preText = docText
            insertedText = ''
            postText = ''

            // If the non-whitespace characters start with a hyphen, adjust starting position to be after the hyphen
            if (preColonTextTrimmed.charAt(0) === '-') {
                startPositionForInsertion.character = Math.min(startPositionForInsertion.character + 2, colonIndex)
            }
        }

    // Cursor is right of the colon.
    } else {
        // Starting position is immediately after the colon, end is end of line
        startPositionForInsertion.character = colonIndex + 1
        endPositionForInsertion.character = currentLine.length
        shouldPrependSpace = true

        // Remove all trailing whitespace on that line and adjust tempPositionForCompletions if needed
        let numTrailingSpacesToRemove = 0
        while (currentLine.charAt(currentLine.length - numTrailingSpacesToRemove - 1) === ' ') {
            numTrailingSpacesToRemove++
        }
        tempPositionForCompletions.character = Math.min(tempPositionForCompletions.character, currentLine.length - numTrailingSpacesToRemove - 1)

        // If after removing trailing spaces, the last character is the colon, add a placeholder empty string value
        const postColonTextTrimmed = currentLine.substring(colonIndex + 1).trim()
        const hasOnlyWhitespaceAfterColon = postColonTextTrimmed.length === 0

        preText = docText.substring(0, currentLineEnd - numTrailingSpacesToRemove)
        insertedText = (hasOnlyWhitespaceAfterColon ? ' ""' : '') + '\n'
        postText = docText.substr(lineOffsets[cursorPosition.line + 1] || docTextLength)

        if (hasOnlyWhitespaceAfterColon) {
            // Move cursor inside of quotes
            tempPositionForCompletions.character += 2
        } else {
            // Current line has non-whitespace characters following the colon.
            const indexOfPostColonCharacters = currentLine.indexOf(postColonTextTrimmed)

            // Cursor is between colon and start of post-colon characters
            if (charNum < indexOfPostColonCharacters) {
                // Move cursor to be at immediately before the value
                tempPositionForCompletions.character = indexOfPostColonCharacters
            }
        }
    }

    modifiedDocText = `${preText}${insertedText}${postText}`

    return {
        modifiedDocText,
        tempPositionForCompletions,
        startPositionForInsertion,
        endPositionForInsertion,
        shouldPrependSpace
    }
}