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++;");
}
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);
}
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, "}");
}
}
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;
}
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;
}
Aggregations