From 4f7a031b4b6cc85b39d838c68f46fdd969b5b642 Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Fri, 1 Apr 2016 11:59:32 +0300 Subject: [PATCH] EA-79420 - replace key/value delimiter fixed for new property parsing --- .../properties/psi/impl/PropertyImpl.java | 50 +++++++++++++++---- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/psi/impl/PropertyImpl.java b/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/psi/impl/PropertyImpl.java index 2498dec11cad..0913f48e4c18 100644 --- a/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/psi/impl/PropertyImpl.java +++ b/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/psi/impl/PropertyImpl.java @@ -15,6 +15,7 @@ */ package com.intellij.lang.properties.psi.impl; +import com.intellij.lang.ASTFactory; import com.intellij.lang.ASTNode; import com.intellij.lang.properties.PropertyManipulator; import com.intellij.lang.properties.parsing.PropertiesTokenTypes; @@ -36,6 +37,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; +import java.util.ArrayList; +import java.util.List; /** * @author max @@ -451,17 +454,46 @@ public class PropertyImpl extends PropertiesStubElementImpl implem return text.charAt(0); } - public void replaceKeyValueDelimiterWithDefault() { - PropertyImpl property = (PropertyImpl)PropertiesElementFactory.createProperty(getProject(), "yyy", "xxx", null); - final ASTNode oldDelimiter = getNode().findChildByType(PropertiesTokenTypes.KEY_VALUE_SEPARATOR); - LOG.assertTrue(oldDelimiter != null); - final ASTNode newDelimiter = property.getNode().findChildByType(PropertiesTokenTypes.KEY_VALUE_SEPARATOR); - LOG.assertTrue(newDelimiter != null); - getNode().replaceChild(oldDelimiter, newDelimiter); - } - @Override public PsiElement getParent() { return getParentByStub(); } + + public void replaceKeyValueDelimiterWithDefault() { + PropertyImpl property = (PropertyImpl)PropertiesElementFactory.createProperty(getProject(), "yyy", "xxx", null); + final ASTNode newDelimiter = property.getNode().findChildByType(PropertiesTokenTypes.KEY_VALUE_SEPARATOR); + final ASTNode propertyNode = getNode(); + final ASTNode oldDelimiter = propertyNode.findChildByType(PropertiesTokenTypes.KEY_VALUE_SEPARATOR); + if (areDelimitersEqual(newDelimiter, oldDelimiter)) { + return; + } + if (newDelimiter == null) { + propertyNode.replaceChild(oldDelimiter, ASTFactory.whitespace(" ")); + } else { + if (oldDelimiter == null) { + propertyNode.addChild(newDelimiter, getValueNode()); + final ASTNode insertedDelimiter = propertyNode.findChildByType(PropertiesTokenTypes.KEY_VALUE_SEPARATOR); + LOG.assertTrue(insertedDelimiter != null); + ASTNode currentPrev = insertedDelimiter.getTreePrev(); + final List toDelete = new ArrayList<>(); + while (currentPrev != null && currentPrev.getElementType() == PropertiesTokenTypes.WHITE_SPACE) { + toDelete.add(currentPrev); + currentPrev = currentPrev.getTreePrev(); + } + for (ASTNode node : toDelete) { + propertyNode.removeChild(node); + } + } else { + propertyNode.replaceChild(oldDelimiter, newDelimiter); + } + } + } + + private static boolean areDelimitersEqual(@Nullable ASTNode node1, @Nullable ASTNode node2) { + if (node1 == null && node2 == null) return true; + if (node1 == null || node2 == null) return false; + final String text1 = node1.getText(); + final String text2 = node2.getText(); + return text1.equals(text2); + } }