summaryrefslogtreecommitdiff
path: root/platform/platform-tests/testSrc/com/intellij/codeInsight/actions/ReformatOnlyVcsChangedTextTest.java
blob: 476bd66ddde4e2be5d94c450cb0cf3375add9d1e (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Copyright 2000-2014 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.codeInsight.actions;

import com.intellij.lang.LanguageFormatting;
import com.intellij.openapi.components.impl.ComponentManagerImpl;
import com.intellij.openapi.fileTypes.PlainTextLanguage;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.FilePathImpl;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.changes.ChangeListManager;
import com.intellij.openapi.vcs.changes.ContentRevision;
import com.intellij.openapi.vcs.changes.SimpleContentRevision;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.testFramework.LightPlatformTestCase;
import com.intellij.testFramework.PlatformTestCase;
import com.intellij.testFramework.vcs.MockChangeListManager;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.picocontainer.MutablePicoContainer;

import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class ReformatOnlyVcsChangedTextTest extends LightPlatformTestCase {
  private static final String TEMP_DIR_NAME = "dir";
  private PsiDirectory myWorkingDirectory;

  private MockChangeListManager myMockChangeListManager;
  private MockCodeStyleManager myMockCodeStyleManager;
  private MockPlainTextFormattingModelBuilder myMockPlainTextFormattingModelBuilder;

  private ChangeListManager myRealChangeListManager;
  private CodeStyleManager myRealCodeStyleManger;

  private final static String COMMITTED =
    "class Test {\n" +
    "          int a      =       22;\n" +
    "        public String getName() { return \"Test\"; }\n" +
    "}";

  private final static String MODIFIED =
    "class Test {\n" +
    "          int a      =       22;\n" +
    "              long l;\n" +
    "              double d;\n" +
    "              int i;\n" +
    "        public String getName() { return \"Test\"; }\n" +
    "            String test1;\n" +
    "            String test2;\n" +
    "}";

  private final static ChangedLines[] CHANGED_LINES = new ChangedLines[] { line(2, 4), line(6, 7) };
  private final static ChangedLines[] NO_CHANGED_LINES = new ChangedLines[0];

  @Override
  public void setUp() throws Exception {
    PlatformTestCase.initPlatformLangPrefix();
    super.setUp();
    myWorkingDirectory = TestFileStructure.createDirectory(getProject(), getSourceRoot(), TEMP_DIR_NAME);

    myRealChangeListManager = ChangeListManager.getInstance(getProject());
    myMockChangeListManager = new MockChangeListManager();
    registerChangeListManager(myMockChangeListManager);

    myRealCodeStyleManger = CodeStyleManager.getInstance(getProject());
    myMockCodeStyleManager = new MockCodeStyleManager();
    registerCodeStyleManager(myMockCodeStyleManager);

    myMockPlainTextFormattingModelBuilder = new MockPlainTextFormattingModelBuilder();
    LanguageFormatting.INSTANCE.addExplicitExtension(PlainTextLanguage.INSTANCE, myMockPlainTextFormattingModelBuilder);
  }

  @Override
  public void tearDown() throws Exception {
    registerChangeListManager(myRealChangeListManager);
    registerCodeStyleManager(myRealCodeStyleManger);
    LanguageFormatting.INSTANCE.removeExplicitExtension(PlainTextLanguage.INSTANCE, myMockPlainTextFormattingModelBuilder);

    TestFileStructure.delete(myWorkingDirectory.getVirtualFile());
    super.tearDown();
  }

  public void testInsertion() throws IOException {
    doTest(
      "public class B {\n" +
      "       int a = 3;\n" +
      "                 String text;\n" +
      "                               Object last = null;\n" +
      "}",

      "public class B {\n" +
      "       int a = 3;\n" +
      "                               int toIndent1 = 1;\n" +
      "                 String text;\n" +
      "                               int toIndent2\n" +
      "                               Object last = null;\n" +
      "}",

      line(2, 2), line(4, 4)
    );
  }

  private static ChangedLines line(int from, int to) {
    return new ChangedLines(from, to);
  }

  public void testDeletion() throws IOException {
    doTest(
      "public class B {\n" +
      "           int a = 3;\n" +
      "           String text;\n" +
      "           Object last = null;\n" +
      "           Object first = null;\n" +
      "           Object second = null;\n" +
      "}",

      "public class B {\n" +
      "           int newInt = 1;\n" +
      "           Object last = null;\n" +
      "}",

      line(1, 1)
    );
  }

  public void testModification() throws IOException {
    doTest(
      "public class B {\n" +
      "           int a = 3;\n" +
      "           String text;\n" +
      "           Object last = null;\n" +
      "           Object first = null;\n" +
      "           Object second = null;\n" +
      "}",

      "public class B {\n" +
      "           int a = 33;\n" +
      "           String text;\n" +
      "           Object last = new Object();\n" +
      "           Object first = null;\n" +
      "           Object second = new Object();\n" +
      "}",

      line(1, 1), line(3,3), line(5,5)
    );
  }

  public void testReformatFiles() throws IOException {
    ChangedFilesStructure fs = new ChangedFilesStructure(myWorkingDirectory);

    PsiFile m1 = fs.createFile("Test1.java", COMMITTED, MODIFIED);
    PsiFile u1 = fs.createFile("Test2.java", COMMITTED, COMMITTED);

    fs.createDirectoryAndMakeItCurrent();
    PsiFile m2 = fs.createFile("Test3.java", COMMITTED, MODIFIED);
    PsiFile u2 = fs.createFile("Test4.java", COMMITTED, COMMITTED);

    new ReformatCodeProcessor(getProject(), new PsiFile[] {m1, m2, u1, u2}, null, true).run();

    assertFormattedLines(CHANGED_LINES, m1, m2);
    assertFormattedLines(NO_CHANGED_LINES, u1, u2);
  }

  public void testNoChangesNoFormatting() throws IOException {
    ChangedFilesStructure fs = new ChangedFilesStructure(myWorkingDirectory);

    PsiFile u1 = fs.createFile("Test1.java", COMMITTED, COMMITTED);
    PsiFile u2 = fs.createFile("Test2.java", COMMITTED, COMMITTED);

    reformatDirectory(fs.getCurrentDirectory());

    assertFormattedLines(NO_CHANGED_LINES, u1, u2);
  }


  public void testReformatOnlyChanged() throws IOException {
    ChangedFilesStructure fs = new ChangedFilesStructure(myWorkingDirectory);

    PsiFile untouched1 = fs.createFile("Test1.java", COMMITTED, COMMITTED);
    PsiFile untouched2 = fs.createFile("Test2.java", COMMITTED, COMMITTED);

    PsiFile modified1 = fs.createFile("Test4.java", COMMITTED, MODIFIED);
    PsiFile modified2 = fs.createFile("Test5.java", COMMITTED, MODIFIED);
    PsiFile modified3 = fs.createFile("Test6.java", COMMITTED, MODIFIED);

    reformatDirectory(fs.getCurrentDirectory());


    assertFormattedLines(CHANGED_LINES, modified1, modified2, modified3);
    assertFormattedLines(NO_CHANGED_LINES, untouched1, untouched2);
  }

  public void testReformatInAllSubtree() throws IOException {
    ChangedFilesStructure fs = new ChangedFilesStructure(myWorkingDirectory);
    PsiFile modified11 = fs.createFile("Test4.java", COMMITTED, MODIFIED);
    PsiFile modified12 = fs.createFile("Test5.java", COMMITTED, MODIFIED);

    fs.createDirectoryAndMakeItCurrent();
    PsiDirectory dirToReformat = fs.getCurrentDirectory();
    PsiFile modified21 = fs.createFile("Test6.java", COMMITTED, MODIFIED);
    PsiFile modified22 = fs.createFile("Test7.java", COMMITTED, MODIFIED);

    fs.createDirectoryAndMakeItCurrent();
    PsiFile modified31 = fs.createFile("Test8.java", COMMITTED, MODIFIED);
    PsiFile modified32 = fs.createFile("Test9.java", COMMITTED, MODIFIED);

    reformatDirectory(dirToReformat);

    assertFormattedLines(CHANGED_LINES, modified21, modified22, modified31, modified32);
    assertFormattedLines(NO_CHANGED_LINES, modified11, modified12);
  }

  private static void registerChangeListManager(@NotNull ChangeListManager manager) {
    Project project = getProject();
    assert (project instanceof ComponentManagerImpl);
    ComponentManagerImpl projectComponentManager = (ComponentManagerImpl)project;
    projectComponentManager.registerComponentInstance(ChangeListManager.class, manager);
  }

  private static void registerCodeStyleManager(@NotNull CodeStyleManager manager) {
    String componentKey = CodeStyleManager.class.getName();
    MutablePicoContainer container = (MutablePicoContainer)getProject().getPicoContainer();
    container.unregisterComponent(componentKey);
    container.registerComponentInstance(componentKey, manager);
  }

  private void doTest(@NotNull String committed, @NotNull String modified, @NotNull ChangedLines... lines) throws IOException {
    ChangedFilesStructure fs = new ChangedFilesStructure(myWorkingDirectory);
    PsiFile file = fs.createFile("Test.java", committed, modified);
    reformatDirectory(myWorkingDirectory);
    assertFormattedRangesEqualsTo(file, lines);
  }

  private void assertFormattedLines(@NotNull ChangedLines[] expectedLines, @NotNull PsiFile... files) {
    for (PsiFile file : files)
      assertFormattedRangesEqualsTo(file, expectedLines);
  }

  private void assertFormattedRangesEqualsTo(@NotNull PsiFile file, ChangedLines... expected) {
    ChangedLines[] formatted = myMockCodeStyleManager.getFormattedLinesFor(file);

    Comparator<ChangedLines> cmp = new Comparator<ChangedLines>() {
      @Override
      public int compare(ChangedLines o1, ChangedLines o2) {
        return o1.from < o2.from ? -1 : 1;
      }
    };
    Arrays.sort(expected, cmp);
    Arrays.sort(formatted, cmp);

    assertTrue(getErrorMessage(expected, formatted), Arrays.equals(expected, formatted));
  }

  @NotNull
  private static String getErrorMessage(ChangedLines[] expected, ChangedLines[] actual) {
    return "Expected: " + Arrays.toString(expected) + " Actual: " + Arrays.toString(actual);
  }

  private static void reformatDirectory(@NotNull PsiDirectory dir) {
    ReformatCodeProcessor processor = new ReformatCodeProcessor(getProject(), dir, true, true);
    processor.run();
  }

  class ChangedFilesStructure {
    private TestFileStructure myFileStructure;

    public ChangedFilesStructure(@NotNull PsiDirectory directory) {
      myFileStructure = new TestFileStructure(getModule(), directory);
    }

    public void createDirectoryAndMakeItCurrent() throws IOException {
      myFileStructure.createDirectoryAndMakeItCurrent("inner");
    }

    @NotNull
    public PsiFile createFile(@NotNull String fileName,
                              @NotNull String committedContent,
                              @NotNull String actualContent) throws IOException
    {
      PsiFile file = myFileStructure.addTestFile(fileName, actualContent);
      if (committedContent != actualContent) {
        registerCommittedRevision(committedContent, file);
      }
      return file;
    }

    private void registerCommittedRevision(@NotNull String committedContent, @NotNull PsiFile... files) {
      List<Change> changes = createChanges(committedContent, files);
      injectChanges(changes);
    }

    @NotNull
    private List<Change> createChanges(@NotNull String committed, @NotNull PsiFile... files) {
      List<Change> changes = ContainerUtil.newArrayList();
      for (PsiFile file : files) {
        changes.add(createChange(committed, file));
      }
      return changes;
    }

    private void injectChanges(@NotNull List<Change> changes) {
      Change[] arr = new Change[changes.size()];
      ContainerUtil.toArray(changes, arr);
      myMockChangeListManager.addChanges(arr);
    }

    @NotNull
    private Change createChange(@NotNull String committed, @NotNull PsiFile file) {
      FilePathImpl filePath = new FilePathImpl(file.getVirtualFile());
      ContentRevision before = new SimpleContentRevision(committed, filePath, "");
      ContentRevision after = new SimpleContentRevision("", filePath, "");
      return new Change(before, after);
    }

    @NotNull
    public PsiDirectory getCurrentDirectory() {
      return myFileStructure.getCurrentDirectory();
    }
  }
}