mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-93649 Custom regions in XML
This commit is contained in:
@@ -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<ASTNode> nodeSet = ourCustomRegionElements.get();
|
||||
nodeSet.add(startNode);
|
||||
|
||||
+2
-2
@@ -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
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
<html<fold text='...'>>
|
||||
<body<fold text='...'>>
|
||||
<div<fold text='...'>>
|
||||
First
|
||||
</div</fold>>
|
||||
<!--<fold text='Description'>region Description-->
|
||||
<div<fold text='...'>>
|
||||
Second
|
||||
</div</fold>>
|
||||
<div<fold text='...'>>
|
||||
Third
|
||||
</div</fold>>
|
||||
<!--endregion</fold>-->
|
||||
</body</fold>>
|
||||
</html</fold>>
|
||||
@@ -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<FoldingDescriptor> 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<FoldingDescriptor> 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<FoldingDescriptor> 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user