summaryrefslogtreecommitdiff
path: root/compilerCommon/src/main/java/android/databinding/tool/store/Location.java
blob: 3342016cf77da5266868c3398fbd44ea4521f49f (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
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * 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 android.databinding.tool.store;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.apache.commons.lang3.StringUtils;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

/**
 * Identifies the range of a code block inside a file or a string.
 * Note that, unlike antlr4 tokens, the line positions start from 0 (to be compatible with Studio).
 * <p>
 * Both start and end line/column indices are inclusive.
 */
@XmlAccessorType(XmlAccessType.NONE)
public class Location {
    public static final int NaN = -1;
    @XmlAttribute(name = "startLine")
    public int startLine;
    @XmlAttribute(name = "startOffset")
    public int startOffset;
    @XmlAttribute(name = "endLine")
    public int endLine;
    @XmlAttribute(name = "endOffset")
    public int endOffset;
    @XmlElement(name = "parentLocation")
    public Location parentLocation;

    // for XML unmarshalling
    public Location() {
        startOffset = endOffset = startLine = endLine = NaN;
    }

    public Location(Location other) {
        startOffset = other.startOffset;
        endOffset = other.endOffset;
        startLine = other.startLine;
        endLine = other.endLine;
    }

    public Location(Token start, Token end) {
        if (start == null) {
            startLine = startOffset = NaN;
        } else {
            startLine = start.getLine() - 1; //token lines start from 1
            startOffset = start.getCharPositionInLine();
        }

        if (end == null) {
            endLine = endOffset = NaN;
        } else {
            endLine = end.getLine() - 1; // token lines start from 1
            String endText = end.getText();
            int lastLineStart = endText.lastIndexOf(System.lineSeparator());
            String lastLine = lastLineStart < 0 ? endText : endText.substring(lastLineStart + 1);
            endOffset = end.getCharPositionInLine() + lastLine.length() - 1;//end is inclusive
        }
    }

    public Location(ParserRuleContext context) {
        this(context == null ? null : context.getStart(),
                context == null ? null : context.getStop());
    }

    public Location(int startLine, int startOffset, int endLine, int endOffset) {
        this.startOffset = startOffset;
        this.startLine = startLine;
        this.endLine = endLine;
        this.endOffset = endOffset;
    }

    @Override
    public String toString() {
        return "Location{" +
                "startLine=" + startLine +
                ", startOffset=" + startOffset +
                ", endLine=" + endLine +
                ", endOffset=" + endOffset +
                ", parentLocation=" + parentLocation +
                '}';
    }

    public void setParentLocation(Location parentLocation) {
        this.parentLocation = parentLocation;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Location location = (Location) o;

        if (endLine != location.endLine) {
            return false;
        }
        if (endOffset != location.endOffset) {
            return false;
        }
        if (startLine != location.startLine) {
            return false;
        }
        if (startOffset != location.startOffset) {
            return false;
        }
        if (parentLocation != null ? !parentLocation.equals(location.parentLocation)
                : location.parentLocation != null) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = startLine;
        result = 31 * result + startOffset;
        result = 31 * result + endLine;
        result = 31 * result + endOffset;
        return result;
    }

    public boolean isValid() {
        return startLine != NaN && endLine != NaN && startOffset != NaN && endOffset != NaN;
    }

    public boolean contains(Location other) {
        if (startLine > other.startLine) {
            return false;
        }
        if (startLine == other.startLine && startOffset > other.startOffset) {
            return false;
        }
        if (endLine < other.endLine) {
            return false;
        }
        if (endLine == other.endLine && endOffset < other.endOffset) {
            return false;
        }
        return true;
    }

    private Location getValidParentAbsoluteLocation() {
        if (parentLocation == null) {
            return null;
        }
        if (parentLocation.isValid()) {
            return parentLocation.toAbsoluteLocation();
        }
        return parentLocation.getValidParentAbsoluteLocation();
    }

    public Location toAbsoluteLocation() {
        Location absoluteParent = getValidParentAbsoluteLocation();
        if (absoluteParent == null) {
            return this;
        }
        Location copy = new Location(this);
        boolean sameLine = copy.startLine == copy.endLine;
        if (copy.startLine == 0) {
            copy.startOffset += absoluteParent.startOffset;
        }
        if (sameLine) {
            copy.endOffset += absoluteParent.startOffset;
        }

        copy.startLine += absoluteParent.startLine;
        copy.endLine += absoluteParent.startLine;
        return copy;
    }

    public String toUserReadableString() {
        return startLine + ":" + startOffset + " - " + endLine + ":" + endOffset;
    }

    public static Location fromUserReadableString(String str) {
        int glue = str.indexOf('-');
        if (glue == -1) {
            return new Location();
        }
        String start = str.substring(0, glue);
        String end = str.substring(glue + 1);
        int[] point = new int[]{-1, -1};
        Location location = new Location();
        parsePoint(start, point);
        location.startLine = point[0];
        location.startOffset = point[1];
        point[0] = point[1] = -1;
        parsePoint(end, point);
        location.endLine = point[0];
        location.endOffset = point[1];
        return location;
    }

    private static boolean parsePoint(String content, int[] into) {
        int index = content.indexOf(':');
        if (index == -1) {
            return false;
        }
        into[0] = Integer.parseInt(content.substring(0, index).trim());
        into[1] = Integer.parseInt(content.substring(index + 1).trim());
        return true;
    }
}