implements PLN-0095
This commit is contained in:
+72
@@ -0,0 +1,72 @@
|
||||
package p.studio.compiler.pbs;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class PbsDocumentationTextBlockNormalizer {
|
||||
private static final String DELIMITER = "\"\"\"";
|
||||
|
||||
private PbsDocumentationTextBlockNormalizer() {
|
||||
}
|
||||
|
||||
public static String normalizeLexeme(final String lexeme) {
|
||||
if (lexeme == null || !lexeme.startsWith(DELIMITER) || !lexeme.endsWith(DELIMITER) || lexeme.length() < 6) {
|
||||
return "";
|
||||
}
|
||||
return normalizeContent(lexeme.substring(DELIMITER.length(), lexeme.length() - DELIMITER.length()));
|
||||
}
|
||||
|
||||
public static String normalizeContent(final String rawContent) {
|
||||
final var lines = splitLines(rawContent == null ? "" : rawContent.replace("\r\n", "\n").replace('\r', '\n'));
|
||||
removeIncidentalBoundaryLines(lines);
|
||||
final int commonIndent = commonIndent(lines);
|
||||
if (commonIndent > 0) {
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
final var line = lines.get(i);
|
||||
if (line.isBlank()) {
|
||||
lines.set(i, "");
|
||||
} else {
|
||||
lines.set(i, line.substring(Math.min(commonIndent, leadingWhitespace(line))));
|
||||
}
|
||||
}
|
||||
}
|
||||
return String.join("\n", lines);
|
||||
}
|
||||
|
||||
private static List<String> splitLines(final String text) {
|
||||
final var lines = new ArrayList<String>();
|
||||
final var parts = text.split("\n", -1);
|
||||
for (final var part : parts) {
|
||||
lines.add(part);
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
private static void removeIncidentalBoundaryLines(final List<String> lines) {
|
||||
if (!lines.isEmpty() && lines.getFirst().isBlank()) {
|
||||
lines.removeFirst();
|
||||
}
|
||||
if (!lines.isEmpty() && lines.getLast().isBlank()) {
|
||||
lines.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
private static int commonIndent(final List<String> lines) {
|
||||
var common = Integer.MAX_VALUE;
|
||||
for (final var line : lines) {
|
||||
if (line.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
common = Math.min(common, leadingWhitespace(line));
|
||||
}
|
||||
return common == Integer.MAX_VALUE ? 0 : common;
|
||||
}
|
||||
|
||||
private static int leadingWhitespace(final String line) {
|
||||
var count = 0;
|
||||
while (count < line.length() && Character.isWhitespace(line.charAt(count))) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package p.studio.compiler.pbs;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class PbsDocumentationTextBlockNormalizerTest {
|
||||
|
||||
@Test
|
||||
void shouldNormalizeIncidentalBoundaryLinesAndIndentation() {
|
||||
final var lexeme = "\"\"\"\n" +
|
||||
" Draws a sprite.\n" +
|
||||
"\n" +
|
||||
" - `x`: screen x\n" +
|
||||
" - `y`: screen y\n" +
|
||||
" \"\"\"";
|
||||
|
||||
final var normalized = PbsDocumentationTextBlockNormalizer.normalizeLexeme(lexeme);
|
||||
|
||||
assertEquals("Draws a sprite.\n\n- `x`: screen x\n- `y`: screen y", normalized);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldPreserveInternalLineBreaksAndMarkdownContent() {
|
||||
final var content = "\n" +
|
||||
" Paragraph one.\n" +
|
||||
" \n" +
|
||||
" `code_span()`\n" +
|
||||
" Paragraph two.\n";
|
||||
|
||||
final var normalized = PbsDocumentationTextBlockNormalizer.normalizeContent(content);
|
||||
|
||||
assertEquals("Paragraph one.\n\n`code_span()`\nParagraph two.", normalized);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnEmptyStringForWhitespaceOnlyPayload() {
|
||||
final var normalized = PbsDocumentationTextBlockNormalizer.normalizeLexeme("\"\"\"\n \n \t\n\"\"\"");
|
||||
|
||||
assertEquals("", normalized.trim());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNormalizeCarriageReturnLineEndings() {
|
||||
final var normalized = PbsDocumentationTextBlockNormalizer.normalizeContent("\r\n\tLine one\r\n\tLine two\r\n");
|
||||
|
||||
assertEquals("Line one\nLine two", normalized);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnEmptyStringForMalformedLexeme() {
|
||||
assertEquals("", PbsDocumentationTextBlockNormalizer.normalizeLexeme("\"not a text block\""));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user