@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) { WriteCommandAction.runWriteCommandAction(project, () -> { if (startElement instanceof GoStatement) { startElement.replace(GoElementFactory.createReturnStatement(project)); } }); } }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (element instanceof GoForStatement) { element.replace(GoElementFactory.createSelectStatement(project)); } } }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (!(element instanceof GoImportSpec)) { return; } element.replace(GoElementFactory.createImportSpec(project, ((GoImportSpec)element).getPath(), "_")); } };
@NotNull @Override public PsiElement handleElementRename(@NotNull String newElementName) throws IncorrectOperationException { getIdentifier().replace(GoElementFactory.createIdentifierFromText(myElement.getProject(), newElementName)); return myElement; }
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; } }
@Override public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException { myElement.replace(GoElementFactory.createIdentifierFromText(myElement.getProject(), newElementName)); return myElement; }
@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())); } } }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (element == null || !element.isValid()) return; if (element instanceof GoStatement) { element.replace(GoElementFactory.createDeferStatement(project, "func() {\n" + "\tif r := recover(); r != nil {\n" + "\t\t\n" + "\t}\n" + "\t}()}")); } } }
@NotNull @Override public PsiElement setName(@NonNls @NotNull String newName) throws IncorrectOperationException { PsiElement identifier = getIdentifier(); if (identifier != null) { identifier.replace(GoElementFactory.createIdentifierFromText(getProject(), newName)); } return this; }
@Override public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException { myElement.replace(BallerinaElementFactory.createIdentifierFromText(myElement.getProject(), newElementName)); return myElement; }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (element.isValid() && element instanceof GoLabelDefinition) { PsiElement parent = element.getParent(); if (parent instanceof GoLabeledStatement) { GoStatement innerStatement = ((GoLabeledStatement)parent).getStatement(); if (innerStatement != null) { parent.replace(innerStatement); } } } } }
@Override public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException { getIdentifier().replace(GoElementFactory.createIdentifierFromText(myElement.getProject(), newElementName)); return myElement; } }
@NotNull @Override public PsiElement setName(@NonNls @NotNull String newName) throws IncorrectOperationException { PsiElement identifier = getIdentifier(); if (identifier != null) { identifier.replace(BallerinaElementFactory.createIdentifierFromText(getProject(), newName)); } return this; }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getStartElement(); if (element instanceof GoParenthesesExpr) { GoExpression innerExpression = ((GoParenthesesExpr)element).getExpression(); while (innerExpression instanceof GoParenthesesExpr) { innerExpression = ((GoParenthesesExpr)innerExpression).getExpression(); } if (innerExpression != null) { element.replace(GoElementFactory.createExpression(project, innerExpression.getText())); } } } }
@Override public void apply(@NotNull Editor editor, @NotNull SmartEnterProcessorWithFixers processor, @NotNull PsiElement element) throws IncorrectOperationException { Project project = element.getProject(); if (element instanceof GoGoStatement) { PsiElement expr = GoDeferGoInspection.GoAddParensQuickFix.addParensIfNeeded(project, ((GoGoStatement)element).getExpression()); if (expr != null) { element.replace(GoElementFactory.createGoStatement(project, expr.getText())); } } else if (element instanceof GoDeferStatement) { PsiElement expr = GoDeferGoInspection.GoAddParensQuickFix.addParensIfNeeded(project, ((GoDeferStatement)element).getExpression()); if (expr != null) { element.replace(GoElementFactory.createDeferStatement(project, expr.getText())); } } } }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (element instanceof GoAssignmentStatement) { String left = GoPsiImplUtil.joinPsiElementText(((GoAssignmentStatement)element).getLeftHandExprList().getExpressionList()); String right = GoPsiImplUtil.joinPsiElementText(((GoAssignmentStatement)element).getExpressionList()); element.replace(GoElementFactory.createExpression(project, left + " == " + right)); } else if (element instanceof GoShortVarDeclaration) { String left = GoPsiImplUtil.joinPsiElementText(((GoShortVarDeclaration)element).getVarDefinitionList()); String right = GoPsiImplUtil.joinPsiElementText(((GoShortVarDeclaration)element).getRightExpressionsList()); element.replace(GoElementFactory.createComparison(project, left + " == " + right)); } } }
public static void replaceWithAssignment(@NotNull Project project, @NotNull PsiElement element) { if (element instanceof GoShortVarDeclaration) { PsiElement parent = element.getParent(); if (parent instanceof GoSimpleStatement) { String left = GoPsiImplUtil.joinPsiElementText(((GoShortVarDeclaration)element).getVarDefinitionList()); String right = GoPsiImplUtil.joinPsiElementText(((GoShortVarDeclaration)element).getRightExpressionsList()); parent.replace(GoElementFactory.createAssignmentStatement(project, left, right)); } } else if (element instanceof GoRangeClause) { String left = GoPsiImplUtil.joinPsiElementText(((GoRangeClause)element).getVarDefinitionList()); GoExpression rangeExpression = ((GoRangeClause)element).getRangeExpression(); String right = rangeExpression != null ? rangeExpression.getText() : ""; element.replace(GoElementFactory.createRangeClauseAssignment(project, left, right)); } else if (element instanceof GoRecvStatement) { String left = GoPsiImplUtil.joinPsiElementText(((GoRecvStatement)element).getVarDefinitionList()); GoExpression recvExpression = ((GoRecvStatement)element).getRecvExpression(); String right = recvExpression != null ? recvExpression.getText() : ""; element.replace(GoElementFactory.createRecvStatementAssignment(project, left, right)); } }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (element != null && element.isValid()) { if (element instanceof GoUnaryExpr) { GoUnaryExpr unaryExpr = (GoUnaryExpr)element; GoExpression expr = unaryExpr.getExpression(); if (unaryExpr.getBitAnd() != null && expr instanceof GoCompositeLit) { GoLiteralValue literalValue = ((GoCompositeLit)expr).getLiteralValue(); if (literalValue != null) { unaryExpr.replace(literalValue); } } } else if (element instanceof GoTypeReferenceExpression) { PsiElement parent = element.getParent(); if (parent instanceof GoCompositeLit) { GoLiteralValue literalValue = ((GoCompositeLit)parent).getLiteralValue(); if (literalValue != null) { parent.replace(literalValue); } } } } } }
newOccurrences.add(occurrence.replace(GoElementFactory.createReferenceExpression(project, name)));
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement psiElement1 = descriptor.getPsiElement(); YAMLKeyValue replacement = YamlPsiElementFactory.createFromText( project, YAMLKeyValue.class, "class: " + replacementFQN ); if (replacement != null && replacement.getValue() != null) { psiElement1.replace(replacement.getValue()); } } }