■ ■ ■ ■ ■ ■ ■
commons-utils/src/main/java/io/onedev/commons/utils/PlanarRange.java
1 | 1 | | package io.onedev.commons.utils; |
2 | 2 | | |
3 | 3 | | import java.io.Serializable; |
| 4 | + | import java.util.List; |
4 | 5 | | |
5 | 6 | | import javax.annotation.Nullable; |
6 | 7 | | |
7 | 8 | | import org.apache.commons.lang3.StringUtils; |
8 | 9 | | import org.apache.commons.lang3.builder.EqualsBuilder; |
9 | 10 | | import org.apache.commons.lang3.builder.HashCodeBuilder; |
| 11 | + | |
| 12 | + | import com.google.common.base.Splitter; |
10 | 13 | | |
11 | 14 | | public class PlanarRange implements Serializable { |
12 | 15 | | |
13 | 16 | | private static final long serialVersionUID = 1L; |
14 | 17 | | |
15 | | - | private final int fromRow, fromColumn, toRow, toColumn; |
| 18 | + | private final int fromRow, fromColumn, toRow, toColumn, tabWidth; |
16 | 19 | | |
17 | | - | public PlanarRange(int fromRow, int fromColumn, int toRow, int toColumn) { |
| 20 | + | public PlanarRange(int fromRow, int fromColumn, int toRow, int toColumn, int tabWidth) { |
18 | 21 | | this.fromRow = fromRow; |
19 | 22 | | this.fromColumn = fromColumn; |
20 | 23 | | this.toRow = toRow; |
21 | 24 | | this.toColumn = toColumn; |
| 25 | + | this.tabWidth = tabWidth; |
| 26 | + | } |
| 27 | + | |
| 28 | + | public PlanarRange(int fromRow, int fromColumn, int toRow, int toColumn) { |
| 29 | + | this(fromRow, fromColumn, toRow, toColumn, 1); |
22 | 30 | | } |
23 | 31 | | |
24 | 32 | | public PlanarRange(PlanarRange range) { |
| skipped 1 lines |
26 | 34 | | fromColumn = range.fromColumn; |
27 | 35 | | toRow = range.toRow; |
28 | 36 | | toColumn = range.toColumn; |
| 37 | + | tabWidth = range.tabWidth; |
29 | 38 | | } |
30 | 39 | | |
31 | 40 | | public PlanarRange(String string) { |
32 | | - | String from = StringUtils.substringBefore(string, "-"); |
33 | | - | String to = StringUtils.substringAfter(string, "-"); |
| 41 | + | List<String> fields = Splitter.on("-").splitToList(string); |
| 42 | + | String from = fields.get(0); |
| 43 | + | String to = fields.get(1); |
34 | 44 | | fromRow = Integer.parseInt(StringUtils.substringBefore(from, "."))-1; |
35 | 45 | | fromColumn = Integer.parseInt(StringUtils.substringAfter(from, ".")); |
36 | 46 | | toRow = Integer.parseInt(StringUtils.substringBefore(to, "."))-1; |
37 | 47 | | toColumn = Integer.parseInt(StringUtils.substringAfter(to, ".")); |
| 48 | + | |
| 49 | + | if (fields.size() >= 3) |
| 50 | + | tabWidth = Integer.parseInt(fields.get(2)); |
| 51 | + | else |
| 52 | + | tabWidth = 1; |
38 | 53 | | } |
39 | 54 | | |
40 | 55 | | public int getFromRow() { |
| skipped 10 lines |
51 | 66 | | |
52 | 67 | | public int getToColumn() { |
53 | 68 | | return toColumn; |
| 69 | + | } |
| 70 | + | |
| 71 | + | public int getTabWidth() { |
| 72 | + | return tabWidth; |
54 | 73 | | } |
55 | 74 | | |
56 | 75 | | @Override |
57 | 76 | | public String toString() { |
58 | | - | return (fromRow+1) + "." + fromColumn + "-" + (toRow+1) + "." + toColumn; |
| 77 | + | return (fromRow+1) + "." + fromColumn + "-" + (toRow+1) + "." + toColumn + "-" + tabWidth; |
| 78 | + | } |
| 79 | + | |
| 80 | + | private int normalizeColumn(String lineContent, int column, int tabWidth) { |
| 81 | + | int pos = 0; |
| 82 | + | int normalizedColumn = 0; |
| 83 | + | for (int i = 0; i < lineContent.length(); i++) { |
| 84 | + | if (pos >= column) |
| 85 | + | break; |
| 86 | + | char ch = lineContent.charAt(i); |
| 87 | + | if (ch == '\t') |
| 88 | + | pos += tabWidth; |
| 89 | + | else |
| 90 | + | pos++; |
| 91 | + | normalizedColumn++; |
| 92 | + | } |
| 93 | + | return normalizedColumn; |
| 94 | + | } |
| 95 | + | |
| 96 | + | public PlanarRange normalize(List<String> lines) { |
| 97 | + | int normalizedFromRow, normalizedFromColumn, normalizedToRow, normalizedToColumn; |
| 98 | + | if (fromColumn == -1) { // only fromRow is set |
| 99 | + | normalizedFromRow = fromRow; |
| 100 | + | normalizedFromColumn = 0; |
| 101 | + | normalizedToRow = fromRow; |
| 102 | + | normalizedToColumn = lines.get(fromRow).length(); |
| 103 | + | } else if (toRow == -1) { // only fromRow and fromColumn is set |
| 104 | + | normalizedFromRow = fromRow; |
| 105 | + | normalizedFromColumn = normalizeColumn(lines.get(fromRow), fromColumn, tabWidth); |
| 106 | + | normalizedToRow = fromRow; |
| 107 | + | normalizedToColumn = normalizedFromColumn+1; |
| 108 | + | } else { |
| 109 | + | normalizedFromRow = fromRow; |
| 110 | + | normalizedFromColumn = normalizeColumn(lines.get(fromRow), fromColumn, tabWidth); |
| 111 | + | normalizedToRow = toRow; |
| 112 | + | normalizedToColumn = normalizeColumn(lines.get(toRow), toColumn, tabWidth); |
| 113 | + | } |
| 114 | + | return new PlanarRange(normalizedFromRow, normalizedFromColumn, normalizedToRow, |
| 115 | + | normalizedToColumn, 1); |
59 | 116 | | } |
60 | 117 | | |
61 | 118 | | @Override |
| skipped 8 lines |
70 | 127 | | .append(fromColumn, otherRange.fromColumn) |
71 | 128 | | .append(toRow, otherRange.toRow) |
72 | 129 | | .append(toColumn, otherRange.toColumn) |
| 130 | + | .append(tabWidth, otherRange.tabWidth) |
73 | 131 | | .isEquals(); |
74 | 132 | | } |
75 | 133 | | |
| skipped 4 lines |
80 | 138 | | .append(fromColumn) |
81 | 139 | | .append(toRow) |
82 | 140 | | .append(toColumn) |
| 141 | + | .append(tabWidth) |
83 | 142 | | .toHashCode(); |
84 | 143 | | } |
85 | 144 | | |
| skipped 9 lines |