summaryrefslogtreecommitdiff
path: root/platform/util/src/com/intellij/openapi/diff/impl/ComparisonPolicy.java
blob: 0767413e3f5b5173c89e3432ddc540c6750250a9 (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
/*
 * Copyright 2000-2013 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.openapi.diff.impl;

import com.intellij.CommonBundle;
import com.intellij.openapi.diff.ex.DiffFragment;
import com.intellij.openapi.diff.impl.highlighting.FragmentSide;
import com.intellij.openapi.diff.impl.highlighting.Util;
import com.intellij.openapi.diff.impl.processing.DiffCorrection;
import com.intellij.openapi.diff.impl.processing.Formatting;
import com.intellij.openapi.diff.impl.processing.Word;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.diff.Diff;
import com.intellij.util.diff.FilesTooBigForDiffException;

public abstract class ComparisonPolicy {
  private final String myName;

  protected ComparisonPolicy(final String name) {
    myName = name;
  }

  public DiffFragment[] buildFragments(String[] strings1, String[] strings2) throws FilesTooBigForDiffException {
    DiffFragmentBuilder builder = new DiffFragmentBuilder(strings1, strings2);
    Object[] wrappers1 = getWrappers(strings1);
    Object[] wrappers2 = getWrappers(strings2);
    Diff.Change change = Diff.buildChanges(wrappers1, wrappers2);
    return builder.buildFragments(Util.concatEquals(change, wrappers1, wrappers2));
  }

  public DiffFragment[] buildDiffFragmentsFromLines(String[] lines1, String[] lines2) throws FilesTooBigForDiffException {
    DiffFragmentBuilder builder = new DiffFragmentBuilder(lines1, lines2);
    Object[] wrappers1 = getLineWrappers(lines1);
    Object[] wrappers2 = getLineWrappers(lines2);
    Diff.Change change = Diff.buildChanges(wrappers1, wrappers2);
    return builder.buildFragments(change);
  }

  public static final ComparisonPolicy DEFAULT = new ComparisonPolicy(CommonBundle.message("comparison.policy.default.name")) {
    @Override
    protected Object[] getWrappers(String[] strings) {
      return strings;
    }

    @Override
    protected Object[] getLineWrappers(String[] lines) {
      return lines;
    }

    @Override
    public DiffFragment createFragment(Word word1, Word word2) {
      return createFragment(word1.getText(), word2.getText());
    }

    @SuppressWarnings({"HardCodedStringLiteral"})
    public String toString() {
      return "DEFAULT";
    }
  };

  public static final ComparisonPolicy TRIM_SPACE = new ComparisonPolicy(CommonBundle.message("comparison.policy.trim.space.name")) {
    @Override
    protected Object[] getLineWrappers(String[] lines) {
      return trimStrings(lines);
    }

    @Override
    public DiffFragment createFragment(Word word1, Word word2) {
      String text1 = word1.getText();
      String text2 = word2.getText();
      if (word1.isWhitespace() && word2.isWhitespace() &&
          word1.atEndOfLine() && word2.atEndOfLine()) {
        return DiffFragment.unchanged(text1, text2);
      }
      return createFragment(text1, text2);
    }

    @Override
    protected Object[] getWrappers(String[] strings) {
      Object[] result = new Object[strings.length];
      boolean atBeginning = true;
      for (int i = 0; i < strings.length; i++) {
        String string = strings[i];
        String wrapper = atBeginning ? StringUtil.trimLeading(string) : string;
        if (StringUtil.endsWithChar(wrapper, '\n')) {
          atBeginning = true;
          wrapper = StringUtil.trimTrailing(wrapper);
        }
        else {
          atBeginning = false;
        }
        result[i] = wrapper;
      }
      return result;
    }



    @SuppressWarnings({"HardCodedStringLiteral"})
    public String toString() {
      return "TRIM";
    }
  };

  public static final ComparisonPolicy IGNORE_SPACE = new IgnoreSpacePolicy();

  private static String toNotNull(String text) {
    return text == null ? "" : text;
  }

  protected abstract Object[] getWrappers(String[] strings);

  protected abstract Object[] getLineWrappers(String[] lines);

  protected Object[] trimStrings(String[] strings) {
    Object[] result = new Object[strings.length];
    for (int i = 0; i < strings.length; i++) {
      String string = strings[i];
      result[i] = string.trim();
    }
    return result;
  }

  public DiffFragment createFragment(String text1, String text2) {
    text1 = toNull(text1);
    text2 = toNull(text2);
    if (text1 == null && text2 == null) return new DiffFragment("", "");
    DiffFragment result = new DiffFragment(text1, text2);
    if (text1 != null && text2 != null) {
      result.setModified(!getWrapper(text1).equals(getWrapper(text2)));
    }
    return result;
  }

  private String toNull(String text1) {
    return text1 == null || text1.isEmpty() ? null : text1;
  }

  private Object getWrapper(String text) {
    return getWrappers(new String[]{text})[0];
  }

  public boolean isEqual(DiffFragment fragment) {
    if (fragment.isOneSide()) return false;
    Object[] wrappers = getLineWrappers(new String[]{fragment.getText1(), fragment.getText2()});
    return Comparing.equal(wrappers[0], wrappers[1]);
  }

  public Word createFormatting(String text, TextRange textRange) {
    return new Formatting(text, textRange);
  }

  public abstract DiffFragment createFragment(Word word1, Word word2);

  public String getName() {
    return myName;
  }

  public static final ComparisonPolicy[] COMPARISON_POLICIES = new ComparisonPolicy[]{DEFAULT, IGNORE_SPACE, TRIM_SPACE};
  
  public static ComparisonPolicy[] getAllInstances() {
    return COMPARISON_POLICIES;
  }

  private static class IgnoreSpacePolicy extends ComparisonPolicy implements DiffCorrection.FragmentProcessor<DiffCorrection.FragmentsCollector> {
    public IgnoreSpacePolicy() {
      super(CommonBundle.message("comparison.policy.ignore.spaces.name"));
    }

    @Override
    protected Object[] getLineWrappers(String[] lines) {
      Object[] result = new Object[lines.length];
      for (int i = 0; i < lines.length; i++) {
        String line = lines[i];
        result[i] = getWrapper(line);
      }
      return result;
    }

    @Override
    public DiffFragment[] buildFragments(String[] strings1, String[] strings2) throws FilesTooBigForDiffException {
      DiffFragment[] fragments = super.buildFragments(strings1, strings2);
      DiffCorrection.FragmentsCollector collector = new DiffCorrection.FragmentsCollector();
      collector.processAll(fragments, this);
      return collector.toArray();
    }

    private Object getWrapper(String line) {
      StringBuilder builder = new StringBuilder(line.length());
      for (int i = 0; i < line.length(); i++) {
        char aChar = line.charAt(i);
        if (StringUtil.isWhiteSpace(aChar)) continue;
        builder.append(aChar);
      }
      return builder.toString();
    }

    @Override
    public DiffFragment createFragment(Word word1, Word word2) {
      String text1 = word1.getText();
      String text2 = word2.getText();
      return word1.isWhitespace() && word2.isWhitespace() ?
             DiffFragment.unchanged(text1, text2) :
             createFragment(text1, text2);
    }

    @Override
    public DiffFragment createFragment(String text1, String text2) {
      String toCompare1 = toNotNull(text1);
      String toCompare2 = toNotNull(text2);
      if (getWrapper(toCompare1).equals(getWrapper(toCompare2))) {
        return DiffFragment.unchanged(toCompare1, toCompare2);
      }
      return new DiffFragment(text1, text2);
    }

    @Override
    protected Object[] getWrappers(String[] strings) {
      return trimStrings(strings);
    }

    @SuppressWarnings({"HardCodedStringLiteral"})
    public String toString() {
      return "IGNORE";
    }

    @Override
    public void process(DiffFragment fragment, DiffCorrection.FragmentsCollector collector) {
      if (fragment.isEqual()) {
        collector.add(fragment);
        return;
      }
      if (fragment.isOneSide()) {
        FragmentSide side = FragmentSide.chooseSide(fragment);
        String text = side.getText(fragment);
        String trimed = text.trim();
        if (trimed.isEmpty()) {
          collector.add(side.createFragment(text, "", false));
          return;
        }
      }
      collector.add(fragment);
    }
  }
}