From cfd7dac7a72d13dc339046a7df890a65fcab3605 Mon Sep 17 00:00:00 2001 From: Piotr Tomiak Date: Fri, 24 Aug 2018 16:17:33 +0200 Subject: [PATCH] Angular 2 Templates - properly update and create attributes --- .../intellij/psi/XmlElementFactoryImpl.java | 39 ++++++++++++++----- .../psi/impl/source/xml/XmlAttributeImpl.java | 4 +- .../source/xml/XmlAttributeValueImpl.java | 4 +- 3 files changed, 36 insertions(+), 11 deletions(-) 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 d99798af9446..62031afcc792 100644 --- a/xml/xml-psi-impl/src/com/intellij/psi/XmlElementFactoryImpl.java +++ b/xml/xml-psi-impl/src/com/intellij/psi/XmlElementFactoryImpl.java @@ -20,6 +20,7 @@ import com.intellij.ide.highlighter.XHtmlFileType; import com.intellij.ide.highlighter.XmlFileType; import com.intellij.lang.ASTFactory; import com.intellij.lang.Language; +import com.intellij.lang.html.HTMLLanguage; import com.intellij.lang.xml.XMLLanguage; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.fileTypes.FileType; @@ -49,10 +50,8 @@ public class XmlElementFactoryImpl extends XmlElementFactory { @Override @NotNull public XmlTag createTagFromText(@NotNull @NonNls CharSequence text, @NotNull Language language) throws IncorrectOperationException { - assert language instanceof XMLLanguage:"Tag can be created only for xml language"; - FileType type = language.getAssociatedFileType(); - if (type == null) type = XmlFileType.INSTANCE; - final XmlDocument document = createXmlDocument(text, "dummy."+ type.getDefaultExtension(), type); + final FileType type = getFileType(language); + final XmlDocument document = createXmlDocument(text, "dummy." + type.getDefaultExtension(), type); final XmlTag tag = document.getRootTag(); if (tag == null) throw new IncorrectOperationException("Incorrect tag text"); return tag; @@ -72,8 +71,9 @@ public class XmlElementFactoryImpl extends XmlElementFactory { @NotNull @Override - public XmlAttribute createAttribute(@NotNull @NonNls String name, @NotNull String value, @Nullable PsiElement context) throws IncorrectOperationException { - return createAttribute(name, value, PsiTreeUtil.getParentOfType(context, XmlTag.class, false) instanceof HtmlTag ? HtmlFileType.INSTANCE : XmlFileType.INSTANCE); + public XmlAttribute createAttribute(@NotNull @NonNls String name, @NotNull String value, @Nullable PsiElement context) + throws IncorrectOperationException { + return createAttribute(name, value, getFileType(context)); } @NotNull @@ -81,13 +81,16 @@ public class XmlElementFactoryImpl extends XmlElementFactory { final char quoteChar; if (!value.contains("\"")) { quoteChar = '"'; - } else if (!value.contains("'")) { + } + else if (!value.contains("'")) { quoteChar = '\''; - } else { + } + else { quoteChar = '"'; value = StringUtil.replace(value, "\"", """); } - final XmlDocument document = createXmlDocument("", "dummy.xml", fileType); + final XmlDocument document = createXmlDocument("", + "dummy." + fileType.getDefaultExtension(), fileType); XmlTag tag = document.getRootTag(); assert tag != null; XmlAttribute[] attributes = tag.getAttributes(); @@ -138,5 +141,23 @@ public class XmlElementFactoryImpl extends XmlElementFactory { return document; } + @NotNull + private static FileType getFileType(@Nullable PsiElement context) { + if (context == null) { + return XmlFileType.INSTANCE; + } + if (context.getLanguage().isKindOf(HTMLLanguage.INSTANCE)) { + return getFileType(context.getLanguage()); + } + return PsiTreeUtil.getParentOfType(context, XmlTag.class, false) instanceof HtmlTag + ? HtmlFileType.INSTANCE : XmlFileType.INSTANCE; + } + + private static FileType getFileType(@NotNull Language language) { + assert language instanceof XMLLanguage : "Tag can be created only for xml language"; + FileType type = language.getAssociatedFileType(); + return type == null ? XmlFileType.INSTANCE : type; + } + private static final Logger LOG = Logger.getInstance(XmlElementFactoryImpl.class); } 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 9bf58b37c923..018022bd22c7 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 @@ -24,6 +24,7 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.ModificationTracker; import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.TextRange; +import com.intellij.openapi.util.text.StringUtil; import com.intellij.pom.PomManager; import com.intellij.pom.PomModel; import com.intellij.pom.event.PomModelEvent; @@ -101,7 +102,8 @@ public class XmlAttributeImpl extends XmlElementImpl implements XmlAttribute, Hi public void setValue(String valueText) throws IncorrectOperationException { final ASTNode value = XmlChildRole.ATTRIBUTE_VALUE_FINDER.findChild(this); final PomModel model = PomManager.getModel(getProject()); - final XmlAttribute attribute = XmlElementFactory.getInstance(getProject()).createAttribute("a", valueText, this); + final XmlAttribute attribute = XmlElementFactory.getInstance(getProject()).createAttribute( + StringUtil.defaultIfEmpty(getName(), "a"), valueText, this); final ASTNode newValue = XmlChildRole.ATTRIBUTE_VALUE_FINDER.findChild((ASTNode)attribute); final XmlAspect aspect = model.getModelAspect(XmlAspect.class); model.runTransaction(new PomTransactionBase(this, aspect) { diff --git a/xml/xml-psi-impl/src/com/intellij/psi/impl/source/xml/XmlAttributeValueImpl.java b/xml/xml-psi-impl/src/com/intellij/psi/impl/source/xml/XmlAttributeValueImpl.java index 9a5949ea73d5..e6efafdf8153 100644 --- a/xml/xml-psi-impl/src/com/intellij/psi/impl/source/xml/XmlAttributeValueImpl.java +++ b/xml/xml-psi-impl/src/com/intellij/psi/impl/source/xml/XmlAttributeValueImpl.java @@ -137,7 +137,9 @@ public class XmlAttributeValueImpl extends XmlElementImpl implements XmlAttribut final String quoteChar = getTextLength() > 0 ? getText().substring(0, 1) : ""; String contents = StringUtil.containsAnyChar(quoteChar, "'\"") ? StringUtil.trimEnd(StringUtil.trimStart(text, quoteChar), quoteChar) : text; - XmlAttribute newAttribute = XmlElementFactory.getInstance(getProject()).createAttribute("q", contents, this); + XmlAttribute newAttribute = XmlElementFactory.getInstance(getProject()).createAttribute( + StringUtil.defaultIfEmpty((getParent() instanceof XmlAttribute) ? ((XmlAttribute)getParent()).getName() : null, "q"), + contents, this); XmlAttributeValue newValue = newAttribute.getValueElement(); CheckUtil.checkWritable(this);