summaryrefslogtreecommitdiff
path: root/platform/vcs-impl/src/com/intellij/openapi/vcs/ex/Range.java
blob: 5727c62f4d14f5ae16bfa9aa49502b6523f6a011 (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
/*
 * Copyright 2000-2009 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.vcs.ex;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.markup.RangeHighlighter;
import com.intellij.util.diff.Diff;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * author: lesya
 */
public class Range {
  private static final Logger LOG = Logger.getInstance(Range.class);
  public static final byte MODIFIED = 1;
  public static final byte INSERTED = 2;
  public static final byte DELETED = 3;

  // offset1/offset2 - line numbers
  // (2,3) - modified 2nd line
  // (2,2) - empty range between 1 and 2 lines
  // index of first line is 0
  private int myOffset1;
  private int myOffset2;
  private final int myUpToDateOffset1;
  private final int myUpToDateOffset2;
  private final byte myType;
  @Nullable private RangeHighlighter myRangeHighlighter;

  public static Range createOn(@NotNull Diff.Change change, int shift, int upToDateShift) {
    byte type = getChangeTypeFrom(change);

    int offset1 = shift + change.line1;
    int offset2 = offset1 + change.inserted;

    int uOffset1 = upToDateShift + change.line0;
    int uOffset2 = uOffset1 + change.deleted;

    return new Range(offset1, offset2, uOffset1, uOffset2, type);
  }

  private static byte getChangeTypeFrom(@NotNull Diff.Change change) {
    if ((change.deleted > 0) && (change.inserted > 0)) return MODIFIED;
    if ((change.deleted > 0)) return DELETED;
    if ((change.inserted > 0)) return INSERTED;
    LOG.error("Unknown change type");
    return 0;
  }

  public Range(int offset1, int offset2, int uOffset1, int uOffset2, byte type) {
    myOffset1 = offset1;
    myOffset2 = offset2;
    myUpToDateOffset1 = uOffset1;
    myUpToDateOffset2 = uOffset2;
    myType = type;
  }

  public int hashCode() {
    return myUpToDateOffset1 ^ myUpToDateOffset2 ^ myType ^ myOffset1 ^ myOffset2;
  }

  public boolean equals(Object object) {
    if (!(object instanceof Range)) return false;
    Range other = (Range)object;
    return
      (myUpToDateOffset1 == other.myUpToDateOffset1)
      && (myUpToDateOffset2 == other.myUpToDateOffset2)
      && (myOffset1 == other.myOffset1)
      && (myOffset2 == other.myOffset2)
      && (myType == other.myType);
  }

  public String toString() {
    return String.format("%s, %s, %s, %s, %s", myOffset1, myOffset2, myUpToDateOffset1, myUpToDateOffset2, getTypeName());
  }

  @NonNls
  private String getTypeName() {
    switch (myType) {
      case MODIFIED:
        return "MODIFIED";
      case INSERTED:
        return "INSERTED";
      case DELETED:
        return "DELETED";
    }
    return "UNKNOWN";
  }

  public byte getType() {
    return myType;
  }

  public int getUpToDateRangeLength() {
    return myUpToDateOffset2 - myUpToDateOffset1;
  }

  public void shift(int shift) {
    myOffset1 += shift;
    myOffset2 += shift;
  }

  public int getOffset1() {
    return myOffset1;
  }

  public int getOffset2() {
    return myOffset2;
  }

  public int getUOffset1() {
    return myUpToDateOffset1;
  }

  public int getUOffset2() {
    return myUpToDateOffset2;
  }

  public boolean canBeMergedWith(@NotNull Range range) {
    return myOffset2 == range.myOffset1;
  }

  @NotNull
  public Range mergeWith(@NotNull Range range) {
    return new Range(myOffset1, range.myOffset2, myUpToDateOffset1, range.myUpToDateOffset2, mergedStatusWith(range));
  }

  private byte mergedStatusWith(@NotNull Range range) {
    byte type = myType;
    if (myType != range.myType) type = MODIFIED;
    return type;
  }

  public boolean hasHighlighter() {
    return myRangeHighlighter != null;
  }

  public void setHighlighter(RangeHighlighter highlighter) {
    myRangeHighlighter = highlighter;
  }

  @Nullable
  public RangeHighlighter getHighlighter() {
    return myRangeHighlighter;
  }
}