|
| 1 | +/* |
| 2 | + * Copyright Doma Tools Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.domaframework.doma.intellij.document |
| 17 | + |
| 18 | +import com.intellij.lang.documentation.AbstractDocumentationProvider |
| 19 | +import com.intellij.psi.PsiClassType |
| 20 | +import com.intellij.psi.PsiElement |
| 21 | +import org.domaframework.doma.intellij.common.dao.findDaoMethod |
| 22 | +import org.domaframework.doma.intellij.common.psi.PsiParentClass |
| 23 | +import org.domaframework.doma.intellij.common.sql.PsiClassTypeUtil |
| 24 | +import org.domaframework.doma.intellij.common.sql.foritem.ForItem |
| 25 | +import org.domaframework.doma.intellij.common.sql.validator.SqlElForItemFieldAccessorChildElementValidator |
| 26 | +import org.domaframework.doma.intellij.common.sql.validator.result.ValidationCompleteResult |
| 27 | +import org.domaframework.doma.intellij.common.sql.validator.result.ValidationResult |
| 28 | +import org.domaframework.doma.intellij.extension.psi.getForItem |
| 29 | +import org.domaframework.doma.intellij.extension.psi.getForItemDeclaration |
| 30 | +import org.domaframework.doma.intellij.inspection.ForDirectiveInspection |
| 31 | +import org.domaframework.doma.intellij.psi.SqlElForDirective |
| 32 | +import org.domaframework.doma.intellij.psi.SqlElIdExpr |
| 33 | +import org.domaframework.doma.intellij.psi.SqlTypes |
| 34 | +import org.toml.lang.psi.ext.elementType |
| 35 | +import java.util.LinkedList |
| 36 | + |
| 37 | +class ForItemElementDocumentationProvider : AbstractDocumentationProvider() { |
| 38 | + override fun generateDoc( |
| 39 | + element: PsiElement?, |
| 40 | + originalElement: PsiElement?, |
| 41 | + ): String? { |
| 42 | + val result: MutableList<String?> = LinkedList<String?>() |
| 43 | + if (originalElement !is SqlElIdExpr && originalElement?.elementType != SqlTypes.EL_IDENTIFIER) { |
| 44 | + return super.generateDoc( |
| 45 | + element, |
| 46 | + originalElement, |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + val file = originalElement.containingFile |
| 51 | + val daoMethod = findDaoMethod(file) ?: return "" |
| 52 | + val forDirectiveInspection = ForDirectiveInspection(daoMethod, "") |
| 53 | + |
| 54 | + val currentForItem = ForItem(originalElement) |
| 55 | + val forDirectiveExpr = currentForItem.getParentForDirectiveExpr() |
| 56 | + if (forDirectiveExpr != null) { |
| 57 | + val declarationClassType = |
| 58 | + forDirectiveInspection.validateFieldAccessByForItem( |
| 59 | + listOf(originalElement), |
| 60 | + skipSelf = false, |
| 61 | + ) |
| 62 | + if (declarationClassType != null) { |
| 63 | + generateDocumentInForDirective( |
| 64 | + forDirectiveInspection, |
| 65 | + declarationClassType, |
| 66 | + forDirectiveExpr, |
| 67 | + originalElement, |
| 68 | + result, |
| 69 | + ) |
| 70 | + } |
| 71 | + } else { |
| 72 | + generateDocumentInBindVariable(forDirectiveInspection, originalElement, result) |
| 73 | + } |
| 74 | + return result.joinToString("\n") |
| 75 | + } |
| 76 | + |
| 77 | + private fun generateDocumentInForItem( |
| 78 | + originalElement: PsiElement, |
| 79 | + declarationChildren: List<SqlElIdExpr>, |
| 80 | + declarationClassType: PsiParentClass, |
| 81 | + result: MutableList<String?>, |
| 82 | + ) { |
| 83 | + val parentClass = declarationClassType |
| 84 | + val forItemValidator = |
| 85 | + SqlElForItemFieldAccessorChildElementValidator( |
| 86 | + declarationChildren, |
| 87 | + parentClass, |
| 88 | + ) |
| 89 | + forItemValidator.validateChildren(complete = { lastType -> |
| 90 | + var resultParent = lastType |
| 91 | + var classType: PsiClassType? = resultParent.type as? PsiClassType |
| 92 | + if (classType != null && |
| 93 | + PsiClassTypeUtil.isIterableType(classType, originalElement.project) |
| 94 | + ) { |
| 95 | + classType = classType.parameters.firstOrNull() as? PsiClassType |
| 96 | + } |
| 97 | + if (classType != null) { |
| 98 | + resultParent = PsiParentClass(classType) |
| 99 | + result.add("${generateTypeLink(resultParent)} ${originalElement.text}") |
| 100 | + } |
| 101 | + }) |
| 102 | + } |
| 103 | + |
| 104 | + private fun generateDocumentInBindVariable( |
| 105 | + forDirectiveInspection: ForDirectiveInspection, |
| 106 | + originalElement: PsiElement, |
| 107 | + result: MutableList<String?>, |
| 108 | + ) { |
| 109 | + val declarationClassType = |
| 110 | + forDirectiveInspection.validateFieldAccessByForItem(listOf(originalElement)) |
| 111 | + if (declarationClassType != null) { |
| 112 | + val parentClass = declarationClassType.parentClass |
| 113 | + result.add("${generateTypeLink(parentClass)} ${originalElement.text}") |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private fun generateDocumentInForDirective( |
| 118 | + forDirectiveInspection: ForDirectiveInspection, |
| 119 | + declarationClassType: ValidationResult, |
| 120 | + forDirectiveExpr: SqlElForDirective, |
| 121 | + originalElement: PsiElement, |
| 122 | + result: MutableList<String?>, |
| 123 | + ) { |
| 124 | + val parentClass = declarationClassType.parentClass |
| 125 | + val parentType = parentClass?.type as? PsiClassType |
| 126 | + |
| 127 | + if (forDirectiveExpr.getForItem()?.textOffset != originalElement.textOffset) { |
| 128 | + generateDocumentForItemSelf(parentType, originalElement, result) |
| 129 | + } else { |
| 130 | + val declarationSide = forDirectiveExpr.getForItemDeclaration() ?: return |
| 131 | + val children = declarationSide.getDeclarationChildren() |
| 132 | + |
| 133 | + val declarationClassType = |
| 134 | + forDirectiveInspection.validateFieldAccessByForItem(listOf(originalElement), false) |
| 135 | + if (declarationClassType is ValidationCompleteResult) { |
| 136 | + generateDocumentInForItem( |
| 137 | + originalElement, |
| 138 | + children, |
| 139 | + declarationClassType.parentClass, |
| 140 | + result, |
| 141 | + ) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private fun generateDocumentForItemSelf( |
| 147 | + parentType: PsiClassType?, |
| 148 | + originalElement: PsiElement, |
| 149 | + result: MutableList<String?>, |
| 150 | + ) { |
| 151 | + if (parentType != null) { |
| 152 | + val forItemParentClassType = PsiParentClass(parentType) |
| 153 | + result.add("${generateTypeLink(forItemParentClassType)} ${originalElement.text}") |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + override fun generateHoverDoc( |
| 158 | + element: PsiElement, |
| 159 | + originalElement: PsiElement?, |
| 160 | + ): String? = generateDoc(element, originalElement) |
| 161 | + |
| 162 | + override fun getQuickNavigateInfo( |
| 163 | + element: PsiElement, |
| 164 | + originalElement: PsiElement?, |
| 165 | + ): String? { |
| 166 | + val result: MutableList<String?> = LinkedList<String?>() |
| 167 | + val typeDocument = generateDoc(element, originalElement) |
| 168 | + result.add(typeDocument) |
| 169 | + return result.joinToString("\n") |
| 170 | + } |
| 171 | + |
| 172 | + private fun generateTypeLink(parentClass: PsiParentClass?): String { |
| 173 | + if (parentClass?.type != null) { |
| 174 | + return generateTypeLinkFromCanonicalText(parentClass.type.canonicalText) |
| 175 | + } |
| 176 | + return "" |
| 177 | + } |
| 178 | + |
| 179 | + private fun generateTypeLinkFromCanonicalText(canonicalText: String): String { |
| 180 | + val regex = Regex("([a-zA-Z0-9_]+\\.)*([a-zA-Z0-9_]+)") |
| 181 | + val result = StringBuilder() |
| 182 | + var lastIndex = 0 |
| 183 | + |
| 184 | + for (match in regex.findAll(canonicalText)) { |
| 185 | + val fullMatch = match.value |
| 186 | + val typeName = match.groups[2]?.value ?: fullMatch |
| 187 | + val startIndex = match.range.first |
| 188 | + val endIndex = match.range.last + 1 |
| 189 | + |
| 190 | + if (lastIndex < startIndex) { |
| 191 | + result.append(canonicalText.substring(lastIndex, startIndex)) |
| 192 | + } |
| 193 | + result.append("<a href=\"psi_element://$fullMatch\">$typeName</a>") |
| 194 | + lastIndex = endIndex |
| 195 | + } |
| 196 | + |
| 197 | + if (lastIndex < canonicalText.length) { |
| 198 | + result.append(canonicalText.substring(lastIndex)) |
| 199 | + } |
| 200 | + |
| 201 | + return result.toString() |
| 202 | + } |
| 203 | +} |
0 commit comments