diff options
author | Dmitry Jemerov <intelliyole@gmail.com> | 2015-07-27 15:13:11 +0200 |
---|---|---|
committer | Dmitry Jemerov <intelliyole@gmail.com> | 2015-07-27 15:13:11 +0200 |
commit | befbe69c3863b1048b585e795ec2f99d5a10390e (patch) | |
tree | d2392c9a9fafbd5b2e7b9c5c35c593fd73d59d0d /src/Model/SourceLinks.kt | |
parent | 28a3891ffa2c4984a9d49be41d76fe97854872c9 (diff) | |
parent | 2930cd9685c5e6296b24e9530b296c7ce3ea89fa (diff) | |
download | dokka-befbe69c3863b1048b585e795ec2f99d5a10390e.tar.gz dokka-befbe69c3863b1048b585e795ec2f99d5a10390e.tar.bz2 dokka-befbe69c3863b1048b585e795ec2f99d5a10390e.zip |
Merge pull request #31 from cy6erGn0m/javadoc
Kotlin upgrade + source position in documentation node
Diffstat (limited to 'src/Model/SourceLinks.kt')
-rw-r--r-- | src/Model/SourceLinks.kt | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/src/Model/SourceLinks.kt b/src/Model/SourceLinks.kt index fc3293f2..4530518f 100644 --- a/src/Model/SourceLinks.kt +++ b/src/Model/SourceLinks.kt @@ -4,16 +4,15 @@ import com.intellij.psi.PsiElement import java.io.File import com.intellij.psi.PsiDocumentManager import com.intellij.psi.PsiNameIdentifierOwner +import org.jetbrains.kotlin.psi.psiUtil.startOffset class SourceLinkDefinition(val path: String, val url: String, val lineSuffix: String?) fun DocumentationNode.appendSourceLink(psi: PsiElement?, sourceLinks: List<SourceLinkDefinition>) { - val path = psi?.getContainingFile()?.getVirtualFile()?.getPath() - if (path == null) { - return - } - val target = if (psi is PsiNameIdentifierOwner) psi.getNameIdentifier() else psi - val absPath = File(path).getAbsolutePath() + val path = psi?.containingFile?.virtualFile?.path ?: return + + val target = if (psi is PsiNameIdentifierOwner) psi.nameIdentifier else psi + val absPath = File(path).absolutePath val linkDef = sourceLinks.firstOrNull { absPath.startsWith(it.path) } if (linkDef != null) { var url = linkDef.url + path.substring(linkDef.path.length()) @@ -26,10 +25,33 @@ fun DocumentationNode.appendSourceLink(psi: PsiElement?, sourceLinks: List<Sourc append(DocumentationNode(url, Content.Empty, DocumentationNode.Kind.SourceUrl), DocumentationReference.Kind.Detail); } + + if (target != null) { + append(DocumentationNode(target.sourcePosition(), Content.Empty, DocumentationNode.Kind.SourcePosition), DocumentationReference.Kind.Detail) + } +} + +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(getProject()).getDocument(getContainingFile()) + val doc = PsiDocumentManager.getInstance(project).getDocument(containingFile) // IJ uses 0-based line-numbers; external source browsers use 1-based - return doc?.getLineNumber(getTextRange().getStartOffset())?.plus(1) + return doc?.getLineNumber(textRange.startOffset)?.plus(1) } + +fun PsiElement.columnNumber(): Int? { + val doc = PsiDocumentManager.getInstance(project).getDocument(containingFile) + // IJ uses 0-based line-numbers; external source browsers use 1-based + val lineNumber = doc?.getLineNumber(textRange.startOffset)?.plus(1) ?: return null + return startOffset - doc!!.getLineStartOffset(lineNumber) +}
\ No newline at end of file |