private PsiType processForeach( PsiElement parentDeclarationScope ) { PsiType result = null; if( parentDeclarationScope instanceof PsiForeachStatement ) { final PsiForeachStatement foreachStatement = (PsiForeachStatement)parentDeclarationScope; final PsiExpression iteratedValue = foreachStatement.getIteratedValue(); if( iteratedValue != null ) { result = JavaGenericsUtil.getCollectionItemType( iteratedValue ); } } return result; }
@Override public void visitParameter(@NotNull PsiParameter variable) { final PsiElement scope = variable.getDeclarationScope(); if (scope instanceof PsiCatchSection || scope instanceof PsiForeachStatement) { return; } final String name = variable.getName(); if (name == null || isValid(name)) { return; } registerVariableError(variable, name); }
public void verifyParameter(@NotNull final PsiParameter psiParameter, @NotNull final ProblemsHolder holder) { final PsiTypeElement typeElement = psiParameter.getTypeElement(); boolean isVal = null != typeElement && isSameName(typeElement.getText()); boolean isVar = null != typeElement && isVar(typeElement.getText()); if (isVar || isVal) { PsiElement scope = psiParameter.getDeclarationScope(); boolean isForeachStatement = scope instanceof PsiForeachStatement; boolean isForStatement = scope instanceof PsiForStatement; if (isVal && !isForeachStatement) { holder.registerProblem(psiParameter, "'val' works only on local variables and on foreach loops", ProblemHighlightType.ERROR); } else if (isVar && !(isForeachStatement || isForStatement)) { holder.registerProblem(psiParameter, "'var' works only on local variables and on for/foreach loops", ProblemHighlightType.ERROR); } } }
private PsiType processParameterDeclaration(PsiElement parentDeclarationScope) { PsiType result = null; if (parentDeclarationScope instanceof PsiForeachStatement) { final PsiForeachStatement foreachStatement = (PsiForeachStatement) parentDeclarationScope; final PsiExpression iteratedValue = foreachStatement.getIteratedValue(); if (iteratedValue != null) { result = JavaGenericsUtil.getCollectionItemType(iteratedValue); } } return result; }
@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { PsiForeachStatement forStatement = getForeachStatementParent(psiElement); if(forStatement == null) { return; } final Document doc = editor.getDocument(); PsiElement body = forStatement.getBody(); if(body instanceof PsiBlockStatement) { return; } if(body != null && startLine(doc, body) == startLine(doc, forStatement)) { return; } PsiElement eltToInsertAfter = forStatement.getRParenth(); String text = "{}"; if(eltToInsertAfter == null) { eltToInsertAfter = forStatement; text = "){}"; } doc.insertString(eltToInsertAfter.getTextRange().getEndOffset(), text); }
private static PsiForeachStatement getForeachStatementParent(PsiElement psiElement) { PsiForeachStatement statement = PsiTreeUtil.getParentOfType(psiElement, PsiForeachStatement.class); if(statement == null) { return null; } PsiExpression iterated = statement.getIteratedValue(); PsiParameter parameter = statement.getIterationParameter(); return PsiTreeUtil.isAncestor(iterated, psiElement, false) || PsiTreeUtil.isAncestor(parameter, psiElement, false) ? statement : null; }
private boolean isVarForEach( @NotNull PsiParameter psiParameter ) { return psiParameter.getParent() instanceof PsiForeachStatement && isVar( psiParameter.getTypeElement().getText() ); }
@Override public void visitForeachStatement(PsiForeachStatement statement) { mVisitor.report("PsiForeachStatement", statement.getText(), statement); super.visitStatement(statement); }
@Override protected void createForLoopDeclaration(PsiForeachStatement statement, PsiExpression iteratedValue, boolean isArray, String iteratedValueText, StringBuilder newStatement, final String indexText) { final String lengthText; if (isArray) { lengthText = createVariableName(iteratedValueText + "Length", PsiType.INT, statement); } else { lengthText = createVariableName(iteratedValueText + "Size", PsiType.INT, statement); } newStatement.append("for(int "); newStatement.append(indexText); newStatement.append(" = 0, "); newStatement.append(lengthText); newStatement.append(" = "); if (iteratedValue instanceof PsiTypeCastExpression) { newStatement.append('('); newStatement.append(iteratedValueText); newStatement.append(')'); } else { newStatement.append(iteratedValueText); } if (isArray) { newStatement.append(".length;"); } else { newStatement.append(".size();"); } newStatement.append(indexText); newStatement.append('<'); newStatement.append(lengthText); newStatement.append(';'); newStatement.append(indexText); newStatement.append("++)"); newStatement.append("{ "); }
private boolean isValOrVarForEach(@NotNull PsiParameter psiParameter) { return psiParameter.getParent() instanceof PsiForeachStatement && isValOrVar(psiParameter.getTypeElement().getText()); }