Fix Hyperlink highlighting in the integrated terminal
The logic was not properly considering the prefix length when calculating the highlight length (the part of the text that's underlined) in the result object.
I've also added some comments on the parameters that are passed to ResultItem and to #createLabelFromString.
@@ -32,6 +32,7 @@ import com.intellij.util.ui.UIUtil;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;
+import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
@@ -59,12 +60,10 @@ public class BlazeTargetFilter implements Filter {
Matcher matcher = TARGET_PATTERN.matcher(line);
List<ResultItem> results = new ArrayList<>();
while (matcher.find()) {
- String labelString = matcher.group();
- String prefix = matcher.group(1);
- if (prefix != null) {
- labelString = labelString.substring(prefix.length());
- }
- Label label = LabelUtils.createLabelFromString(null, labelString);
+ int prefixLength = Optional.ofNullable(matcher.group(1)).map(String::length).orElse(0);
+
+ String labelString = matcher.group().substring(prefixLength);
+ Label label = LabelUtils.createLabelFromString(/* blazePackage= */ null, labelString);
if (label == null) {
continue;
}
@@ -79,7 +78,10 @@ public class BlazeTargetFilter implements Filter {
int offset = entireLength - line.length();
results.add(
new ResultItem(
- matcher.start() + offset, matcher.end() + offset, link, getHighlightAttributes()));
+ /* highlightStartOffset= */ matcher.start() + offset + prefixLength,
+ /* highlightEndOffset= */ matcher.end() + offset,
+ link,
+ getHighlightAttributes()));
}
return results.isEmpty() ? null : new Result(results);
}
