IDEA-184098 2017.3.1 IJ Forms code generator removes comment before generated code - Regression from 2017.2

This commit is contained in:
Alexander Lobas
2018-07-04 17:39:23 +03:00
parent 5d072dca21
commit e3338d5958
9 changed files with 311 additions and 8 deletions
@@ -233,9 +233,33 @@ public final class FormSourceCodeGenerator {
PsiClass newClass = (PsiClass) classToBind.copy();
PsiElement initializerComment = null;
PsiElement firstMethodMarker = null;
PsiMethod[] methods = newClass.findMethodsByName(AsmCodeGenerator.SETUP_METHOD_NAME, false);
PsiClassInitializer[] initializers = newClass.getInitializers();
for (PsiMethod method: methods) {
if (method.hasParameters()) {
continue;
}
for (PsiClassInitializer initializer : initializers) {
if (containsMethodIdentifier(initializer, method)) {
PsiElement child = initializer.getFirstChild();
// very smart java PSI move comment node before class initializer to initializer children
if (child instanceof PsiComment && child.getTextOffset() == initializer.getTextOffset()) {
initializerComment = child.copy();
break;
}
}
}
firstMethodMarker = newClass
.addBefore(elementFactory.createFieldFromText("private int ____temp_field__" + System.currentTimeMillis() + ";", null), method);
break;
}
cleanup(newClass);
// [anton] the comments are written according to the SCR 26896
// [anton] the comments are written according to the SCR 26896
final PsiClass fakeClass = elementFactory.createClassFromText(
"{\n" +
"// GUI initializer generated by " + ApplicationNamesInfo.getInstance().getFullProductName() + " GUI Designer\n" +
@@ -259,7 +283,12 @@ public final class FormSourceCodeGenerator {
final CodeStyleManager formatter = CodeStyleManager.getInstance(module.getProject());
final JavaCodeStyleManager styler = JavaCodeStyleManager.getInstance(module.getProject());
PsiMethod method = (PsiMethod) newClass.add(fakeClass.getMethods()[0]);
PsiMethod method = (PsiMethod)(firstMethodMarker == null
? newClass.add(fakeClass.getMethods()[0])
: newClass.addAfter(fakeClass.getMethods()[0], firstMethodMarker));
if (firstMethodMarker != null) {
firstMethodMarker.delete();
}
// don't generate initializer block if $$$setupUI$$$() is called explicitly from one of the constructors
boolean needInitializer = true;
@@ -281,7 +310,10 @@ public final class FormSourceCodeGenerator {
}
if (needInitializer) {
newClass.addBefore(fakeClass.getInitializers()[0], method);
PsiElement initializer = newClass.addBefore(fakeClass.getInitializers()[0], method);
if (initializerComment != null) {
newClass.addBefore(initializerComment, initializer);
}
}
@NonNls final String grcMethodText = "/** @noinspection ALL */ public javax.swing.JComponent " +
@@ -456,8 +488,8 @@ public final class FormSourceCodeGenerator {
" component.setText(result.toString()); " +
" if (haveMnemonic) {" +
(componentClass.equals(AbstractButton.class)
? " component.setMnemonic(mnemonic);"
: " component.setDisplayedMnemonic(mnemonic);") +
? " component.setMnemonic(mnemonic);"
: " component.setDisplayedMnemonic(mnemonic);") +
(needIndex ? "component.setDisplayedMnemonicIndex(mnemonicIndex);" : "") +
"} }";
}
@@ -481,7 +513,7 @@ public final class FormSourceCodeGenerator {
}
}
public static void cleanup(final PsiClass aClass) throws IncorrectOperationException{
public static void cleanup(final PsiClass aClass) throws IncorrectOperationException {
final PsiMethod[] methods = aClass.findMethodsByName(AsmCodeGenerator.SETUP_METHOD_NAME, false);
for (final PsiMethod method: methods) {
final PsiClassInitializer[] initializers = aClass.getInitializers();
@@ -1025,7 +1057,7 @@ public final class FormSourceCodeGenerator {
pushFont(variable, fontDescriptor, getterName);
myBuffer.append("if (").append(variable).append("Font != null) ").append(variable).append(".").append(setterName).append("(")
.append(variable).append("Font);\n");
.append(variable).append("Font);\n");
}
private void pushFont(final String variable, final FontDescriptor fontDescriptor, @NonNls final String getterName) {
@@ -1213,7 +1245,7 @@ public final class FormSourceCodeGenerator {
}
void append(short value) {
myBuffer.append("(short) ");
myBuffer.append("(short) ");
myBuffer.append(value);
}
@@ -57,4 +57,5 @@ public class BindingTest {
public JComponent $$$getRootComponent$$$() {
return myRootComponent;
}
}
@@ -0,0 +1,52 @@
/*
* Copyright 2000-2009 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
*
* 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.
*/
import javax.swing.*;
public class BindingTest {
public JComponent myRootComponent;
public static JComponent myStaticField;
public final JComponent myFinalField = null;
public int myIntField;
public String myStringField;
// CHECKSTYLE:OFF
{
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
$$$setupUI$$$();
}
/**
* Method generated by IntelliJ IDEA GUI Designer
* >>> IMPORTANT!! <<<
* DO NOT edit this method OR call it in your code!
*
* @noinspection ALL
*/
private void $$$setupUI$$$() {
myRootComponent = new JPanel();
myRootComponent.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 1, new java.awt.Insets(0, 0, 0, 0), -1, -1));
}
/**
* @noinspection ALL
*/
public JComponent $$$getRootComponent$$$() {
return myRootComponent;
}
// CHECKSTYLE:ON
}
@@ -0,0 +1,52 @@
/*
* Copyright 2000-2009 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
*
* 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.
*/
import javax.swing.*;
public class BindingTest {
public JComponent myRootComponent;
public static JComponent myStaticField;
public final JComponent myFinalField = null;
public int myIntField;
public String myStringField;
// CHECKSTYLE:OFF
{
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
$$$setupUI$$$();
}
/**
* Method generated by IntelliJ IDEA GUI Designer
* >>> IMPORTANT!! <<<
* DO NOT edit this method OR call it in your code!
*
* @noinspection ALL
*/
private void $$$setupUI$$$() {
myRootComponent = new JPanel();
myRootComponent.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 1, new java.awt.Insets(0, 0, 0, 0), -1, -1));
}
/**
* @noinspection ALL
*/
public JComponent $$$getRootComponent$$$() {
return myRootComponent;
}
// CHECKSTYLE:ON
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="BindingTest">
<grid id="91062" binding="myRootComponent" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="131" y="90" width="24" height="24"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
</form>
@@ -0,0 +1,60 @@
/*
* Copyright 2000-2009 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
*
* 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.
*/
import javax.swing.*;
public class BindingTest {
private int a;
public JComponent myRootComponent;
private JLabel myLabel;
public BindingTest() {
a = 0;
$$$setupUI$$$();
myLabel.setText("My Text");
}
public BindingTest(int i) {
a = i;
myLabel.setText("My Text");
}
private void createUIComponents() {
myLabel = new JLabel();
}
// CHECKSTYLE:OFF
/**
* Method generated by IntelliJ IDEA GUI Designer
* >>> IMPORTANT!! <<<
* DO NOT edit this method OR call it in your code!
*
* @noinspection ALL
*/
private void $$$setupUI$$$() {
createUIComponents();
myRootComponent = new JPanel();
myRootComponent.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 1, new java.awt.Insets(0, 0, 0, 0), -1, -1));
myRootComponent.add(myLabel, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
}
/**
* @noinspection ALL
*/
public JComponent $$$getRootComponent$$$() {
return myRootComponent;
}
// CHECKSTYLE:ON
}
@@ -0,0 +1,64 @@
/*
* Copyright 2000-2009 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
*
* 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.
*/
import javax.swing.*;
public class BindingTest {
private int a;
public JComponent myRootComponent;
private JLabel myLabel;
public BindingTest() {
a = 0;
$$$setupUI$$$();
myLabel.setText("My Text");
}
public BindingTest(int i) {
a = i;
$$$setupUI$$$();
myLabel.setText("My Text");
}
private void createUIComponents() {
myLabel = new JLabel();
}
// CHECKSTYLE:OFF
/**
* Method generated by IntelliJ IDEA GUI Designer
* >>> IMPORTANT!! <<<
* DO NOT edit this method OR call it in your code!
*
* @noinspection ALL
*/
private void $$$setupUI$$$() {
createUIComponents();
myRootComponent = new JPanel();
myRootComponent.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 1, new java.awt.Insets(0, 0, 0, 0), -1, -1));
myLabel.setText("Label");
myRootComponent.add(myLabel, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
}
/**
* @noinspection ALL
*/
public JComponent $$$getRootComponent$$$() {
return myRootComponent;
}
// CHECKSTYLE:ON
}
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="BindingTest">
<grid id="91062" binding="myRootComponent" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="131" y="90" width="33" height="24"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="105ca" class="javax.swing.JLabel" binding="myLabel" custom-create="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Label"/>
</properties>
</component>
</children>
</grid>
</form>
@@ -72,6 +72,14 @@ public class FormSourceCodeGeneratorTest extends PsiTestCase {
doTest();
}
public void testSetupCallWithComments() throws IOException {
doTest();
}
public void testInitializerWithComments() throws IOException {
doTest();
}
private void doTest() throws IOException {
final VirtualFile form = myTestProjectRoot.findChild("Test.form");
assertNotNull(form);