diff options
author | David Green | 2015-05-23 01:10:43 +0000 |
---|---|---|
committer | David Green | 2015-05-23 01:10:43 +0000 |
commit | 8e8f82c48949b6fd2bf93db58152aca59f6945b5 (patch) | |
tree | 5035de29c82752dcf2b9bd5caf36024a2d8c6b45 | |
parent | 09b08d749c1c6183261d13a3c15687af444126fb (diff) | |
download | org.eclipse.mylyn.docs-8e8f82c48949b6fd2bf93db58152aca59f6945b5.tar.gz org.eclipse.mylyn.docs-8e8f82c48949b6fd2bf93db58152aca59f6945b5.tar.xz org.eclipse.mylyn.docs-8e8f82c48949b6fd2bf93db58152aca59f6945b5.zip |
467275: introduce a builder for ProcessingContext
Introduce a builder for ProcessingContext to make the possibility of
modifications explicit.
Change-Id: If627e8001487ea97cade0d178176db6a1ec03256
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=467275
15 files changed, 132 insertions, 112 deletions
diff --git a/org.eclipse.mylyn.wikitext.commonmark.tests/src/org/eclipse/mylyn/internal/wikitext/commonmark/ProcessingContextTest.java b/org.eclipse.mylyn.wikitext.commonmark.tests/src/org/eclipse/mylyn/internal/wikitext/commonmark/ProcessingContextTest.java index d69b0b858..2bfd62877 100644 --- a/org.eclipse.mylyn.wikitext.commonmark.tests/src/org/eclipse/mylyn/internal/wikitext/commonmark/ProcessingContextTest.java +++ b/org.eclipse.mylyn.wikitext.commonmark.tests/src/org/eclipse/mylyn/internal/wikitext/commonmark/ProcessingContextTest.java @@ -14,27 +14,29 @@ package org.eclipse.mylyn.internal.wikitext.commonmark; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContext.NamedUriWithTitle; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class ProcessingContextTest { + @Rule + public final ExpectedException thrown = ExpectedException.none(); + @Test public void empty() { ProcessingContext context = ProcessingContext.empty(); assertNotNull(context); assertTrue(context.isEmpty()); - assertSame(context, ProcessingContext.empty()); } @Test - public void withReferenceDefinition() { - ProcessingContext context = ProcessingContext.withReferenceDefinition("onE", "/uri", "a title"); + public void referenceDefinition() { + ProcessingContext context = ProcessingContext.builder().referenceDefinition("onE", "/uri", "a title").build(); assertNotNull(context); assertFalse(context.isEmpty()); assertNotNull(context.namedUriWithTitle("one")); @@ -44,30 +46,22 @@ public class ProcessingContextTest { assertEquals("/uri", link.getUri()); assertEquals("a title", link.getTitle()); assertNull(context.namedUriWithTitle("Unknown")); - assertSame(ProcessingContext.empty(), ProcessingContext.withReferenceDefinition("", "one", "two")); + } @Test - public void merge() { - ProcessingContext empty1 = ProcessingContext.empty(); - ProcessingContext empty2 = ProcessingContext.empty(); - assertSame(empty1, empty1.merge(empty2)); - ProcessingContext other = ProcessingContext.withReferenceDefinition("one", "/uri", "a title"); - assertSame(other, other.merge(empty1)); - assertSame(other, empty1.merge(other)); - ProcessingContext other2 = ProcessingContext.withReferenceDefinition("two", "/uri2", "a title"); - ProcessingContext merged = other.merge(other2); - assertNotSame(other2, merged); - assertNotSame(other, merged); - assertNotNull(merged.namedUriWithTitle("two")); - assertNotNull(merged.namedUriWithTitle("one")); + public void referenceDefinitionEmptyName() { + assertTrue(ProcessingContext.builder().referenceDefinition("", "one", "two").build().isEmpty()); } @Test - public void precedence() { - ProcessingContext one = ProcessingContext.withReferenceDefinition("one", "1", "a title"); - ProcessingContext one2 = ProcessingContext.withReferenceDefinition("one", "2", "a title"); - ProcessingContext merged = one.merge(one2); - assertEquals("1", merged.namedUriWithTitle("one").getUri()); + public void referenceDefinitionDuplicate() { + ProcessingContext context = ProcessingContext.builder() + .referenceDefinition("a", "/uri", "a title") + .referenceDefinition("a", "/uri2", "a title2") + .build(); + NamedUriWithTitle uriWithTitle = context.namedUriWithTitle("a"); + assertEquals("/uri", uriWithTitle.getUri()); } + } diff --git a/org.eclipse.mylyn.wikitext.commonmark.tests/src/org/eclipse/mylyn/internal/wikitext/commonmark/inlines/InlineTest.java b/org.eclipse.mylyn.wikitext.commonmark.tests/src/org/eclipse/mylyn/internal/wikitext/commonmark/inlines/InlineTest.java index 19e3350f4..9294c0c1e 100644 --- a/org.eclipse.mylyn.wikitext.commonmark.tests/src/org/eclipse/mylyn/internal/wikitext/commonmark/inlines/InlineTest.java +++ b/org.eclipse.mylyn.wikitext.commonmark.tests/src/org/eclipse/mylyn/internal/wikitext/commonmark/inlines/InlineTest.java @@ -13,9 +13,11 @@ package org.eclipse.mylyn.internal.wikitext.commonmark.inlines; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; import org.eclipse.mylyn.internal.wikitext.commonmark.Line; import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContext; +import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContextBuilder; import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder; import org.eclipse.mylyn.wikitext.core.parser.Locator; import org.junit.Test; @@ -45,12 +47,14 @@ public class InlineTest { @Test public void createContext() { - ProcessingContext context = new Inline(new Line(1, 2, "text"), 0, 1) { + ProcessingContextBuilder builder = ProcessingContext.builder(); + new Inline(new Line(1, 2, "text"), 0, 1) { @Override public void emit(DocumentBuilder builder) { } - }.createContext(); - assertSame(ProcessingContext.empty(), context); + }.createContext(builder); + ProcessingContext context = builder.build(); + assertTrue(context.isEmpty()); } } diff --git a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/ProcessingContext.java b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/ProcessingContext.java index 3e161bd82..5b36aa14c 100644 --- a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/ProcessingContext.java +++ b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/ProcessingContext.java @@ -13,16 +13,17 @@ package org.eclipse.mylyn.internal.wikitext.commonmark; import static com.google.common.base.Preconditions.checkNotNull; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; - -import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; public class ProcessingContext { - private static final ProcessingContext EMPTY_CONTEXT = new ProcessingContext(); + public static ProcessingContextBuilder builder() { + return new ProcessingContextBuilder(); + } + + public static ProcessingContext empty() { + return builder().build(); + } public static class NamedUriWithTitle { @@ -53,45 +54,15 @@ public class ProcessingContext { private final ImmutableMap<String, NamedUriWithTitle> links; - private ProcessingContext(ImmutableMap<String, NamedUriWithTitle> links) { + ProcessingContext(ImmutableMap<String, NamedUriWithTitle> links) { this.links = checkNotNull(links); } - public ProcessingContext() { - this(ImmutableMap.<String, NamedUriWithTitle> of()); - } - - public static ProcessingContext empty() { - return EMPTY_CONTEXT; - } - - public static ProcessingContext withReferenceDefinition(String name, String href, String title) { - if (Strings.isNullOrEmpty(name)) { - return empty(); - } - return new ProcessingContext(ImmutableMap.of(name.toLowerCase(Locale.ROOT), new NamedUriWithTitle(name, href, - title))); - } - public boolean isEmpty() { return links.isEmpty(); } - public ProcessingContext merge(ProcessingContext other) { - checkNotNull(other); - if (other.isEmpty()) { - return this; - } else if (isEmpty()) { - return other; - } - Map<String, NamedUriWithTitle> mergedLinks = new HashMap<>(); - mergedLinks.putAll(other.links); - mergedLinks.putAll(links); - return new ProcessingContext(ImmutableMap.copyOf(mergedLinks)); - } - public NamedUriWithTitle namedUriWithTitle(String name) { return links.get(name.toLowerCase()); } - } diff --git a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/ProcessingContextBuilder.java b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/ProcessingContextBuilder.java new file mode 100644 index 000000000..8cd11de47 --- /dev/null +++ b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/ProcessingContextBuilder.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2015 David Green. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * David Green - initial API and implementation + *******************************************************************************/ + +package org.eclipse.mylyn.internal.wikitext.commonmark; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContext.NamedUriWithTitle; + +import com.google.common.base.Strings; +import com.google.common.collect.ImmutableMap; + +public class ProcessingContextBuilder { + + private final Map<String, NamedUriWithTitle> linkByName = new HashMap<>(); + + public ProcessingContextBuilder referenceDefinition(String name, String href, String title) { + if (!Strings.isNullOrEmpty(name)) { + String key = name.toLowerCase(Locale.ROOT); + if (!linkByName.containsKey(key)) { + linkByName.put(key, new NamedUriWithTitle(name, href, title)); + } + } + return this; + } + + public ProcessingContext build() { + return new ProcessingContext(ImmutableMap.copyOf(linkByName)); + } + + ProcessingContextBuilder() { + // prevent instantiation + } +} diff --git a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/SourceBlock.java b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/SourceBlock.java index 753e67c0d..a0df18e33 100644 --- a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/SourceBlock.java +++ b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/SourceBlock.java @@ -19,9 +19,7 @@ public abstract class SourceBlock { public abstract boolean canStart(LineSequence lineSequence); - public ProcessingContext createContext(LineSequence lineSequence) { - ProcessingContext context = ProcessingContext.empty(); - process(context, new NoOpDocumentBuilder(), lineSequence); - return context; + public void createContext(ProcessingContextBuilder contextBuilder, LineSequence lineSequence) { + process(contextBuilder.build(), new NoOpDocumentBuilder(), lineSequence); } } diff --git a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/SourceBlocks.java b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/SourceBlocks.java index 772f32bdc..2958433fa 100644 --- a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/SourceBlocks.java +++ b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/SourceBlocks.java @@ -16,7 +16,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.concurrent.atomic.AtomicReference; import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder; import org.eclipse.mylyn.wikitext.core.parser.builder.NoOpDocumentBuilder; @@ -69,22 +68,20 @@ public class SourceBlocks extends SourceBlock { void run(LineSequence lineSequence, SourceBlock sourceBlock); } - public ProcessingContext createContext(LineSequence lineSequence, Predicate<BlockContext> contextPredicate) { - final AtomicReference<ProcessingContext> context = new AtomicReference<ProcessingContext>( - ProcessingContext.empty()); + public void createContext(final ProcessingContextBuilder contextBuilder, LineSequence lineSequence, + Predicate<BlockContext> contextPredicate) { process(lineSequence, new SourceBlockRunnable() { @Override public void run(LineSequence lineSequence, SourceBlock sourceBlock) { - context.set(checkNotNull(context.get()).merge(sourceBlock.createContext(lineSequence))); + sourceBlock.createContext(contextBuilder, lineSequence); } }, contextPredicate); - return checkNotNull(context.get()); } @Override - public ProcessingContext createContext(LineSequence lineSequence) { - return createContext(lineSequence, Predicates.<BlockContext> alwaysTrue()); + public void createContext(ProcessingContextBuilder contextBuilder, LineSequence lineSequence) { + createContext(contextBuilder, lineSequence, Predicates.<BlockContext> alwaysTrue()); } @Override diff --git a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/BlockQuoteBlock.java b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/BlockQuoteBlock.java index 4b5036002..bcd5069d8 100644 --- a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/BlockQuoteBlock.java +++ b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/BlockQuoteBlock.java @@ -22,6 +22,7 @@ import org.eclipse.mylyn.internal.wikitext.commonmark.CommonMark; import org.eclipse.mylyn.internal.wikitext.commonmark.Line; import org.eclipse.mylyn.internal.wikitext.commonmark.LineSequence; import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContext; +import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContextBuilder; import org.eclipse.mylyn.internal.wikitext.commonmark.SourceBlock; import org.eclipse.mylyn.internal.wikitext.commonmark.SourceBlocks; import org.eclipse.mylyn.internal.wikitext.commonmark.SourceBlocks.BlockContext; @@ -50,10 +51,10 @@ public class BlockQuoteBlock extends BlockWithNestedBlocks { } @Override - public ProcessingContext createContext(LineSequence lineSequence) { + public void createContext(ProcessingContextBuilder contextBuilder, LineSequence lineSequence) { SourceBlocks sourceBlocks = CommonMark.sourceBlocks(); BlockQuoteState blockQuoteState = new BlockQuoteState(); - return sourceBlocks.createContext(blockQuoteState.blockQuoteLineSequence(lineSequence), + sourceBlocks.createContext(contextBuilder, blockQuoteState.blockQuoteLineSequence(lineSequence), blockQuoteState.contextPredicate()); } @@ -107,7 +108,8 @@ public class BlockQuoteBlock extends BlockWithNestedBlocks { } private boolean isLazyContinuation(SourceBlock lineBlock) { - return lineBlock instanceof ParagraphBlock && (currentBlock instanceof BlockQuoteBlock || currentBlock instanceof ParagraphBlock); + return lineBlock instanceof ParagraphBlock + && (currentBlock instanceof BlockQuoteBlock || currentBlock instanceof ParagraphBlock); } private LineSequence createLookAhead(LineSequence lineSequence, Line line) { diff --git a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/BlockWithNestedBlocks.java b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/BlockWithNestedBlocks.java index 66386890c..e40035af2 100644 --- a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/BlockWithNestedBlocks.java +++ b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/BlockWithNestedBlocks.java @@ -12,11 +12,11 @@ package org.eclipse.mylyn.internal.wikitext.commonmark.blocks; import org.eclipse.mylyn.internal.wikitext.commonmark.LineSequence; -import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContext; +import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContextBuilder; import org.eclipse.mylyn.internal.wikitext.commonmark.SourceBlock; abstract class BlockWithNestedBlocks extends SourceBlock { @Override - public abstract ProcessingContext createContext(LineSequence lineSequence); + public abstract void createContext(ProcessingContextBuilder contextBuilder, LineSequence lineSequence); } diff --git a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/ListBlock.java b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/ListBlock.java index 7024dc818..23e481477 100644 --- a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/ListBlock.java +++ b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/ListBlock.java @@ -14,7 +14,6 @@ package org.eclipse.mylyn.internal.wikitext.commonmark.blocks; import static com.google.common.base.Preconditions.checkState; import java.util.List; -import java.util.concurrent.atomic.AtomicReference; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -22,6 +21,7 @@ import org.eclipse.mylyn.internal.wikitext.commonmark.CommonMark; import org.eclipse.mylyn.internal.wikitext.commonmark.Line; import org.eclipse.mylyn.internal.wikitext.commonmark.LineSequence; import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContext; +import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContextBuilder; import org.eclipse.mylyn.internal.wikitext.commonmark.SourceBlock; import org.eclipse.mylyn.internal.wikitext.commonmark.SourceBlocks; import org.eclipse.mylyn.internal.wikitext.commonmark.SourceBlocks.BlockContext; @@ -37,7 +37,8 @@ import com.google.common.base.Predicate; public class ListBlock extends BlockWithNestedBlocks { - private final Pattern bulletPattern = Pattern.compile("\\s{0,3}(([*+-])|(([0-9]{0,5})[.)]))(?:(?:\\s(\\s*)(.*))|\\s*$)"); + private final Pattern bulletPattern = Pattern + .compile("\\s{0,3}(([*+-])|(([0-9]{0,5})[.)]))(?:(?:\\s(\\s*)(.*))|\\s*$)"); private final HorizontalRuleBlock horizontalRuleBlock = new HorizontalRuleBlock(); @@ -64,19 +65,16 @@ public class ListBlock extends BlockWithNestedBlocks { } @Override - public ProcessingContext createContext(LineSequence lineSequence) { - final AtomicReference<ProcessingContext> context = new AtomicReference<>(ProcessingContext.empty()); - process(ProcessingContext.empty(), new NoOpDocumentBuilder(), lineSequence, new ListItemHandler() { - - @Override - public void emitListItem(ProcessingContext dummyContext, DocumentBuilder builder, ListMode listMode, - LineSequence lineSequence) { - ProcessingContext itemContext = CommonMark.sourceBlocks().createContext( - listItemLineSequence(lineSequence)); - context.set(context.get().merge(itemContext)); - } - }); - return context.get(); + public void createContext(final ProcessingContextBuilder contextBuilder, LineSequence lineSequence) { + process(ProcessingContext.builder().build(), new NoOpDocumentBuilder(), lineSequence, + new ListItemHandler() { + + @Override + public void emitListItem(ProcessingContext dummyContext, DocumentBuilder builder, ListMode listMode, + LineSequence lineSequence) { + CommonMark.sourceBlocks().createContext(contextBuilder, listItemLineSequence(lineSequence)); + } + }); } private void process(ProcessingContext context, DocumentBuilder builder, LineSequence lineSequence, diff --git a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/ParagraphBlock.java b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/ParagraphBlock.java index 1932ceffa..15e6ccac5 100644 --- a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/ParagraphBlock.java +++ b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/ParagraphBlock.java @@ -22,6 +22,7 @@ import org.eclipse.mylyn.internal.wikitext.commonmark.Line; import org.eclipse.mylyn.internal.wikitext.commonmark.LinePredicates; import org.eclipse.mylyn.internal.wikitext.commonmark.LineSequence; import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContext; +import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContextBuilder; import org.eclipse.mylyn.internal.wikitext.commonmark.SourceBlock; import org.eclipse.mylyn.internal.wikitext.commonmark.TextSegment; import org.eclipse.mylyn.internal.wikitext.commonmark.inlines.Inline; @@ -39,9 +40,9 @@ public class ParagraphBlock extends SourceBlock { SetextHeaderBlock.class); @Override - public ProcessingContext createContext(LineSequence lineSequence) { + public void createContext(ProcessingContextBuilder contextBuilder, LineSequence lineSequence) { TextSegment textSegment = extractTextSegment(lineSequence); - return new InlineContent().createContext(textSegment); + new InlineContent().createContext(contextBuilder, textSegment); } @Override @@ -49,7 +50,8 @@ public class ParagraphBlock extends SourceBlock { processInlines(context, builder, lineSequence, true); } - void processInlines(ProcessingContext context, DocumentBuilder builder, LineSequence lineSequence, boolean asBlock) { + void processInlines(ProcessingContext context, DocumentBuilder builder, LineSequence lineSequence, + boolean asBlock) { TextSegment textSegment = extractTextSegment(lineSequence); List<Inline> inlines = new InlineContent().parse(context, textSegment); if (!emptyParagraph(inlines)) { @@ -75,7 +77,8 @@ public class ParagraphBlock extends SourceBlock { private TextSegment extractTextSegment(LineSequence lineSequence) { List<Line> lines = new ArrayList<Line>(); - while (lineSequence.getCurrentLine() != null && notEmptyLine(lineSequence) && !anotherBlockStart(lineSequence)) { + while (lineSequence.getCurrentLine() != null && notEmptyLine(lineSequence) + && !anotherBlockStart(lineSequence)) { lines.add(lineSequence.getCurrentLine()); lineSequence.advance(); } diff --git a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/SetextHeaderBlock.java b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/SetextHeaderBlock.java index 9ab6c9017..9d1449e1b 100644 --- a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/SetextHeaderBlock.java +++ b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/blocks/SetextHeaderBlock.java @@ -41,7 +41,8 @@ public class SetextHeaderBlock extends SourceBlock { lineSequence.advance(); builder.setLocator(currentLine.toLocator()); - builder.beginHeading(headingLevel(matcher), new HeadingAttributes()); + int headingLevel = headingLevel(matcher); + builder.beginHeading(headingLevel, new HeadingAttributes()); new InlineContent().emit(context, builder, new TextSegment(Collections.singletonList(currentLine))); diff --git a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/inlines/Inline.java b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/inlines/Inline.java index b41ed77b8..9ced30004 100644 --- a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/inlines/Inline.java +++ b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/inlines/Inline.java @@ -19,6 +19,7 @@ import java.util.Objects; import org.eclipse.mylyn.internal.wikitext.commonmark.Line; import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContext; +import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContextBuilder; import org.eclipse.mylyn.internal.wikitext.commonmark.SimpleLocator; import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder; import org.eclipse.mylyn.wikitext.core.parser.Locator; @@ -69,8 +70,8 @@ public abstract class Inline { return Optional.absent(); } - public ProcessingContext createContext() { - return ProcessingContext.empty(); + public void createContext(ProcessingContextBuilder contextBuilder) { + // nothing to do } @Override diff --git a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/inlines/InlineParser.java b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/inlines/InlineParser.java index 28c8d4859..ef119702c 100644 --- a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/inlines/InlineParser.java +++ b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/inlines/InlineParser.java @@ -16,6 +16,7 @@ import java.util.Arrays; import java.util.List; import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContext; +import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContextBuilder; import org.eclipse.mylyn.internal.wikitext.commonmark.TextSegment; import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder; import org.eclipse.mylyn.wikitext.core.parser.builder.NoOpDocumentBuilder; @@ -47,12 +48,10 @@ public class InlineParser { } } - public ProcessingContext createContext(TextSegment textSegment) { - ProcessingContext context = ProcessingContext.empty(); - for (Inline inline : parse(context, textSegment)) { - context = context.merge(inline.createContext()); + public void createContext(ProcessingContextBuilder contextBuilder, TextSegment textSegment) { + for (Inline inline : parse(contextBuilder.build(), textSegment)) { + inline.createContext(contextBuilder); } - return context; } public List<Inline> parse(ProcessingContext context, TextSegment segment) { diff --git a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/inlines/ReferenceDefinition.java b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/inlines/ReferenceDefinition.java index 3a0d3bef2..237609e63 100644 --- a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/inlines/ReferenceDefinition.java +++ b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/internal/wikitext/commonmark/inlines/ReferenceDefinition.java @@ -16,7 +16,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.util.Objects; import org.eclipse.mylyn.internal.wikitext.commonmark.Line; -import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContext; +import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContextBuilder; import org.eclipse.mylyn.internal.wikitext.commonmark.ToStringHelper; import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder; @@ -45,8 +45,8 @@ public class ReferenceDefinition extends Inline { } @Override - public ProcessingContext createContext() { - return ProcessingContext.withReferenceDefinition(name, href, title); + public void createContext(ProcessingContextBuilder contextBuilder) { + contextBuilder.referenceDefinition(name, href, title); } @Override diff --git a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/wikitext/commonmark/CommonMarkLanguage.java b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/wikitext/commonmark/CommonMarkLanguage.java index 9b6cd566e..3f39c6de8 100644 --- a/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/wikitext/commonmark/CommonMarkLanguage.java +++ b/org.eclipse.mylyn.wikitext.commonmark/src/org/eclipse/mylyn/wikitext/commonmark/CommonMarkLanguage.java @@ -15,6 +15,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import org.eclipse.mylyn.internal.wikitext.commonmark.CommonMark; import org.eclipse.mylyn.internal.wikitext.commonmark.LineSequence; import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContext; +import org.eclipse.mylyn.internal.wikitext.commonmark.ProcessingContextBuilder; import org.eclipse.mylyn.internal.wikitext.commonmark.SourceBlocks; import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder; import org.eclipse.mylyn.wikitext.core.parser.MarkupParser; @@ -37,7 +38,9 @@ public class CommonMarkLanguage extends MarkupLanguage { } SourceBlocks sourceBlocks = CommonMark.sourceBlocks(); - ProcessingContext context = sourceBlocks.createContext(LineSequence.create(markupContent)); + + ProcessingContext context = createContext(sourceBlocks, markupContent); + sourceBlocks.process(context, builder, LineSequence.create(markupContent)); if (asDocument) { @@ -45,4 +48,9 @@ public class CommonMarkLanguage extends MarkupLanguage { } } + private ProcessingContext createContext(SourceBlocks sourceBlocks, String markupContent) { + ProcessingContextBuilder contextBuilder = ProcessingContext.builder(); + sourceBlocks.createContext(contextBuilder, LineSequence.create(markupContent)); + return contextBuilder.build(); + } } |