10
10
import com .intellij .openapi .util .NlsSafe ;
11
11
import com .intellij .psi .PsiFile ;
12
12
import com .intellij .util .execution .ParametersListUtil ;
13
- import org .jetbrains .annotations .NonNls ;
14
13
import org .jetbrains .annotations .NotNull ;
15
14
import org .jetbrains .annotations .Nullable ;
16
15
import org .nixos .idea .file .NixFile ;
@@ -47,7 +46,6 @@ public boolean canFormat(@NotNull PsiFile psiFile) {
47
46
return psiFile instanceof NixFile ;
48
47
}
49
48
50
-
51
49
@ Override
52
50
protected @ Nullable FormattingTask createFormattingTask (@ NotNull AsyncFormattingRequest request ) {
53
51
NixExternalFormatterSettings nixSettings = NixExternalFormatterSettings .getInstance ();
@@ -58,55 +56,62 @@ public boolean canFormat(@NotNull PsiFile psiFile) {
58
56
var ioFile = request .getIOFile ();
59
57
if (ioFile == null ) return null ;
60
58
61
- @ NonNls
62
59
var command = nixSettings .getFormatCommand ();
63
60
List <String > argv = ParametersListUtil .parse (command , false , true );
64
-
65
61
var commandLine = new GeneralCommandLine (argv );
66
62
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 ;
74
66
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
+ }
86
73
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 ) {
92
76
request .onError ("NixIDEA" , e .getMessage ());
77
+ return ;
93
78
}
94
79
}
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 ) {
98
97
handler .destroyProcess ();
99
- return true ;
98
+ request . onError ( "NixIDEA" , e . getMessage ()) ;
100
99
}
100
+ }
101
101
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 ();
105
107
}
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
+ };
111
116
}
112
117
}
0 commit comments