fun computeAlignment()

in src/org/jetbrains/r/editor/formatting/RFormattingContext.kt [53:101]


  fun computeAlignment(node: ASTNode): Alignment? {
    val common = settings.getCommonSettings(RLanguage.INSTANCE)
    val custom = settings.getCustomSettings(RCodeStyleSettings::class.java)

    val nodeParent = node.treeParent ?: return null

    val alignParameters = common.ALIGN_MULTILINE_PARAMETERS && nodeParent.elementType == RElementTypes.R_PARAMETER_LIST &&
                          (node.elementType == RElementTypes.R_PARAMETER ||
                           node.elementType == RElementTypes.R_COMMA ||
                           isCommentAtEmptyLine(node))

    val alignCallArguments = common.ALIGN_MULTILINE_PARAMETERS_IN_CALLS &&
                             nodeParent.elementType == RElementTypes.R_ARGUMENT_LIST &&
                             (node.firstChildNode != null || isCommentAtEmptyLine(node))
                             && !hasMultilineBlock(nodeParent)

    val alignSubscriptionArguments = common.ALIGN_MULTILINE_PARAMETERS_IN_CALLS && nodeParent.elementType == RElementTypes.R_SUBSCRIPTION_EXPRESSION &&
                             (node != nodeParent.firstChildNode && node.firstChildNode != null || isCommentAtEmptyLine(node))

    if (alignCallArguments || alignParameters || alignSubscriptionArguments) {
      return childIndentAlignments[nodeParent]
    }

    if (custom.ALIGN_COMMENTS &&
        node.elementType == RTokenTypes.END_OF_LINE_COMMENT &&
        nodeParent.elementType == RElementTypes.R_ARGUMENT_LIST &&
        (findPrevNonSpaceNode(node)?.elementType == RElementTypes.R_COMMA ||
         findNextMeaningSibling(node)?.elementType == RElementTypes.R_RPAR)
    ) {
      findFirstCommentAfterComma(nodeParent.firstChildNode)?.let {
        return alignmentByAnchor[it]
      }
    }

    val nodeGrandParent = nodeParent.treeParent ?: return null
    if (custom.ALIGN_ASSIGNMENT_OPERATORS &&
        node.elementType == RElementTypes.R_ASSIGN_OPERATOR &&
        (nodeParent.elementType == RElementTypes.R_ASSIGNMENT_STATEMENT || nodeParent.elementType == RElementTypes.R_NAMED_ARGUMENT)) {
      if (nodeGrandParent.elementType == RElementTypes.R_ARGUMENT_LIST) {
        return assignInParametersAlignments[nodeGrandParent]
      }
      if (!isFunctionDeclarationNode(nodeParent) && nodeGrandParent.elementType == RElementTypes.R_BLOCK_EXPRESSION ||
          nodeGrandParent.elementType == RParserDefinition.FILE) {
        val anchor = findFirstAssignmentInTable(nodeParent)
        return alignmentByAnchor[anchor]
      }
    }
    return null
  }