summaryrefslogtreecommitdiff
path: root/platform/platform-tests/testSrc/com/intellij/formatting/GeneralCodeFormatterTest.java
blob: 861a4b2c6e4bf51e34d133b43024fc1ae0b686dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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.setDefaultRightMargin(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.setDefaultRightMargin(myRightMargin);
    try {
      FormatterEx.getInstanceEx().format(model, settings, indentOptions, indentOptions, null);
    }
    catch (IncorrectOperationException e) {
      fail();
    }

    assertEquals(expected, FormatterImpl.getText(model));
  }


}