Search in sources :

Example 1 with PsiBlockStatement

use of com.intellij.psi.PsiBlockStatement in project intellij-community by JetBrains.

the class DoWhileConditionFixer method apply.

@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
    if (psiElement instanceof PsiDoWhileStatement) {
        final Document doc = editor.getDocument();
        final PsiDoWhileStatement stmt = (PsiDoWhileStatement) psiElement;
        if (stmt.getBody() == null || !(stmt.getBody() instanceof PsiBlockStatement) && stmt.getWhileKeyword() == null) {
            final int startOffset = stmt.getTextRange().getStartOffset();
            doc.replaceString(startOffset, startOffset + "do".length(), "do {} while()");
            return;
        }
        if (stmt.getCondition() == null) {
            if (stmt.getWhileKeyword() == null) {
                final int endOffset = stmt.getTextRange().getEndOffset();
                doc.insertString(endOffset, "while()");
            } else if (stmt.getLParenth() == null || stmt.getRParenth() == null) {
                final TextRange whileRange = stmt.getWhileKeyword().getTextRange();
                doc.replaceString(whileRange.getStartOffset(), whileRange.getEndOffset(), "while()");
            } else {
                processor.registerUnresolvedError(stmt.getLParenth().getTextRange().getEndOffset());
            }
        }
    }
}
Also used : PsiDoWhileStatement(com.intellij.psi.PsiDoWhileStatement) PsiBlockStatement(com.intellij.psi.PsiBlockStatement) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document)

Example 2 with PsiBlockStatement

use of com.intellij.psi.PsiBlockStatement in project intellij-community by JetBrains.

the class MissingWhileBodyFixer method apply.

@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
    if (!(psiElement instanceof PsiWhileStatement))
        return;
    PsiWhileStatement whileStatement = (PsiWhileStatement) psiElement;
    final Document doc = editor.getDocument();
    PsiElement body = whileStatement.getBody();
    if (body instanceof PsiBlockStatement)
        return;
    if (body != null && startLine(doc, body) == startLine(doc, whileStatement) && whileStatement.getCondition() != null)
        return;
    final PsiJavaToken rParenth = whileStatement.getRParenth();
    assert rParenth != null;
    doc.insertString(rParenth.getTextRange().getEndOffset(), "{}");
}
Also used : PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiBlockStatement(com.intellij.psi.PsiBlockStatement) PsiWhileStatement(com.intellij.psi.PsiWhileStatement) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement)

Aggregations

Document (com.intellij.openapi.editor.Document)2 PsiBlockStatement (com.intellij.psi.PsiBlockStatement)2 TextRange (com.intellij.openapi.util.TextRange)1 PsiDoWhileStatement (com.intellij.psi.PsiDoWhileStatement)1 PsiElement (com.intellij.psi.PsiElement)1 PsiJavaToken (com.intellij.psi.PsiJavaToken)1 PsiWhileStatement (com.intellij.psi.PsiWhileStatement)1