18
18
import java .io .InputStream ;
19
19
import java .io .OutputStream ;
20
20
import java .net .Socket ;
21
+ import java .util .Arrays ;
22
+ import java .util .Collection ;
23
+ import java .util .List ;
21
24
import java .util .Optional ;
22
25
import org .eclipse .lsp4j .jsonrpc .Launcher ;
23
26
import org .eclipse .lsp4j .launch .LSPLauncher ;
24
27
import org .eclipse .lsp4j .services .LanguageClient ;
28
+ import software .amazon .smithy .lsp .websocket .WebSocketRunner ;
25
29
26
30
/**
27
31
* Main launcher for the Language server, started by the editor.
@@ -61,21 +65,28 @@ public static void main(String[] args) {
61
65
Socket socket = null ;
62
66
InputStream in ;
63
67
OutputStream out ;
64
-
68
+ List < String > argList = Arrays . asList ( args );
65
69
try {
66
- String port = args [0 ];
70
+ Optional <Exception > launchFailure ;
71
+ String port = getOrDefault (argList , 0 , "0" );
72
+ String type = getOrDefault (argList , 1 , null );
67
73
// If port is set to "0", use System.in/System.out.
68
74
if (port .equals ("0" )) {
69
75
in = System .in ;
70
76
out = System .out ;
77
+ launchFailure = launch (in , out );
78
+ } else if (type != null && type .equals ("--ws" )) {
79
+ WebSocketRunner webSocketRunner = new WebSocketRunner ();
80
+ String hostname = "localhost" ;
81
+ String contextPath = "/" ;
82
+ webSocketRunner .run (hostname , Integer .parseInt (port ), contextPath );
83
+ return ;
71
84
} else {
72
85
socket = new Socket ("localhost" , Integer .parseInt (port ));
73
86
in = socket .getInputStream ();
74
87
out = socket .getOutputStream ();
88
+ launchFailure = launch (in , out );
75
89
}
76
-
77
- Optional <Exception > launchFailure = launch (in , out );
78
-
79
90
if (launchFailure .isPresent ()) {
80
91
throw launchFailure .get ();
81
92
} else {
@@ -86,7 +97,7 @@ public static void main(String[] args) {
86
97
} catch (NumberFormatException e ) {
87
98
System .out .println ("Port number must be a valid integer" );
88
99
} catch (Exception e ) {
89
- System .out .println (e );
100
+ System .out .println ("Failed to start: " + e );
90
101
91
102
e .printStackTrace ();
92
103
} finally {
@@ -95,9 +106,22 @@ public static void main(String[] args) {
95
106
socket .close ();
96
107
}
97
108
} catch (Exception e ) {
98
- System .out .println ("Failed to close the socket" );
99
- System .out .println (e );
109
+ System .out .println ("Failed to close the socket: " + e );
100
110
}
101
111
}
102
112
}
113
+
114
+ private static boolean isEmpty (Collection <?> c ) {
115
+ return c == null || c .isEmpty ();
116
+ }
117
+
118
+ private static <T > T getOrDefault (List <T > list , int index , T t ) {
119
+ if (isEmpty (list )) {
120
+ return t ;
121
+ }
122
+ if (index < 0 || index >= list .size ()) {
123
+ return t ;
124
+ }
125
+ return list .get (index );
126
+ }
103
127
}
0 commit comments