use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class ExtractIfConditionAction method invoke.
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
final PsiIfStatement ifStatement = PsiTreeUtil.getParentOfType(element, PsiIfStatement.class);
if (ifStatement == null) {
return;
}
final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
final PsiStatement newIfStatement = create(factory, ifStatement, element);
if (newIfStatement == null) {
return;
}
ifStatement.replace(codeStyleManager.reformat(newIfStatement));
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class JavaWithBlockSurrounder method surroundStatements.
@Override
public TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) throws IncorrectOperationException {
PsiManager manager = PsiManager.getInstance(project);
PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
statements = SurroundWithUtil.moveDeclarationsOut(container, statements, false);
if (statements.length == 0) {
return null;
}
String text = "{\n}";
PsiBlockStatement blockStatement = (PsiBlockStatement) factory.createStatementFromText(text, null);
blockStatement = (PsiBlockStatement) codeStyleManager.reformat(blockStatement);
blockStatement = (PsiBlockStatement) container.addBefore(blockStatement, statements[0]);
PsiCodeBlock body = blockStatement.getCodeBlock();
SurroundWithUtil.indentCommentIfNecessary(body, statements);
body.addRange(statements[0], statements[statements.length - 1]);
container.deleteChildRange(statements[0], statements[statements.length - 1]);
PsiElement firstChild = blockStatement.getFirstChild();
if (firstChild == null) {
return null;
}
TextRange range = firstChild.getTextRange();
return new TextRange(range.getEndOffset(), range.getEndOffset());
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class JavaWithDoWhileSurrounder method surroundStatements.
@Override
public TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) throws IncorrectOperationException {
PsiManager manager = PsiManager.getInstance(project);
PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
statements = SurroundWithUtil.moveDeclarationsOut(container, statements, false);
if (statements.length == 0) {
return null;
}
@NonNls String text = "do{\n}while(true);";
PsiDoWhileStatement doWhileStatement = (PsiDoWhileStatement) factory.createStatementFromText(text, null);
doWhileStatement = (PsiDoWhileStatement) codeStyleManager.reformat(doWhileStatement);
doWhileStatement = (PsiDoWhileStatement) container.addAfter(doWhileStatement, statements[statements.length - 1]);
PsiStatement body = doWhileStatement.getBody();
if (!(body instanceof PsiBlockStatement)) {
return null;
}
PsiCodeBlock bodyBlock = ((PsiBlockStatement) body).getCodeBlock();
SurroundWithUtil.indentCommentIfNecessary(bodyBlock, statements);
bodyBlock.addRange(statements[0], statements[statements.length - 1]);
container.deleteChildRange(statements[0], statements[statements.length - 1]);
PsiExpression condition = doWhileStatement.getCondition();
return condition == null ? null : condition.getTextRange();
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class JavaWithIfExpressionSurrounder method surroundExpression.
@Override
public TextRange surroundExpression(Project project, Editor editor, PsiExpression expr) throws IncorrectOperationException {
PsiManager manager = expr.getManager();
PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
@NonNls String text = "if(a){\nst;\n}";
PsiIfStatement ifStatement = (PsiIfStatement) factory.createStatementFromText(text, null);
ifStatement = (PsiIfStatement) codeStyleManager.reformat(ifStatement);
PsiExpression condition = ifStatement.getCondition();
if (condition != null) {
condition.replace(expr);
}
PsiExpressionStatement statement = (PsiExpressionStatement) expr.getParent();
ifStatement = (PsiIfStatement) statement.replace(ifStatement);
PsiStatement thenBranch = ifStatement.getThenBranch();
if (thenBranch != null && thenBranch instanceof PsiBlockStatement) {
PsiCodeBlock block = ((PsiBlockStatement) thenBranch).getCodeBlock();
block = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(block);
TextRange range = block.getStatements()[0].getTextRange();
editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
return TextRange.from(range.getStartOffset(), 0);
}
return TextRange.from(editor.getCaretModel().getOffset(), 0);
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class JavaWithIfSurrounder method surroundStatements.
@Override
public TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) throws IncorrectOperationException {
PsiManager manager = PsiManager.getInstance(project);
PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
statements = SurroundWithUtil.moveDeclarationsOut(container, statements, true);
if (statements.length == 0) {
return null;
}
@NonNls String text = "if(a){\n}";
PsiIfStatement ifStatement = (PsiIfStatement) factory.createStatementFromText(text, null);
ifStatement = (PsiIfStatement) codeStyleManager.reformat(ifStatement);
ifStatement = (PsiIfStatement) container.addAfter(ifStatement, statements[statements.length - 1]);
final PsiStatement thenBranch = ifStatement.getThenBranch();
if (thenBranch != null) {
PsiCodeBlock thenBlock = ((PsiBlockStatement) thenBranch).getCodeBlock();
SurroundWithUtil.indentCommentIfNecessary(thenBlock, statements);
thenBlock.addRange(statements[0], statements[statements.length - 1]);
container.deleteChildRange(statements[0], statements[statements.length - 1]);
}
ifStatement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(ifStatement);
if (ifStatement == null) {
return null;
}
final PsiExpression condition = ifStatement.getCondition();
if (condition != null) {
TextRange range = condition.getTextRange();
TextRange textRange = new TextRange(range.getStartOffset(), range.getStartOffset());
editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
return textRange;
}
return ifStatement.getTextRange();
}
Aggregations