Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce guava code usage #886

Closed
wants to merge 1 commit into from
Closed

reduce guava code usage #886

wants to merge 1 commit into from

Conversation

sebthom
Copy link
Member

@sebthom sebthom commented Dec 23, 2023

This PR addresses #865 and removes most usages of the guava library.

The only location left is at

protected int getMaxSeverity(@NonNull IResource resource, @NonNull IDocument doc, @NonNull Range range)
throws CoreException, BadLocationException {
if (!severities.containsKey(resource)) {
refreshMarkersByLine(resource);
}
RangeMap<Integer, Integer> severitiesForResource = severities.get(resource);
if (severitiesForResource == null) {
return -1;
}
int bound1 = LSPEclipseUtils.toOffset(range.getStart(), doc);
int bound2 = LSPEclipseUtils.toOffset(range.getEnd(), doc);
// using bounds here because doc may have changed in the meantime so toOffset can return wrong results.
com.google.common.collect.Range<Integer> subRange = com.google.common.collect.Range.closed(
Math.min(bound1, bound2), // we guard that lower <= endOffset
bound2);
return severitiesForResource.subRangeMap(subRange)
.asMapOfRanges()
.values()
.stream()
.max(Comparator.naturalOrder())
.orElse(-1);
}
private void refreshMarkersByLine(IResource resource) throws CoreException {
RangeMap<Integer, Integer> rangeMap = TreeRangeMap.create();
Arrays.stream(resource.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_ZERO))
.filter(marker -> marker.getAttribute(IMarker.SEVERITY, -1) > IMarker.SEVERITY_INFO)
.sorted(Comparator.comparingInt(marker -> marker.getAttribute(IMarker.SEVERITY, -1)))
.forEach(marker -> {
int start = marker.getAttribute(IMarker.CHAR_START, -1);
int end = marker.getAttribute(IMarker.CHAR_END, -1);
if (end < start) {
end = start;
}
int severity = marker.getAttribute(IMarker.SEVERITY, -1);
if (start != end) {
com.google.common.collect.Range<Integer> markerRange = com.google.common.collect.Range.closed(start,end - 1);
rangeMap.remove(markerRange);
rangeMap.put(markerRange, severity);
}
});
severities.put(resource, rangeMap);
}
where com.google.common.collect.RangeMap is used.

I cannot really wrap my head around that code introduced by @mickaelistria with ae5a762. Maybe he is able to change the implementation to not require guava.

@mickaelistria
Copy link
Contributor

The change here doesn't make things simpler. So I wouldn't merge it yet if it's not enabling us to fully remove guava.
The getMaxServerity is a way to get the highest severity that affects a range: LSP diagnostics are attached to ranges, some can be overlapping, and the RangeMap allows to easily access the diagnostics for a range, including the included or overlapping ranges. I'm not aware of a better structure for that in standard JDK.

@mickaelistria
Copy link
Contributor

The initial target branch master was deleted and replaced by main, so this PR got closed automatically. If this is still relevant, please re-create this PR targetting the main branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants