[formatter tests] migrating old formatter engine tests

This commit is contained in:
Yaroslav Lepenkin
2016-11-25 20:57:30 +03:00
parent c3c2827c52
commit f31db1b95d
21 changed files with 425 additions and 529 deletions
@@ -1,19 +0,0 @@
<Block start="0" end="23">
<Block start="0" end="3">
<Indent type="NONE" count="1" spaces="0"/>
</Block>
<Block start="4" end="7">
<Indent type="NONE" count="1" spaces="0"/>
</Block>
<Block start="8" end="23">
<Indent type="NONE" count="1" spaces="0"/>
<Block start="8" end="11"><Indent type="NORMAL" count="1" spaces="0"/></Block>
<Block start="12" end="15"><Indent type="NORMAL" count="1" spaces="0"/></Block>
<Block start="16" end="23">
<Indent type="NORMAL" count="1" spaces="0"/>
<Block start="16" end="19"><Indent type="LABEL" count="1" spaces="0"/></Block>
<Block start="20" end="23"><Indent type="LABEL" count="1" spaces="0"/></Block>
</Block>
</Block>
</Block>
@@ -1,19 +0,0 @@
<Block start="0" end="23">
<Block start="0" end="3"></Block>
<Block start="4" end="7">
<Alignment id="1"/>
</Block>
<Block start="8" end="23">
<Alignment id="1"/>
<Block start="8" end="11"><Indent type="LABEL" count="1" spaces="0"/></Block>
<Block start="12" end="15"><Indent type="LABEL" count="1" spaces="0"/></Block>
<Block start="16" end="23">
<Indent type="LABEL" count="1" spaces="0"/>
<Alignment id="1"/>
<Block start="16" end="19"></Block>
<Block start="20" end="23">
<Alignment id="1"/>
</Block>
</Block>
</Block>
</Block>
@@ -1,18 +0,0 @@
<Block start="0" end="23">
<Block start="0" end="3"/>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="4" end="7"/>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100" keepLineBreaks="true"/>
<Block start="8" end="23">
<Block start="8" end="11"><Indent type="NORMAL" count="1" spaces="0"/></Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100" keepLineBreaks="true"/>
<Block start="12" end="15"><Indent type="NORMAL" count="1" spaces="0"/></Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100" keepLineBreaks="true"/>
<Block start="16" end="23">
<Indent type="NORMAL" count="1" spaces="0"/>
<Block start="16" end="19"><Indent type="LABEL" count="1" spaces="0"/></Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="2" maxlinefeeds="100" keepLineBreaks="true"/>
<Block start="20" end="23"><Indent type="LABEL" count="1" spaces="0"/></Block>
</Block>
</Block>
</Block>
@@ -1,8 +0,0 @@
<Block start="0" end="5">
<Block start = "0" end="1"><Indent type="NONE"/></Block>
<Block start="2" end="5">
<Indent type="NONE"/>
<Block start="2" end="3"><Indent type="CONTINUATION" count="1"/></Block>
<Block start="4" end="5"><Indent type="CONTINUATION" count="1"/></Block>
</Block>
</Block>
@@ -1,14 +0,0 @@
<Block start="0" end="5">
<Block start = "0" end="1"><Indent type="NONE"/></Block>
<Block start="2" end="5">
<Indent type="NONE"/>
<Block start="2" end="3">
<Indent type="NORMAL" count="1"/>
<Block start="2" end="3"></Block>
</Block>
<Block start="4" end="5">
<Indent type="NORMAL" count="1"/>
<Block start="4" end="5"></Block>
</Block>
</Block>
</Block>
@@ -1,12 +0,0 @@
<Block start="0" end="6">
<Block start="0" end="1"><Indent type="NONE"/></Block>
<Block start="1" end="6">
<Indent type="NONE"/>
<Block start="1" end="4">
<Indent type="NORMAL"/>
<Block start="1" end="2"><Indent type="NONE"/></Block>
<Block start="3" end="4"><Indent type="NONE"/></Block>
</Block>
<Block start="5" end="6"><Indent type="NORMAL"/></Block>
</Block>
</Block>
@@ -1,26 +0,0 @@
<Block start="0" end="23">
<Wrap type="CHOP" id="1"/>
<Block start="0" end="3"></Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="4" end="7"><Wrap type="CHOP" id="1"/></Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="8" end="23">
<Wrap type="CHOP" id="1"/>
<Block start="8" end="11"><Indent type="LABEL" count="1" spaces="0"/></Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="12" end="15">
<Indent type="LABEL" count="1" spaces="0"/>
<Wrap type="CHOP" id="1"/>
</Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="16" end="23">
<Indent type="LABEL" count="1" spaces="0"/>
<Wrap type="CHOP" id="1"/>
<Block start="16" end="19"></Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="20" end="23">
<Wrap type="CHOP" id="1"/>
</Block>
</Block>
</Block>
</Block>
@@ -1,22 +0,0 @@
<Block start="0" end="11">
<Block start="0" end="11">
<Block start="0" end="11">
<Block start="0" end="1"/>
<Block start="2" end="3"/>
<Block start="4" end="11">
<Block start="4" end="11">
<Block start="4" end="11">
<Block start="4" end="5"/>
<Block start="6" end="7"/>
<Block start="8" end="11">
<Block start="8" end="11">
<Block start="8" end="9"/>
<Block start="10" end="11"/>
</Block>
</Block>
</Block>
</Block>
</Block>
</Block>
</Block>
</Block>
@@ -1,8 +0,0 @@
<Block start="0" end="23">
<Block start="0" end="3"/>
<Block start="4" end="7"/>
<Block start="8" end="11"/>
<Block start="12" end="15"/>
<Block start="16" end="19"/>
<Block start="20" end="23"/>
</Block>
@@ -1,54 +0,0 @@
<Block start="0" end="12">
<Block start="0" end="4">
<Indent type="NONE"/>
<Wrap type="NORMAL" id="1"/>
<Block start="0" end="1">
<Wrap type="NORMAL" id="2" parent="1"/>
</Block>
<Block start="1" end="2">
<Wrap type="NORMAL" id="2"/>
</Block>
<Block start="2" end="3">
<Wrap type="NORMAL" id="2"/>
</Block>
<Block start="3" end="4">
<Wrap type="NORMAL" id="2"/>
</Block>
</Block>
<Block start="4" end="8">
<Indent type="NONE"/>
<Wrap type="NORMAL" id="1"/>
<Block start="4" end="5">
<Wrap type="NORMAL" id="3" parent="1"/>
</Block>
<Block start="5" end="6">
<Wrap type="NORMAL" id="3"/>
</Block>
<Block start="6" end="7">
<Wrap type="NORMAL" id="3"/>
</Block>
<Block start="7" end="8">
<Wrap type="NORMAL" id="3"/>
</Block>
</Block>
<Block start="8" end="12">
<Indent type="NONE"/>
<Wrap type="NORMAL" id="1"/>
<Block start="8" end="9">
<Wrap type="NORMAL" id="4" parent="1"/>
</Block>
<Block start="9" end="10">
<Wrap type="NORMAL" id="4"/>
</Block>
<Block start="10" end="11">
<Wrap type="NORMAL" id="4"/>
</Block>
<Block start="11" end="12">
<Wrap type="NORMAL" id="4"/>
</Block>
</Block>
</Block>
@@ -1,16 +0,0 @@
<Block start="0" end="3">
<Block start="0" end="1">
<Wrap type="NORMAL" id="1"/>
</Block>
<Block start="1" end="2">
<Wrap type="NORMAL" id="1"/>
</Block>
<Block start="2" end="3">
<Wrap type="NORMAL" id="1"/>
<Block start="2" end="3">
<Wrap type="NORMAL" id="2"/>
</Block>
</Block>
</Block>
@@ -1,32 +0,0 @@
<Block start="0" end="9">
<Block start="0" end="1"><Indent type="NONE"/></Block>
<Block start="2" end="9">
<Indent type="NONE"/>
<Block start="2" end="3"><Indent type="CONTINUATION"/></Block>
<Block start="3" end="9">
<Indent type="NONE"/>
<Block start="3" end="4"><Indent type="CONTINUATION"/></Block>
<Block start="5" end="9">
<Indent type="NONE"/>
<Block start="5" end="6"><Indent type="CONTINUATION"/></Block>
<Block start="6" end="9">
<Indent type="NONE"/>
<Block start="6" end="7"><Indent type="CONTINUATION"/></Block>
<Block start="8" end="9">
<Indent type="NONE"/>
<Block start="8" end="9"><Indent type="CONTINUATION"/></Block>
</Block>
</Block>
</Block>
</Block>
</Block>
</Block>
@@ -1,12 +0,0 @@
<Block start="0" end="7">
<Block start="0" end="3"/>
<Block start="4" end="7">
<Indent type="CONTINUATION"/>
<Block start="4" end="7">
<Indent type="CONTINUATION"/>
<Block start="4" end="7">
<Indent type="CONTINUATION"/>
</Block>
</Block>
</Block>
</Block>
@@ -1,21 +0,0 @@
<Block start="0" end="20">
<Block start = "0" end="1"></Block>
<Space minspaces="0" maxspaces="0" minlinefeeds="0" maxlinefeeds="0"/>
<Block start = "2" end="3"></Block>
<Space minspaces="0" maxspaces="0" minlinefeeds="0" maxlinefeeds="0"/>
<Block start = "4" end="5"></Block>
<Space minspaces="0" maxspaces="0" minlinefeeds="0" maxlinefeeds="0"/>
<Block start = "6" end="7"></Block>
<Space minspaces="0" maxspaces="0" minlinefeeds="0" maxlinefeeds="0"/>
<Block start = "8" end="9"></Block>
<Space minspaces="0" maxspaces="0" minlinefeeds="0" maxlinefeeds="0"/>
<Block start = "10" end="11"></Block>
<Space minspaces="0" maxspaces="0" minlinefeeds="0" maxlinefeeds="0"/>
<Block start = "12" end="13"></Block>
<Space minspaces="0" maxspaces="0" minlinefeeds="0" maxlinefeeds="0"/>
<Block start = "14" end="15"></Block>
<Space minspaces="0" maxspaces="0" minlinefeeds="0" maxlinefeeds="0"/>
<Block start = "16" end="17"></Block>
<Space minspaces="0" maxspaces="0" minlinefeeds="0" maxlinefeeds="0"/>
<Block start = "18" end="19"></Block>
</Block>
@@ -1,27 +0,0 @@
<Block start="0" end="23">
<Wrap type="ALWAYS" id="1"/>
<Block start="0" end="3"><Indent type="NONE" count="1" spaces="0"/></Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="4" end="7"><Wrap type="ALWAYS" id="1"/><Indent type="NONE" count="1" spaces="0"/></Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="8" end="23">
<Indent type="NONE" count="1" spaces="0"/>
<Wrap type="ALWAYS" id="1"/>
<Block start="8" end="11"><Indent type="LABEL" count="1" spaces="0"/></Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="12" end="15">
<Indent type="LABEL" count="1" spaces="0"/>
<Wrap type="ALWAYS" id="1"/>
</Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="16" end="23">
<Indent type="LABEL" count="1" spaces="0"/>
<Wrap type="ALWAYS" id="1"/>
<Block start="16" end="19"></Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="20" end="23">
<Wrap type="ALWAYS" id="1"/>
</Block>
</Block>
</Block>
</Block>
@@ -1,26 +0,0 @@
<Block start="0" end="23">
<Wrap type="NORMAL" id="1"/>
<Block start="0" end="3"/>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="4" end="7"><Wrap type="NORMAL" id="1"/></Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="8" end="23">
<Wrap type="NORMAL" id="1"/>
<Block start="8" end="11"><Indent type="LABEL" count="1" spaces="0"/></Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="12" end="15">
<Indent type="LABEL" count="1" spaces="0"/>
<Wrap type="NORMAL" id="1"/>
</Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="16" end="23">
<Indent type="LABEL" count="1" spaces="0"/>
<Wrap type="NORMAL" id="1"/>
<Block start="16" end="19"/>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="20" end="23">
<Wrap type="NORMAL" id="1"/>
</Block>
</Block>
</Block>
</Block>
@@ -1,22 +0,0 @@
<Block start="0" end="23">
<Block start="0" end="3"/>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="4" end="7"><Wrap type="NORMAL" id="1"/></Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="8" end="23">
<Block start="8" end="11"><Indent type="LABEL" count="1" spaces="0"/></Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="12" end="15">
<Indent type="LABEL" count="1" spaces="0"/>
<Wrap type="NORMAL" id="1"/>
</Block>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="16" end="23">
<Indent type="LABEL" count="1" spaces="0"/>
<Block start="16" end="19"/>
<Space minspaces="2" maxspaces="2" minlinefeeds="0" maxlinefeeds="100"/>
<Block start="20" end="23">
</Block>
</Block>
</Block>
</Block>
@@ -5,64 +5,10 @@ import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.testFramework.LightPlatformTestCase;
import com.intellij.util.IncorrectOperationException;
import org.jdom.JDOMException;
import org.jetbrains.annotations.NonNls;
import java.io.IOException;
@NonNls
public class GeneralCodeFormatterTest extends LightPlatformTestCase {
private int myRightMargin = 120;
public void testDefaultContinuationIndent() throws Exception {
doTest("defaultContinuationIndent", "aaa bbb ccc\nddd eee fff", "aaa bbb ccc\n" +
" ddd eee fff");
}
public void testContinuationIndent() throws Exception {
doTest("4", "a\nb\nc", "a\n" +
" b\n" +
" c");
}
public void testContinuationIndent2() throws Exception{
doTest("5", "a\nb\nc", "a\n" +
" b\n" +
" c");
}
public void testParentContinuationIndent() throws Exception {
doTest("continuationIndent", "a\nb\nc\nd\ne\nf",
"a\n" +
" b\n" +
" c\n" +
" d\n" +
" e\n" +
" f");
}
public void test1() throws Exception {
doTest("1", "aaa bbb\nccc ddd\neee\nfff",
"aaa bbb\n" +
" ccc ddd\n" +
" eee\n" +
" fff");
}
public void test2() throws Exception {
doTest("2", "aaa bbb\nccc ddd\neee\nfff", "aaa bbb\n" +
" ccc ddd\n" +
" eee\n" +
" fff");
}
public void testNestedIndents() throws IOException, JDOMException {
String before = "xxx\n" +
"yyy";
String after = "xxx\n" +
" yyy";
doTest("nestedIndents", before, after);
}
public void testLastLineIndent() throws Exception{
final String initialText = "a\n";
@@ -74,7 +20,7 @@ public class GeneralCodeFormatterTest extends LightPlatformTestCase {
indentOptions.INDENT_SIZE = 4;
indentOptions.LABEL_INDENT_SIZE = 1;
final CodeStyleSettings settings = new CodeStyleSettings(false);
settings.setDefaultRightMargin(myRightMargin);
settings.setDefaultRightMargin(120);
try {
FormatterEx.getInstanceEx().adjustLineIndent(model, settings, indentOptions, initialText.length() - 1, new TextRange(0, initialText.length()));
}
@@ -86,95 +32,4 @@ public class GeneralCodeFormatterTest extends LightPlatformTestCase {
}
public void test22() throws Exception {
doTest("2", "aaa bbb ccc ddd eee\nfff", "aaa bbb ccc ddd eee\n" +
" fff");
}
public void testSpaceProperties() throws Exception {
doTest("3",
"aaa bbb ccc\n" +
"ddd eee fff",
"aaa bbb ccc\n" +
" ddd eee\n" +
"\n" +
" fff"
);
}
public void testSimpleWrapping() throws Exception {
doTest("wrapAlways", "aaa bbb ccc ddd eee fff", "aaa\n" +
"bbb\n" +
" ccc\n" +
" ddd\n" +
" eee\n" +
" fff");
myRightMargin = 16;
doTest("wrapAsNeeded", "aaa bbb ccc ddd eee fff", "aaa bbb ccc\n" +
" ddd eee fff");
}
public void testWrapping() throws Exception {
myRightMargin = 10;
doTest("chopDownIfLong", "aaa bbb ccc ddd eee fff", "aaa\n" +
" bbb\n" +
" ccc\n" +
" ddd\n" +
" eee\n" +
" fff");
myRightMargin = 20;
doTest("wrapAtMiddle", "aaa bbb ccc ddd eee fff", "aaa bbb ccc\n" +
" ddd eee fff");
}
public void testMultipleWrap() throws Exception {
myRightMargin = 2;
doTest("multipleWrap", "abc", "ab\n c");
}
public void testDifferentWraps() throws Exception {
myRightMargin = 10;
doTest("differentWraps", "(ab)(cd)(ef)", "(ab)(cd)\n" +
"(ef)");
}
public void testNestedCalls2() throws Exception{
doTest("nestedCalls",
"1 a2 b3\nc",
"1 a2 b3\n" +
" c");
}
public void testRemoveAllSpaces() throws Exception{
doTest("removeAllSpaces",
"0 1\n2\n3\t4 5\n6 7 8\t9 ",
"0123456789 ");
}
private void doTest(final String formattingModelName,
final String initial,
final String expected) throws IOException, JDOMException {
final TestFormattingModel model = new TestFormattingModel(initial);
model.setRootBlock(new FormattingModelXmlReader(model).readTestBlock(formattingModelName));
final CommonCodeStyleSettings.IndentOptions indentOptions = new CommonCodeStyleSettings.IndentOptions();
indentOptions.CONTINUATION_INDENT_SIZE = 8;
indentOptions.INDENT_SIZE = 4;
indentOptions.LABEL_INDENT_SIZE = 1;
final CodeStyleSettings settings = new CodeStyleSettings(false);
settings.setDefaultRightMargin(myRightMargin);
try {
FormatterEx.getInstanceEx().format(model, settings, indentOptions, indentOptions, null);
}
catch (IncorrectOperationException e) {
fail();
}
assertEquals(expected, FormatterImpl.getText(model));
}
}
@@ -15,7 +15,6 @@
*/
package com.intellij.formatting
import com.intellij.formatting.*
import com.intellij.formatting.engine.testModel.TestBlock
import com.intellij.openapi.util.TextRange
@@ -24,11 +23,29 @@ class BlockAttributes(val alignment: Alignment? = null, val wrap: Wrap? = null,
class CompositeTestBlock(startOffset: Int,
attributes: BlockAttributes,
private val children: List<TestBlockBase>) : TestBlockBase(startOffset, attributes)
{
private val children: List<TestBlockBase>) : TestBlockBase(startOffset, attributes) {
override fun getSubBlocks() = children
override fun isLeaf() = false
override val endOffset = children.last().endOffset
override fun getSpacing(child1: Block?, child2: Block): Spacing? {
val right: TestBlockBase = child2 as TestBlockBase
if (right.getSpacing() != null) {
return right.getSpacing()
}
val offset = child2.startOffset
var firstBlock = child2.firstChild() as? TestBlockBase
while (firstBlock != null && offset == firstBlock.startOffset) {
val spacing = firstBlock.getSpacing()
if (spacing != null) {
return spacing
}
firstBlock = firstBlock.firstChild() as? TestBlockBase
}
return null
}
}
class LeafTestBlock(startOffset: Int, attributes: BlockAttributes, val text: String) : TestBlockBase(startOffset, attributes) {
@@ -38,7 +55,7 @@ class LeafTestBlock(startOffset: Int, attributes: BlockAttributes, val text: Str
}
abstract class TestBlockBase(val startOffset: Int,
private val attributes: BlockAttributes) : Block {
val attributes: BlockAttributes) : Block {
override fun getAlignment() = attributes.alignment
override fun getTextRange() = TextRange(startOffset, endOffset)
@@ -46,32 +63,92 @@ abstract class TestBlockBase(val startOffset: Int,
override fun getIndent() = attributes.indent
override fun getSpacing(child1: Block?, child2: Block) = attributes.spacing
fun getSpacing() = attributes.spacing
fun firstChild() = subBlocks.firstOrNull()
override fun getChildAttributes(newChildIndex: Int): ChildAttributes = ChildAttributes.DELEGATE_TO_NEXT_CHILD
override fun isIncomplete() = false
abstract val endOffset: Int
}
class AttributesProvider {
private val alignments = hashMapOf<Int, Alignment>()
private val wraps = hashMapOf<String, Wrap>()
fun extract(text: String): BlockAttributes {
val attributes: List<String> = text.split(",").map(String::trim)
val attributes: List<String> = text.split(" ").map(String::trim)
val alignment = getAlignment(attributes)
return BlockAttributes(alignment = alignment)
val indent = getIndent(attributes)
val spacing = getSpacing(attributes)
val wrap = getWrap(attributes)
return BlockAttributes(alignment, wrap, indent, spacing)
}
private fun getWrap(attributes: List<String>): Wrap? {
val wrap = attributes.find { it.startsWith("w_") }?.substring(2) ?: return null
val matcher = "([a-z]*)([0-9]*)".toPattern().matcher(wrap)
if (!matcher.matches()) return null
val type = matcher.group(1)
val id = matcher.group(2)
return when (type) {
"normal" -> Wrap.createWrap(WrapType.NORMAL, true)
"always" -> Wrap.createWrap(WrapType.ALWAYS, true)
"chop" -> getChopWrapById(id)
else -> null
}
}
private fun getChopWrapById(id: String): Wrap {
val wrap = wraps[id]
if (wrap != null) {
return wrap
}
wraps[id] = Wrap.createWrap(WrapType.CHOP_DOWN_IF_LONG, true)
return getChopWrapById(id)
}
private fun getSpacing(attributes: List<String>): Spacing? {
val spaceProperties = attributes.find { it.startsWith("s_") }?.substring(2)?.split("_") ?: return null
val minSpaces = spaceProperties.getPropertyValue("min") ?: 1
val maxSpaces = spaceProperties.getPropertyValue("max") ?: 100
val minLfs = spaceProperties.getPropertyValue("minlf") ?: 0
val keepLineBreaks = if (spaceProperties.getPropertyValue("keepLb") == 0) false else true
return Spacing.createSpacing(minSpaces, maxSpaces, minLfs, keepLineBreaks, 100)
}
private fun List<String>.getPropertyValue(name: String): Int? {
return find { it.startsWith(name) }?.substring(name.length)?.toInt()
}
private fun getIndent(attributes: List<String>): Indent? {
val type = attributes.find { it.startsWith("i_") }?.substring(2)
return when (type) {
"cont" -> Indent.getContinuationIndent()
"none" -> Indent.getNoneIndent()
"norm" -> Indent.getNormalIndent()
"label" -> Indent.getLabelIndent()
else -> null
}
}
private fun getAlignment(attributes: List<String>): Alignment? {
val index = attributes.find { it.startsWith("a") }?.substring(1)?.toInt()
if (index == null || index < 0) return null
var alignment = alignments[index]
if (alignment != null) {
return alignment
}
alignment = AlignmentImpl(true, Alignment.Anchor.LEFT)
alignments[index] = alignment
return alignment
@@ -79,15 +156,15 @@ class AttributesProvider {
}
fun TestBlock.Composite.toFormattingBlock(startOffset: Int): CompositeTestBlock {
fun TestBlock.Composite.toFormattingBlock(startOffset: Int,
attributesProvider: AttributesProvider = AttributesProvider()): CompositeTestBlock {
val childBlocks = mutableListOf<TestBlockBase>()
val attributesProvider = AttributesProvider()
var previousStartOffset = startOffset
children.forEach {
val block = when (it) {
is TestBlock.Composite -> {
val composite = it.toFormattingBlock(previousStartOffset)
val composite = it.toFormattingBlock(previousStartOffset, attributesProvider)
previousStartOffset = composite.endOffset
composite
}
@@ -15,13 +15,11 @@
*/
package com.intellij.formatting.engine
import com.intellij.formatting.FormatTextRanges
import com.intellij.formatting.FormatterImpl
import com.intellij.formatting.FormatterEx
import com.intellij.formatting.engine.testModel.TestFormattingModel
import com.intellij.formatting.engine.testModel.getRoot
import com.intellij.formatting.toFormattingBlock
import com.intellij.openapi.editor.EditorFactory
import com.intellij.openapi.util.TextRange
import com.intellij.psi.codeStyle.CodeStyleSettings
import com.intellij.testFramework.LightPlatformTestCase
import junit.framework.TestCase
@@ -58,19 +56,23 @@ go boo
}
fun doTest(before: String, expectedText: String) {
val root = getRoot(before.trimStart())
fun doTest(before: String, expectedText: String, settings: CodeStyleSettings = CodeStyleSettings()) {
var root = getRoot(before.trimStart())
var beforeText = root.text
val rightMargin = beforeText.indexOf('|')
if (rightMargin > 0) {
settings.setRightMargin(null, rightMargin)
root = getRoot(before.trimStart().replace("|", ""))
beforeText = root.text
}
val rootFormattingBlock = root.toFormattingBlock(0)
val beforeText = root.text
val document = EditorFactory.getInstance().createDocument(beforeText)
val model = TestFormattingModel(rootFormattingBlock, document)
val settings = CodeStyleSettings()
val textRanges = FormatTextRanges(TextRange(0, document.textLength - 1), true)
FormatterEx.getInstanceEx().format(model, settings, settings.indentOptions, null)
val formatter = FormatterImpl()
formatter.format(model, settings, settings.indentOptions, textRanges)
TestCase.assertEquals(document.text, expectedText.trimStart())
TestCase.assertEquals(expectedText.trimStart(), document.text)
}
@@ -0,0 +1,318 @@
/*
* Copyright 2000-2016 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.formatting.engine
import com.intellij.psi.codeStyle.CodeStyleSettings
import com.intellij.testFramework.LightPlatformTestCase
import org.junit.Test
/**
* Just a GeneralCodeFormatterTest.java migration to simplified formatting model specification
*/
class GeneralCodeFormatterTest : LightPlatformTestCase() {
@Test
fun `test null indent is treated as continuation indent`() {
doTest(
"""
[]aaa []bbb []ccc
[]ddd []eee []fff
""",
"""
aaa bbb ccc
ddd eee fff
"""
)
}
@Test
fun `test continuation indent`() {
doTest(
"""
[i_none]a
[i_none]([i_cont]b
[i_cont]c)
""",
"""
a
b
c
"""
)
}
@Test
fun `test normal indent`() {
doTest(
"""
[i_none]a
[i_none]([i_norm]b
[i_norm]c)
""",
"""
a
b
c
"""
)
}
@Test
fun `test many nested blocks and continuation indent`() {
doTest(
"""
[]([]([]([]a
[]b
[]([]([]c
[]d
[]([]e
[]f))))))
""",
"""
a
b
c
d
e
f
"""
)
}
@Test
fun `test indents composition`() {
val settings = CodeStyleSettings()
settings.indentOptions!!.LABEL_INDENT_SIZE = 1
doTest("""
[i_none]aaa [i_none]bbb
[i_none]([i_norm]ccc [i_norm]ddd
[i_norm]([i_label]eee
[i_label]fff))
""",
"""
aaa bbb
ccc ddd
eee
fff
""", settings)
}
@Test
fun `test alignments on different block levels`() {
doTest(
"""
[]aaa [a1]bbb
[a1]([i_label]ccc [i_label]ddd
[i_label a1]([]eee
[a1]fff))
""",
"""
aaa bbb
ccc ddd
eee
fff
"""
)
}
@Test
fun `test nested indents`() {
doTest(
"""
[]xxx
[i_cont]([i_cont]([i_cont]yyy))
""",
"""
xxx
yyy
"""
)
}
@Test
fun `test one more alignment test`() {
doTest(
"""
[]aaa [a1]bbb [a1]([i_label]ccc [i_label]ddd [i_label a1]([]eee
[a1]fff))
""",
"""
aaa bbb ccc ddd eee
fff
"""
)
}
@Test
fun `test trivial spaces`() {
doTest(
"""
[]foo [s_min3]goo
""",
"""
foo goo
"""
)
}
@Test
fun `test space properties`() {
val settings = CodeStyleSettings()
settings.indentOptions!!.LABEL_INDENT_SIZE = 1
doTest(
"""
[]aaa [s_min2_max2]bbb [s_min2_max2]([i_norm]ccc
[i_norm s_min2_max2]ddd [i_norm s_min2_max2]([i_label]eee [i_label s_min2_max2_minlf2]fff))
""",
"""
aaa bbb ccc
ddd eee
fff
""", settings)
}
@Test
fun `test remove all spaces`() {
doTest(
"""
[s_min0_max0_keepLb0]0 [s_min0_max0_keepLb0]1
[s_min0_max0_keepLb0]2
[s_min0_max0_keepLb0]3 [s_min0_max0_keepLb0]4 [s_min0_max0_keepLb0]5
[s_min0_max0_keepLb0]6 [s_min0_max0_keepLb0]7 [s_min0_max0_keepLb0]8 [s_min0_max0_keepLb0]9 """,
"0123456789 ")
}
@Test
fun `test no wrap object no text wrap`() {
doTest("[]aaa []bbb []ccc []ddd []eee []f|ff", "aaa bbb ccc ddd eee fff")
}
@Test
fun `test simple wrap`() {
doTest(
"""
[]aaa [w_normal i_cont]b|bb
""",
"""
aaa
bbb
""")
}
@Test
fun `test wrap`() {
val settings = CodeStyleSettings()
settings.indentOptions!!.LABEL_INDENT_SIZE = 1
doTest(
"[i_none w_always]aaa " +
"[i_none s_min2_max2 w_always]bbb " +
"[i_none s_min2_max2]" +
"([i_label w_always]ccc " +
"[i_label s_min2_max2 w_always]ddd " +
"[i_label s_min2_max2]([w_always]eee " +
"[s_min2_max2 w_always]fff))",
"""
aaa
bbb
ccc
ddd
eee
fff""", settings)
}
@Test
fun `test wrap one more time`() {
val settings = CodeStyleSettings()
settings.indentOptions!!.LABEL_INDENT_SIZE = 1
doTest(
"[w_normal]aaa " +
"[s_min2_max2 w_normal]bbb " +
"[s_min2_max2 w_normal]" +
"([i_label]ccc " +
"[i_label s_min2_max2 w_normal]ddd |" +
"[i_label s_min2_max2 w_normal]" +
"([]eee [s_min2_max2 w_normal]fff))",
"""aaa bbb ccc
ddd eee fff""", settings)
}
@Test
fun `test chop down`() {
val settings = CodeStyleSettings()
settings.indentOptions!!.LABEL_INDENT_SIZE = 1
doTest(
"[w_chop1]aaa [s_min2_max2 w_chop1]bbb " +
"[]([i_label s_min2_max2 w_chop1]cc|c " +
"[i_label s_min2_max2 w_chop1]ddd " +
"[i_label]([s_min2_max2 w_chop1]eee [s_min2_max2 w_chop1]fff))",
"""
aaa
bbb
ccc
ddd
eee
fff""",
settings)
}
@Test
fun `test wrap in the middle`() {
val settings = CodeStyleSettings()
settings.indentOptions!!.LABEL_INDENT_SIZE = 1
doTest(
"[]aaa [s_min2_max2]bbb " +
"[]" +
"([i_label s_min2_max2]ccc " +
"[i_label s_min2_max2 w_normal]ddd " +
"[i_label]" +
"([s_min2_max2]eee [s_min2_max2]|fff))",
"""
aaa bbb ccc
ddd eee fff""", settings)
}
@Test
fun `test multiple wrap`() {
doTest("[w_normal]a[w_normal]b|[w_normal]([w_normal]c)",
"ab\n" +
" c")
}
@Test
fun `test nested calls`() {
doTest("[i_none]1 [i_none]([i_cont]a[i_none]([i_cont]2 [i_none]([i_cont]b[i_none]([i_cont]3\n[i_none]([i_cont]c)))))",
"""
1 a2 b3
c""")
}
@Test
fun `test different wraps`() {
doTest(
"[i_none w_normal]([w_normal]x[w_normal]a[w_normal]b[w_normal]x)" +
"[i_none w_normal]([w_normal]x[w_normal]c[w_normal]d[w_normal]x)" +
"[i_none w_normal]([w_normal]x[w_normal]e[w_normal]|f[w_normal]x)",
"xabxxcdx\nxefx")
}
}