Skip to content

Commit 092d614

Browse files
committed
Simplify lookup without ceiling.
1 parent 71c1308 commit 092d614

File tree

1 file changed

+7
-19
lines changed

1 file changed

+7
-19
lines changed

rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/util/locations/impl/TreeMapLookup.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,18 @@ private static int compareRanges(Range a, Range b) {
6969
return Integer.compare(aStart.getCharacter(), bStart.getCharacter());
7070
}
7171

72-
private @Nullable T contains(@Nullable Entry<Range, T> entry, Range from) {
73-
if (entry != null) {
74-
Range match = entry.getKey();
75-
if (Ranges.containsRange(match, from)) {
76-
return entry.getValue();
77-
}
78-
}
79-
return null;
80-
}
81-
8272
@Override
8373
public @Nullable T lookup(Range from) {
8474
// since we allow for overlapping ranges, it might be that we have to
8575
// search all the way to the "bottom" of the tree to see if we are
8676
// contained in something larger than the closest key
87-
var previousKeys = data.headMap(from, true).descendingMap();
88-
for (var candidate : previousKeys.entrySet()) {
89-
T result = contains(candidate, from);
90-
if (result != null) {
91-
return result;
92-
}
93-
}
94-
// could be that it's at the start of the entry (so the entry it not in the head map)
95-
return contains(data.ceilingEntry(from), from);
77+
// if we could come up with a *valid* ordering such that `data.floorKey(from)` is always
78+
// the smallest key containing `from` (or another key when none contain `from`), we could use `data.floorEntry` here instead of iterating
79+
return data.headMap(from, true).descendingMap().entrySet()
80+
.stream()
81+
.filter(e -> Ranges.containsRange(e.getKey(), from))
82+
.map(Entry::getValue)
83+
.findFirst().orElse(null);
9684
}
9785

9886
@Override

0 commit comments

Comments
 (0)