diff --git a/platform/core-api/src/com/intellij/lang/folding/CustomFoldingBuilder.java b/platform/core-api/src/com/intellij/lang/folding/CustomFoldingBuilder.java index fde4177b83ac..111b6cc85b82 100644 --- a/platform/core-api/src/com/intellij/lang/folding/CustomFoldingBuilder.java +++ b/platform/core-api/src/com/intellij/lang/folding/CustomFoldingBuilder.java @@ -32,6 +32,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; +import static com.intellij.lang.folding.CompositeFoldingBuilder.FOLDING_BUILDER; + /** * Builds custom folding regions. If custom folding is supported for a language, its FoldingBuilder must be inherited from this class. * @@ -97,6 +99,8 @@ public abstract class CustomFoldingBuilder extends FoldingBuilderEx implements P ASTNode startNode = localFoldingStack.pop(); int startOffset = startNode.getTextRange().getStartOffset(); TextRange range = new TextRange(startOffset, child.getTextRange().getEndOffset()); + startNode.getPsi().putUserData(FOLDING_BUILDER, this); // Required by CompositeFoldingBuilder when getting info from the original + // folding builder descriptors.add(new FoldingDescriptor(startNode, range)); Set nodeSet = ourCustomRegionElements.get(); nodeSet.add(startNode); diff --git a/platform/lang-impl/src/com/intellij/lang/customFolding/VisualStudioCustomFoldingProvider.java b/platform/lang-impl/src/com/intellij/lang/customFolding/VisualStudioCustomFoldingProvider.java index 8aea576691a7..b3bdd2578f81 100644 --- a/platform/lang-impl/src/com/intellij/lang/customFolding/VisualStudioCustomFoldingProvider.java +++ b/platform/lang-impl/src/com/intellij/lang/customFolding/VisualStudioCustomFoldingProvider.java @@ -24,7 +24,7 @@ import com.intellij.lang.folding.CustomFoldingProvider; public class VisualStudioCustomFoldingProvider extends CustomFoldingProvider { @Override public boolean isCustomRegionStart(String elementText) { - return elementText.contains("region") && elementText.matches("..?\\s*region.*"); + return elementText.contains("region") && elementText.matches("[/*#-]*\\s*region.*"); } @Override @@ -34,7 +34,7 @@ public class VisualStudioCustomFoldingProvider extends CustomFoldingProvider { @Override public String getPlaceholderText(String elementText) { - return elementText.replaceFirst("..?\\s*region(.*)","$1").trim(); + return elementText.replaceFirst("[/*#-]*\\s*region(.*)","$1").trim(); } @Override diff --git a/xml/tests/src/com/intellij/xml/XmlFoldingTest.java b/xml/tests/src/com/intellij/xml/XmlFoldingTest.java index 6edd92831436..27d8fe6132dc 100644 --- a/xml/tests/src/com/intellij/xml/XmlFoldingTest.java +++ b/xml/tests/src/com/intellij/xml/XmlFoldingTest.java @@ -35,6 +35,8 @@ public class XmlFoldingTest extends LightPlatformCodeInsightFixtureTestCase { public void testDataUri() throws Throwable { doTest(); } + public void testCustomRegions() throws Throwable { doTest(); } + private void doTest() throws Throwable { doTest(".html"); } diff --git a/xml/tests/testData/folding/customRegions.html b/xml/tests/testData/folding/customRegions.html new file mode 100644 index 000000000000..a8c37734cdc2 --- /dev/null +++ b/xml/tests/testData/folding/customRegions.html @@ -0,0 +1,15 @@ +> +> +> + First + > + +> + Second + > +> + Third + > + +> +> \ No newline at end of file diff --git a/xml/xml-psi-impl/src/com/intellij/lang/XmlCodeFoldingBuilder.java b/xml/xml-psi-impl/src/com/intellij/lang/XmlCodeFoldingBuilder.java index 20afcf681f10..98af12fea500 100644 --- a/xml/xml-psi-impl/src/com/intellij/lang/XmlCodeFoldingBuilder.java +++ b/xml/xml-psi-impl/src/com/intellij/lang/XmlCodeFoldingBuilder.java @@ -16,6 +16,7 @@ package com.intellij.lang; +import com.intellij.lang.folding.CustomFoldingBuilder; import com.intellij.lang.folding.FoldingBuilder; import com.intellij.lang.folding.FoldingDescriptor; import com.intellij.lang.folding.LanguageFolding; @@ -40,17 +41,18 @@ import com.intellij.xml.util.XmlTagUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; import java.util.List; -public abstract class XmlCodeFoldingBuilder implements FoldingBuilder, DumbAware { +public abstract class XmlCodeFoldingBuilder extends CustomFoldingBuilder implements DumbAware { private static final TokenSet XML_ATTRIBUTE_SET = TokenSet.create(XmlElementType.XML_ATTRIBUTE); private static final int MIN_TEXT_RANGE_LENGTH = 3; @Override - @NotNull - public FoldingDescriptor[] buildFoldRegions(@NotNull ASTNode node, @NotNull Document document) { - final PsiElement psiElement = node.getPsi(); + public void buildLanguageFoldRegions(@NotNull List foldings, + @NotNull PsiElement psiElement, + @NotNull Document document, + boolean quick + ) { XmlDocument xmlDocument = null; if (psiElement instanceof XmlFile) { @@ -65,15 +67,11 @@ public abstract class XmlCodeFoldingBuilder implements FoldingBuilder, DumbAware if (rootTag == null) { rootTag = xmlDocument; } - List foldings = null; if (rootTag != null) { - foldings = new ArrayList<>(); doAddForChildren(xmlDocument, foldings, document); } - - return foldings != null ? foldings.toArray(new FoldingDescriptor[foldings.size()]):FoldingDescriptor.EMPTY; } protected void addElementsToFold(List foldings, XmlElement tag, Document document) { @@ -224,7 +222,7 @@ public abstract class XmlCodeFoldingBuilder implements FoldingBuilder, DumbAware } @Override - public String getPlaceholderText(@NotNull ASTNode node) { + public String getLanguagePlaceholderText(@NotNull ASTNode node, @NotNull TextRange range) { final PsiElement psi = node.getPsi(); if (psi instanceof XmlAttribute && "src".equalsIgnoreCase(((XmlAttribute)psi).getName())) { return "data:"; @@ -265,7 +263,7 @@ public abstract class XmlCodeFoldingBuilder implements FoldingBuilder, DumbAware } @Override - public boolean isCollapsedByDefault(@NotNull ASTNode node) { + public boolean isRegionCollapsedByDefault(@NotNull ASTNode node) { final PsiElement psi = node.getPsi(); final XmlCodeFoldingSettings foldingSettings = getFoldingSettings(); return (psi instanceof XmlTag && foldingSettings.isCollapseXmlTags()) @@ -293,4 +291,14 @@ public abstract class XmlCodeFoldingBuilder implements FoldingBuilder, DumbAware } protected abstract XmlCodeFoldingSettings getFoldingSettings(); + + @Override + protected boolean isCustomFoldingRoot(@NotNull ASTNode node) { + return node.getElementType() == XmlElementType.XML_TAG; + } + + @Override + protected boolean isCustomFoldingCandidate(@NotNull ASTNode node) { + return node.getElementType() == XmlTokenType.XML_COMMENT_CHARACTERS; + } }