All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.fir.analysis.PsiElementFinderByType.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.fir.analysis

import com.intellij.psi.PsiElement
import com.intellij.psi.tree.IElementType
import org.jetbrains.kotlin.psi.psiUtil.allChildren

class PsiElementFinderByType(
    private val types: Collection,
    private var index: Int,
    private val depth: Int
) {
    fun find(root: PsiElement): PsiElement? {
        return visitElement(root, 0)
    }

    fun visitElement(element: PsiElement, currentDepth: Int): PsiElement? {
        if (element.node.elementType in types) {
            if (index == 0) {
                return element
            }
            index--
        }

        if (currentDepth == depth) return null

        for (children in element.allChildren) {
            val result = visitElement(children, currentDepth + 1)
            if (result != null) return result
        }

        return null
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy