From 124689fa7b12aee1899897e33fc0dc5f07c1a4a2 Mon Sep 17 00:00:00 2001 From: anna Date: Mon, 25 Mar 2013 18:34:03 +0100 Subject: [PATCH] javafx: collapse tag to attr: accept tags with multiple subtags with fx:value attributes (cherry picked from commit 173a039f4f4b09ae8a107d514ae71414be1cc5d4) --- .../JavaFXCollapseSubtagToAttributeTest.java | 4 ++ ...aFxCollapseSubTagToAttributeIntention.java | 45 ++++++++++++++----- .../intentions/collapseToAttr/styleclass.fxml | 9 ++++ .../collapseToAttr/styleclass_after.fxml | 5 +++ 4 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 plugins/javaFX/testData/intentions/collapseToAttr/styleclass.fxml create mode 100644 plugins/javaFX/testData/intentions/collapseToAttr/styleclass_after.fxml diff --git a/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFXCollapseSubtagToAttributeTest.java b/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFXCollapseSubtagToAttributeTest.java index d27cd08ce0e9..9839bef63707 100644 --- a/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFXCollapseSubtagToAttributeTest.java +++ b/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFXCollapseSubtagToAttributeTest.java @@ -46,6 +46,10 @@ public class JavaFXCollapseSubtagToAttributeTest extends DaemonAnalyzerTestCase doTest(true, "GridPane.rowIndex"); } + public void testStyleclass() throws Exception { + doTest(true, "styleClass"); + } + private void doTest(boolean available) throws Exception { doTest(available, "text"); } diff --git a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/codeInsight/intentions/JavaFxCollapseSubTagToAttributeIntention.java b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/codeInsight/intentions/JavaFxCollapseSubTagToAttributeIntention.java index 1f772ba4765b..abf7ed5c2c36 100644 --- a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/codeInsight/intentions/JavaFxCollapseSubTagToAttributeIntention.java +++ b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/codeInsight/intentions/JavaFxCollapseSubTagToAttributeIntention.java @@ -19,11 +19,17 @@ import com.intellij.codeInsight.CodeInsightUtilBase; import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiElement; import com.intellij.psi.XmlElementFactory; -import com.intellij.psi.xml.*; +import com.intellij.psi.xml.XmlAttribute; +import com.intellij.psi.xml.XmlTag; +import com.intellij.psi.xml.XmlToken; +import com.intellij.psi.xml.XmlTokenType; +import com.intellij.util.Function; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; +import org.jetbrains.plugins.javaFX.fxml.FxmlConstants; import org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxClassBackedElementDescriptor; import org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyElementDescriptor; @@ -36,7 +42,23 @@ public class JavaFxCollapseSubTagToAttributeIntention extends PsiElementBaseInte public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException { if (!CodeInsightUtilBase.preparePsiElementsForWrite(element)) return; final XmlTag tag = (XmlTag)element.getParent(); - final XmlAttribute attribute = XmlElementFactory.getInstance(project).createXmlAttribute(tag.getName(), tag.getValue().getText().trim()); + final String value; + if (tag.getSubTags().length == 0) { + value = tag.getValue().getText().trim(); + } + else { + value = StringUtil.join(tag.getSubTags(), new Function() { + @Override + public String fun(XmlTag childTag) { + final XmlAttribute valueAttr = childTag.getAttribute(FxmlConstants.FX_VALUE); + if (valueAttr != null) { + return valueAttr.getValue(); + } + return ""; + } + }, ", "); + } + final XmlAttribute attribute = XmlElementFactory.getInstance(project).createXmlAttribute(tag.getName(), value); final XmlTag parentTag = tag.getParentTag(); parentTag.add(attribute); tag.delete(); @@ -46,15 +68,16 @@ public class JavaFxCollapseSubTagToAttributeIntention extends PsiElementBaseInte public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) { if (element instanceof XmlToken && ((XmlToken)element).getTokenType() == XmlTokenType.XML_NAME && element.getParent() instanceof XmlTag) { final XmlTag tag = (XmlTag)element.getParent(); - if (tag.getSubTags().length == 0) { - final XmlTag parentTag = tag.getParentTag(); - if (parentTag != null && - tag.getDescriptor() instanceof JavaFxPropertyElementDescriptor && - parentTag.getDescriptor() instanceof JavaFxClassBackedElementDescriptor) { - - setText("Collapse tag '" + tag.getName() + "' to attribute"); - return true; - } + for (XmlTag xmlTag : tag.getSubTags()) { + if (xmlTag.getAttribute(FxmlConstants.FX_VALUE) == null) return false; + } + final XmlTag parentTag = tag.getParentTag(); + if (parentTag != null && + tag.getDescriptor() instanceof JavaFxPropertyElementDescriptor && + parentTag.getDescriptor() instanceof JavaFxClassBackedElementDescriptor) { + + setText("Collapse tag '" + tag.getName() + "' to attribute"); + return true; } } return false; diff --git a/plugins/javaFX/testData/intentions/collapseToAttr/styleclass.fxml b/plugins/javaFX/testData/intentions/collapseToAttr/styleclass.fxml new file mode 100644 index 000000000000..fa6206448c57 --- /dev/null +++ b/plugins/javaFX/testData/intentions/collapseToAttr/styleclass.fxml @@ -0,0 +1,9 @@ + + + + + Class> + + + + \ No newline at end of file diff --git a/plugins/javaFX/testData/intentions/collapseToAttr/styleclass_after.fxml b/plugins/javaFX/testData/intentions/collapseToAttr/styleclass_after.fxml new file mode 100644 index 000000000000..4bcd223a6f63 --- /dev/null +++ b/plugins/javaFX/testData/intentions/collapseToAttr/styleclass_after.fxml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file