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,9 +65,21 @@ 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 );
73
+
74
+ // Check if websocket option is present
75
+ if ("--ws" .equals (type )) {
76
+ WebSocketRunner webSocketRunner = new WebSocketRunner ();
77
+ String hostname = "localhost" ;
78
+ String contextPath = "/" ;
79
+ webSocketRunner .run (hostname , Integer .parseInt (port ), contextPath );
80
+ return ;
81
+ }
82
+
67
83
// If port is set to "0", use System.in/System.out.
68
84
if (port .equals ("0" )) {
69
85
in = System .in ;
@@ -73,9 +89,7 @@ public static void main(String[] args) {
73
89
in = socket .getInputStream ();
74
90
out = socket .getOutputStream ();
75
91
}
76
-
77
- Optional <Exception > launchFailure = launch (in , out );
78
-
92
+ launchFailure = launch (in , out );
79
93
if (launchFailure .isPresent ()) {
80
94
throw launchFailure .get ();
81
95
} else {
@@ -86,7 +100,7 @@ public static void main(String[] args) {
86
100
} catch (NumberFormatException e ) {
87
101
System .out .println ("Port number must be a valid integer" );
88
102
} catch (Exception e ) {
89
- System .out .println (e );
103
+ System .out .println ("Failed to start: " + e );
90
104
91
105
e .printStackTrace ();
92
106
} finally {
@@ -95,9 +109,22 @@ public static void main(String[] args) {
95
109
socket .close ();
96
110
}
97
111
} catch (Exception e ) {
98
- System .out .println ("Failed to close the socket" );
99
- System .out .println (e );
112
+ System .out .println ("Failed to close the socket: " + e );
100
113
}
101
114
}
102
115
}
116
+
117
+ private static boolean isEmpty (Collection <?> c ) {
118
+ return c == null || c .isEmpty ();
119
+ }
120
+
121
+ private static <T > T getOrDefault (List <T > list , int index , T t ) {
122
+ if (isEmpty (list )) {
123
+ return t ;
124
+ }
125
+ if (index < 0 || index >= list .size ()) {
126
+ return t ;
127
+ }
128
+ return list .get (index );
129
+ }
103
130
}
0 commit comments