use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class ExpressionReturnValue method createReplacement.
public PsiStatement createReplacement(final PsiMethod extractedMethod, final PsiMethodCallExpression methodCallExpression) throws IncorrectOperationException {
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(methodCallExpression.getProject()).getElementFactory();
final CodeStyleManager styleManager = CodeStyleManager.getInstance(methodCallExpression.getProject());
PsiExpressionStatement expressionStatement;
expressionStatement = (PsiExpressionStatement) elementFactory.createStatementFromText("x = y();", null);
expressionStatement = (PsiExpressionStatement) styleManager.reformat(expressionStatement);
final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) expressionStatement.getExpression();
assignmentExpression.getLExpression().replace(getExpression());
assignmentExpression.getRExpression().replace(methodCallExpression);
return expressionStatement;
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class InvertIfConditionAction method wrapWithCodeBlock.
private static PsiStatement wrapWithCodeBlock(@NotNull PsiStatement statement) {
final Project project = statement.getProject();
final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
final CodeStyleManager codeStyle = CodeStyleManager.getInstance(project);
PsiCodeBlock codeBlock = factory.createCodeBlockFromText("{}", statement);
codeBlock = (PsiCodeBlock) codeStyle.reformat(codeBlock);
codeBlock.add(statement);
codeBlock = (PsiCodeBlock) statement.replace(codeBlock);
return codeBlock.getStatements()[0];
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class InvertIfConditionAction method formatIf.
private static void formatIf(PsiIfStatement ifStatement) throws IncorrectOperationException {
final Project project = ifStatement.getProject();
PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
PsiElement thenBranch = ifStatement.getThenBranch().copy();
PsiElement elseBranch = ifStatement.getElseBranch() != null ? ifStatement.getElseBranch().copy() : null;
PsiElement condition = ifStatement.getCondition().copy();
final CodeStyleManager codeStyle = CodeStyleManager.getInstance(project);
PsiBlockStatement codeBlock = (PsiBlockStatement) factory.createStatementFromText("{}", ifStatement);
codeBlock = (PsiBlockStatement) codeStyle.reformat(codeBlock);
ifStatement.getThenBranch().replace(codeBlock);
if (elseBranch != null) {
ifStatement.getElseBranch().replace(codeBlock);
}
ifStatement.getCondition().replace(factory.createExpressionFromText("true", null));
ifStatement = (PsiIfStatement) codeStyle.reformat(ifStatement);
if (!(thenBranch instanceof PsiBlockStatement)) {
PsiBlockStatement codeBlock1 = (PsiBlockStatement) ifStatement.getThenBranch().replace(codeBlock);
codeBlock1 = (PsiBlockStatement) codeStyle.reformat(codeBlock1);
codeBlock1.getCodeBlock().add(thenBranch);
} else {
ifStatement.getThenBranch().replace(thenBranch);
}
if (elseBranch != null) {
if (!(elseBranch instanceof PsiBlockStatement)) {
PsiBlockStatement codeBlock1 = (PsiBlockStatement) ifStatement.getElseBranch().replace(codeBlock);
codeBlock1 = (PsiBlockStatement) codeStyle.reformat(codeBlock1);
codeBlock1.getCodeBlock().add(elseBranch);
} else {
elseBranch = ifStatement.getElseBranch().replace(elseBranch);
if (emptyBlock(((PsiBlockStatement) elseBranch).getCodeBlock())) {
ifStatement.getElseBranch().delete();
}
}
}
ifStatement.getCondition().replace(condition);
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class InvertIfConditionAction method setupBranches.
private static PsiIfStatement setupBranches(PsiIfStatement ifStatement, ControlFlow flow) throws IncorrectOperationException {
PsiElementFactory factory = JavaPsiFacade.getInstance(ifStatement.getProject()).getElementFactory();
Project project = ifStatement.getProject();
PsiStatement thenBranch = ifStatement.getThenBranch();
PsiStatement elseBranch = ifStatement.getElseBranch();
if (elseBranch != null) {
elseBranch = (PsiStatement) elseBranch.copy();
setElseBranch(ifStatement, thenBranch, flow);
ifStatement.getThenBranch().replace(elseBranch);
return ifStatement;
}
final CodeStyleManager codeStyle = CodeStyleManager.getInstance(project);
if (flow.getSize() == 0) {
ifStatement.setElseBranch(thenBranch);
PsiStatement statement = factory.createStatementFromText("{}", ifStatement);
statement = (PsiStatement) codeStyle.reformat(statement);
statement = (PsiStatement) ifStatement.getThenBranch().replace(statement);
codeStyle.reformat(statement);
return ifStatement;
}
int endOffset = calcEndOffset(flow, ifStatement);
LOG.assertTrue(endOffset >= 0);
if (endOffset >= flow.getSize()) {
PsiStatement statement = factory.createStatementFromText("return;", ifStatement);
statement = (PsiStatement) codeStyle.reformat(statement);
if (thenBranch instanceof PsiBlockStatement) {
PsiStatement[] statements = ((PsiBlockStatement) thenBranch).getCodeBlock().getStatements();
if (statements.length > 0) {
PsiElement firstElement = statements[0];
while (firstElement.getPrevSibling() instanceof PsiWhiteSpace || firstElement.getPrevSibling() instanceof PsiComment) {
firstElement = firstElement.getPrevSibling();
}
ifStatement.getParent().addRangeAfter(firstElement, statements[statements.length - 1], ifStatement);
}
} else {
if (!(thenBranch instanceof PsiReturnStatement)) {
ifStatement = addAfterWithinCodeBlock(ifStatement, thenBranch);
}
}
ifStatement.getThenBranch().replace(statement);
return ifStatement;
}
PsiElement element = flow.getElement(endOffset);
while (element != null && !(element instanceof PsiStatement)) element = element.getParent();
if (element != null && element.getParent() instanceof PsiForStatement && ((PsiForStatement) element.getParent()).getUpdate() == element || element instanceof PsiWhileStatement && flow.getStartOffset(element) == endOffset || element instanceof PsiForeachStatement && flow.getStartOffset(element) + 1 == endOffset) {
PsiStatement statement = factory.createStatementFromText("continue;", ifStatement);
statement = (PsiStatement) codeStyle.reformat(statement);
ifStatement = addAfterWithinCodeBlock(ifStatement, thenBranch);
ifStatement.getThenBranch().replace(statement);
return ifStatement;
}
if (element instanceof PsiReturnStatement) {
PsiReturnStatement returnStatement = (PsiReturnStatement) element;
ifStatement = addAfterWithinCodeBlock(ifStatement, thenBranch);
ifStatement.getThenBranch().replace(returnStatement.copy());
ControlFlow flow2 = buildControlFlow(findCodeBlock(ifStatement));
if (!ControlFlowUtil.isInstructionReachable(flow2, flow2.getStartOffset(returnStatement), 0))
returnStatement.delete();
return ifStatement;
}
boolean nextUnreachable = flow.getEndOffset(ifStatement) == flow.getSize();
if (!nextUnreachable) {
PsiElement parent = ifStatement.getParent();
if (parent != null) {
if (!(parent instanceof PsiCodeBlock)) {
ifStatement = (PsiIfStatement) wrapWithCodeBlock(ifStatement);
parent = ifStatement.getParent();
thenBranch = ifStatement.getThenBranch();
}
ControlFlow localFlow = buildControlFlow(parent);
int startThenOffset = getThenOffset(localFlow, ifStatement);
int afterIfOffset = localFlow.getEndOffset(ifStatement);
nextUnreachable = !ControlFlowUtil.isInstructionReachable(localFlow, afterIfOffset, startThenOffset);
}
}
if (nextUnreachable) {
setElseBranch(ifStatement, thenBranch, flow);
PsiElement first = ifStatement.getNextSibling();
if (first != null) {
PsiElement last = first;
PsiElement next = last.getNextSibling();
while (next != null && !(next instanceof PsiSwitchLabelStatement)) {
last = next;
next = next.getNextSibling();
}
while (first != last && (last instanceof PsiWhiteSpace || last instanceof PsiJavaToken && ((PsiJavaToken) last).getTokenType() == JavaTokenType.RBRACE)) last = last.getPrevSibling();
PsiBlockStatement codeBlock = (PsiBlockStatement) factory.createStatementFromText("{}", ifStatement);
codeBlock.getCodeBlock().addRange(first, last);
first.getParent().deleteChildRange(first, last);
ifStatement.getThenBranch().replace(codeBlock);
}
codeStyle.reformat(ifStatement);
return ifStatement;
}
setElseBranch(ifStatement, thenBranch, flow);
PsiStatement statement = factory.createStatementFromText("{}", ifStatement);
statement = (PsiStatement) codeStyle.reformat(statement);
statement = (PsiStatement) ifStatement.getThenBranch().replace(statement);
codeStyle.reformat(statement);
return ifStatement;
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class ReplaceConstructorWithBuilderProcessor method createBuilderClass.
@Nullable
private PsiClass createBuilderClass() {
final PsiClass psiClass = myConstructors[0].getContainingClass();
assert psiClass != null;
final PsiTypeParameterList typeParameterList = psiClass.getTypeParameterList();
final String text = "public class " + myClassName + (typeParameterList != null ? typeParameterList.getText() : "") + "{}";
final PsiFileFactory factory = PsiFileFactory.getInstance(myProject);
final PsiJavaFile newFile = (PsiJavaFile) factory.createFileFromText(myClassName + ".java", JavaFileType.INSTANCE, text);
final PsiFile containingFile = myConstructors[0].getContainingFile();
final PsiDirectory containingDirectory = containingFile.getContainingDirectory();
final PsiDirectory directory;
if (myMoveDestination != null) {
directory = myMoveDestination.getTargetDirectory(containingDirectory);
} else {
final Module module = ModuleUtil.findModuleForPsiElement(containingFile);
assert module != null;
directory = PackageUtil.findOrCreateDirectoryForPackage(module, myPackageName, containingDirectory, true, true);
}
if (directory != null) {
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(PsiManager.getInstance(myProject).getProject());
final PsiJavaFile reformattedFile = (PsiJavaFile) codeStyleManager.reformat(JavaCodeStyleManager.getInstance(newFile.getProject()).shortenClassReferences(newFile));
if (directory.findFile(reformattedFile.getName()) != null)
return reformattedFile.getClasses()[0];
return ((PsiJavaFile) directory.add(reformattedFile)).getClasses()[0];
}
return null;
}
Aggregations