use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class Intention method replaceExpressionWithNegatedExpressionString.
protected static void replaceExpressionWithNegatedExpressionString(@NotNull String newExpression, @NotNull PsiExpression expression) {
final Project project = expression.getProject();
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
final PsiElementFactory factory = psiFacade.getElementFactory();
PsiExpression expressionToReplace = expression;
final String expString;
if (BoolUtils.isNegated(expression)) {
expressionToReplace = BoolUtils.findNegation(expressionToReplace);
expString = newExpression;
} else {
PsiElement parent = expressionToReplace.getParent();
while (parent instanceof PsiParenthesizedExpression) {
expressionToReplace = (PsiExpression) parent;
parent = parent.getParent();
}
expString = "!(" + newExpression + ')';
}
final PsiExpression newCall = factory.createExpressionFromText(expString, expression);
assert expressionToReplace != null;
final PsiElement insertedElement = expressionToReplace.replace(newCall);
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
codeStyleManager.reformat(insertedElement);
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class MoveCommentToSeparateLineIntention method processIntention.
public void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
final PsiComment selectedComment = (PsiComment) element;
PsiElement elementToCheck = selectedComment;
final PsiWhiteSpace whiteSpace;
while (true) {
elementToCheck = PsiTreeUtil.prevLeaf(elementToCheck);
if (elementToCheck == null) {
return;
}
if (isLineBreakWhiteSpace(elementToCheck)) {
whiteSpace = (PsiWhiteSpace) elementToCheck;
break;
}
}
final PsiElement copyWhiteSpace = whiteSpace.copy();
final PsiElement parent = whiteSpace.getParent();
assert parent != null;
final PsiManager manager = selectedComment.getManager();
final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
final String commentText = selectedComment.getText();
final PsiComment newComment = factory.createCommentFromText(commentText, parent);
final PsiElement insertedComment = parent.addBefore(newComment, whiteSpace);
parent.addBefore(copyWhiteSpace, insertedComment);
selectedComment.delete();
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
codeStyleManager.reformat(insertedComment);
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class SurroundAutoCloseableAction method processVariable.
private static void processVariable(Project project, Editor editor, PsiLocalVariable variable) {
PsiExpression initializer = ObjectUtils.assertNotNull(variable.getInitializer());
PsiElement declaration = variable.getParent();
PsiElement codeBlock = declaration.getParent();
LocalSearchScope scope = new LocalSearchScope(codeBlock);
PsiElement last = null;
for (PsiReference reference : ReferencesSearch.search(variable, scope).findAll()) {
PsiElement usage = PsiTreeUtil.findPrevParent(codeBlock, reference.getElement());
if ((last == null || usage.getTextOffset() > last.getTextOffset())) {
last = usage;
}
}
String text = "try (" + variable.getTypeElement().getText() + " " + variable.getName() + " = " + initializer.getText() + ") {}";
PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
PsiTryStatement armStatement = (PsiTryStatement) declaration.replace(factory.createStatementFromText(text, codeBlock));
List<PsiElement> toFormat = null;
if (last != null) {
toFormat = moveStatements(last, armStatement);
}
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
PsiElement formattedElement = codeStyleManager.reformat(armStatement);
if (toFormat != null) {
for (PsiElement psiElement : toFormat) {
codeStyleManager.reformat(psiElement);
}
}
if (last == null) {
PsiCodeBlock tryBlock = ((PsiTryStatement) formattedElement).getTryBlock();
if (tryBlock != null) {
PsiJavaToken brace = tryBlock.getLBrace();
if (brace != null) {
editor.getCaretModel().moveToOffset(brace.getTextOffset() + 1);
}
}
}
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class MoveJavaInnerHandler method copyClass.
@NotNull
@Override
public PsiClass copyClass(@NotNull final MoveInnerOptions options) {
PsiClass innerClass = options.getInnerClass();
PsiClass newClass;
if (options.getTargetContainer() instanceof PsiDirectory) {
newClass = createNewClass(options);
PsiDocComment defaultDocComment = newClass.getDocComment();
if (defaultDocComment != null && innerClass.getDocComment() == null) {
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(defaultDocComment.getProject());
innerClass = (PsiClass) codeStyleManager.reformat(innerClass.addAfter(defaultDocComment, null).getParent());
}
newClass = (PsiClass) newClass.replace(innerClass);
PsiUtil.setModifierProperty(newClass, PsiModifier.STATIC, false);
PsiUtil.setModifierProperty(newClass, PsiModifier.PRIVATE, false);
PsiUtil.setModifierProperty(newClass, PsiModifier.PROTECTED, false);
final boolean makePublic = needPublicAccess(options.getOuterClass(), options.getTargetContainer());
if (makePublic) {
PsiUtil.setModifierProperty(newClass, PsiModifier.PUBLIC, true);
}
} else {
newClass = (PsiClass) options.getTargetContainer().add(innerClass);
}
newClass.setName(options.getNewClassName());
return newClass;
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class SimplifyBooleanExpressionFix method replaceWithStatements.
private static void replaceWithStatements(@NotNull PsiIfStatement orig, @Nullable PsiStatement statement) throws IncorrectOperationException {
if (statement == null) {
orig.delete();
return;
}
PsiElement parent = orig.getParent();
if (parent == null)
return;
if (parent instanceof PsiCodeBlock && blockAlwaysReturns(statement)) {
removeFollowingStatements(orig, (PsiCodeBlock) parent);
}
if (statement instanceof PsiBlockStatement && parent instanceof PsiCodeBlock && !DeclarationSearchUtils.containsConflictingDeclarations(((PsiBlockStatement) statement).getCodeBlock(), (PsiCodeBlock) parent)) {
// See IDEADEV-24277
// Code block can only be inlined into another (parent) code block.
// Code blocks, which are if or loop statement branches should not be inlined.
PsiCodeBlock codeBlock = ((PsiBlockStatement) statement).getCodeBlock();
PsiJavaToken lBrace = codeBlock.getLBrace();
PsiJavaToken rBrace = codeBlock.getRBrace();
if (lBrace == null || rBrace == null)
return;
final PsiElement[] children = codeBlock.getChildren();
if (children.length > 2) {
final PsiElement added = parent.addRangeBefore(children[1], children[children.length - 2], orig);
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(orig.getManager());
codeStyleManager.reformat(added);
}
orig.delete();
} else {
orig.replace(statement);
}
}
Aggregations