@Override public void run() { // deleting old fields Stream.of(sourceJavaPsiClass.getAllFields()) .peek(psiField -> System.out.println("going to delete field: " + psiField.getText())) .forEach(psiField -> psiField.delete()); // deleting orphanage COMMAs Stream.of(sourceJavaPsiClass.getChildren()) .filter(psiElement -> psiElement instanceof PsiJavaToken) .map(psiElement -> (PsiJavaToken) psiElement) .filter(psiJavaToken -> "COMMA".equals(psiJavaToken.getTokenType().toString())) .peek(psiJavaToken -> System.out.println("going to delete token:" + psiJavaToken)) .forEach(psiElement -> psiElement.delete()); // start of additions new ValueClass(extractedVariables, sourceClass).getGeneratedPsiElements(project).forEach( psiElement -> sourceJavaPsiClass.add(psiElement) ); CodeStyleManager.getInstance(project).reformat(rootPsiFile); JavaCodeStyleManager.getInstance(project).shortenClassReferences(rootPsiFile); }
@Override public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) { List<TextRange> result = new ArrayList<TextRange>(); result.addAll(expandToWholeLine(editorText, e.getTextRange(), false)); PsiTypeCastExpression expression = (PsiTypeCastExpression)e; PsiElement[] children = expression.getChildren(); PsiElement lParen = null; PsiElement rParen = null; for (PsiElement child : children) { if (child instanceof PsiJavaToken) { PsiJavaToken token = (PsiJavaToken)child; if (token.getTokenType() == JavaTokenType.LPARENTH) lParen = token; if (token.getTokenType() == JavaTokenType.RPARENTH) rParen = token; } } if (lParen != null && rParen != null) { result.addAll(expandToWholeLine(editorText, new TextRange(lParen.getTextRange().getStartOffset(), rParen.getTextRange().getEndOffset()), false)); } return result; }
@Override public void visitBinaryExpression(@NotNull PsiBinaryExpression expression) { super.visitBinaryExpression(expression); if (!ComparisonUtils.isEqualityComparison(expression)) { return; } final PsiExpression lhs = expression.getLOperand(); if (!ExpressionUtils.hasStringType(lhs)) { return; } final PsiExpression rhs = expression.getROperand(); if (rhs == null || !ExpressionUtils.hasStringType(rhs)) { return; } if (ExpressionUtils.isNullLiteral(lhs) || ExpressionUtils.isNullLiteral(rhs)) { return; } final PsiJavaToken sign = expression.getOperationSign(); registerError(sign); }
public String getTextForElement(PsiElement element) { String operatorText = ""; String flippedOperatorText = ""; final PsiBinaryExpression expression = (PsiBinaryExpression)element; if (expression != null) { final PsiJavaToken sign = expression.getOperationSign(); operatorText = sign.getText(); flippedOperatorText = ComparisonUtils.getFlippedComparison(sign.getTokenType()); } if (operatorText.equals(flippedOperatorText)) { return IntentionPowerPackBundle.message("flip.smth.intention.name", operatorText); } else { return IntentionPowerPackBundle.message( "flip.comparison.intention.name", operatorText, flippedOperatorText); } }
public String getTextForElement(PsiElement element) { String operatorText = ""; String negatedOperatorText = ""; final PsiBinaryExpression exp = (PsiBinaryExpression)element; if (exp != null) { final PsiJavaToken sign = exp.getOperationSign(); operatorText = sign.getText(); negatedOperatorText = ComparisonUtils.getNegatedComparison(sign.getTokenType()); } if (operatorText.equals(negatedOperatorText)) { return IntentionPowerPackBundle.message( "negate.comparison.intention.name", operatorText); } else { return IntentionPowerPackBundle.message( "negate.comparison.intention.name1", operatorText, negatedOperatorText); } }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiJavaToken)) { return false; } final PsiJavaToken token = (PsiJavaToken)element; if (token.getTokenType() != JavaTokenType.IF_KEYWORD) { return false; } final PsiElement parent = element.getParent(); if (!(parent instanceof PsiIfStatement)) { return false; } final PsiIfStatement statement = (PsiIfStatement)parent; if (ErrorUtil.containsError(statement)) { return false; } return SwitchUtils.getSwitchExpression(statement, 0) != null; }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiJavaToken)) { return false; } final PsiJavaToken token = (PsiJavaToken)element; final IElementType tokenType = token.getTokenType(); if (!JavaTokenType.DO_KEYWORD.equals(tokenType)) { return false; } final PsiElement parent = element.getParent(); if (!(parent instanceof PsiDoWhileStatement)) { return false; } final PsiDoWhileStatement doWhileStatement = (PsiDoWhileStatement)parent; return !(doWhileStatement.getCondition() == null || doWhileStatement.getBody() == null); }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiJavaToken)) { return false; } final PsiJavaToken token = (PsiJavaToken)element; final IElementType tokenType = token.getTokenType(); if (!JavaTokenType.WHILE_KEYWORD.equals(tokenType)) { return false; } final PsiElement parent = element.getParent(); if (!(parent instanceof PsiWhileStatement)) { return false; } final PsiWhileStatement whileStatement = (PsiWhileStatement)parent; return !(whileStatement.getCondition() == null || whileStatement.getBody() == null); }
@Override public void processIntention(@NotNull PsiElement element) { final PsiJavaToken token = (PsiJavaToken)element; final PsiElement parent = token.getParent(); if (!(parent instanceof PsiPolyadicExpression)) { return; } final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)parent; final PsiExpression[] operands = polyadicExpression.getOperands(); final StringBuilder newExpression = new StringBuilder(); String prevOperand = null; final String tokenText = token.getText() + ' '; // 2- -1 without the space is not legal for (PsiExpression operand : operands) { final PsiJavaToken token1 = polyadicExpression.getTokenBeforeOperand(operand); if (token == token1) { newExpression.append(operand.getText()).append(tokenText); continue; } if (prevOperand != null) { newExpression.append(prevOperand).append(tokenText); } prevOperand = operand.getText(); } newExpression.append(prevOperand); PsiReplacementUtil.replaceExpression(polyadicExpression, newExpression.toString()); }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiJavaToken)) { return false; } final PsiElement parent = element.getParent(); if (!(parent instanceof PsiPolyadicExpression)) { return false; } final PsiPolyadicExpression expression = (PsiPolyadicExpression)parent; final PsiExpression[] operands = expression.getOperands(); if (operands.length < 2) { return false; } PsiExpression prevOperand = null; for (PsiExpression operand : operands) { final PsiJavaToken token = expression.getTokenBeforeOperand(operand); if (element == token) { if (prevOperand == null || operand.getText().equals(prevOperand.getText())) { return false; } break; } prevOperand = operand; } return !ComparisonUtils.isComparison(expression); }
@NotNull public PsiElementPredicate getElementPredicate() { return new PsiElementPredicate() { public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiJavaToken)) { return false; } final PsiJavaToken token = (PsiJavaToken)element; final PsiElement parent = token.getParent(); if (!(parent instanceof PsiIfStatement)) { return false; } return TrivialIfInspection.isTrivial((PsiIfStatement)parent); } }; }
@Override public void visitBinaryExpression(@NotNull PsiBinaryExpression expression) { super.visitBinaryExpression(expression); if (!ComparisonUtils.isEqualityComparison(expression)) { return; } final PsiExpression lhs = expression.getLOperand(); if (!ExpressionUtils.hasStringType(lhs)) { return; } final PsiExpression rhs = expression.getROperand(); if (rhs == null || !ExpressionUtils.hasStringType(rhs)) { return; } final String lhsText = lhs.getText(); if (PsiKeyword.NULL.equals(lhsText)) { return; } final String rhsText = rhs.getText(); if (PsiKeyword.NULL.equals(rhsText)) { return; } final PsiJavaToken sign = expression.getOperationSign(); registerError(sign); }
public String getTextForElement(PsiElement element) { String operatorText = ""; String flippedOperatorText = ""; final PsiBinaryExpression expression = (PsiBinaryExpression)element; if (expression != null) { final PsiJavaToken sign = expression.getOperationSign(); operatorText = sign.getText(); flippedOperatorText = ComparisonUtils.getFlippedComparison(sign); } if (operatorText.equals(flippedOperatorText)) { return IntentionPowerPackBundle.message("flip.smth.intention.name", operatorText); } else { return IntentionPowerPackBundle.message( "flip.comparison.intention.name", operatorText, flippedOperatorText); } }
@Override public void processIntention(@NotNull PsiElement element) { final PsiJavaToken token = (PsiJavaToken)element; final PsiElement parent = token.getParent(); if (!(parent instanceof PsiPolyadicExpression)) { return; } final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)parent; final PsiExpression[] operands = polyadicExpression.getOperands(); final StringBuilder newExpression = new StringBuilder(); String prevOperand = null; final String tokenText = token.getText() + ' '; // 2- -1 without the space is not legal for (PsiExpression operand : operands) { final PsiJavaToken token1 = polyadicExpression.getTokenBeforeOperand(operand); if (token == token1) { newExpression.append(operand.getText()).append(tokenText); continue; } if (prevOperand != null) { newExpression.append(prevOperand).append(tokenText); } prevOperand = operand.getText(); } newExpression.append(prevOperand); replaceExpression(newExpression.toString(), polyadicExpression); }
@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if(!(psiElement instanceof PsiCatchSection)) { return; } PsiCatchSection catchSection = (PsiCatchSection) psiElement; final Document doc = editor.getDocument(); PsiCodeBlock body = catchSection.getCatchBlock(); if(body != null && startLine(doc, body) == startLine(doc, catchSection)) { return; } final PsiJavaToken rParenth = catchSection.getRParenth(); if(rParenth == null) { return; } doc.insertString(rParenth.getTextRange().getEndOffset(), "{}"); }
@Nullable static PsiPolyadicExpression getSubexpression(PsiPolyadicExpression expression, PsiJavaToken token) { final PsiExpression[] operands = expression.getOperands(); if (operands.length == 2) { return expression; } for (int i = 1; i < operands.length; i++) { final PsiExpression operand = operands[i]; final PsiJavaToken currentToken = expression.getTokenBeforeOperand(operand); if (currentToken == token) { final String binaryExpressionText = operands[i - 1].getText() + ' ' + token.getText() + ' ' + operand.getText(); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(expression.getProject()); return (PsiPolyadicExpression)factory.createExpressionFromText(binaryExpressionText, expression); } } return null; }
public String getTextForElement(PsiElement element) { String operatorText = ""; String flippedOperatorText = ""; final PsiBinaryExpression expression = (PsiBinaryExpression) element; if(expression != null) { final PsiJavaToken sign = expression.getOperationSign(); operatorText = sign.getText(); flippedOperatorText = ComparisonUtils.getFlippedComparison(sign.getTokenType()); } if(operatorText.equals(flippedOperatorText)) { return IntentionPowerPackBundle.message("flip.smth.intention.name", operatorText); } else { return IntentionPowerPackBundle.message("flip.comparison.intention.name", operatorText, flippedOperatorText); } }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiJavaToken)) { return false; } final PsiJavaToken token = (PsiJavaToken)element; if (token.getTokenType() != JavaTokenType.IF_KEYWORD) { return false; } final PsiElement parent = element.getParent(); if (!(parent instanceof PsiIfStatement)) { return false; } final PsiIfStatement statement = (PsiIfStatement)parent; if (ErrorUtil.containsError(statement)) { return false; } return SwitchUtils.getSwitchExpression(statement) != null; }
private static void replaceSimplifiableAssignment( PsiIfStatement statement) throws IncorrectOperationException { final PsiExpression condition = statement.getCondition(); if (condition == null) { return; } final String conditionText = condition.getText(); final PsiStatement thenBranch = statement.getThenBranch(); final PsiExpressionStatement assignmentStatement = (PsiExpressionStatement) ControlFlowUtils.stripBraces(thenBranch); final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)assignmentStatement.getExpression(); final PsiJavaToken operator = assignmentExpression.getOperationSign(); final String operand = operator.getText(); final PsiExpression lhs = assignmentExpression.getLExpression(); final String lhsText = lhs.getText(); replaceStatement(statement, lhsText + operand + conditionText + ';'); }
private static void replaceSimplifiableAssignmentNegated( PsiIfStatement statement) throws IncorrectOperationException { final PsiExpression condition = statement.getCondition(); if (condition == null) { return; } final String conditionText = BoolUtils.getNegatedExpressionText(condition); final PsiStatement thenBranch = statement.getThenBranch(); final PsiExpressionStatement assignmentStatement = (PsiExpressionStatement) ControlFlowUtils.stripBraces(thenBranch); final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) assignmentStatement.getExpression(); final PsiJavaToken operator = assignmentExpression.getOperationSign(); final String operand = operator.getText(); final PsiExpression lhs = assignmentExpression.getLExpression(); final String lhsText = lhs.getText(); replaceStatement(statement, lhsText + operand + conditionText + ';'); }
public final boolean isMatch(RangeEntry entry) // final int modifiers, final String name, final String returnType) { return entry.getEnd().getParent() instanceof PsiClass && entry.getEnd() instanceof PsiJavaToken && // ((PsiJavaToken)entry.getEnd()).getTokenType() == PsiJavaToken.LBRACE && entry.getEnd().getText().equals("{" ) && super.isMatch (entry ) && abAttr.isMatch (entry.getModifiers()) && enumAttr.isMatch (entry.getModifiers()); }
public final boolean isMatch(RangeEntry entry) { // return entry.getEnd() instanceof PsiClass && // entry.getEnd() should be the LBrace of a class. return entry.getEnd().getParent() instanceof PsiClass && ((entry.getModifiers() & ModifierConstants.ENUM ) != 0) || (entry.getEnd() instanceof PsiJavaToken && // ((PsiJavaToken)entry.getEnd()).getTokenType() == PsiJavaToken.LBRACE && entry.getEnd().getText().equals("{" ) ) && super.isMatch (entry ) && abAttr.isMatch (entry.getModifiers()) && enumAttr.isMatch(entry.getModifiers()); }
@Override public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) { if (ServiceManager.getService(project, CamelService.class).isCamelPresent()) { // special for xml XmlTag xml = PsiTreeUtil.getParentOfType(element, XmlTag.class); if (xml != null) { // special check for poll enrich where we add the endpoint on a child node (camel expression) XmlTag parent = xml.getParentTag(); if (parent != null && parent.getLocalName().equals("pollEnrich")) { return true; } } String text = null; // special for java token if (element instanceof PsiJavaToken) { // if its a string literal PsiJavaToken token = (PsiJavaToken) element; if (token.getTokenType() == JavaTokenType.STRING_LITERAL) { text = getIdeaUtils().getInnerText(token.getText()); } } else { // should be a literal element and therefore dont fallback to generic text = getIdeaUtils().extractTextFromElement(element, false, false, true); } return text != null && text.trim().isEmpty(); } return false; }
@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if (psiElement instanceof PsiJavaToken) { if (((PsiJavaToken) psiElement).getTokenType() == JavaTokenType.STRING_LITERAL && !StringUtil.endsWithChar(psiElement.getText(), '\"')) { editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), "\""); } else if (((PsiJavaToken) psiElement).getTokenType() == JavaTokenType.CHARACTER_LITERAL && !StringUtil.endsWithChar(psiElement.getText(), '\'')) { editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), "\'"); } } }
@Override public boolean canSelect(PsiElement e) { if (e instanceof PsiKeyword) { return true; } if (e instanceof PsiJavaToken) { IElementType tokenType = ((PsiJavaToken)e).getTokenType(); return tokenType == JavaTokenType.IDENTIFIER || tokenType == JavaTokenType.STRING_LITERAL; } return false; }
@Override public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) { List<TextRange> ranges = super.select(e, editorText, cursorOffset, editor); if (e instanceof PsiJavaToken && ((PsiJavaToken)e).getTokenType() == JavaTokenType.STRING_LITERAL) { killRangesBreakingEscapes(e, ranges, e.getTextRange()); } return ranges; }
@Override public String getTextForElement(PsiElement element) { final PsiPolyadicExpression expression = (PsiPolyadicExpression)element.getParent(); final PsiExpression[] operands = expression.getOperands(); final PsiJavaToken sign = expression.getTokenBeforeOperand(operands[1]); final String operatorText = sign == null ? "" : sign.getText(); final IElementType tokenType = expression.getOperationTokenType(); final boolean commutative = ParenthesesUtils.isCommutativeOperator(tokenType); if (commutative && !ExpressionUtils.isConcatenation(expression)) { return IntentionPowerPackBundle.message("flip.smth.intention.name", operatorText); } else { return IntentionPowerPackBundle.message("flip.smth.intention.name1", operatorText); } }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiJavaToken)) { return false; } final PsiJavaToken token = (PsiJavaToken)element; final IElementType tokenType = token.getTokenType(); if (!JavaTokenType.FOR_KEYWORD.equals(tokenType)) { return false; } final PsiElement parent = element.getParent(); return parent instanceof PsiForStatement && !ErrorUtil.containsError(parent); }
@Override public String getTextForElement(PsiElement element) { final PsiJavaToken sign; if (element instanceof PsiPostfixExpression) { sign = ((PsiPostfixExpression)element).getOperationSign(); } else { sign = ((PsiPrefixExpression)element).getOperationSign(); } final String operator = sign.getText(); return IntentionPowerPackBundle.message( "extract.increment.intention.name", operator); }