export default function completeStateNames()

in src/completion/completeStateNames.ts [115:184]


export default function completeStateNames(
  node: ASTNode | undefined,
  offset: number,
  document: TextDocument,
  options?: ASLOptions,
): CompletionList | undefined {
  // For property nodes
  if (node && isPropertyNode(node) && node.colonOffset) {
    const states = getListOfItems(node, options)

    if (states.length) {
      const colonPosition = document.positionAt(node.colonOffset + 1)
      let endPosition = document.positionAt(node.offset + node.length)

      // The range shouldn't span multiple lines, if lines are different it is due to
      // lack of comma and text should be inserted in place
      if (colonPosition.line !== endPosition.line) {
        endPosition = colonPosition
      }

      const range = Range.create(colonPosition, endPosition)

      const completeStateNameOptions = {
        shouldAddLeftQuote: true,
        shouldAddRightQuote: true,
        shouldAddLeadingSpace: true,
        shoudlAddTrailingComma: true,
      }

      return getCompletionList(states, range, document.languageId, completeStateNameOptions)
    }
  }

  // For string nodes that have a parent that is a property node
  if (node && node.parent && isPropertyNode(node.parent)) {
    const propNode = node.parent

    if (isStringNode(node)) {
      const states = getListOfItems(propNode, options)

      if (states.length) {
        // Text edit will only work when start position is higher than the node offset
        const startPosition = document.positionAt(node.offset + 1)
        const endPosition = document.positionAt(node.offset + node.length)

        const range = Range.create(startPosition, endPosition)
        if (document.languageId === LANGUAGE_IDS.YAML) {
          const completeStateNameOptions = {
            shouldAddLeftQuote: false,
            shouldAddRightQuote: false,
            shouldAddLeadingSpace: false,
            shoudlAddTrailingComma: false,
          }

          return getCompletionList(states, range, document.languageId, completeStateNameOptions)
        } else {
          const isCursorAtTheBeginning = offset === node.offset
          const completeStateNameOptions = {
            shouldAddLeftQuote: isCursorAtTheBeginning,
            shouldAddRightQuote: true,
            shouldAddLeadingSpace: false,
            shoudlAddTrailingComma: false,
          }

          return getCompletionList(states, range, document.languageId, completeStateNameOptions)
        }
      }
    }
  }
}