IDEA-120694 collapsing one liners is overly eager

This commit is contained in:
peter
2014-02-13 20:25:35 +01:00
parent aef4cb8fe7
commit b1d4696980
2 changed files with 22 additions and 2 deletions
@@ -69,11 +69,23 @@ public abstract class JavaFoldingBuilderBase extends CustomFoldingBuilder implem
return "...";
}
private static boolean areOnAdjacentLines(PsiElement e1, PsiElement e2) {
Document document = e1.getContainingFile().getViewProvider().getDocument();
return document != null &&
document.getLineNumber(e1.getTextRange().getEndOffset()) + 1 == document.getLineNumber(e2.getTextRange().getStartOffset());
}
private static boolean isSimplePropertyAccessor(PsiMethod method) {
PsiCodeBlock body = method.getBody();
if (body == null || body.getLBrace() == null || body.getRBrace() == null) return false;
PsiStatement[] statements = body.getStatements();
if (statements.length == 0) return false;
if (!areOnAdjacentLines(body.getLBrace(), statements[0]) || !areOnAdjacentLines(statements[statements.length - 1], body.getRBrace())) {
//the user might intend to type at an empty line
return false;
}
PsiStatement statement = statements[0];
if (PropertyUtil.isSimplePropertyGetter(method)) {
if (statement instanceof PsiReturnStatement) {
@@ -457,6 +457,7 @@ class Test {
configure """class Foo {
int field;
int field2;
int field3;
int getField()
{
@@ -467,12 +468,18 @@ class Test {
field = f;
}
void setField2(int f){field2=f;}
void setField2(int f){field2=f;} // normal method folding here
// normal method folding here
void setField3(int f){
field2=f;
}
}"""
PsiClass fooClass = JavaPsiFacade.getInstance(project).findClass('Foo', GlobalSearchScope.allScope(project))
def regions = myFixture.editor.foldingModel.allFoldRegions.sort { it.startOffset }
assert regions.size() == 5
assert regions.size() == 6
Closure checkAccessorFolding = { FoldRegion region1, FoldRegion region2, PsiMethod method ->
assert region1.startOffset == method.parameterList.textRange.endOffset
@@ -489,6 +496,7 @@ class Test {
checkAccessorFolding(regions[2], regions[3], fooClass.methods[1])
assert regions[4].placeholderText == '{...}'
assert regions[5].placeholderText == '{...}'
}
private def changeFoldRegions(Closure op) {