mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[code-style] Simplify FormattingProgressTask, add cancelled() method to callback
GitOrigin-RevId: 5d202a42df4ff2805d7aafcc7a3fd6b264fe823d
This commit is contained in:
committed by
intellij-monorepo-bot
parent
9bfb5b617f
commit
c8017c233b
+2
-11
@@ -78,15 +78,7 @@ public interface FormattingProgressCallback {
|
||||
*/
|
||||
void setTask(@Nullable SequentialTask task);
|
||||
|
||||
/**
|
||||
* Allows to register callback for the target event type.
|
||||
*
|
||||
* @param eventType target event type
|
||||
* @param callback callback to register for the given event type
|
||||
* @return {@code true} if given callback is successfully registered for the given event type;
|
||||
* {@code false} otherwise
|
||||
*/
|
||||
boolean addCallback(@NotNull EventType eventType, @NotNull Runnable callback);
|
||||
void cancelled();
|
||||
|
||||
/**
|
||||
* <a hrep="http://en.wikipedia.org/wiki/Null_Object_pattern">Null object</a> for {@link FormattingProgressCallback}.
|
||||
@@ -113,8 +105,7 @@ public interface FormattingProgressCallback {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addCallback(@NotNull EventType eventType, @NotNull Runnable callback) {
|
||||
return false;
|
||||
public void cancelled() {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.util.SequentialModalProgressTask;
|
||||
import com.intellij.util.SequentialTask;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -21,8 +20,6 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
public final class FormattingProgressTask extends SequentialModalProgressTask implements FormattingProgressCallback {
|
||||
public static final ThreadLocal<Boolean> FORMATTING_CANCELLED_FLAG = ThreadLocal.withInitial(() -> false);
|
||||
@@ -31,8 +28,6 @@ public final class FormattingProgressTask extends SequentialModalProgressTask im
|
||||
private static final double TOTAL_WEIGHT =
|
||||
Arrays.stream(FormattingStateId.values()).mapToDouble(FormattingStateId::getProgressWeight).sum();
|
||||
|
||||
private final ConcurrentMap<EventType, Collection<Runnable>> myCallbacks = new ConcurrentHashMap<>();
|
||||
|
||||
private final WeakReference<VirtualFile> myFile;
|
||||
private final WeakReference<Document> myDocument;
|
||||
private final int myFileTextLength;
|
||||
@@ -48,7 +43,6 @@ public final class FormattingProgressTask extends SequentialModalProgressTask im
|
||||
myFile = new WeakReference<>(file.getVirtualFile());
|
||||
myDocument = new WeakReference<>(document);
|
||||
myFileTextLength = file.getTextLength();
|
||||
addCallback(EventType.CANCEL, new MyCancelCallback());
|
||||
}
|
||||
|
||||
private static @NotNull @NlsContexts.DialogTitle String getTitle(@NotNull PsiFile file) {
|
||||
@@ -72,42 +66,15 @@ public final class FormattingProgressTask extends SequentialModalProgressTask im
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addCallback(@NotNull EventType eventType, @NotNull Runnable callback) {
|
||||
return getCallbacks(eventType).add(callback);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
for (Runnable callback : getCallbacks(EventType.SUCCESS)) {
|
||||
callback.run();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancel() {
|
||||
for (Runnable callback : getCallbacks(EventType.CANCEL)) {
|
||||
callback.run();
|
||||
}
|
||||
cancelled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onThrowable(@NotNull Throwable error) {
|
||||
super.onThrowable(error);
|
||||
for (Runnable callback : getCallbacks(EventType.CANCEL)) {
|
||||
callback.run();
|
||||
}
|
||||
}
|
||||
|
||||
private Collection<Runnable> getCallbacks(@NotNull EventType eventType) {
|
||||
Collection<Runnable> result = myCallbacks.get(eventType);
|
||||
if (result == null) {
|
||||
Collection<Runnable> candidate = myCallbacks.putIfAbsent(eventType, result = ContainerUtil.newConcurrentSet());
|
||||
if (candidate != null) {
|
||||
result = candidate;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
cancelled();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -166,21 +133,19 @@ public final class FormattingProgressTask extends SequentialModalProgressTask im
|
||||
}
|
||||
}
|
||||
|
||||
private final class MyCancelCallback implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
FORMATTING_CANCELLED_FLAG.set(true);
|
||||
VirtualFile file = myFile.get();
|
||||
Document document = myDocument.get();
|
||||
if (file == null || document == null || myDocumentModificationStampBefore < 0) {
|
||||
return;
|
||||
}
|
||||
FileEditor editor = FileEditorManager.getInstance(myProject).getSelectedEditor(file);
|
||||
if (editor == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
EditorFacade.getInstance().undo(myProject, editor, document, myDocumentModificationStampBefore);
|
||||
@Override
|
||||
public void cancelled() {
|
||||
FORMATTING_CANCELLED_FLAG.set(true);
|
||||
VirtualFile file = myFile.get();
|
||||
Document document = myDocument.get();
|
||||
if (file == null || document == null || myDocumentModificationStampBefore < 0) {
|
||||
return;
|
||||
}
|
||||
FileEditor editor = FileEditorManager.getInstance(myProject).getSelectedEditor(file);
|
||||
if (editor == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
EditorFacade.getInstance().undo(myProject, editor, document, myDocumentModificationStampBefore);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user