|
| 1 | +package com.github.lppedd.cc.lookupElement |
| 2 | + |
| 3 | +import com.github.lppedd.cc.CCBundle |
| 4 | +import com.github.lppedd.cc.api.CommitToken |
| 5 | +import com.github.lppedd.cc.brighter |
| 6 | +import com.github.lppedd.cc.darker |
| 7 | +import com.github.lppedd.cc.scaled |
| 8 | +import com.intellij.codeInsight.lookup.LookupElement |
| 9 | +import com.intellij.model.Pointer |
| 10 | +import com.intellij.openapi.util.NlsSafe |
| 11 | +import com.intellij.platform.backend.documentation.DocumentationResult |
| 12 | +import com.intellij.platform.backend.documentation.DocumentationTarget |
| 13 | +import com.intellij.platform.backend.documentation.LookupElementDocumentationTargetProvider |
| 14 | +import com.intellij.platform.backend.presentation.TargetPresentation |
| 15 | +import com.intellij.psi.PsiFile |
| 16 | +import com.intellij.ui.ColorUtil |
| 17 | +import com.intellij.ui.JBColor |
| 18 | +import com.intellij.util.ui.UIUtil |
| 19 | +import java.awt.Color |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Edoardo Luppi |
| 23 | + */ |
| 24 | +@Suppress("UnstableApiUsage") |
| 25 | +internal class CommitTokenLookupElementDocumentationTargetProvider : LookupElementDocumentationTargetProvider { |
| 26 | + override fun documentationTarget(psiFile: PsiFile, element: LookupElement, offset: Int): DocumentationTarget? { |
| 27 | + var lookupElement = element |
| 28 | + |
| 29 | + if (element is DelegatingLookupElement<*>) { |
| 30 | + lookupElement = element.getDelegate() |
| 31 | + } |
| 32 | + |
| 33 | + if (lookupElement is CommitTokenLookupElement) { |
| 34 | + return ConventionalCommitDocumentationTarget(lookupElement) |
| 35 | + } |
| 36 | + |
| 37 | + return null |
| 38 | + } |
| 39 | + |
| 40 | + private class ConventionalCommitDocumentationTarget(val element: CommitTokenLookupElement) : DocumentationTarget { |
| 41 | + override fun createPointer(): Pointer<out DocumentationTarget> = |
| 42 | + Pointer.hardPointer(this) |
| 43 | + |
| 44 | + override fun computePresentation(): TargetPresentation = |
| 45 | + TargetPresentation.builder(element.getItemText()) |
| 46 | + .icon(element.psiElement.getIcon(false)) |
| 47 | + .presentation() |
| 48 | + |
| 49 | + override fun computeDocumentation(): DocumentationResult? { |
| 50 | + val title = when (element) { |
| 51 | + is CommitTypeLookupElement -> CCBundle["cc.completion.documentation.definition.type"] |
| 52 | + is CommitScopeLookupElement -> CCBundle["cc.completion.documentation.definition.scope"] |
| 53 | + is CommitSubjectLookupElement -> CCBundle["cc.completion.documentation.definition.subject"] |
| 54 | + is CommitBodyLookupElement -> CCBundle["cc.completion.documentation.definition.body"] |
| 55 | + is CommitFooterTypeLookupElement -> CCBundle["cc.completion.documentation.definition.footerType"] |
| 56 | + is CommitFooterValueLookupElement -> CCBundle["cc.completion.documentation.definition.footerValue"] |
| 57 | + else -> return null |
| 58 | + } |
| 59 | + |
| 60 | + val html = buildHtml(element.getToken(), title) |
| 61 | + return DocumentationResult.documentation(html) |
| 62 | + } |
| 63 | + |
| 64 | + @NlsSafe |
| 65 | + private fun buildHtml(token: CommitToken, title: String): String { |
| 66 | + val description = token.getDescription().trim() |
| 67 | + val value = token.getValue().trim() |
| 68 | + val hasCustomDocumentation = description.isNotEmpty() && token.getPresentation().hasCustomDocumentation() |
| 69 | + val totalLength = value.length + description.length |
| 70 | + |
| 71 | + if (totalLength == 0) { |
| 72 | + return "" |
| 73 | + } |
| 74 | + |
| 75 | + val grayedColorHex = ColorUtil.toHex(UIUtil.getContextHelpForeground()) |
| 76 | + val sb = StringBuilder(totalLength + 200) |
| 77 | + |
| 78 | + val px2 = "${2.scaled}px" |
| 79 | + val px4 = "${4.scaled}px" |
| 80 | + |
| 81 | + // See DocumentationMarkup for the actual HTML elements used by default |
| 82 | + sb.append("<div class='content'") |
| 83 | + |
| 84 | + if (hasCustomDocumentation) { |
| 85 | + sb.append(" style='padding: 0;'>") |
| 86 | + } else { |
| 87 | + sb.append('>') |
| 88 | + sb.append("<div style='color: #$grayedColorHex; margin-bottom: $px4;'>$title</div>") |
| 89 | + } |
| 90 | + |
| 91 | + if (description.isNotEmpty()) { |
| 92 | + sb.append(description) |
| 93 | + } else { |
| 94 | + sb.append("<span style='color: #$grayedColorHex;'>") |
| 95 | + .append(CCBundle["cc.completion.documentation.noDescription"]) |
| 96 | + .append("</span>") |
| 97 | + } |
| 98 | + |
| 99 | + sb.append("</div>") |
| 100 | + |
| 101 | + if (value.isNotEmpty()) { |
| 102 | + if (hasCustomDocumentation) { |
| 103 | + // Emulate a styled <hr>, which is still unsupported in JEditorPane |
| 104 | + val colorHex = ColorUtil.toHex(getSeparatorColor()) |
| 105 | + sb.append("<div style='margin: $px4 0 $px2; font-size: 0; border-top: thin solid #$colorHex;'></div>") |
| 106 | + } |
| 107 | + |
| 108 | + sb.append("<table class='sections' style='margin-top: $px4;'>") |
| 109 | + .append("<tr>") |
| 110 | + .append("<td class='section'>") |
| 111 | + .append("<span style='color: #$grayedColorHex;'>") |
| 112 | + .append(CCBundle["cc.completion.documentation.section.value"]) |
| 113 | + .append("</span>") |
| 114 | + .append("</td>") |
| 115 | + .append("<td>") |
| 116 | + .append(value.replace("\n", "<br/>")) |
| 117 | + .append("</td>") |
| 118 | + .append("</tr>") |
| 119 | + .append("</table>") |
| 120 | + } |
| 121 | + |
| 122 | + return "$sb" |
| 123 | + } |
| 124 | + |
| 125 | + private fun getSeparatorColor(): Color { |
| 126 | + val color = UIUtil.getTooltipSeparatorColor() |
| 127 | + return if (JBColor.isBright()) { |
| 128 | + color.brighter(0.97) |
| 129 | + } else { |
| 130 | + color.darker(0.97) |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments