IDEA-76021 Groovy: Indentation of closing brace for closure is wrong

This commit is contained in:
Maxim.Medvedev
2011-10-27 18:06:54 +04:00
parent 3bb7db4a41
commit 2e7a08c68c
7 changed files with 117 additions and 10 deletions

View File

@@ -24,6 +24,9 @@ import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
import java.util.Map;
@@ -99,6 +102,17 @@ public class GeeseUtil {
//search for start of the line
cur = parent;
if (cur.getParent() instanceof GrMethodCall) {
GrMethodCall call = (GrMethodCall)cur.getParent();
GrExpression invoked = call.getInvokedExpression();
if (invoked instanceof GrReferenceExpression && ((GrReferenceExpression)invoked).getReferenceNameElement() != null) {
cur = ((GrReferenceExpression)invoked).getReferenceNameElement();
}
else {
cur = call;
}
}
cur = PsiTreeUtil.getDeepestFirst(cur);
while (!PsiUtil.isNewLine(next = PsiTreeUtil.prevLeaf(cur, true))) {
if (next == null) break;
cur = next;

View File

@@ -171,6 +171,7 @@ public class GroovyEnterHandler extends EnterHandlerDelegateAdapter {
String text = document.getText();
int nextLineFeed = text.indexOf('\n', caretOffset + 1);
if (nextLineFeed == -1) nextLineFeed = text.length();
CodeStyleManager.getInstance(project).reformatText(file, caretOffset, nextLineFeed);
return true;

View File

@@ -243,5 +243,33 @@ def c = { a ->
}
public void testGeese1() {
doTest '''[1, 2, 3, 4].
toSet().
findAllXxx {
it > 2
foo {<caret>}
}''',
'''[1, 2, 3, 4].
toSet().
findAllXxx {
it > 2
foo {
<caret>
} }'''
}
public void testGeese2() {
doTest '''foo {
bar {<caret>}
}
''', '''foo {
bar {
<caret>
} }
'''
}
}

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2000-2007 JetBrains s.r.o.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Copyright 2000-2011 JetBrains s.r.o.
*
* http://www.apache.org/licenses/LICENSE-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.groovy.lang.formatter;
@@ -218,5 +218,20 @@ public class FormatterTest extends GroovyFormatterTestCase {
doTest();
}
public void testGeese1() {doGeeseTest();}
public void testGeese2() {doGeeseTest();}
public void testGeese3() {doGeeseTest();}
private void doGeeseTest() {
GroovyCodeStyleSettings customSettings = myTempSettings.getCustomSettings(GroovyCodeStyleSettings.class);
boolean oldvalue = customSettings.USE_FLYING_GEESE_BRACES;
try {
customSettings.USE_FLYING_GEESE_BRACES = true;
doTest();
}
finally {
customSettings.USE_FLYING_GEESE_BRACES = oldvalue;
}
}
}

View File

@@ -0,0 +1,25 @@
foo(2) {
foo(2, {
foo(2) {
print f
}})}
foo(2) {
foo(2) {
foo(2, {
print f
})}}
-----
foo(2) {
foo(2, {
foo(2) {
print f
} })
}
foo(2) {
foo(2) {
foo(2, {
print f
})
} }

View File

@@ -0,0 +1,11 @@
foo {
bar {
test {
}}}
-----
foo {
bar {
test {
} } }

View File

@@ -0,0 +1,13 @@
foo (
2) {
bar(
3) {
print a
}}
-----
foo(
2) {
bar(
3) {
print a
} }