Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ State getState() {
}

List<SourceSpan> removeLines(int lines) {
var removedSpans =
Collections.unmodifiableList(
new ArrayList<>(
sourceSpans.subList(
Math.max(sourceSpans.size() - lines, 0),
sourceSpans.size())));
var l = paragraphLines.getLines();
paragraphLines = SourceLines.of(l.subList(0, Math.max(l.size() - lines, 0)));
removeLast(lines, sourceSpans);
int removedLineCount = Math.min(lines, l.size());
int removedSpanCount =
Math.min(removedLineCount, sourceSpans.size() - referenceSourceSpanCount);
var removedSpanView =
sourceSpans.subList(sourceSpans.size() - removedSpanCount, sourceSpans.size());
var removedSpans = Collections.unmodifiableList(new ArrayList<>(removedSpanView));
paragraphLines = SourceLines.of(l.subList(0, l.size() - removedLineCount));
removedSpanView.clear();
return removedSpans;
}

Expand Down Expand Up @@ -308,16 +308,6 @@ private void finishReference() {
title = null;
}

private static <T> void removeLast(int n, List<T> list) {
if (n >= list.size()) {
list.clear();
} else {
for (int i = 0; i < n; i++) {
list.remove(list.size() - 1);
}
}
}

enum State {
// Looking for the start of a definition, i.e. `[`
START_DEFINITION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,45 @@ void testMultilineTitleWithTrailingGarbageKeepsParagraphSourceSpans() {
assertThat(parser.getParagraphSourceSpans()).containsExactly(titleSpan, invalidSpan);
}

@Test
void testRemoveLinesPreservesCommittedReferenceSourceSpans() {
var definitionSpan = SourceSpan.of(0, 0, 0, 11);
var titleSpan = SourceSpan.of(1, 0, 12, 6);

parse("[foo]: /url");
parser.addSourceSpan(definitionSpan);
parse("\"title");
parser.addSourceSpan(titleSpan);

var removed = parser.removeLines(Integer.MAX_VALUE);

assertThat(removed).containsExactly(titleSpan);
var definitions = parser.getDefinitions();
assertThat(definitions).hasSize(1);
assertDef(definitions.get(0), "foo", "/url", null);
assertThat(definitions.get(0).getSourceSpans()).containsExactly(definitionSpan);
assertParagraphLines("", parser);
assertThat(parser.getParagraphSourceSpans()).isEmpty();
}

@Test
void testRemoveLinesWithoutParagraphLinesPreservesCommittedReferenceSourceSpans() {
var definitionSpan = SourceSpan.of(0, 0, 0, 11);

parse("[foo]: /url");
parser.addSourceSpan(definitionSpan);

var removed = parser.removeLines(Integer.MAX_VALUE);

assertThat(removed).isEmpty();
var definitions = parser.getDefinitions();
assertThat(definitions).hasSize(1);
assertDef(definitions.get(0), "foo", "/url", null);
assertThat(definitions.get(0).getSourceSpans()).containsExactly(definitionSpan);
assertParagraphLines("", parser);
assertThat(parser.getParagraphSourceSpans()).isEmpty();
}

@Test
void testTitleInvalid() {
assertParagraph("[foo]: /url (invalid(");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,33 @@ public void replaceActiveBlockParser() {
assertThat(bc.getSourceSpans()).isEqualTo(List.of(SourceSpan.of(1, 0, 2, 2)));
}

@Test
public void replaceActiveBlockParserAfterLinkReferenceDefinitionWithUnclosedTitle() {
var parser =
Parser.builder()
.customBlockParserFactory(new StarHeadingBlockParser.Factory())
.includeSourceSpans(IncludeSourceSpans.BLOCKS_AND_INLINES)
.build();

var doc = parser.parse("[foo]: /url\n\"title\n***\n");

var def = (LinkReferenceDefinition) doc.getFirstChild();
assertThat(def.getLabel()).isEqualTo("foo");
assertThat(def.getDestination()).isEqualTo("/url");
assertThat(def.getTitle()).isNull();
assertThat(def.getSourceSpans()).isEqualTo(List.of(SourceSpan.of(0, 0, 0, 11)));

var heading = def.getNext();
assertThat(heading).isInstanceOf(StarHeading.class);
assertThat(heading.getNext()).isNull();
var text = (Text) heading.getFirstChild();
assertThat(text.getLiteral()).isEqualTo("\"title");
assertThat(text.getNext()).isNull();
assertThat(heading.getSourceSpans())
.isEqualTo(List.of(SourceSpan.of(1, 0, 12, 6), SourceSpan.of(2, 0, 19, 3)));
assertThat(text.getSourceSpans()).isEqualTo(List.of(SourceSpan.of(1, 0, 12, 6)));
}

private static class DashBlock extends CustomBlock {}

private static class DashBlockParser extends AbstractBlockParser {
Expand Down