Search in sources :

Example 1 with PsiWhileStatement

use of com.intellij.psi.PsiWhileStatement in project Main by SpartanRefactoring.

the class BooleanLiteralTest method testTipper.

public void testTipper() throws Exception {
    File f = new File(Resources.getResource("BooleanLiteralTestTip.java").getPath());
    LeonidasTipper lt = new LeonidasTipper("BooleanLiteralTestTip.java", IOUtils.toString(new BufferedReader(new InputStreamReader(new FileInputStream(f)))));
    PsiWhileStatement pws1 = createTestWhileStatementFromString("while (true) {\nx++;\n}");
    assertTrue(lt.canTip(pws1));
    PsiWhileStatement pws2 = createTestWhileStatementFromString("while (false) {\nx++;\n}");
    assertTrue(lt.canTip(pws2));
    PsiWhileStatement pws3 = createTestWhileStatementFromString("while (x > 2) {\nx++;\n}");
    assertFalse(lt.canTip(pws3));
    PsiWhileStatement pws4 = createTestWhileStatementFromString("while (true) {\nx++; x--;\n}");
    assertFalse(lt.canTip(pws4));
}
Also used : InputStreamReader(java.io.InputStreamReader) LeonidasTipper(il.org.spartan.Leonidas.plugin.tippers.LeonidasTipper) BufferedReader(java.io.BufferedReader) PsiWhileStatement(com.intellij.psi.PsiWhileStatement) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with PsiWhileStatement

use of com.intellij.psi.PsiWhileStatement in project Main by SpartanRefactoring.

the class OptionalTest method testTip1.

public void testTip1() throws Exception {
    File f = new File(Resources.getResource("OptionalTestTipper1.java").getPath());
    LeonidasTipper lt = new LeonidasTipper("OptionalTestTipper1", IOUtils.toString(new BufferedReader(new InputStreamReader(new FileInputStream(f)))));
    PsiWhileStatement pws1 = createTestWhileStatementFromString("while (x > 2) {\nx++; y--; x--;  \n}");
    assertTrue(lt.canTip(pws1));
    PsiWhileStatement pws2 = createTestWhileStatementFromString("while (x > 2) {\nx++; x--;  \n}");
    assertTrue(lt.canTip(pws2));
    PsiWhileStatement pws3 = createTestWhileStatementFromString("while (x > 2) {\nx++;\n}");
    assertFalse(lt.canTip(pws3));
    PsiWhileStatement pws4 = createTestWhileStatementFromString("while (x > 2) {\nx++; x--; x--;  \n}");
    assertTrue(lt.canTip(pws4));
    PsiWhileStatement pws5 = createTestWhileStatementFromString("while (x > 2) {\nx++; x++; x--;  \n}");
    assertTrue(lt.canTip(pws5));
}
Also used : InputStreamReader(java.io.InputStreamReader) LeonidasTipper(il.org.spartan.Leonidas.plugin.tippers.LeonidasTipper) BufferedReader(java.io.BufferedReader) PsiWhileStatement(com.intellij.psi.PsiWhileStatement) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 3 with PsiWhileStatement

use of com.intellij.psi.PsiWhileStatement 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)

Example 4 with PsiWhileStatement

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

the class WhileLoopPredicate method satisfiedBy.

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);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiWhileStatement(com.intellij.psi.PsiWhileStatement) PsiElement(com.intellij.psi.PsiElement)

Example 5 with PsiWhileStatement

use of com.intellij.psi.PsiWhileStatement in project Main by SpartanRefactoring.

the class AnyNumberOfTest method testTipper.

public void testTipper() throws Exception {
    File f = new File(Resources.getResource("AnyNumberOfTipperTest.java").getPath());
    LeonidasTipper lt = new LeonidasTipper("AnyNumberOfTipperTest", IOUtils.toString(new BufferedReader(new InputStreamReader(new FileInputStream(f)))));
    PsiWhileStatement pws1 = createTestWhileStatementFromString("while (x > 2) {\nx++; y--; x--;  \n}");
    assertTrue(lt.canTip(pws1));
    PsiWhileStatement pws2 = createTestWhileStatementFromString("while (x > 2) {\nx++; x--;  \n}");
    assertTrue(lt.canTip(pws2));
    PsiWhileStatement pws3 = createTestWhileStatementFromString("while (x > 2) {\nx++;\n}");
    assertFalse(lt.canTip(pws3));
    PsiWhileStatement pws4 = createTestWhileStatementFromString("while (x > 2) {\nx++; x--; x--;  \n}");
    assertTrue(lt.canTip(pws4));
    PsiWhileStatement pws5 = createTestWhileStatementFromString("while (x > 2) {\nx++; x++; x--;  \n}");
    assertTrue(lt.canTip(pws5));
    PsiWhileStatement pws6 = createTestWhileStatementFromString("while (x > 2) {\nx++; y--; y++; x--;  \n}");
    assertTrue(lt.canTip(pws6));
}
Also used : InputStreamReader(java.io.InputStreamReader) LeonidasTipper(il.org.spartan.Leonidas.plugin.tippers.LeonidasTipper) BufferedReader(java.io.BufferedReader) PsiWhileStatement(com.intellij.psi.PsiWhileStatement) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

PsiWhileStatement (com.intellij.psi.PsiWhileStatement)5 LeonidasTipper (il.org.spartan.Leonidas.plugin.tippers.LeonidasTipper)3 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 InputStreamReader (java.io.InputStreamReader)3 PsiElement (com.intellij.psi.PsiElement)2 PsiJavaToken (com.intellij.psi.PsiJavaToken)2 Document (com.intellij.openapi.editor.Document)1 PsiBlockStatement (com.intellij.psi.PsiBlockStatement)1 IElementType (com.intellij.psi.tree.IElementType)1