generate attributes with proper file type to generate correct attributes in HTML

This commit is contained in:
Dennis Ushakov
2015-12-23 16:17:01 +03:00
parent 2abed8dc94
commit a3885d858c
4 changed files with 30 additions and 4 deletions
@@ -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 <code>name</code> or <code>value</code> are not valid.
*/
@NotNull
public abstract XmlAttribute createAttribute(@NotNull @NonNls String name, @NotNull String value, @Nullable PsiElement context) throws IncorrectOperationException;
}
@@ -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, "\"", "&quot;");
}
final XmlDocument document = createXmlDocument("<tag " + name + "=" + quoteChar + value + quoteChar + "/>", "dummy.xml",
XmlFileType.INSTANCE);
final XmlDocument document = createXmlDocument("<tag " + name + "=" + quoteChar + value + quoteChar + "/>", "dummy.xml", fileType);
XmlTag tag = document.getRootTag();
assert tag != null;
XmlAttribute[] attributes = tag.getAttributes();
@@ -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) {
@@ -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;
}