Angular 2 Templates - properly update and create attributes

This commit is contained in:
Piotr Tomiak
2018-09-07 10:06:29 +02:00
parent 9d39f7300e
commit cfd7dac7a7
3 changed files with 36 additions and 11 deletions
@@ -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("<tag " + name + "=" + quoteChar + value + quoteChar + "/>", "dummy.xml", fileType);
final XmlDocument document = createXmlDocument("<tag " + name + "=" + quoteChar + value + quoteChar + "/>",
"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);
}
@@ -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) {
@@ -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);