|
4 | 4 | import com.github.maiqingqiang.goormhelper.orm.ORMCompletionProvider;
|
5 | 5 | import com.github.maiqingqiang.goormhelper.orm.goframe.GoFrameTypes;
|
6 | 6 | import com.github.maiqingqiang.goormhelper.ui.Icons;
|
| 7 | +import com.github.maiqingqiang.goormhelper.utils.Strings; |
| 8 | +import com.goide.documentation.GoDocumentationProvider; |
7 | 9 | import com.goide.inspections.core.GoCallableDescriptor;
|
8 | 10 | import com.goide.inspections.core.GoCallableDescriptorSet;
|
9 | 11 | import com.goide.psi.*;
|
10 | 12 | import com.goide.psi.impl.GoPsiUtil;
|
| 13 | +import com.google.common.base.CaseFormat; |
| 14 | +import com.intellij.codeInsight.completion.CompletionParameters; |
| 15 | +import com.intellij.codeInsight.completion.CompletionResultSet; |
| 16 | +import com.intellij.psi.PsiReference; |
11 | 17 | import com.intellij.psi.ResolveState;
|
12 | 18 | import com.intellij.util.ObjectUtils;
|
13 | 19 | import org.jetbrains.annotations.Contract;
|
14 | 20 | import org.jetbrains.annotations.NotNull;
|
15 | 21 | import org.jetbrains.annotations.Nullable;
|
16 | 22 |
|
17 | 23 | import javax.swing.*;
|
18 |
| -import java.util.List; |
19 |
| -import java.util.Map; |
20 |
| -import java.util.Objects; |
21 |
| -import java.util.Set; |
| 24 | +import java.util.*; |
22 | 25 |
|
23 | 26 | public class GoFrameColumnCompletionProvider extends ORMCompletionProvider {
|
24 | 27 |
|
@@ -147,12 +150,97 @@ protected GoCompositeElement customFindGoCompositeElementByGoCallExpr(GoCallExpr
|
147 | 150 | }
|
148 | 151 |
|
149 | 152 | @Override
|
150 |
| - protected GoCompositeElement findAgainArgument(GoCompositeElement argument) { |
| 153 | + protected GoCompositeElement findAgainArgument(GoCompositeElement argument, @NotNull CompletionParameters parameters, GoCallableDescriptor descriptor, @NotNull CompletionResultSet result) { |
151 | 154 | if (argument instanceof GoCallExpr goCallExpr) {
|
152 | 155 | GoType newArgument = findGoTypeByReceiver(goCallExpr, Set.of(GoTypeSpecDescriptor.of("builtin.string")));
|
153 |
| - if (newArgument != null) return newArgument; |
| 156 | + if (newArgument != null && newArgument.resolve(ResolveState.initial()) instanceof GoTypeSpec goTypeSpec) { |
| 157 | + |
| 158 | + HashMap<String, String> columnMap = new HashMap<>(); |
| 159 | + |
| 160 | + for (PsiReference search : GoReferencesSearch.search(goTypeSpec)) { |
| 161 | + if (search.getElement().getParent() instanceof GoCompositeLit goCompositeLit) { |
| 162 | + GoLiteralValue goLiteralValue = goCompositeLit.getLiteralValue(); |
| 163 | + if (goLiteralValue != null) { |
| 164 | + for (GoElement goElement : goLiteralValue.getElementList()) { |
| 165 | + |
| 166 | + if (goElement.getKey() != null && goElement.getKey().getFieldName() != null && goElement.getValue() != null && goElement.getValue().getExpression() instanceof GoStringLiteral goStringLiteral) { |
| 167 | + columnMap.put(goElement.getKey().getFieldName().getIdentifier().getText(), goStringLiteral.getDecodedText()); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + scanFieldsByGoFrame(parameters, descriptor, result, goTypeSpec, columnMap); |
| 175 | + |
| 176 | + return null; |
| 177 | + } |
154 | 178 | }
|
155 | 179 | return argument;
|
156 | 180 | }
|
157 | 181 |
|
| 182 | + |
| 183 | + private void scanFieldsByGoFrame(@NotNull CompletionParameters parameters, GoCallableDescriptor descriptor, @NotNull CompletionResultSet result, @NotNull GoTypeSpec goTypeSpec, HashMap<String, String> columnMap) { |
| 184 | + if (goTypeSpec.getSpecType().getType() instanceof GoStructType goStructType) { |
| 185 | + for (GoFieldDeclaration field : goStructType.getFieldDeclarationList()) { |
| 186 | + |
| 187 | + if (!checkGoFieldDeclaration(field)) continue; |
| 188 | + |
| 189 | + String name = field.getFieldDefinitionList().get(0).getName(); |
| 190 | + |
| 191 | + String column = columnMap.get(name); |
| 192 | + String comment = getComment(field); |
| 193 | + String type = ""; |
| 194 | + |
| 195 | + if (field.getType() != null) { |
| 196 | + type = field.getType().getPresentationText(); |
| 197 | + } |
| 198 | + |
| 199 | + if (column != null && column.isEmpty()) { |
| 200 | + column = getColumn(field); |
| 201 | + |
| 202 | + if (column != null && column.isEmpty()) { |
| 203 | + if (field.getFieldDefinitionList().isEmpty() && field.getAnonymousFieldDefinition() != null) { |
| 204 | + GoType goType = field.getAnonymousFieldDefinition().getGoType(ResolveState.initial()); |
| 205 | + if (goType == null) continue; |
| 206 | + |
| 207 | + GoTypeSpec spec = (GoTypeSpec) goType.resolve(ResolveState.initial()); |
| 208 | + if (spec == null) continue; |
| 209 | + |
| 210 | + scanFieldsByGoFrame(parameters, descriptor, result, spec, columnMap); |
| 211 | + continue; |
| 212 | + } |
| 213 | + |
| 214 | + if (name != null) { |
| 215 | + column = Strings.replaceCommonInitialisms(name); |
| 216 | + column = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, column); |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + |
| 222 | + if (comment != null && comment.isEmpty()) { |
| 223 | + comment = GoDocumentationProvider.getCommentText(GoDocumentationProvider.getCommentsForElement(field), false); |
| 224 | + } |
| 225 | + |
| 226 | + if (column != null && !column.contains(result.getPrefixMatcher().getPrefix())) continue; |
| 227 | + |
| 228 | + addElement(result, column, comment, type, goTypeSpec); |
| 229 | + |
| 230 | + if (!(parameters.getPosition().getParent().getParent() instanceof GoKey)) { |
| 231 | + Map<GoCallableDescriptor, List<String>> queryExpr = queryExpr(); |
| 232 | + if (queryExpr != null) { |
| 233 | + List<String> whereExpr = queryExpr.get(descriptor); |
| 234 | + if (whereExpr != null) { |
| 235 | + for (String s : whereExpr) { |
| 236 | + addElement(result, String.format(s, column), comment, type, goTypeSpec); |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + |
158 | 246 | }
|
0 commit comments