Skip to content

Commit 7a71811

Browse files
committed
Fix error reporting in NixExternalFormatter
AsyncFormattingRequest.onError(...) should not be called before FormattingTask.run() has been called by the framework.
1 parent b9c8e85 commit 7a71811

File tree

1 file changed

+44
-39
lines changed

1 file changed

+44
-39
lines changed

src/main/java/org/nixos/idea/format/NixExternalFormatter.java

+44-39
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.intellij.openapi.util.NlsSafe;
1111
import com.intellij.psi.PsiFile;
1212
import com.intellij.util.execution.ParametersListUtil;
13-
import org.jetbrains.annotations.NonNls;
1413
import org.jetbrains.annotations.NotNull;
1514
import org.jetbrains.annotations.Nullable;
1615
import org.nixos.idea.file.NixFile;
@@ -47,7 +46,6 @@ public boolean canFormat(@NotNull PsiFile psiFile) {
4746
return psiFile instanceof NixFile;
4847
}
4948

50-
5149
@Override
5250
protected @Nullable FormattingTask createFormattingTask(@NotNull AsyncFormattingRequest request) {
5351
NixExternalFormatterSettings nixSettings = NixExternalFormatterSettings.getInstance();
@@ -58,55 +56,62 @@ public boolean canFormat(@NotNull PsiFile psiFile) {
5856
var ioFile = request.getIOFile();
5957
if (ioFile == null) return null;
6058

61-
@NonNls
6259
var command = nixSettings.getFormatCommand();
6360
List<String> argv = ParametersListUtil.parse(command, false, true);
64-
6561
var commandLine = new GeneralCommandLine(argv);
6662

67-
try {
68-
var handler = new OSProcessHandler(commandLine.withCharset(StandardCharsets.UTF_8));
69-
OutputStream processInput = handler.getProcessInput();
70-
return new FormattingTask() {
71-
@Override
72-
public void run() {
73-
handler.addProcessListener(new CapturingProcessAdapter() {
63+
return new FormattingTask() {
64+
private @Nullable OSProcessHandler handler;
65+
private boolean canceled;
7466

75-
@Override
76-
public void processTerminated(@NotNull ProcessEvent event) {
77-
int exitCode = event.getExitCode();
78-
if (exitCode == 0) {
79-
request.onTextReady(getOutput().getStdout());
80-
} else {
81-
request.onError("NixIDEA", getOutput().getStderr());
82-
}
83-
}
84-
});
85-
handler.startNotify();
67+
@Override
68+
public void run() {
69+
synchronized (this) {
70+
if (canceled) {
71+
return;
72+
}
8673
try {
87-
Files.copy(ioFile.toPath(), processInput);
88-
processInput.flush();
89-
processInput.close();
90-
} catch (IOException e) {
91-
handler.destroyProcess();
74+
handler = new OSProcessHandler(commandLine.withCharset(StandardCharsets.UTF_8));
75+
} catch (ExecutionException e) {
9276
request.onError("NixIDEA", e.getMessage());
77+
return;
9378
}
9479
}
95-
96-
@Override
97-
public boolean cancel() {
80+
handler.addProcessListener(new CapturingProcessAdapter() {
81+
@Override
82+
public void processTerminated(@NotNull ProcessEvent event) {
83+
int exitCode = event.getExitCode();
84+
if (exitCode == 0) {
85+
request.onTextReady(getOutput().getStdout());
86+
} else {
87+
request.onError("NixIDEA", getOutput().getStderr());
88+
}
89+
}
90+
});
91+
handler.startNotify();
92+
try {
93+
OutputStream processInput = handler.getProcessInput();
94+
Files.copy(ioFile.toPath(), processInput);
95+
processInput.close();
96+
} catch (IOException e) {
9897
handler.destroyProcess();
99-
return true;
98+
request.onError("NixIDEA", e.getMessage());
10099
}
100+
}
101101

102-
@Override
103-
public boolean isRunUnderProgress() {
104-
return true;
102+
@Override
103+
public synchronized boolean cancel() {
104+
canceled = true;
105+
if (handler != null) {
106+
handler.destroyProcess();
105107
}
106-
};
107-
} catch (ExecutionException e) {
108-
request.onError("NixIDEA", e.getMessage());
109-
return null;
110-
}
108+
return true;
109+
}
110+
111+
@Override
112+
public boolean isRunUnderProgress() {
113+
return true;
114+
}
115+
};
111116
}
112117
}

0 commit comments

Comments
 (0)