Skip to content
This repository has been archived by the owner on Dec 20, 2017. It is now read-only.

Commit

Permalink
#111 : Fix thread safety issue
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoulaj committed May 9, 2014
1 parent 3965fa4 commit 67e64af
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,24 @@ public class MarkdownAnnotator extends ExternalAnnotator<String, Set<MarkdownAnn
private static final SyntaxHighlighter SYNTAX_HIGHLIGHTER = new MarkdownSyntaxHighlighter();

/** The {@link PegDownProcessor} used for building the document AST. */
private PegDownProcessor processor = new PegDownProcessor(MarkdownGlobalSettings.getInstance().getExtensionsValue(),
MarkdownGlobalSettings.getInstance().getParsingTimeout());
private ThreadLocal<PegDownProcessor> processor = initProcessor();

/** Init/reinit thread local {@link PegDownProcessor}. */
private static ThreadLocal<PegDownProcessor> initProcessor() {
return new ThreadLocal<PegDownProcessor>() {
@Override protected PegDownProcessor initialValue() {
return new PegDownProcessor(MarkdownGlobalSettings.getInstance().getExtensionsValue(),
MarkdownGlobalSettings.getInstance().getParsingTimeout());
}
};
}

/** Build a new instance of {@link MarkdownAnnotator}. */
public MarkdownAnnotator() {
// Listen to global settings changes.
MarkdownGlobalSettings.getInstance().addListener(new MarkdownGlobalSettingsListener() {
public void handleSettingsChanged(@NotNull final MarkdownGlobalSettings newSettings) {
processor = new PegDownProcessor(newSettings.getExtensionsValue(),
newSettings.getParsingTimeout());
initProcessor();
}
});
}
Expand All @@ -97,7 +105,7 @@ public String collectInformation(@NotNull PsiFile file) {
public Set<HighlightableToken> doAnnotate(final String source) {
final MarkdownASTVisitor visitor = new MarkdownASTVisitor();
try {
processor.parseMarkdown(source.toCharArray()).accept(visitor);
processor.get().parseMarkdown(source.toCharArray()).accept(visitor);
} catch (Exception e) {
LOGGER.error("Failed processing Markdown document", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,18 @@ public class MarkdownPreviewEditor extends UserDataHolderBase implements FileEdi
/** The {@link Document} previewed in this editor. */
protected final Document document;

/** The MarkdownJ {@link PegDownProcessor} used to generate HTML from Markdown. */
protected PegDownProcessor markdownProcessor = new PegDownProcessor(MarkdownGlobalSettings.getInstance().getExtensionsValue(),
MarkdownGlobalSettings.getInstance().getParsingTimeout());
/** The {@link PegDownProcessor} used for building the document AST. */
private ThreadLocal<PegDownProcessor> processor = initProcessor();

/** Init/reinit thread local {@link PegDownProcessor}. */
private static ThreadLocal<PegDownProcessor> initProcessor() {
return new ThreadLocal<PegDownProcessor>() {
@Override protected PegDownProcessor initialValue() {
return new PegDownProcessor(MarkdownGlobalSettings.getInstance().getExtensionsValue(),
MarkdownGlobalSettings.getInstance().getParsingTimeout());
}
};
}

/** Indicates whether the HTML preview is obsolete and should regenerated from the Markdown {@link #document}. */
protected boolean previewIsObsolete = true;
Expand All @@ -108,8 +117,7 @@ public void documentChanged(DocumentEvent e) {
// Listen to settings changes
MarkdownGlobalSettings.getInstance().addListener(new MarkdownGlobalSettingsListener() {
public void handleSettingsChanged(@NotNull final MarkdownGlobalSettings newSettings) {
markdownProcessor = new PegDownProcessor(newSettings.getExtensionsValue(),
newSettings.getParsingTimeout());
initProcessor();
previewIsObsolete = true;
}
});
Expand All @@ -123,7 +131,7 @@ public void handleSettingsChanged(@NotNull final MarkdownGlobalSettings newSetti
jEditorPane.setEditable(false);

// Set the editor pane position to top left, and do not let it reset it
jEditorPane.getCaret().setMagicCaretPosition(new Point(0,0));
jEditorPane.getCaret().setMagicCaretPosition(new Point(0, 0));
((DefaultCaret) jEditorPane.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE);

// Add a custom link listener which can resolve local link references.
Expand Down Expand Up @@ -213,7 +221,7 @@ public void selectNotify() {
if (previewIsObsolete) {
try {
jEditorPane.setText("<div id=\"markdown-preview\">" +
markdownProcessor.markdownToHtml(document.getText()) +
processor.get().markdownToHtml(document.getText()) +
"</div>");
previewIsObsolete = false;
} catch (Exception e) {
Expand Down

0 comments on commit 67e64af

Please sign in to comment.