From a3885d858c8d3511b9bf9e994d110db3101de630 Mon Sep 17 00:00:00 2001 From: Dennis Ushakov Date: Wed, 23 Dec 2015 16:07:35 +0300 Subject: [PATCH] generate attributes with proper file type to generate correct attributes in HTML --- .../src/com/intellij/psi/XmlElementFactory.java | 13 +++++++++++++ .../com/intellij/psi/XmlElementFactoryImpl.java | 17 +++++++++++++++-- .../psi/impl/source/xml/XmlAttributeImpl.java | 2 +- .../psi/impl/source/xml/XmlTagImpl.java | 2 +- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/xml/xml-psi-api/src/com/intellij/psi/XmlElementFactory.java b/xml/xml-psi-api/src/com/intellij/psi/XmlElementFactory.java index de913eb543d3..93493c8a3a51 100644 --- a/xml/xml-psi-api/src/com/intellij/psi/XmlElementFactory.java +++ b/xml/xml-psi-api/src/com/intellij/psi/XmlElementFactory.java @@ -24,6 +24,7 @@ import com.intellij.psi.xml.XmlText; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * @author Dmitry Avdeev @@ -97,4 +98,16 @@ public abstract class XmlElementFactory { */ @NotNull public abstract XmlAttribute createXmlAttribute(@NotNull @NonNls String name, @NotNull String value) throws IncorrectOperationException; + + /** + * Creates an attribute with the specified name and value with given context. + * + * @param name the name of the attribute to create. + * @param value the value of the attribute to create. + * @param context element which can be used to determine created attribute file type. + * @return the created attribute instance. + * @throws IncorrectOperationException if either name or value are not valid. + */ + @NotNull + public abstract XmlAttribute createAttribute(@NotNull @NonNls String name, @NotNull String value, @Nullable PsiElement context) throws IncorrectOperationException; } diff --git a/xml/xml-psi-impl/src/com/intellij/psi/XmlElementFactoryImpl.java b/xml/xml-psi-impl/src/com/intellij/psi/XmlElementFactoryImpl.java index 20abde77589e..47d7f9d53f23 100644 --- a/xml/xml-psi-impl/src/com/intellij/psi/XmlElementFactoryImpl.java +++ b/xml/xml-psi-impl/src/com/intellij/psi/XmlElementFactoryImpl.java @@ -18,6 +18,7 @@ package com.intellij.psi; import com.intellij.ide.highlighter.HtmlFileType; import com.intellij.ide.highlighter.XHtmlFileType; import com.intellij.ide.highlighter.XmlFileType; +import com.intellij.ide.highlighter.XmlLikeFileType; import com.intellij.lang.ASTFactory; import com.intellij.lang.Language; import com.intellij.lang.xml.XMLLanguage; @@ -31,6 +32,7 @@ import com.intellij.util.IncorrectOperationException; import com.intellij.xml.util.XmlTagUtil; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * @author Dmitry Avdeev @@ -64,6 +66,18 @@ public class XmlElementFactoryImpl extends XmlElementFactory { @Override @NotNull public XmlAttribute createXmlAttribute(@NotNull String name, @NotNull String value) throws IncorrectOperationException { + return createAttribute(name, value, XmlFileType.INSTANCE); + } + + @NotNull + @Override + public XmlAttribute createAttribute(@NotNull @NonNls String name, @NotNull String value, @Nullable PsiElement context) throws IncorrectOperationException { + final FileType type = context != null ? context.getContainingFile().getFileType() : null; + return createAttribute(name, value, type instanceof XmlLikeFileType ? type : XmlFileType.INSTANCE); + } + + @NotNull + private XmlAttribute createAttribute(@NotNull String name, @NotNull String value, @NotNull FileType fileType) { final char quoteChar; if (!value.contains("\"")) { quoteChar = '"'; @@ -73,8 +87,7 @@ public class XmlElementFactoryImpl extends XmlElementFactory { quoteChar = '"'; value = StringUtil.replace(value, "\"", """); } - final XmlDocument document = createXmlDocument("", "dummy.xml", - XmlFileType.INSTANCE); + final XmlDocument document = createXmlDocument("", "dummy.xml", fileType); XmlTag tag = document.getRootTag(); assert tag != null; XmlAttribute[] attributes = tag.getAttributes(); diff --git a/xml/xml-psi-impl/src/com/intellij/psi/impl/source/xml/XmlAttributeImpl.java b/xml/xml-psi-impl/src/com/intellij/psi/impl/source/xml/XmlAttributeImpl.java index 375c0488e6cc..14beaf05ad89 100644 --- a/xml/xml-psi-impl/src/com/intellij/psi/impl/source/xml/XmlAttributeImpl.java +++ b/xml/xml-psi-impl/src/com/intellij/psi/impl/source/xml/XmlAttributeImpl.java @@ -310,7 +310,7 @@ public class XmlAttributeImpl extends XmlElementImpl implements XmlAttribute, Hi final ASTNode name = XmlChildRole.ATTRIBUTE_NAME_FINDER.findChild(this); final String oldName = name.getText(); final PomModel model = PomManager.getModel(getProject()); - final XmlAttribute attribute = XmlElementFactory.getInstance(getProject()).createXmlAttribute(nameText, ""); + final XmlAttribute attribute = XmlElementFactory.getInstance(getProject()).createAttribute(nameText, "", this); final ASTNode newName = XmlChildRole.ATTRIBUTE_NAME_FINDER.findChild((ASTNode)attribute); final XmlAspect aspect = model.getModelAspect(XmlAspect.class); model.runTransaction(new PomTransactionBase(getParent(), aspect) { diff --git a/xml/xml-psi-impl/src/com/intellij/psi/impl/source/xml/XmlTagImpl.java b/xml/xml-psi-impl/src/com/intellij/psi/impl/source/xml/XmlTagImpl.java index 986ff7540a96..10e7fb1d7bb8 100644 --- a/xml/xml-psi-impl/src/com/intellij/psi/impl/source/xml/XmlTagImpl.java +++ b/xml/xml-psi-impl/src/com/intellij/psi/impl/source/xml/XmlTagImpl.java @@ -1021,7 +1021,7 @@ public class XmlTagImpl extends XmlElementImpl implements XmlTag, HintedReferenc return null; } else { - PsiElement xmlAttribute = add(XmlElementFactory.getInstance(getProject()).createXmlAttribute(qname, value)); + PsiElement xmlAttribute = add(XmlElementFactory.getInstance(getProject()).createAttribute(qname, value, this)); while (!(xmlAttribute instanceof XmlAttribute)) xmlAttribute = xmlAttribute.getNextSibling(); return (XmlAttribute)xmlAttribute; }