aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/Model/SourceLinks.kt
blob: 99ee362ebfd806b6b581c30b826059991315338f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package org.jetbrains.dokka

import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiNameIdentifierOwner
import org.jetbrains.dokka.DokkaConfiguration.SourceLinkDefinition
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import java.io.File


fun DocumentationNode.appendSourceLink(psi: PsiElement?, sourceLinks: List<SourceLinkDefinition>) {
    val path = psi?.containingFile?.virtualFile?.path ?: return
    val canonicalPath = File(path).canonicalPath

    val target = if (psi is PsiNameIdentifierOwner) psi.nameIdentifier else psi
    val pair = determineSourceLinkDefinition(canonicalPath, sourceLinks)
    if (pair != null) {
        val (sourceLinkDefinition, sourceLinkCanonicalPath) = pair
        var url = determineUrl(canonicalPath, sourceLinkDefinition, sourceLinkCanonicalPath)
        if (sourceLinkDefinition.lineSuffix != null) {
            val line = target?.lineNumber()
            if (line != null) {
                url += sourceLinkDefinition.lineSuffix + line.toString()
            }
        }
        append(DocumentationNode(url, Content.Empty, NodeKind.SourceUrl), RefKind.Detail)
    }

    if (target != null) {
        append(DocumentationNode(target.sourcePosition(), Content.Empty, NodeKind.SourcePosition), RefKind.Detail)
    }
}

private fun determineSourceLinkDefinition(
    canonicalPath: String,
    sourceLinks: List<SourceLinkDefinition>
): Pair<SourceLinkDefinition, String>? {
    return sourceLinks
        .asSequence()
        .map { it to File(it.path).canonicalPath }
        .firstOrNull { (_, sourceLinkCanonicalPath) ->
            canonicalPath.startsWith(sourceLinkCanonicalPath)
        }
}

private fun determineUrl(
    canonicalPath: String,
    sourceLinkDefinition: SourceLinkDefinition,
    sourceLinkCanonicalPath: String
): String {
    val relativePath = canonicalPath.substring(sourceLinkCanonicalPath.length)
    val relativeUrl = relativePath.replace('\\', '/').removePrefix("/")
    return "${sourceLinkDefinition.url.removeSuffix("/")}/$relativeUrl"
}

private fun PsiElement.sourcePosition(): String {
    val path = containingFile.virtualFile.path
    val lineNumber = lineNumber()
    val columnNumber = columnNumber()

    return when {
        lineNumber == null -> path
        columnNumber == null -> "$path:$lineNumber"
        else -> "$path:$lineNumber:$columnNumber"
    }
}

fun PsiElement.lineNumber(): Int? {
    val doc = PsiDocumentManager.getInstance(project).getDocument(containingFile)
    // IJ uses 0-based line-numbers; external source browsers use 1-based
    return doc?.getLineNumber(textRange.startOffset)?.plus(1)
}

fun PsiElement.columnNumber(): Int? {
    val doc = PsiDocumentManager.getInstance(project).getDocument(containingFile) ?: return null
    val lineNumber = doc.getLineNumber(textRange.startOffset)
    return startOffset - doc.getLineStartOffset(lineNumber)
}