@Override public String getDescription(PsiElement e) { String text = e.getText(); if (text.length() > 20) text = text.substring(0, 17) + "..."; return "Unwrap " + text; }
@Nullable public static String getName(@NotNull GoAnonymousFieldDefinition o) { PsiElement identifier = o.getIdentifier(); return identifier != null ? identifier.getText() : null; }
private static boolean isSuppressedInStatement(@NotNull String toolId, @Nullable PsiElement statement) { if (statement != null) { PsiElement prev = PsiTreeUtil.skipSiblingsBackward(statement, PsiWhiteSpace.class); if (prev instanceof PsiComment) { String text = prev.getText(); Matcher matcher = SuppressionUtil.SUPPRESS_IN_LINE_COMMENT_PATTERN.matcher(text); return matcher.matches() && SuppressionUtil.isInspectionToolIdMentioned(matcher.group(1), toolId); } } return false; } }
@Nullable public static String getName(@NotNull GoPackageClause packageClause) { GoPackageClauseStub stub = packageClause.getStub(); if (stub != null) return stub.getName(); PsiElement packageIdentifier = packageClause.getIdentifier(); return packageIdentifier != null ? packageIdentifier.getText().trim() : null; }
public static PsiElement addParensIfNeeded(@NotNull Project project, @Nullable PsiElement element) { if (element instanceof GoExpression && !(element instanceof GoCallExpr || element instanceof GoBuiltinCallExpr)) { if (((GoExpression)element).getGoType(null) instanceof GoFunctionType) { return element.replace(GoElementFactory.createExpression(project, element.getText() + "()")); } } return null; } }
@Nullable public static String getName(@NotNull GoMethodSpec o) { GoNamedStub<GoMethodSpec> stub = o.getStub(); if (stub != null) { return stub.getName(); } PsiElement identifier = o.getIdentifier(); if (identifier != null) return identifier.getText(); GoTypeReferenceExpression typeRef = o.getTypeReferenceExpression(); return typeRef != null ? typeRef.getIdentifier().getText() : null; }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement startElement = descriptor.getStartElement(); if (startElement instanceof GoElement) { startElement.replace(GoElementFactory.createLiteralValueElement(project, myStructField, startElement.getText())); } } }
@Nullable public static String calcTypeText(@NotNull GoMethodDeclaration psi) { GoTypeReferenceExpression reference = GoPsiImplUtil.getTypeReference(psi.getReceiverType()); return reference != null ? reference.getIdentifier().getText() : null; } }
@Nullable private static String getFieldName(@NotNull GoFieldDeclaration declaration) { List<GoFieldDefinition> list = declaration.getFieldDefinitionList(); GoFieldDefinition fieldDefinition = ContainerUtil.getFirstItem(list); return fieldDefinition != null ? fieldDefinition.getIdentifier().getText() : null; }
@Nullable @Override public String getName() { T stub = getStub(); if (stub != null) { return stub.getName(); } PsiElement identifier = getIdentifier(); return identifier != null ? identifier.getText() : null; }
@NotNull static Set<String> alreadyAssignedFields(@Nullable GoLiteralValue literal) { if (literal == null) { return Collections.emptySet(); } return ContainerUtil.map2SetNotNull(literal.getElementList(), element -> { GoKey key = element.getKey(); GoFieldName fieldName = key != null ? key.getFieldName() : null; PsiElement identifier = fieldName != null ? fieldName.getIdentifier() : null; return identifier != null ? identifier.getText() : null; }); }
@NotNull public static LookupElement createVariableLookupElement(@NotNull PsiElement element, @Nullable String type) { LookupElementBuilder builder = LookupElementBuilder.createWithSmartPointer(element.getText(), element) .withIcon(BallerinaIcons.VARIABLE); if (type == null || type.isEmpty()) { builder = builder.withTypeText("Variable"); } else { builder = builder.withTypeText(type); } return PrioritizedLookupElement.withPriority(builder, VARIABLE_PRIORITY); }
public static String getAlias(@NotNull GoImportSpec importSpec) { GoImportSpecStub stub = importSpec.getStub(); if (stub != null) { return stub.getAlias(); } PsiElement identifier = importSpec.getIdentifier(); if (identifier != null) { return identifier.getText(); } return importSpec.isDot() ? "." : null; }
@NotNull public static LookupElement createWorkerLookupElement(@NotNull PsiElement workerName) { LookupElementBuilder builder = LookupElementBuilder.createWithSmartPointer(workerName.getText(), workerName) .withTypeText("Worker").withIcon(BallerinaIcons.WORKER); return PrioritizedLookupElement.withPriority(builder, VARIABLE_PRIORITY); }
@NotNull public static LookupElement createServiceLookupElement(@NotNull PsiElement element) { LookupElementBuilder builder = LookupElementBuilder.createWithSmartPointer(element.getText(), element) .withTypeText("Service").withIcon(BallerinaIcons.SERVICE); return PrioritizedLookupElement.withPriority(builder, VARIABLE_PRIORITY); }
@NotNull public static LookupElement createTypeLookupElement(@NotNull BallerinaTopLevelDefinition definition, @Nullable InsertHandler<LookupElement> insertHandler) { LookupElementBuilder builder = LookupElementBuilder.createWithSmartPointer(definition.getIdentifier() .getText(), definition).withInsertHandler(insertHandler).withTypeText("Type") .withIcon(definition.getIcon(Iconable.ICON_FLAG_VISIBILITY)).bold(); return PrioritizedLookupElement.withPriority(builder, TYPE_PRIORITY); }
private static void moveFieldReferenceExpressions(@NotNull Data data) { GoLiteralValue literalValue = data.getCompositeLit().getLiteralValue(); if (literalValue == null) return; for (GoReferenceExpression expression : data.getReferenceExpressions()) { GoExpression anchor = getTopmostExpression(expression); GoExpression fieldValue = GoPsiImplUtil.getRightExpression(data.getAssignment(), anchor); if (fieldValue == null) continue; GoPsiImplUtil.deleteExpressionFromAssignment(data.getAssignment(), anchor); addFieldDefinition(literalValue, expression.getIdentifier().getText(), fieldValue.getText()); } }
private void doTopLevelTest(@NotNull String text, @NotNull String expectedTypeText) { myFixture.configureByText("a.go", "package a;" + text); PsiElement elementAt = findElementAtCaretOrInSelection(); GoTypeOwner typeOwner = PsiTreeUtil.getNonStrictParentOfType(elementAt, GoTypeOwner.class); assertNotNull("Cannot find type owner. Context element: " + elementAt.getText(), typeOwner); GoType type = typeOwner.getGoType(null); assertEquals(expectedTypeText, type == null ? "<unknown>" : type.getText()); }
private void doTopLevelTest(@NotNull String text, @NotNull String expectedTypeText) { myFixture.configureByText("a.go", "package a;" + text); PsiElement elementAt = findElementAtCaretOrInSelection(); GoExpression typeOwner = PsiTreeUtil.getNonStrictParentOfType(elementAt, GoExpression.class); assertNotNull("Cannot find type owner. Context element: " + elementAt.getText(), typeOwner); assertEquals(expectedTypeText, StringUtil.join(GoTypeUtil.getExpectedTypes(typeOwner), PsiElement::getText, "; ")); }
private void doResolveTest() { if (myReference == null) fail("no reference defined in test case"); if (myShouldBeResolved && !allowNullDefinition() && myDefinition == null) fail("no definition defined in test case"); PsiElement resolve = myReference.resolve(); if (myShouldBeResolved) { assertNotNull("cannot resolve reference " + myReference.getCanonicalText(), resolve); if (myDefinition != null) { PsiElement def = PsiTreeUtil.getParentOfType(myDefinition, resolve.getClass(), false); assertSame("element resolved in non-expected element from " + getFileName(resolve) + ":\n" + resolve.getText(), def, resolve); } } else if (resolve != null) { fail("element is resolved but it wasn't should. resolved to element from " + getFileName(resolve) + ":\n" + resolve.getText()); } }