@Nullable private static GoReferenceExpression unwrapParensAndCast(@Nullable PsiElement e) { while (e instanceof GoParenthesesExpr) { e = ((GoParenthesesExpr)e).getExpression(); } return ObjectUtils.tryCast(e, GoReferenceExpression.class); }
@Nullable protected GoExpression getExpression(@NotNull PsiElement[] elements) { return ObjectUtils.tryCast(ArrayUtil.getFirstElement(elements), GoExpression.class); } }
@Nullable public static GoSignatureOwner resolveCall(@Nullable GoExpression call) { return ObjectUtils.tryCast(resolveCallRaw(call), GoSignatureOwner.class); }
/** * Sets the insert/replace state of the editor. */ private void setInsertEditorState(@NotNull Editor editor, boolean value) { final EditorEx editorEx = ObjectUtils.tryCast(editor, EditorEx.class); if (editorEx == null) return; editorEx.setInsertMode(value); }
@Contract("null,_->null") private static <T> T parent(@Nullable PsiElement of, @NotNull Class<T> parentClass) { return ObjectUtils.tryCast(of != null ? of.getParent() : null, parentClass); } }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { GoFunctionDeclaration function = ObjectUtils.tryCast(startElement, GoFunctionDeclaration.class); GoSignature signature = function != null ? function.getSignature() : null; if (signature == null) return; signature.replace(GoElementFactory.createFunctionSignatureFromText(project, "")); } }
private static boolean hasStructTypeWithField(@NotNull GoVarDefinition structVarDefinition, @NotNull GoNamedElement field) { GoType type = structVarDefinition.getGoType(null); GoStructType structType = type != null ? ObjectUtils.tryCast(type.getUnderlyingType(), GoStructType.class) : null; return structType != null && PsiTreeUtil.isAncestor(structType, field, true); }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { GoImportSpec element = ObjectUtils.tryCast(descriptor.getPsiElement(), GoImportSpec.class); if (element != null) { ElementManipulators.handleContentChange(element.getImportString(), myNewImportPath); } } }
@NotNull @Override public final PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) { GoFile file = ObjectUtils.tryCast(session.getFile(), GoFile.class); return file != null && GoPsiImplUtil.allowed(file, null, ModuleUtilCore.findModuleForPsiElement(file)) ? buildGoVisitor(holder, session) : DUMMY_VISITOR; }
public static boolean isConversionExpression(@Nullable GoExpression expression) { if (expression instanceof GoConversionExpr) { return true; } GoReferenceExpression referenceExpression = null; if (expression instanceof GoCallExpr) { referenceExpression = ObjectUtils.tryCast(((GoCallExpr)expression).getExpression(), GoReferenceExpression.class); } else if (expression instanceof GoBuiltinCallExpr) { referenceExpression = ((GoBuiltinCallExpr)expression).getReferenceExpression(); } return referenceExpression != null && referenceExpression.resolve() instanceof GoTypeSpec; }
@NotNull private DlvCommandProcessor getProcessor() { return assertNotNull(tryCast(getVm(), DlvVm.class)).getCommandProcessor(); }
@Nullable public static GoTypeSpec getTypeSpecSafe(@NotNull GoType type) { GoTypeStub stub = type.getStub(); PsiElement parent = stub == null ? type.getParent() : stub.getParentStub().getPsi(); return ObjectUtils.tryCast(parent, GoTypeSpec.class); }
@Nullable private static GoCompositeLit getStructLiteral(@NotNull GoReferenceExpression fieldReferenceExpression, @NotNull GoSimpleStatement structDeclaration) { GoShortVarDeclaration varDeclaration = structDeclaration.getShortVarDeclaration(); if (varDeclaration == null) return null; PsiElement resolve = resolveQualifier(fieldReferenceExpression); GoVarDefinition structVarDefinition = find(varDeclaration.getVarDefinitionList(), definition -> resolve == definition); return structVarDefinition != null ? ObjectUtils.tryCast(structVarDefinition.getValue(), GoCompositeLit.class) : null; }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { WriteCommandAction.runWriteCommandAction(project, () -> { PsiElement element = ObjectUtils.tryCast(descriptor.getStartElement(), myClazz); if (element != null && (myElementType == null || element.getNode().getElementType() == myElementType)) { element.delete(); } }); } }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getStartElement(); if (element == null) return; GoFunctionDeclaration function = ObjectUtils.tryCast(element.getParent(), GoFunctionDeclaration.class); GoSignature signature = function != null ? function.getSignature() : null; if (signature == null) return; GoFile file = ObjectUtils.tryCast(signature.getContainingFile(), GoFile.class); if (file == null) return; String testingQualifier = GotestGenerateAction.importTestingPackageIfNeeded(file); signature.replace(GoElementFactory.createFunctionSignatureFromText(project, myType.getSignature(testingQualifier))); } }
@Nullable private static GoCompositeLit getStructLiteral(@NotNull GoReferenceExpression fieldReferenceExpression, @NotNull GoAssignmentStatement structAssignment) { GoVarDefinition varDefinition = ObjectUtils.tryCast(resolveQualifier(fieldReferenceExpression), GoVarDefinition.class); PsiElement field = fieldReferenceExpression.resolve(); if (varDefinition == null || !isFieldDefinition(field) || !hasStructTypeWithField(varDefinition, (GoNamedElement)field)) { return null; } GoExpression structReferenceExpression = find(structAssignment.getLeftHandExprList().getExpressionList(), expression -> isResolvedTo(expression, varDefinition)); if (structReferenceExpression == null) return null; GoExpression compositeLit = GoPsiImplUtil.getRightExpression(structAssignment, structReferenceExpression); return ObjectUtils.tryCast(compositeLit, GoCompositeLit.class); }
/** * Processes the user pressing the Insert key while in INSERT or REPLACE mode. This simply toggles the * Insert/Overwrite state which updates the status bar. * * @param editor The editor to toggle the state in */ public void processInsert(Editor editor) { final EditorEx editorEx = ObjectUtils.tryCast(editor, EditorEx.class); if (editorEx == null) return; editorEx.setInsertMode(!editorEx.isInsertMode()); CommandState.getInstance(editor).toggleInsertOverwrite(); }
@Override protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException { PsiElement parent = element.getParent(); GoCallExpr call = parent != null ? ObjectUtils.tryCast(parent.getParent(), GoCallExpr.class) : null; if (call != null) { context.extractElement(element, call); context.delete(call); } }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { GoImportSpec element = ObjectUtils.tryCast(descriptor.getPsiElement(), GoImportSpec.class); if (element != null) { WriteCommandAction.runWriteCommandAction(project, () -> { PsiElement dot = element.getDot(); if (dot != null) { dot.delete(); return; } PsiElement identifier = element.getIdentifier(); if (identifier != null) { identifier.delete(); } }); } } }
@Override public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException { PsiElement parent = element.getParent(); if (parent instanceof GoFunctionOrMethodDeclaration) { GoBlock block = ((GoFunctionOrMethodDeclaration)parent).getBlock(); if (block == null) { GoBlock newBlock = ObjectUtils.tryCast(parent.add(GoElementFactory.createBlock(project)), GoBlock.class); if (newBlock != null) { PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument()); new GoSmartEnterProcessor.PlainEnterProcessor().doEnter(newBlock, newBlock.getContainingFile(), editor, false); } } } } }