move GeneralFormatterTest to platform-tests; fix NPE in DocumentImpl when running standalone GeneralFormatterTest

This commit is contained in:
Dmitry Jemerov
2013-11-08 12:52:43 +01:00
parent 4d22f5e7fb
commit be45c85e40
22 changed files with 957 additions and 5 deletions
@@ -556,11 +556,14 @@ public class DocumentImpl extends UserDataHolderBase implements DocumentEx {
@NotNull
private DocumentEvent doBeforeChangedUpdate(int offset, CharSequence oldString, CharSequence newString, boolean wholeTextReplaced) {
FileDocumentManager manager = FileDocumentManager.getInstance();
if (manager != null) {
VirtualFile file = manager.getFile(this);
if (file != null && !file.isValid()) {
LOG.error("File of this document has been deleted.");
Application app = ApplicationManager.getApplication();
if (app != null) {
FileDocumentManager manager = FileDocumentManager.getInstance();
if (manager != null) {
VirtualFile file = manager.getFile(this);
if (file != null && !file.isValid()) {
LOG.error("File of this document has been deleted.");
}
}
}
@@ -0,0 +1,19 @@
<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>
@@ -0,0 +1,19 @@
<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>
@@ -0,0 +1,18 @@
<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>
@@ -0,0 +1,8 @@
<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>
@@ -0,0 +1,14 @@
<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>
@@ -0,0 +1,12 @@
<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>
@@ -0,0 +1,26 @@
<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>
@@ -0,0 +1,22 @@
<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>
@@ -0,0 +1,8 @@
<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>
@@ -0,0 +1,54 @@
<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>
@@ -0,0 +1,3 @@
<Block start="0" end="1" incomplete="true">
<Indent type="NORMAL" count="1" spaces="0"/>
</Block>
@@ -0,0 +1,16 @@
<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>
@@ -0,0 +1,32 @@
<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>
@@ -0,0 +1,21 @@
<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>
@@ -0,0 +1,27 @@
<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>
@@ -0,0 +1,26 @@
<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>
@@ -0,0 +1,22 @@
<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>
@@ -0,0 +1,140 @@
package com.intellij.formatting;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.openapi.util.TextRange;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.util.containers.HashMap;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jetbrains.annotations.NonNls;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@NonNls public class FormattingModelXmlReader {
private final Map<String, Alignment> myIdToAlignemt = new HashMap<String, Alignment>();
private final Map<String, Wrap> myIdToWrap = new HashMap<String, Wrap>();
private final FormattingDocumentModel myModel;
public FormattingModelXmlReader(final FormattingDocumentModel model) {
myModel = model;
}
public TestBlock readTestBlock(String dataName) throws IOException, JDOMException {
final File dataFile = new File( PlatformTestUtil.getCommunityPath().replace(File.separatorChar, '/') +
"/platform/platform-tests/testData/newGeneralFormatter", dataName + ".xml");
final Document document = JDOMUtil.loadDocument(dataFile);
return readBlock(document.getRootElement());
}
private TestBlock readBlock(final Element rootElement) {
final int startOffset = Integer.parseInt(rootElement.getAttributeValue("start"));
final int endOffset = Integer.parseInt(rootElement.getAttributeValue("end"));
final TextRange textRange = new TextRange(startOffset, endOffset);
final TestBlock result = new TestBlock(textRange);
result.setIsIncomplete(Boolean.valueOf(rootElement.getAttributeValue("incomplete")).booleanValue());
final CharSequence text = myModel.getText(textRange);
if (text != null) {
result.setText(text.toString());
}
final Element indentElement = rootElement.getChild("Indent");
if (indentElement != null) {
result.setIndent(readIndent(indentElement));
}
final Element alignmentElement = rootElement.getChild("Alignment");
if (alignmentElement != null) {
result.setAlignment(readAlignment(alignmentElement));
}
final Element wrapElement = rootElement.getChild("Wrap");
if (wrapElement != null) {
result.setWrap(readWrap(wrapElement));
}
final List children = rootElement.getChildren();
for (final Object aChildren : children) {
Element element = (Element)aChildren;
if (element.getName().equals("Space")) {
result.addSpace(readSpace(element));
}
else if (element.getName().equals("Block")) {
result.addBlock(readBlock(element));
}
}
return result;
}
private Wrap readWrap(final Element wrapElement) {
final String wrapId = wrapElement.getAttributeValue("id");
if (myIdToWrap.containsKey(wrapId)) return myIdToWrap.get(wrapId);
final String type = wrapElement.getAttributeValue("type");
String parentId = wrapElement.getAttributeValue("parent");
boolean wrapFirst = "true".equals(wrapElement.getAttributeValue("ignoreParents"));
if (myIdToWrap.containsKey(parentId)) {
final Wrap wrap = Wrap.createChildWrap(myIdToWrap.get(parentId),readWrapType(type), wrapFirst);
if ("true".equals(wrapElement.getAttributeValue("ignoreParents"))) {
wrap.ignoreParentWraps();
}
myIdToWrap.put(wrapId, wrap);
return wrap;
} else {
final Wrap wrap = Wrap.createWrap(readWrapType(type), wrapFirst);
if ("true".equals(wrapElement.getAttributeValue("ignoreParents"))) {
wrap.ignoreParentWraps();
}
myIdToWrap.put(wrapId, wrap);
return wrap;
}
}
private WrapType readWrapType(final String type) {
if ("ALWAYS".equals(type)) return WrapType.ALWAYS;
if ("NORMAL".equals(type)) return WrapType.NORMAL;
if ("CHOP".equals(type)) return WrapType.CHOP_DOWN_IF_LONG;
return WrapType.NONE;
}
private Spacing readSpace(final Element element) {
return Spacing.createSpacing(
getInt(element.getAttributeValue("minspaces")),
getInt(element.getAttributeValue("maxspaces")),
getInt(element.getAttributeValue("minlinefeeds")),
"true".equals(element.getAttributeValue("keepLineBreaks")), 0);
}
private Alignment readAlignment(final Element alignmentElement) {
final String alignId = alignmentElement.getAttributeValue("id");
if (myIdToAlignemt.containsKey(alignId)) return myIdToAlignemt.get(alignId);
final Alignment alignment = Alignment.createAlignment();
myIdToAlignemt.put(alignId, alignment);
return alignment;
}
private Indent readIndent(final Element indentElement) {
return createIndent(indentElement.getAttributeValue("type"));
}
private int getInt(final String count) {
try {
return Integer.parseInt(count);
}
catch (Exception e) {
return 0;
}
}
private Indent createIndent(final String type) {
if ("LABEL".equals(type)) return Indent.getLabelIndent();
if ("NONE".equals(type)) return Indent.getNoneIndent();
if ("CONTINUATION".equals(type)) return Indent.getContinuationIndent();
return Indent.getNormalIndent();
}
}
@@ -0,0 +1,215 @@
package com.intellij.formatting;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ui.UIUtil;
import junit.framework.TestCase;
import org.jdom.JDOMException;
import org.jetbrains.annotations.NonNls;
import java.io.IOException;
@NonNls public class GeneralCodeFormatterTest extends TestCase {
private int myRightMargin = 120;
@Override
public void runBare() throws Throwable {
if (ApplicationManager.getApplication() != null) {
final Throwable[] throwable = new Throwable[1];
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
@Override
public void run() {
try {
runBareImpl();
}
catch (Throwable th) {
throwable[0] = th;
}
}
private void runBareImpl() throws Throwable {
setUp();
try {
runTest();
}
finally {
tearDown();
}
}
});
if (throwable[0] != null) {
throw throwable[0];
}
}
else {
super.runBare();
}
}
@Override
protected void setUp() throws Exception {
super.setUp();
FormatterImpl.getInstance();
}
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 testLastLineIndent() throws Exception{
final String initialText = "a\n";
final TestFormattingModel model = new TestFormattingModel(initialText);
model.setRootBlock(new FormattingModelXmlReader(model).readTestBlock("lineIndent"));
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.RIGHT_MARGIN = myRightMargin;
try {
FormatterEx.getInstanceEx().adjustLineIndent(model, settings, indentOptions, initialText.length() - 1, new TextRange(0, initialText.length()));
}
catch (IncorrectOperationException e) {
fail();
}
assertEquals("a\n ", FormatterImpl.getText(model));
}
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.RIGHT_MARGIN = myRightMargin;
try {
FormatterEx.getInstanceEx().format(model, settings, indentOptions, indentOptions, null);
}
catch (IncorrectOperationException e) {
fail();
}
assertEquals(expected, FormatterImpl.getText(model));
}
}
@@ -0,0 +1,126 @@
package com.intellij.formatting;
import com.intellij.openapi.util.TextRange;
import java.util.ArrayList;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class TestBlock implements Block{
private TextRange myTextRange;
private final List<Object> myElements = new ArrayList<Object>();
private Wrap myWrap;
private Indent myIndent;
private Alignment myAlignment;
private String myText = "";
private boolean myIsIncomplete = false;
public TestBlock(final TextRange textRange) {
myTextRange = textRange;
}
@Override
@NotNull
public TextRange getTextRange() {
return myTextRange;
}
@Override
@NotNull
public List<Block> getSubBlocks() {
return getBlockList();
}
public String toString() {
return myText;
}
private List<Block> getBlockList() {
final ArrayList<Block> blocks = new ArrayList<Block>();
for (Object o : myElements) {
if (o instanceof Block) blocks.add((Block)o);
}
return blocks;
}
@Override
public Wrap getWrap() {
return myWrap;
}
@Override
public Indent getIndent() {
return myIndent;
}
@Override
public Alignment getAlignment() {
return myAlignment;
}
public TestBlock setWrap(final Wrap wrap) {
myWrap = wrap;
return this;
}
public TestBlock setAlignment(final Alignment alignment) {
myAlignment = alignment;
return this;
}
public TestBlock setIndent(final Indent indent) {
myIndent = indent;
return this;
}
@Override
public Spacing getSpacing(@Nullable Block child1, @NotNull Block child2) {
if (child1 == null) {
return null;
}
final int index = myElements.indexOf(child2);
if (myElements.get(index - 1) instanceof Spacing) {
return (Spacing)myElements.get(index - 1);
}
return null;
}
public void addBlock(final Block block) {
myElements.add(block);
}
public void setTextRange(final TextRange textRange) {
myTextRange = textRange;
}
public void addSpace(final Spacing spacing) {
myElements.add(spacing);
}
public void setText(final String s) {
myText = s;
}
@Override
@NotNull
public ChildAttributes getChildAttributes(final int newChildIndex) {
return new ChildAttributes(getIndent(), null);
}
@Override
public boolean isIncomplete() {
return myIsIncomplete;
}
@Override
public boolean isLeaf() {
return myElements.isEmpty();
}
public void setIsIncomplete(final boolean value) {
myIsIncomplete = value;
}
}
@@ -0,0 +1,121 @@
package com.intellij.formatting;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.impl.DocumentImpl;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.formatter.CompositeWhiteSpaceFormattingStrategy;
import com.intellij.psi.formatter.StaticSymbolWhiteSpaceDefinitionStrategy;
import com.intellij.psi.formatter.WhiteSpaceFormattingStrategy;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
public class TestFormattingModel implements FormattingModel, FormattingDocumentModel{
private final CompositeWhiteSpaceFormattingStrategy myWhiteSpaceStrategy = new CompositeWhiteSpaceFormattingStrategy(
Arrays.<WhiteSpaceFormattingStrategy>asList(
new StaticSymbolWhiteSpaceDefinitionStrategy(' ', '\t', '\n')
)
);
private final Document myDocument;
private Block myRootBlock;
public TestFormattingModel(String text) {
myDocument = new DocumentImpl(text);
}
public TestFormattingModel(final Document document) {
myDocument = document;
}
public void setRootBlock(final Block rootBlock) {
myRootBlock = rootBlock;
}
@Override
public int getLineNumber(int offset) {
return myDocument.getLineNumber(offset);
}
@Override
public int getLineStartOffset(int line) {
return myDocument.getLineStartOffset(line);
}
@Override
public TextRange replaceWhiteSpace(final TextRange textRange,
final String whiteSpace
) {
if (ApplicationManager.getApplication() != null) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
myDocument.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), whiteSpace);
}
});
} else {
myDocument.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), whiteSpace);
}
return new TextRange(textRange.getStartOffset(), textRange.getStartOffset() + whiteSpace.length());
}
@Override
public CharSequence getText(final TextRange textRange) {
return myDocument.getCharsSequence().subSequence(textRange.getStartOffset(), textRange.getEndOffset());
}
@Override
@NotNull
public FormattingDocumentModel getDocumentModel() {
return this;
}
@Override
@NotNull
public Block getRootBlock() {
return myRootBlock;
}
@Override
public void commitChanges() {
}
@Override
public int getTextLength() {
return myDocument.getTextLength();
}
@NotNull
@Override
public Document getDocument() {
return myDocument;
}
@Override
public TextRange shiftIndentInsideRange(TextRange range, int indent) {
return range;
}
@Override
public boolean containsWhiteSpaceSymbolsOnly(int startOffset, int endOffset) {
return myWhiteSpaceStrategy.check(myDocument.getCharsSequence(), startOffset, endOffset) >= endOffset;
}
@NotNull
@Override
public CharSequence adjustWhiteSpaceIfNecessary(@NotNull CharSequence whiteSpaceText, int startOffset, int endOffset,
boolean changedViaPsi) {
return whiteSpaceText;
}
//@Override
//public boolean isWhiteSpaceSymbol(char symbol) {
// return containsWhiteSpaceSymbolsOnly(CharBuffer.wrap(new char[] {symbol}), 0, 1);
//}
//private boolean containsWhiteSpaceSymbolsOnly(CharSequence text, int startOffset, int endOffset) {
// return myWhiteSpaceStrategy.check(myDocument.getCharsSequence(), startOffset, endOffset) >= endOffset;
//}
}