Skip to content

Commit

Permalink
avoid blocking lock when switching AsciiDoc editor when trying to sav…
Browse files Browse the repository at this point in the history
…e contents of changed documents (#520)
  • Loading branch information
ahus1 committed Jul 9, 2020
1 parent 6d51d64 commit 0daac52
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ This document provides a high-level view of the changes introduced by release.
[[releasenotes]]
== Release notes

=== 0.31.12 (work in progress)
=== 0.31.12 (preview, available from GitHub releases)

- highlighting and spell checking improved for arrows
- fix resolving of references by their titles
- support of JSON schema for Antora playbooks
- avoid blocking lock when switching AsciiDoc editor when trying to save contents of changed documents (#520)

=== 0.31.11 (preview, available from GitHub releases)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
* @author Julien Viet
Expand All @@ -83,6 +85,8 @@ public class AsciiDocPreviewEditor extends UserDataHolderBase implements FileEdi

private final Logger log = Logger.getInstance(AsciiDocPreviewEditor.class);

private static final ReentrantLock SAVE_ALL_LOCK = new ReentrantLock();

/**
* single threaded with one task queue (one for each editor window).
*/
Expand Down Expand Up @@ -408,21 +412,45 @@ public boolean isValid() {
@Override
public void selectNotify() {
myHtmlPanelWrapper.repaint();
ApplicationManager.getApplication().invokeLater(() -> {
// don't try to run save-all in paralle, therefore synchronize on current class
synchronized (this) {
ApplicationManager.getApplication().runWriteAction(() -> {
// project might be already closed (yes, this really happens when you work in multiple projects opened in separate windows)
if (!project.isDisposed()) {
if (FileDocumentManager.getInstance().getUnsavedDocuments().length > 0) {
ApplicationManager.getApplication().invokeLater(() -> {
try {
// don't try to run save-all in parallel, therefore synchronize
if (SAVE_ALL_LOCK.tryLock(5, TimeUnit.SECONDS)) {
try {
ApplicationManager.getApplication().runWriteAction(() -> {
// project might be already closed (yes, this really happens when you work in multiple projects opened in separate windows)
if (!project.isDisposed()) {
currentContent = null; // force a refresh of the preview by resetting the current memorized content
// save the content in all other editors as their content might be referenced in preview
// don't use ApplicationManager.getApplication().saveAll() as it will save in the background and will save settings as well
for (Document unsavedDocument : FileDocumentManager.getInstance().getUnsavedDocuments()) {
FileDocumentManager.getInstance().saveDocument(unsavedDocument);
}
reprocessAnnotations();
renderIfVisible();
}
});
} finally {
SAVE_ALL_LOCK.unlock();
}
} else {
currentContent = null; // force a refresh of the preview by resetting the current memorized content
reprocessAnnotations();
// save the content in all other editors as their content might be referenced in preview
ApplicationManager.getApplication().saveAll();
renderIfVisible();
log.warn("unable to acquire lock for save-all-docs");
}
});
} catch (InterruptedException e) {
log.warn("interrupted while save-all-docs", e);
}
});
} else {
if (!project.isDisposed()) {
currentContent = null; // force a refresh of the preview by resetting the current memorized content
reprocessAnnotations();
renderIfVisible();
}
}, ModalityState.NON_MODAL);
}
}

private void reprocessAnnotations() {
Expand Down

0 comments on commit 0daac52

Please sign in to comment.