fold entities to their char value

#WEB-422 fixed
This commit is contained in:
Dennis Ushakov
2016-04-29 21:24:34 +03:00
parent 1940ef659f
commit 4b004d3b92
7 changed files with 58 additions and 7 deletions
@@ -371,6 +371,7 @@ checkbox.show.tabs.tooltips=Show tabs tooltips
group.code.folding=Code Folding
checkbox.collapse.xml.tags=XML tags
checkbox.collapse.html.style.attribute=HTML 'style' attribute
checkbox.collapse.entities=XML entities
checkbox.collapse.anonymous.classes=<html>Anonymous classes</html>
checkbox.collapse.closures=<html>"Closures" (anonymous classes implementing one method, before Java 8)</html>
checkbox.collapse.generic.constructor.parameters=<html>Generic constructor and method parameters</html>
@@ -28,5 +28,6 @@ public class XmlCodeFoldingOptionsProvider extends BeanConfigurable<XmlFoldingSe
super(XmlFoldingSettings.getInstance());
checkBox("COLLAPSE_XML_TAGS", ApplicationBundle.message("checkbox.collapse.xml.tags"));
checkBox("COLLAPSE_HTML_STYLE_ATTRIBUTE", ApplicationBundle.message("checkbox.collapse.html.style.attribute"));
checkBox("COLLAPSE_ENTITIES", ApplicationBundle.message("checkbox.collapse.entities"));
}
}
@@ -15,7 +15,6 @@
*/
package com.intellij.xml;
import com.intellij.testFramework.IdeaTestCase;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase;
@@ -32,7 +31,7 @@ public class XmlFoldingTest extends LightPlatformCodeInsightFixtureTestCase {
public void testStyleAttributeFolding() throws Throwable { doTest(); }
public void testStyleAttributeFolding2() throws Throwable { doTest(".xml"); }
public void testEntities() throws Throwable { doTest(); }
private void doTest() throws Throwable {
doTest(".html");
+10
View File
@@ -0,0 +1,10 @@
<!doctype html>
<html lang="en"<fold text='...'>>
<head<fold text='...'>>
<meta charset="UTF-8">
<title>Document</title>
</head</fold>>
<body<fold text='...'>>
<fold text='©'>&copy;</fold><fold text='<'>&lt;</fold>
</body</fold>>
</html</fold>>
@@ -3,4 +3,5 @@ package com.intellij.lang;
public interface XmlCodeFoldingSettings {
boolean isCollapseXmlTags();
boolean isCollapseHtmlStyleAttribute();
boolean isCollapseEntities();
}
@@ -45,12 +45,21 @@ public class XmlFoldingSettings implements XmlCodeFoldingSettings, PersistentSta
return COLLAPSE_HTML_STYLE_ATTRIBUTE;
}
public boolean isCollapseEntities() {
return COLLAPSE_ENTITIES;
}
public void setCollapseEntities(boolean COLLAPSE_ENTITIES) {
this.COLLAPSE_ENTITIES = COLLAPSE_ENTITIES;
}
public void setCollapseHtmlStyleAttribute(boolean value) {
this.COLLAPSE_HTML_STYLE_ATTRIBUTE = value;
}
@SuppressWarnings({"WeakerAccess"}) public boolean COLLAPSE_XML_TAGS = false;
@SuppressWarnings({"WeakerAccess"}) public boolean COLLAPSE_HTML_STYLE_ATTRIBUTE = true;
@SuppressWarnings({"WeakerAccess"}) public boolean COLLAPSE_ENTITIES = true;
@Override
public XmlFoldingSettings getState() {
@@ -25,9 +25,12 @@ import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.UnfairTextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.impl.source.html.HtmlFileImpl;
import com.intellij.psi.impl.source.xml.XmlEntityRefImpl;
import com.intellij.psi.impl.source.xml.XmlTokenImpl;
import com.intellij.psi.tree.TokenSet;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.psi.xml.*;
@@ -96,12 +99,12 @@ public abstract class XmlCodeFoldingBuilder implements FoldingBuilder, DumbAware
for (PsiElement grandChild : grandChildren) {
ProgressManager.checkCanceled();
if (grandChild instanceof XmlComment) {
if (grandChild instanceof XmlComment || isEntity(grandChild)) {
addToFold(foldings, grandChild, document);
}
}
}
else if(child instanceof XmlAttribute && isAttributeShouldBeFolded((XmlAttribute)child)) {
else if (isEntity(child) || child instanceof XmlAttribute && isAttributeShouldBeFolded((XmlAttribute)child)) {
addToFold(foldings, child, document);
}
else {
@@ -180,6 +183,9 @@ public abstract class XmlCodeFoldingBuilder implements FoldingBuilder, DumbAware
final XmlAttributeValue valueElement = ((XmlAttribute)element).getValueElement();
return valueElement != null ? valueElement.getValueTextRange() : null;
}
else if (isEntity(element)) {
return element.getTextRange();
}
else {
return null;
}
@@ -205,8 +211,9 @@ public abstract class XmlCodeFoldingBuilder implements FoldingBuilder, DumbAware
int startLine = document.getLineNumber(range.getStartOffset());
int endLine = document.getLineNumber(range.getEndOffset() - 1);
if (startLine < endLine || elementToFold instanceof XmlAttribute) {
if (range.getStartOffset() + MIN_TEXT_RANGE_LENGTH < range.getEndOffset()) {
final boolean entity = isEntity(elementToFold);
if (startLine < endLine || elementToFold instanceof XmlAttribute || entity) {
if (range.getStartOffset() + MIN_TEXT_RANGE_LENGTH < range.getEndOffset() || entity) {
foldings.add(new FoldingDescriptor(elementToFold.getNode(), range));
return true;
}
@@ -224,6 +231,23 @@ public abstract class XmlCodeFoldingBuilder implements FoldingBuilder, DumbAware
psi instanceof XmlAttribute ||
psi instanceof XmlConditionalSection
) return "...";
if (isEntity(psi)) {
final XmlEntityDecl resolve = XmlEntityRefImpl.resolveEntity((XmlElement)psi, psi.getText(), psi.getContainingFile());
final XmlAttributeValue value = resolve != null ? resolve.getValueElement() : null;
if (value != null) {
return getEntityValue(value);
}
}
return null;
}
private static String getEntityValue(XmlAttributeValue value) {
final String result = value.getValue();
final int i = result.indexOf('#');
if (i > 0) {
final int charNum = StringUtil.parseInt(StringUtil.trimEnd(result.substring(i + 1), ";"), -1);
return charNum >= 0 ? String.valueOf((char)charNum) : null;
}
return null;
}
@@ -232,7 +256,13 @@ public abstract class XmlCodeFoldingBuilder implements FoldingBuilder, DumbAware
final PsiElement psi = node.getPsi();
final XmlCodeFoldingSettings foldingSettings = getFoldingSettings();
return (psi instanceof XmlTag && foldingSettings.isCollapseXmlTags())
|| (psi instanceof XmlAttribute && foldingSettings.isCollapseHtmlStyleAttribute());
|| (psi instanceof XmlAttribute && foldingSettings.isCollapseHtmlStyleAttribute())
|| isEntity(psi) && foldingSettings.isCollapseEntities();
}
protected boolean isEntity(PsiElement psi) {
return psi instanceof XmlEntityRef ||
(psi instanceof XmlTokenImpl && ((XmlTokenImpl)psi).getElementType() == XmlTokenType.XML_CHAR_ENTITY_REF);
}
private static boolean isAttributeShouldBeFolded(XmlAttribute child) {