From 21a9c1bb66cbb738c472bd51b27f640336a9ffc2 Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Fri, 3 Aug 2012 15:56:43 +0400 Subject: [PATCH] Xml folding tests --- .../intellij/lang/xml/XmlFoldingBuilder.java | 18 ++++--- .../src/com/intellij/xml/XmlFoldingTest.java | 54 +++++++++++++++++++ .../folding/styleAttributeFolding.html | 5 ++ xml/tests/testData/folding/tagFolding.html | 13 +++++ 4 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 xml/tests/src/com/intellij/xml/XmlFoldingTest.java create mode 100644 xml/tests/testData/folding/styleAttributeFolding.html create mode 100644 xml/tests/testData/folding/tagFolding.html diff --git a/xml/impl/src/com/intellij/lang/xml/XmlFoldingBuilder.java b/xml/impl/src/com/intellij/lang/xml/XmlFoldingBuilder.java index a51fc7f5b4c9..5a303c906ae0 100644 --- a/xml/impl/src/com/intellij/lang/xml/XmlFoldingBuilder.java +++ b/xml/impl/src/com/intellij/lang/xml/XmlFoldingBuilder.java @@ -180,7 +180,8 @@ public class XmlFoldingBuilder implements FoldingBuilder, DumbAware { } } else if (element instanceof XmlAttribute) { - return element.getTextRange(); + final XmlAttributeValue valueElement = ((XmlAttribute)element).getValueElement(); + return valueElement != null ? valueElement.getValueTextRange() : null; } else { return null; @@ -205,9 +206,13 @@ public class XmlFoldingBuilder implements FoldingBuilder, DumbAware { range.getEndOffset() <= document.getTextLength() // psi and document maybe not in sync after error ) { - if (range.getStartOffset() + MIN_TEXT_RANGE_LENGTH < range.getEndOffset()) { - foldings.add(new FoldingDescriptor(elementToFold.getNode(), range)); - return true; + 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()) { + foldings.add(new FoldingDescriptor(elementToFold.getNode(), range)); + return true; + } } } @@ -216,12 +221,9 @@ public class XmlFoldingBuilder implements FoldingBuilder, DumbAware { public String getPlaceholderText(@NotNull ASTNode node) { final PsiElement psi = node.getPsi(); - if(psi instanceof XmlAttribute) { - final String attributeName = ((XmlAttribute)psi).getName(); - return attributeName.isEmpty() ? "..." : attributeName; - } if (psi instanceof XmlTag || psi instanceof XmlComment || + psi instanceof XmlAttribute || psi instanceof XmlConditionalSection ) return "..."; return null; diff --git a/xml/tests/src/com/intellij/xml/XmlFoldingTest.java b/xml/tests/src/com/intellij/xml/XmlFoldingTest.java new file mode 100644 index 000000000000..2f76f1706c4a --- /dev/null +++ b/xml/tests/src/com/intellij/xml/XmlFoldingTest.java @@ -0,0 +1,54 @@ +/* + * Copyright 2000-2012 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.xml; + +import com.intellij.testFramework.IdeaTestCase; +import com.intellij.testFramework.PlatformTestUtil; +import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase; + +import java.io.File; + +/** + * User: zolotov + * Date: 8/3/12 + */ +public class XmlFoldingTest extends LightPlatformCodeInsightFixtureTestCase { + + public void testTagFolding() throws Throwable { doTest(); } + + public void testStyleAttributeFolding() throws Throwable { doTest(); } + + + + @SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors") + public XmlFoldingTest() { + IdeaTestCase.initPlatformPrefix(); + } + + private void doTest() throws Throwable { + myFixture.testFolding(getTestDataPath() + getTestName(true) + ".html"); + } + + @Override + protected String getBasePath() { + return "/xml/tests/testData/folding/"; + } + + @Override + protected String getTestDataPath() { + return PlatformTestUtil.getCommunityPath().replace(File.separatorChar, '/') + getBasePath(); + } +} diff --git a/xml/tests/testData/folding/styleAttributeFolding.html b/xml/tests/testData/folding/styleAttributeFolding.html new file mode 100644 index 000000000000..92b0dfc32bd1 --- /dev/null +++ b/xml/tests/testData/folding/styleAttributeFolding.html @@ -0,0 +1,5 @@ +> +> + +> +> \ No newline at end of file diff --git a/xml/tests/testData/folding/tagFolding.html b/xml/tests/testData/folding/tagFolding.html new file mode 100644 index 000000000000..2ae5365d3210 --- /dev/null +++ b/xml/tests/testData/folding/tagFolding.html @@ -0,0 +1,13 @@ + +> + > + Title + > + > +

this is a

+ > + test + > + > +> \ No newline at end of file