Skip to content

Commit

Permalink
Use string builder for truncation
Browse files Browse the repository at this point in the history
  • Loading branch information
RadCod3 committed Mar 31, 2024
1 parent 08937c6 commit 876501f
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,24 @@ protected static TruncateResult truncate(String line, int maxLength, int diagnos
if (line.length() < maxLength - 3) {
return new TruncateResult(line, diagnosticStart, diagnosticLength);
}

StringBuilder truncatedLineBuilder = new StringBuilder();
String ellipsis = "...";
if (diagnosticStart + diagnosticLength <= maxLength - 3) {
return new TruncateResult(line.substring(0, maxLength - 3) + "...", diagnosticStart, diagnosticLength);
truncatedLineBuilder.append(line, 0, maxLength - 3).append(ellipsis);
return new TruncateResult(truncatedLineBuilder.toString(), diagnosticStart, diagnosticLength);
}

int diagnosticMid = diagnosticStart + (diagnosticLength / 2);
int stepsToMoveWindow = Math.max(0, diagnosticMid - (maxLength / 2));
int border = Math.min(line.length() - 1, stepsToMoveWindow + maxLength - 3);
int newDiagnosticStart = Math.max(3, diagnosticStart - stepsToMoveWindow);
int newDiagnosticLength = Math.min(diagnosticLength, maxLength - newDiagnosticStart - 3);
int stringStart = Math.min(stepsToMoveWindow + 3, border);
int stringEnd = border;

String truncatedLine = "..." + line.substring(stringStart, stringEnd) + "...";
return new TruncateResult(truncatedLine, newDiagnosticStart, Math.max(0, newDiagnosticLength));
truncatedLineBuilder.append(ellipsis).append(line, stringStart, border).append(ellipsis);
return new TruncateResult(truncatedLineBuilder.toString(), newDiagnosticStart,
Math.max(0, newDiagnosticLength));

}

Expand Down

0 comments on commit 876501f

Please sign in to comment.