fun findPrecedingInstruction()

in psi/src/com/intellij/r/psi/psi/references/RResolveUtil.kt [14:42]


  fun findPrecedingInstruction(variable: RIdentifierExpression, accessor: (element: PsiElement) -> PsiElement?): PsiElement? {
    var currentElement: PsiElement = variable
    var rControlFlowHolder = PsiTreeUtil.getParentOfType(currentElement, RControlFlowHolder::class.java)
    while (rControlFlowHolder != null) {
      val variableInstruction = rControlFlowHolder.controlFlow.getInstructionByElement(currentElement)
      if (variableInstruction != null) {
        val queue = LinkedList<Instruction>()
        queue.add(variableInstruction)
        while (queue.isNotEmpty()) {
          val currentInstruction = queue.remove()
          val instructionElement = currentInstruction.element
          if (instructionElement != null) {
            val tableModificator = accessor(instructionElement)
            if (tableModificator != null) {
              return tableModificator
            }
          }
          for (prevInstruction in currentInstruction.allPred()) {
            if (prevInstruction.num() < currentInstruction.num() && prevInstruction !in queue) {
              queue.add(prevInstruction)
            }
          }
        }
      }
      currentElement = rControlFlowHolder
      rControlFlowHolder = PsiTreeUtil.getParentOfType(currentElement, RControlFlowHolder::class.java)
    }
    return null
  }