use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class JavaWithNotSurrounder 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);
PsiPrefixExpression prefixExpr = (PsiPrefixExpression) factory.createExpressionFromText("!(a)", null);
prefixExpr = (PsiPrefixExpression) codeStyleManager.reformat(prefixExpr);
((PsiParenthesizedExpression) prefixExpr.getOperand()).getExpression().replace(expr);
expr = (PsiExpression) IntroduceVariableBase.replace(expr, prefixExpr, project);
int offset = expr.getTextRange().getEndOffset();
return new TextRange(offset, offset);
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class JavaWithNullCheckSurrounder 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 != null){\nst;\n}";
PsiIfStatement ifStatement = (PsiIfStatement) factory.createStatementFromText(text, null);
ifStatement = (PsiIfStatement) codeStyleManager.reformat(ifStatement);
((PsiBinaryExpression) ifStatement.getCondition()).getLOperand().replace(expr);
PsiExpressionStatement statement = PsiTreeUtil.getParentOfType(expr, PsiExpressionStatement.class);
String oldText = statement.getText();
ifStatement = (PsiIfStatement) statement.replace(ifStatement);
PsiCodeBlock block = ((PsiBlockStatement) ifStatement.getThenBranch()).getCodeBlock();
block = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(block);
PsiElement replace = block.getStatements()[0].replace(factory.createStatementFromText(oldText, block));
int offset = replace.getTextRange().getEndOffset();
return new TextRange(offset, offset);
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class JavaWithParenthesesSurrounder 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);
PsiParenthesizedExpression parenthExpr = (PsiParenthesizedExpression) factory.createExpressionFromText("(a)", null);
parenthExpr = (PsiParenthesizedExpression) codeStyleManager.reformat(parenthExpr);
parenthExpr.getExpression().replace(expr);
expr = (PsiExpression) IntroduceVariableBase.replace(expr, parenthExpr, project);
int offset = expr.getTextRange().getEndOffset();
return new TextRange(offset, offset);
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class JavaWithTryFinallySurrounder 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 = "try{\n}finally{\n\n}";
PsiTryStatement tryStatement = (PsiTryStatement) factory.createStatementFromText(text, null);
tryStatement = (PsiTryStatement) codeStyleManager.reformat(tryStatement);
tryStatement = (PsiTryStatement) container.addAfter(tryStatement, statements[statements.length - 1]);
PsiCodeBlock tryBlock = tryStatement.getTryBlock();
if (tryBlock == null) {
return null;
}
SurroundWithUtil.indentCommentIfNecessary(tryBlock, statements);
tryBlock.addRange(statements[0], statements[statements.length - 1]);
container.deleteChildRange(statements[0], statements[statements.length - 1]);
PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock();
if (finallyBlock == null) {
return null;
}
Document document = editor.getDocument();
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
TextRange finallyBlockRange = finallyBlock.getTextRange();
int newLineOffset = finallyBlockRange.getStartOffset() + 2;
editor.getCaretModel().moveToOffset(newLineOffset);
editor.getSelectionModel().removeSelection();
CodeStyleManager.getInstance(project).adjustLineIndent(document, newLineOffset);
PsiDocumentManager.getInstance(project).commitDocument(document);
return new TextRange(editor.getCaretModel().getOffset(), editor.getCaretModel().getOffset());
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class ImportHelper method prepareOptimizeImportsResult.
@Nullable("null means no need to replace the import list because they are the same")
PsiImportList prepareOptimizeImportsResult(@NotNull final PsiJavaFile file) {
PsiImportList oldList = file.getImportList();
if (oldList == null)
return null;
// Java parser works in a way that comments may be included to the import list, e.g.:
// import a;
// /* comment */
// import b;
// We want to preserve those comments then.
List<PsiElement> nonImports = new ArrayList<>();
// Note: this array may contain "<packageOrClassName>.*" for unresolved imports!
List<Pair<String, Boolean>> names = new ArrayList<>(collectNamesToImport(file, nonImports));
Collections.sort(names, Comparator.comparing(o -> o.getFirst()));
List<Pair<String, Boolean>> resultList = sortItemsAccordingToSettings(names, mySettings);
final Map<String, Boolean> classesOrPackagesToImportOnDemand = new THashMap<>();
collectOnDemandImports(resultList, mySettings, classesOrPackagesToImportOnDemand);
MultiMap<String, String> conflictingMemberNames = new MultiMap<>();
for (Pair<String, Boolean> pair : resultList) {
if (pair.second) {
conflictingMemberNames.putValue(StringUtil.getShortName(pair.first), StringUtil.getPackageName(pair.first));
}
}
for (String methodName : conflictingMemberNames.keySet()) {
Collection<String> collection = conflictingMemberNames.get(methodName);
if (!classesOrPackagesToImportOnDemand.keySet().containsAll(collection)) {
for (String name : collection) {
classesOrPackagesToImportOnDemand.remove(name);
}
}
}
Set<String> classesToUseSingle = findSingleImports(file, resultList, classesOrPackagesToImportOnDemand.keySet());
Set<String> toReimport = new THashSet<>();
calcClassesConflictingViaOnDemandImports(file, classesOrPackagesToImportOnDemand, file.getResolveScope(), toReimport);
classesToUseSingle.addAll(toReimport);
try {
StringBuilder text = buildImportListText(resultList, classesOrPackagesToImportOnDemand.keySet(), classesToUseSingle);
for (PsiElement nonImport : nonImports) {
text.append("\n").append(nonImport.getText());
}
String ext = StdFileTypes.JAVA.getDefaultExtension();
PsiFileFactory factory = PsiFileFactory.getInstance(file.getProject());
final PsiJavaFile dummyFile = (PsiJavaFile) factory.createFileFromText("_Dummy_." + ext, StdFileTypes.JAVA, text);
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(file.getProject());
codeStyleManager.reformat(dummyFile);
PsiImportList newImportList = dummyFile.getImportList();
assert newImportList != null : dummyFile.getText();
PsiImportList result = (PsiImportList) newImportList.copy();
if (oldList.isReplaceEquivalent(result))
return null;
if (!nonImports.isEmpty()) {
PsiElement firstPrevious = newImportList.getPrevSibling();
while (firstPrevious != null && firstPrevious.getPrevSibling() != null) {
firstPrevious = firstPrevious.getPrevSibling();
}
for (PsiElement element = firstPrevious; element != null && element != newImportList; element = element.getNextSibling()) {
result.add(element.copy());
}
for (PsiElement element = newImportList.getNextSibling(); element != null; element = element.getNextSibling()) {
result.add(element.copy());
}
}
return result;
} catch (IncorrectOperationException e) {
LOG.error(e);
return null;
}
}
Aggregations