diff --git a/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/psi/impl/PropertiesFileImpl.java b/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/psi/impl/PropertiesFileImpl.java index 3152d1b2464a..3e4e8c622ee5 100644 --- a/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/psi/impl/PropertiesFileImpl.java +++ b/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/psi/impl/PropertiesFileImpl.java @@ -20,6 +20,7 @@ import com.intellij.lang.ASTFactory; import com.intellij.lang.ASTNode; import com.intellij.lang.properties.*; import com.intellij.lang.properties.parsing.PropertiesElementTypes; +import com.intellij.lang.properties.parsing.PropertiesTokenTypes; import com.intellij.lang.properties.psi.*; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.fileTypes.FileType; @@ -155,12 +156,22 @@ public class PropertiesFileImpl extends PsiFileBase implements PropertiesFile { } private void insertLineBreakBefore(final ASTNode anchorBefore) { - getPropertiesList().addChild(ASTFactory.whitespace("\n"), anchorBefore); + ASTNode propertiesList = getPropertiesList(); + if (anchorBefore == null && propertiesList.getFirstChildNode() == null) { + getNode().addChild(ASTFactory.whitespace("\n"), propertiesList); + } else { + propertiesList.addChild(ASTFactory.whitespace("\n"), anchorBefore); + } } private boolean haveToAddNewLine() { - ASTNode lastChild = getPropertiesList().getLastChildNode(); - return lastChild != null && !lastChild.getText().endsWith("\n"); + ASTNode propertiesList = getPropertiesList(); + ASTNode lastChild = propertiesList.getLastChildNode(); + if (lastChild != null) { + return !lastChild.getText().endsWith("\n"); + } + ASTNode prev = propertiesList.getTreePrev(); + return prev == null || !PropertiesTokenTypes.WHITESPACES.contains(prev.getElementType()); } @Override diff --git a/plugins/properties/testSrc/com/intellij/lang/properties/PropertiesFileTest.java b/plugins/properties/testSrc/com/intellij/lang/properties/PropertiesFileTest.java index 36eb74219dd6..cf5b89e75fc9 100644 --- a/plugins/properties/testSrc/com/intellij/lang/properties/PropertiesFileTest.java +++ b/plugins/properties/testSrc/com/intellij/lang/properties/PropertiesFileTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -21,6 +21,8 @@ import com.intellij.lang.properties.psi.Property; import com.intellij.lang.properties.psi.codeStyle.PropertiesCodeStyleSettings; import com.intellij.lang.properties.psi.impl.PropertyImpl; import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.psi.PsiFile; +import com.intellij.testFramework.PsiTestUtil; import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NonNls; @@ -45,7 +47,20 @@ public class PropertiesFileTest extends LightPlatformCodeInsightFixtureTestCase WriteCommandAction.runWriteCommandAction(getProject(), () -> { propertiesFile.addProperty(myPropertyToAdd); }); + PsiTestUtil.checkFileStructure((PsiFile)propertiesFile); + List properties = propertiesFile.getProperties(); + IProperty added = properties.get(0); + assertPropertyEquals(added, myPropertyToAdd.getName(), myPropertyToAdd.getValue()); + } + + public void testAddPropertyAfterComment2() throws Exception { + final PropertiesFile propertiesFile = + PropertiesImplUtil.getPropertiesFile(myFixture.configureByText(PropertiesFileType.INSTANCE, "#xxxxx\n")); + WriteCommandAction.runWriteCommandAction(getProject(), () -> { + propertiesFile.addProperty(myPropertyToAdd); + }); + PsiTestUtil.checkFileStructure((PsiFile)propertiesFile); List properties = propertiesFile.getProperties(); IProperty added = properties.get(0); @@ -63,7 +78,7 @@ public class PropertiesFileTest extends LightPlatformCodeInsightFixtureTestCase WriteCommandAction.runWriteCommandAction(null, () -> { propertiesFile.addProperty(myPropertyToAdd); }); - + PsiTestUtil.checkFileStructure((PsiFile)propertiesFile); List properties = propertiesFile.getProperties(); assertEquals(2, properties.size()); @@ -92,7 +107,7 @@ public class PropertiesFileTest extends LightPlatformCodeInsightFixtureTestCase final Property property = (Property)propertiesFile.findPropertyByKey("xxx2"); WriteCommandAction.runWriteCommandAction(null, property::delete); - + PsiTestUtil.checkFileStructure((PsiFile)propertiesFile); assertEquals("xxx=yyy\nxxx3=ttt\n\n", propertiesFile.getContainingFile().getText()); } @@ -102,7 +117,7 @@ public class PropertiesFileTest extends LightPlatformCodeInsightFixtureTestCase final Property property = (Property)propertiesFile.findPropertyByKey("xxx"); WriteCommandAction.runWriteCommandAction(null, property::delete); - + PsiTestUtil.checkFileStructure((PsiFile)propertiesFile); assertEquals("xxx2=tyrt\nxxx3=ttt\n\n", propertiesFile.getText()); } @@ -114,6 +129,7 @@ public class PropertiesFileTest extends LightPlatformCodeInsightFixtureTestCase WriteCommandAction.runWriteCommandAction(null, () -> { propertiesFile.addProperty(myPropertyToAdd); }); + PsiTestUtil.checkFileStructure((PsiFile)propertiesFile); assertEquals("a=b\\nccc\nkkk=vvv", propertiesFile.getText()); } @@ -137,6 +153,7 @@ public class PropertiesFileTest extends LightPlatformCodeInsightFixtureTestCase WriteCommandAction.runWriteCommandAction(null, () -> { propertiesFile.addPropertyAfter(myPropertyToAdd, c); }); + PsiTestUtil.checkFileStructure((PsiFile)propertiesFile); assertEquals("a=b\nc=d\nkkk=vvv\ne=f", propertiesFile.getText()); } @@ -147,6 +164,7 @@ public class PropertiesFileTest extends LightPlatformCodeInsightFixtureTestCase WriteCommandAction.runWriteCommandAction(null, () -> { propertiesFile.addPropertyAfter(myPropertyToAdd, p); }); + PsiTestUtil.checkFileStructure((PsiFile)propertiesFile); assertEquals("a=b\nc=d\ne=f\nkkk=vvv", propertiesFile.getText()); } @@ -156,6 +174,7 @@ public class PropertiesFileTest extends LightPlatformCodeInsightFixtureTestCase WriteCommandAction.runWriteCommandAction(getProject(), () -> { propertiesFile.addPropertyAfter(myPropertyToAdd, null); }); + PsiTestUtil.checkFileStructure((PsiFile)propertiesFile); assertEquals("kkk=vvv\na=b\nc=d\ne=f", propertiesFile.getText()); }