IDEA-81531 Groovy: Let 'implement/override methods' action adjust blank lines

This commit is contained in:
Max Medvedev
2012-02-19 12:42:33 +04:00
parent 2a429829d3
commit aeb479595a
4 changed files with 49 additions and 20 deletions
@@ -26,8 +26,10 @@ import com.intellij.psi.PsiMember;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.GrReferenceAdjuster;
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
import org.jetbrains.plugins.groovy.lang.lexer.TokenSets;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
@@ -52,11 +54,24 @@ public class GroovyGenerationInfo<T extends PsiMember> extends PsiGenerationInfo
@Override
public void insert(PsiClass aClass, PsiElement anchor, boolean before) throws IncorrectOperationException {
super.insert(aClass, anchor, before);
final T member = getPsiMember();
if (member != null) {
assert member instanceof GroovyPsiElement;
GrReferenceAdjuster.shortenReferences(member);
if (member == null) return;
LOG.assertTrue(member instanceof GroovyPsiElement);
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(member.getProject());
final PsiElement prev = member.getPrevSibling();
if (prev!=null && GroovyTokenTypes.mNLS == prev.getNode().getElementType()) {
prev.replace(factory.createLineTerminator(1));
}
final PsiElement next = member.getNextSibling();
if (next != null && GroovyTokenTypes.mNLS == next.getNode().getElementType()) {
next.replace(factory.createLineTerminator(1));
}
GrReferenceAdjuster.shortenReferences(member);
}
@Override
@@ -707,6 +707,7 @@ public abstract class GrTypeDefinitionImpl extends GrStubElementBase<GrTypeDefin
return element;
}
if (body == null) throw new IncorrectOperationException("Class must have body");
return body.addBefore(element, nextChild);
}
else {
@@ -725,6 +726,7 @@ public abstract class GrTypeDefinitionImpl extends GrStubElementBase<GrTypeDefin
return super.addBefore(element, anchor);
}
if (body == null) throw new IncorrectOperationException("Class must have body");
return body.addBefore(element, anchor);
}
@@ -811,13 +813,7 @@ public abstract class GrTypeDefinitionImpl extends GrStubElementBase<GrTypeDefin
GrTypeDefinitionBody body = getBody();
if (body == null) throw new IncorrectOperationException("Type definition without a body");
// ASTNode anchorNode;
// anchorNode = anchorBefore.getNode();
// ASTNode bodyNode = body.getNode();
decl = (T)body.addBefore(decl, anchorBefore);
// bodyNode.addLeaf(GroovyTokenTypes.mWS, " ", decl.getNode()); //add whitespaces before and after to hack over incorrect auto reformat
// bodyNode.addLeaf(GroovyTokenTypes.mWS, " ", anchorNode);
return decl;
return (T)body.addBefore(decl, anchorBefore);
}
}
@@ -17,6 +17,7 @@ import org.jetbrains.plugins.groovy.actions.generate.accessors.GroovyGenerateGet
import org.jetbrains.plugins.groovy.actions.generate.constructors.GroovyGenerateConstructorHandler
import org.jetbrains.plugins.groovy.util.TestUtils
import com.intellij.codeInsight.generation.*
import com.intellij.psi.impl.source.PostprocessReformattingAspect
/**
* @author peter
@@ -65,7 +66,6 @@ class Foo extends Super {
this.d = d
this.e = e
}
}
"""
}
@@ -113,7 +113,6 @@ class Test {
def getFoo() {
return foo
}
}'''
}
@@ -132,7 +131,6 @@ class Test {
int getFoo() {
return foo
}
}'''
}
@@ -151,7 +149,6 @@ class Test {
static getFoo() {
return foo
}
}'''
}
@@ -178,7 +175,6 @@ class Test {
@Nullable getFoo() {
return foo
}
}'''
}
@@ -198,7 +194,6 @@ class Test {
void setFoo(def foo) {
this.foo = foo
}
}'''
}
@@ -219,7 +214,6 @@ class Test {
void setFoo(int foo) {
this.foo = foo
}
}'''
}
@@ -241,7 +235,6 @@ class Test {
static void setFoo(def foo) {
Test.foo = foo
}
}'''
}
@@ -270,14 +263,36 @@ class Test {
void setFoo(@Nullable foo) {
this.foo = foo
}
}'''
}
void testConstructorInTheMiddle() {
myFixture.configureByText("a.groovy", """
class Foo {
def foo() {}
<caret>
def bar() {}
}""")
generateConstructor()
myFixture.checkResult """
class Foo {
def foo() {}
Foo() {
}
def bar() {}
}"""
}
void generateGetter() {
//noinspection GroovyResultOfObjectAllocationIgnored
new GroovyGenerateGetterSetterAction() //don't remove it!!!
new WriteCommandAction(project, new PsiFile[0]) {
protected void run(Result result) throws Throwable {
@@ -287,11 +302,13 @@ class Test {
return members
}
}.invoke(project, myFixture.editor, myFixture.file);
PostprocessReformattingAspect.getInstance(project).doPostponedFormatting()
}
}.execute()
}
void generateSetter() {
//noinspection GroovyResultOfObjectAllocationIgnored
new GroovyGenerateGetterSetterAction() //don't remove it!!!
new WriteCommandAction(project, new PsiFile[0]) {
protected void run(Result result) throws Throwable {
@@ -301,6 +318,7 @@ class Test {
return members
}
}.invoke(project, myFixture.editor, myFixture.file);
PostprocessReformattingAspect.getInstance(project).doPostponedFormatting()
}
}.execute()
}
@@ -322,6 +340,7 @@ class Test {
}
}.invoke(project, myFixture.editor, myFixture.file);
PostprocessReformattingAspect.getInstance(project).doPostponedFormatting()
}
}.execute()
}
@@ -6,5 +6,4 @@ class ConstructorAtEnd {
ConstructorAtEnd() {<caret>
}
}