Search in sources :

Example 1 with PsiCodeBlock

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

the class UtilsTest method testGetFirstElementInsideBody.

public void testGetFirstElementInsideBody() throws Exception {
    PsiCodeBlock cb = createTestCodeBlockFromString("{x++;}");
    assertEquals(Utils.getFirstElementInsideBody(cb).getText(), "x++;");
}
Also used : PsiCodeBlock(com.intellij.psi.PsiCodeBlock)

Example 2 with PsiCodeBlock

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

the class ControlFlowTest method doTestFor.

private static void doTestFor(final File file) throws Exception {
    String contents = StringUtil.convertLineSeparators(FileUtil.loadFile(file));
    configureFromFileText(file.getName(), contents);
    // extract factory policy class name
    Pattern pattern = Pattern.compile("^// (\\S*).*", Pattern.DOTALL);
    Matcher matcher = pattern.matcher(contents);
    assertTrue(matcher.matches());
    final String policyClassName = matcher.group(1);
    final ControlFlowPolicy policy;
    if ("LocalsOrMyInstanceFieldsControlFlowPolicy".equals(policyClassName)) {
        policy = LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance();
    } else {
        policy = null;
    }
    final int offset = getEditor().getCaretModel().getOffset();
    PsiElement element = getFile().findElementAt(offset);
    element = PsiTreeUtil.getParentOfType(element, PsiCodeBlock.class, false);
    assertTrue("Selected element: " + element, element instanceof PsiCodeBlock);
    ControlFlow controlFlow = ControlFlowFactory.getInstance(getProject()).getControlFlow(element, policy);
    String result = controlFlow.toString().trim();
    final String expectedFullPath = StringUtil.trimEnd(file.getPath(), ".java") + ".txt";
    VirtualFile expectedFile = LocalFileSystem.getInstance().findFileByPath(expectedFullPath);
    String expected = LoadTextUtil.loadText(expectedFile).toString().trim();
    expected = expected.replaceAll("\r", "");
    assertEquals("Text mismatch (in file " + expectedFullPath + "):\n", expected, result);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Pattern(java.util.regex.Pattern) PsiCodeBlock(com.intellij.psi.PsiCodeBlock) Matcher(java.util.regex.Matcher) PsiElement(com.intellij.psi.PsiElement)

Example 3 with PsiCodeBlock

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

the class BlockBraceFixer method apply.

@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
    if (psiElement instanceof PsiCodeBlock && afterUnmatchedBrace(editor, psiElement.getContainingFile().getFileType())) {
        PsiCodeBlock block = (PsiCodeBlock) psiElement;
        int stopOffset = block.getTextRange().getEndOffset();
        final PsiStatement[] statements = block.getStatements();
        if (statements.length > 0) {
            stopOffset = statements[0].getTextRange().getEndOffset();
        }
        editor.getDocument().insertString(stopOffset, "}");
    }
}
Also used : PsiStatement(com.intellij.psi.PsiStatement) PsiCodeBlock(com.intellij.psi.PsiCodeBlock)

Example 4 with PsiCodeBlock

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

the class LeaveCodeBlockEnterProcessor method doEnter.

@Override
public boolean doEnter(Editor editor, PsiElement psiElement, boolean isModified) {
    PsiElement parent = psiElement.getParent();
    if (!(parent instanceof PsiCodeBlock)) {
        return false;
    }
    final ASTNode node = psiElement.getNode();
    if (node != null && CONTROL_FLOW_ELEMENT_TYPES.contains(node.getElementType())) {
        return false;
    }
    boolean leaveCodeBlock = isControlFlowBreak(psiElement);
    if (!leaveCodeBlock) {
        return false;
    }
    final int offset = parent.getTextRange().getEndOffset();
    // Check if there is empty line after the code block. Just move caret there in the case of the positive answer.
    final CharSequence text = editor.getDocument().getCharsSequence();
    if (offset < text.length() - 1) {
        final int i = CharArrayUtil.shiftForward(text, offset + 1, " \t");
        if (i < text.length() && text.charAt(i) == '\n') {
            editor.getCaretModel().moveToOffset(offset + 1);
            EditorActionManager actionManager = EditorActionManager.getInstance();
            EditorActionHandler actionHandler = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_MOVE_LINE_END);
            final DataContext dataContext = DataManager.getInstance().getDataContext(editor.getComponent());
            if (dataContext != null) {
                actionHandler.execute(editor, dataContext);
                return true;
            }
        }
    }
    editor.getCaretModel().moveToOffset(offset);
    return false;
}
Also used : EditorActionManager(com.intellij.openapi.editor.actionSystem.EditorActionManager) PsiCodeBlock(com.intellij.psi.PsiCodeBlock) DataContext(com.intellij.openapi.actionSystem.DataContext) ASTNode(com.intellij.lang.ASTNode) EditorActionHandler(com.intellij.openapi.editor.actionSystem.EditorActionHandler) PsiElement(com.intellij.psi.PsiElement)

Example 5 with PsiCodeBlock

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

the class FinallyBlockSelectioner method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    List<TextRange> result = new ArrayList<>();
    final PsiElement parent = e.getParent();
    if (parent instanceof PsiTryStatement) {
        final PsiTryStatement tryStatement = (PsiTryStatement) parent;
        final PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock();
        if (finallyBlock != null) {
            result.add(new TextRange(e.getTextRange().getStartOffset(), finallyBlock.getTextRange().getEndOffset()));
        }
    }
    return result;
}
Also used : PsiCodeBlock(com.intellij.psi.PsiCodeBlock) ArrayList(java.util.ArrayList) PsiTryStatement(com.intellij.psi.PsiTryStatement) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiCodeBlock (com.intellij.psi.PsiCodeBlock)10 PsiElement (com.intellij.psi.PsiElement)3 PsiStatement (com.intellij.psi.PsiStatement)2 ASTNode (com.intellij.lang.ASTNode)1 DataContext (com.intellij.openapi.actionSystem.DataContext)1 Document (com.intellij.openapi.editor.Document)1 EditorActionHandler (com.intellij.openapi.editor.actionSystem.EditorActionHandler)1 EditorActionManager (com.intellij.openapi.editor.actionSystem.EditorActionManager)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiCallExpression (com.intellij.psi.PsiCallExpression)1 PsiClass (com.intellij.psi.PsiClass)1 PsiExpression (com.intellij.psi.PsiExpression)1 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)1 PsiJavaFile (com.intellij.psi.PsiJavaFile)1 PsiJavaToken (com.intellij.psi.PsiJavaToken)1 PsiMethod (com.intellij.psi.PsiMethod)1 PsiSwitchStatement (com.intellij.psi.PsiSwitchStatement)1 PsiSynchronizedStatement (com.intellij.psi.PsiSynchronizedStatement)1 PsiTryStatement (com.intellij.psi.PsiTryStatement)1