fun getImportReferences()

in plugin-bazel/src/main/kotlin/org/jetbrains/bazel/golang/resolve/BazelGoPackage.kt [163:201]


    fun getImportReferences(
      label: Label,
      buildElement: PsiElement,
      importPath: String,
    ): Array<PsiElement?> {
      val pathComponents = importPath.split("/")
      val importReferences = arrayOfNulls<PsiElement>(pathComponents.size)

      if (pathComponents.isEmpty()) {
        return importReferences
      }

      // Get the last element (e.g., `bar` for the import path ending in `github.com/user/foo/bar`)
      val lastElement = getLastElement(pathComponents.last(), label, buildElement) ?: return importReferences

      importReferences[pathComponents.size - 1] = lastElement
      var currentElement =
        if (lastElement is PsiDirectory) {
          lastElement.parent
        } else {
          lastElement.containingFile?.parent
        }

      // Iterate backwards through the path components, linking names to the proper directories or elements
      for (i in pathComponents.size - 2 downTo 0) {
        if (currentElement == null) break

        val name = currentElement.name
        val pathComponent = pathComponents[i]
        if (name == pathComponent) {
          importReferences[i] = currentElement
          currentElement = currentElement.parent
        } else {
          break
        }
      }

      return importReferences
    }