use of com.intellij.lang.folding.NamedFoldingDescriptor in project intellij-community by JetBrains.
the class ClosureFolding method createDescriptors.
@Nullable
private List<NamedFoldingDescriptor> createDescriptors(PsiElement classRBrace, int rangeStart, int rangeEnd, String header, String footer) {
if (rangeStart >= rangeEnd)
return null;
FoldingGroup group = FoldingGroup.newGroup("lambda");
List<NamedFoldingDescriptor> foldElements = new ArrayList<>();
foldElements.add(new NamedFoldingDescriptor(myNewExpression, getClosureStartOffset(), rangeStart, group, header));
if (rangeEnd + 1 < getClosureEndOffset()) {
foldElements.add(new NamedFoldingDescriptor(classRBrace, rangeEnd, getClosureEndOffset(), group, footer));
}
return foldElements;
}
use of com.intellij.lang.folding.NamedFoldingDescriptor in project intellij-community by JetBrains.
the class JavaFoldingBuilderBase method addOneLineMethodFolding.
private boolean addOneLineMethodFolding(@NotNull List<FoldingDescriptor> descriptorList, @NotNull PsiMethod method) {
if (!JavaCodeFoldingSettings.getInstance().isCollapseOneLineMethods()) {
return false;
}
Document document = method.getContainingFile().getViewProvider().getDocument();
PsiCodeBlock body = method.getBody();
PsiIdentifier nameIdentifier = method.getNameIdentifier();
if (body == null || document == null || nameIdentifier == null) {
return false;
}
if (document.getLineNumber(nameIdentifier.getTextRange().getStartOffset()) != document.getLineNumber(method.getParameterList().getTextRange().getEndOffset())) {
return false;
}
PsiJavaToken lBrace = body.getLBrace();
PsiJavaToken rBrace = body.getRBrace();
PsiStatement[] statements = body.getStatements();
if (lBrace == null || rBrace == null || statements.length != 1) {
return false;
}
PsiStatement statement = statements[0];
if (statement.textContains('\n')) {
return false;
}
if (!areOnAdjacentLines(lBrace, statement, document) || !areOnAdjacentLines(statement, rBrace, document)) {
//the user might intend to type at an empty line
return false;
}
int leftStart = method.getParameterList().getTextRange().getEndOffset();
int bodyStart = body.getTextRange().getStartOffset();
if (bodyStart > leftStart && !StringUtil.isEmptyOrSpaces(document.getCharsSequence().subSequence(leftStart + 1, bodyStart))) {
return false;
}
int leftEnd = statement.getTextRange().getStartOffset();
int rightStart = statement.getTextRange().getEndOffset();
int rightEnd = body.getTextRange().getEndOffset();
if (leftEnd <= leftStart + 1 || rightEnd <= rightStart + 1) {
return false;
}
String leftText = " { ";
String rightText = " }";
if (!fitsRightMargin(method, document, leftStart, rightEnd, rightStart - leftEnd + leftText.length() + rightText.length())) {
return false;
}
FoldingGroup group = FoldingGroup.newGroup("one-liner");
descriptorList.add(new NamedFoldingDescriptor(lBrace, leftStart, leftEnd, group, leftText));
descriptorList.add(new NamedFoldingDescriptor(rBrace, rightStart, rightEnd, group, rightText));
return true;
}
Aggregations