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

com.itangcent.intellij.jvm.scala.ScalaLinkExtractor.kt Maven / Gradle / Ivy

The newest version!
package com.itangcent.intellij.jvm.scala

import com.intellij.psi.PsiElement
import com.itangcent.intellij.jvm.standard.AbstractLinkExtractor
import java.util.regex.Pattern

/**
 * see https://docs.scala-lang.org/style/scaladoc.html
 */
open class ScalaLinkExtractor : AbstractLinkExtractor() {

    override fun findLink(doc: String, psiElement: PsiElement, resolver: (String) -> String?): String {

        if (!ScPsiUtils.isScPsiInst(psiElement)) {
            throw NotImplementedError()
        }

        if (doc.contains("@link") || doc.contains("[[")) {
            val pattern = Pattern.compile("\\{@link(.*?)}|\\[\\[(.*?)]]")
            val matcher = pattern.matcher(doc)

            val sb = StringBuffer()
            while (matcher.find()) {
                matcher.appendReplacement(sb, "")
                val linkClassAndMethod = matcher.group(1)
                    ?: matcher.group(2)
                    ?: continue
                resolver(linkClassAndMethod)?.let { sb.append(it) }
            }
            matcher.appendTail(sb)
            return sb.toString()
        }

        return doc
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy