[properties] IDEA-248873 - Cannot programmatically add comments to properties file

PropertiesParserDefinition.spaceExistenceTypeBetweenTokens didn't use to check if the left node is a comment and always returned SpaceRequirements.MAY, which resulted in merging newly added comment with the property the comment is for.

This patch adds a check if the left node is a comment and if so the method returns SpaceRequirements.MUST_LINE_BREAK which forces IDEA insert a newline between newly created comment and the property the comment is for

Signed-off-by: Nikita Eshkeev <nikita.eshkeev@jetbrains.com>

GitOrigin-RevId: 703548e456b129f01486c8f5a5775923002a336c
This commit is contained in:
Nikita Eshkeev
2020-10-20 22:08:15 +00:00
committed by intellij-monorepo-bot
parent 4009c6aa0d
commit 5b5caf37a3
8 changed files with 68 additions and 0 deletions
@@ -0,0 +1,5 @@
# " euler constant" "true"
# pi
pi=3.14
# euler constant
e=2.7
@@ -0,0 +1,5 @@
# " euler constant" "true"
# pi
pi=3.14
# euler constant
e=2.7
@@ -0,0 +1,3 @@
# " the first words" "true"
# the first words
hello=world
@@ -0,0 +1,4 @@
# " euler constant" "true"
# pi
pi=3.14
<caret>e=2.7
@@ -0,0 +1,6 @@
# " euler constant" "true"
# pi
pi=3.14
<caret>e=2.7
@@ -0,0 +1,2 @@
# " the first words" "true"
<caret>hello=world
@@ -60,6 +60,7 @@ public class PropertiesParserDefinition implements ParserDefinition {
@Override
public SpaceRequirements spaceExistenceTypeBetweenTokens(ASTNode left, ASTNode right) {
if (PropertiesTokenTypes.COMMENTS.contains(left.getElementType())) return SpaceRequirements.MUST_LINE_BREAK;
return SpaceRequirements.MAY;
}
@@ -0,0 +1,42 @@
package com.intellij.lang.properties;
import com.intellij.codeInsight.daemon.quickFix.ActionHint;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
import com.intellij.lang.properties.psi.Property;
import com.intellij.openapi.application.WriteAction;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiParserFacade;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class AddCommentViaAPITest extends LightQuickFixParameterizedTestCase {
private PsiParserFacade myParser;
@Override
protected void setUp() throws Exception {
super.setUp();
myParser = PsiParserFacade.SERVICE.getInstance(getProject());
}
@Override
protected @NonNls String getBasePath() {
return "/properties/addcomment";
}
@Override
protected void doAction(@NotNull ActionHint actionHint, @NotNull String testFullPath, @NotNull String testName) {
final int offset = getEditor().getCaretModel().getOffset();
final PsiElement psiElement = getFile().findElementAt(offset);
assert psiElement != null;
final Property property = PsiTreeUtil.getParentOfType(psiElement, Property.class);
final PsiComment comment = myParser.createLineCommentFromText(PropertiesFileType.INSTANCE, actionHint.getExpectedText());
WriteAction.runAndWait(() -> property.getParent().addBefore(comment, property));
final String expectedFilePath = getBasePath() + "/after" + testName;
checkResultByFile("In file: " + expectedFilePath, expectedFilePath, false);
}
}