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

  // (2,3) - modified 2nd line
  // (2,2) - empty range between 1 and 2 lines
  // index of first line is 0
  private int myLine1;
  private int myLine2;
  private final int myVcsLine1;
  private final int myVcsLine2;
  private final byte myType;
  @Nullable private RangeHighlighter myRangeHighlighter;

  private boolean myValid = true;

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

    int line1 = shift + change.line1;
    int line2 = line1 + change.inserted;

    int vcsLine1 = vcsShift + change.line0;
    int vcsLine2 = vcsLine1 + change.deleted;

    return new Range(line1, line2, vcsLine1, vcsLine2, 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(@NotNull Range range) {
    this(range.getLine1(), range.getLine2(), range.getVcsLine1(), range.getVcsLine2(), range.getType());
  }

  public Range(int line1, int line2, int vcsLine1, int vcsLine2, byte type) {
    myLine1 = line1;
    myLine2 = line2;
    myVcsLine1 = vcsLine1;
    myVcsLine2 = vcsLine2;
    myType = type;
  }

  public int hashCode() {
    return myVcsLine1 ^ myVcsLine2 ^ myType ^ myLine1 ^ myLine2;
  }

  public boolean equals(Object object) {
    if (!(object instanceof Range)) return false;
    Range other = (Range)object;
    return
      (myVcsLine1 == other.myVcsLine1)
      && (myVcsLine2 == other.myVcsLine2)
      && (myLine1 == other.myLine1)
      && (myLine2 == other.myLine2)
      && (myType == other.myType);
  }

  public String toString() {
    return String.format("%s, %s, %s, %s, %s", myLine1, myLine2, myVcsLine1, myVcsLine2, 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 myVcsLine2 - myVcsLine1;
  }

  public void shift(int shift) {
    myLine1 += shift;
    myLine2 += shift;
  }

  public int getLine1() {
    return myLine1;
  }

  public int getLine2() {
    return myLine2;
  }

  public int getVcsLine1() {
    return myVcsLine1;
  }

  public int getVcsLine2() {
    return myVcsLine2;
  }

  public boolean rightBefore(@NotNull Range range) {
    return myLine2 == range.myLine1;
  }

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

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

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

  public boolean isValid() {
    return myValid;
  }

  public void invalidate() {
    myValid = false;
  }
}