[live-templates] Enable "HTML/XML/T" template on XML tags

It was explicitly enabled on text elements only but with dummy identifier it was available in much more contexts like any element (even unbalanced). After removing dummy element for surround, this caused failure of the SurroundWithTest.testSurroundWithTagFirstElement test, which looks logical, as that test doesn't surround a text. Now a separate 'XML tag' context is created that also matches any balanced tag.

GitOrigin-RevId: 347ae75073de7e5eb065762e1b7b8b7bd9a5a346
This commit is contained in:
Tagir Valeev
2020-10-16 12:47:59 +07:00
committed by intellij-monorepo-bot
parent fa8b36629f
commit 8a543ca0bd
5 changed files with 61 additions and 2 deletions

View File

@@ -339,6 +339,7 @@
<liveTemplateContext implementation="com.intellij.codeInsight.template.XslTextContextType"/>
<liveTemplateContext implementation="com.intellij.codeInsight.template.XmlContextType"/>
<liveTemplateContext implementation="com.intellij.codeInsight.template.XmlTextContextType"/>
<liveTemplateContext implementation="com.intellij.codeInsight.template.XmlElementContextType"/>
<errorQuickFixProvider implementation="com.intellij.codeInsight.daemon.impl.analysis.XmlErrorQuickFixProvider"/>

View File

@@ -10,6 +10,7 @@
<option name="JAVA_STRING" value="false" />
<option name="XML" value="false" />
<option name="XML_TEXT" value="true" />
<option name="XML_TAG" value="true" />
<option name="HTML" value="true" />
<option name="JSX_HTML" value="true" />
<option name="JSP" value="true" />

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.template;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlTag;
import com.intellij.xml.XmlBundle;
import org.jetbrains.annotations.NotNull;
public class XmlElementContextType extends TemplateContextType {
public XmlElementContextType() {
super("XML_TAG", XmlBundle.message("xml.tag"), XmlContextType.class);
}
@Override
public boolean isInContext(@NotNull TemplateActionContext templateActionContext) {
PsiFile file = templateActionContext.getFile();
int startOffset = templateActionContext.getStartOffset();
int endOffset = templateActionContext.getEndOffset();
if (endOffset <= startOffset) return false;
if (!XmlContextType.isInXml(file, startOffset)) return false;
PsiElement start = file.findElementAt(startOffset);
PsiElement end = file.findElementAt(endOffset - 1);
if (start instanceof PsiWhiteSpace) {
start = start.getNextSibling();
}
if (end instanceof PsiWhiteSpace) {
end = end.getPrevSibling();
}
if (start == null || end == null) return false;
PsiElement parent = PsiTreeUtil.findCommonParent(start, end);
if (!(parent instanceof XmlTag)) return false;
TextRange range = parent.getTextRange();
return range.getStartOffset() >= startOffset && range.getEndOffset() <= endOffset;
}
}

View File

@@ -34,7 +34,9 @@ public class XmlTextContextType extends TemplateContextType {
}
@Override
public boolean isInContext(@NotNull PsiFile file, int offset) {
public boolean isInContext(@NotNull TemplateActionContext templateActionContext) {
PsiFile file = templateActionContext.getFile();
int offset = templateActionContext.getStartOffset();
if (!XmlContextType.isInXml(file, offset)) return false;
PsiElement element = file.findElementAt(offset);
if (element == null) return false;

View File

@@ -228,7 +228,8 @@ choose.schema.file=Choose Schema File
convert.text.to.cdata=Convert text to CData
convert.cdata.to.text=Convert CDATA to text
no.errors.found=No errors found
xml.text=XML Text
xml.text=XML text
xml.tag=XML tag
could.not.save.generated.xml.document.0=Could not save generated XML document: {0}
change.template.data.language=Change template data language
xml.actions=XML Actions