forked from raghav-deepsource/java-tree-sitter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpan.java
More file actions
78 lines (63 loc) · 2.48 KB
/
Span.java
File metadata and controls
78 lines (63 loc) · 2.48 KB
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
package mytree;
import ai.serenade.treesitter.Range;
import java.util.ArrayList;
import java.util.List;
public class Span {
private Range range;
private List<Span> consumedSpans = new ArrayList<>();
public Span(Range range) {
this.range = range;
}
public void reset() {
consumedSpans.clear();
}
// For a span to contain another span, it has to appear first (or equal) in source and end last (or equal).
// TODO: Is it better to compare start and end bytes here? Explore that.
boolean contains(Span span) {
boolean firstInRowStart = range.startRow < span.range.startRow;
boolean equalInRowStart = range.startRow == span.range.startRow;
boolean firstInColStart = range.startCol < span.range.startCol;
boolean equalInColStart = range.startCol == span.range.startCol;
boolean lastInRowEnd = range.endRow > span.range.endRow;
boolean equalInRowEnd = range.endRow == span.range.endRow;
boolean lastInColEnd = range.endCol > span.range.endCol;
boolean equalInColEnd = range.endCol == span.range.endCol;
boolean startOkay = firstInRowStart || (equalInRowStart && (firstInColStart || equalInColStart));
boolean endOkay = lastInRowEnd || (equalInRowEnd && (lastInColEnd || equalInColEnd));
return startOkay && endOkay;
}
private boolean slotIsConsumed(Span span) {
// If a consumed span contains this span, then this span is consumed as well.
for (var consumed : consumedSpans) {
if (consumed.contains(span))
return true;
}
return false;
}
// TODO: Do we have to keep track of consumed spans at all?
public void consume(Span span) {
if (!this.contains(span))
throw new RuntimeException("This span doesn't contain the other span!");
if (slotIsConsumed(span))
throw new RuntimeException("Span slot is consumed!");
consumedSpans.add(span);
}
public int startRow() {
return range.startRow;
}
public int startCol() {
return range.startCol;
}
public int endRow() {
return range.endRow;
}
public int endCol() {
return range.endCol;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Span other))
return false;
return startRow() == other.startRow() && startCol() == other.startCol() && endRow() == other.endRow() && endCol() == other.endCol();
}
}