1919import com .intellij .execution .executors .DefaultDebugExecutor ;
2020import com .intellij .execution .process .ProcessEvent ;
2121import com .intellij .execution .process .ProcessListener ;
22+ import com .intellij .execution .process .ProcessOutputType ;
2223import com .intellij .execution .remote .RemoteConfiguration ;
2324import com .intellij .execution .remote .RemoteConfigurationType ;
2425import com .intellij .execution .runners .ExecutionEnvironment ;
3031import com .intellij .openapi .project .Project ;
3132import com .intellij .openapi .ui .Messages ;
3233import com .intellij .openapi .util .Key ;
34+ import com .intellij .openapi .util .registry .Registry ;
3335import com .redhat .devtools .intellij .qute .run .QuteConfigurationType ;
3436import com .redhat .devtools .intellij .qute .run .QuteRunConfiguration ;
3537import org .jetbrains .annotations .NotNull ;
4143import java .net .ConnectException ;
4244import java .net .Socket ;
4345import java .nio .charset .StandardCharsets ;
46+ import java .util .MissingResourceException ;
4447
4548import static com .redhat .devtools .intellij .quarkus .run .QuarkusRunConfiguration .QUARKUS_CONFIGURATION ;
4649
4750/**
4851 * ProcessListener which tracks the message Listening for transport dt_socket at address: $PORT' to start the
4952 * remote debugger of the given port $PORT.
5053 */
51- class AttachDebuggerProcessListener implements ProcessListener {
54+ public class AttachDebuggerProcessListener implements ProcessListener {
5255
5356 private final static Logger LOGGER = LoggerFactory .getLogger (AttachDebuggerProcessListener .class );
5457
@@ -73,30 +76,32 @@ class AttachDebuggerProcessListener implements ProcessListener {
7376
7477 @ Override
7578 public void onTextAvailable (@ NotNull ProcessEvent event , @ NotNull Key outputType ) {
76- String message = event .getText ();
77- if (!connected && debugPort != null && message .startsWith (LISTENING_FOR_TRANSPORT_DT_SOCKET_AT_ADDRESS + debugPort )) {
78- connected = true ;
79- ProgressManager .getInstance ().run (new Task .Backgroundable (project , QUARKUS_CONFIGURATION , false ) {
80- @ Override
81- public void run (@ NotNull ProgressIndicator indicator ) {
82- String name = env .getRunProfile ().getName ();
83- createRemoteConfiguration (indicator , debugPort , name );
79+ if (ProcessOutputType .isStdout (outputType )) {
80+ String message = event .getText ();
81+ if (!connected && debugPort != null && message .startsWith (LISTENING_FOR_TRANSPORT_DT_SOCKET_AT_ADDRESS + debugPort )) {
82+ connected = true ;
83+ ProgressManager .getInstance ().run (new Task .Backgroundable (project , QUARKUS_CONFIGURATION , true ) {
84+ @ Override
85+ public void run (@ NotNull ProgressIndicator indicator ) {
86+ String name = env .getRunProfile ().getName ();
87+ createRemoteConfiguration (indicator , debugPort , name );
88+ }
89+ });
90+ } else if (!quteConnected && message .startsWith (QUTE_LISTENING_ON_PORT )) {
91+ quteConnected = true ;
92+ Integer quteDebugPort = getQuteDebugPort (message );
93+ if (quteDebugPort == null ) {
94+ LOGGER .error ("Cannot extract Qute debug port from the given message: {}" , message );
95+ return ;
8496 }
85- });
86- } else if (! quteConnected && message . startsWith ( QUTE_LISTENING_ON_PORT )) {
87- quteConnected = true ;
88- Integer quteDebugPort = getQuteDebugPort ( message );
89- if ( quteDebugPort == null ) {
90- LOGGER . error ( "Cannot extract Qute debug port from the given message: {}" , message );
91- return ;
97+ ProgressManager . getInstance (). run ( new Task . Backgroundable ( project , QUARKUS_CONFIGURATION , true ) {
98+ @ Override
99+ public void run ( @ NotNull ProgressIndicator indicator ) {
100+ String name = env . getRunProfile (). getName ( );
101+ createQuteConfiguration ( indicator , quteDebugPort , name );
102+ }
103+ }) ;
92104 }
93- ProgressManager .getInstance ().run (new Task .Backgroundable (project , QUARKUS_CONFIGURATION , false ) {
94- @ Override
95- public void run (@ NotNull ProgressIndicator indicator ) {
96- String name = env .getRunProfile ().getName ();
97- createQuteConfiguration (indicator , quteDebugPort , name );
98- }
99- });
100105 }
101106 }
102107
@@ -131,9 +136,19 @@ private void createRemoteConfiguration(@NotNull ProgressIndicator indicator, int
131136 }
132137
133138 private void createQuteConfiguration (@ NotNull ProgressIndicator indicator , int port , String name ) {
139+ createQuteConfiguration (port , name , project , indicator , true );
140+ }
141+
142+ public static void createQuteConfiguration (int port ,
143+ @ NotNull String name ,
144+ @ NotNull Project project ,
145+ @ NotNull ProgressIndicator indicator ,
146+ boolean waitForPortAvailable ) {
134147 indicator .setText ("Connecting Qute debugger to port " + port );
135148 try {
136- waitForPortAvailable (port , indicator );
149+ if (waitForPortAvailable ) {
150+ waitForPortAvailable (port , indicator );
151+ }
137152 RunnerAndConfigurationSettings settings = RunManager .getInstance (project ).createConfiguration (name + " (Qute)" , QuteConfigurationType .class );
138153 QuteRunConfiguration quteConfiguration = (QuteRunConfiguration ) settings .getConfiguration ();
139154 quteConfiguration .setAttachPort (Integer .toString (port ));
@@ -145,7 +160,15 @@ private void createQuteConfiguration(@NotNull ProgressIndicator indicator, int p
145160 }
146161 }
147162
148- private void waitForPortAvailable (int port , ProgressIndicator monitor ) throws IOException {
163+ public static boolean isDebuggerAutoAttach () {
164+ try {
165+ return Registry .is ("debugger.auto.attach.from.any.console" );
166+ } catch (MissingResourceException e ) {
167+ return false ;
168+ }
169+ }
170+
171+ private static void waitForPortAvailable (int port , ProgressIndicator monitor ) throws IOException {
149172 long start = System .currentTimeMillis ();
150173 while (System .currentTimeMillis () - start < 120_000 && !monitor .isCanceled ()) {
151174 try (Socket socket = new Socket ("localhost" , port )) {
0 commit comments