15
15
16
16
package software .amazon .smithy .lsp ;
17
17
18
- import java .io .IOException ;
19
18
import java .io .InputStream ;
20
19
import java .io .OutputStream ;
21
20
import java .net .Socket ;
22
- import java .util .Optional ;
23
- import org .eclipse .lsp4j .jsonrpc .Launcher ;
24
21
import org .eclipse .lsp4j .launch .LSPLauncher ;
25
- import org .eclipse .lsp4j .services .LanguageClient ;
22
+ import software .amazon .smithy .cli .AnsiColorFormatter ;
23
+ import software .amazon .smithy .cli .CliPrinter ;
24
+ import software .amazon .smithy .cli .HelpPrinter ;
26
25
27
26
/**
28
27
* Main launcher for the Language server, started by the editor.
@@ -32,90 +31,46 @@ private Main() {
32
31
}
33
32
34
33
/**
35
- * Launch the LSP and wait for it to terminate.
36
- *
37
- * @param in input stream for communication
38
- * @param out output stream for communication
39
- * @return Empty Optional if service terminated successfully, error otherwise
34
+ * Main entry point for the language server.
35
+ * @param args Arguments passed to the server.
36
+ * @throws Exception If there is an error starting the server.
40
37
*/
41
- public static Optional <Exception > launch (InputStream in , OutputStream out ) {
42
- SmithyLanguageServer server = new SmithyLanguageServer ();
43
- Launcher <LanguageClient > launcher = LSPLauncher .createServerLauncher (
44
- server ,
45
- exitOnClose (in ),
46
- out );
47
-
48
- LanguageClient client = launcher .getRemoteProxy ();
49
-
50
- server .connect (client );
51
- try {
52
- launcher .startListening ().get ();
53
- return Optional .empty ();
54
- } catch (Exception e ) {
55
- return Optional .of (e );
38
+ public static void main (String [] args ) throws Exception {
39
+ var serverArguments = ServerArguments .create (args );
40
+ if (serverArguments .help ()) {
41
+ printHelp (serverArguments );
42
+ System .exit (0 );
56
43
}
44
+
45
+ launch (serverArguments );
57
46
}
58
47
59
- private static InputStream exitOnClose (InputStream delegate ) {
60
- return new InputStream () {
61
- @ Override
62
- public int read () throws IOException {
63
- int result = delegate .read ();
64
- if (result < 0 ) {
65
- System .exit (0 );
66
- }
67
- return result ;
48
+ private static void launch (ServerArguments serverArguments ) throws Exception {
49
+ if (serverArguments .useSocket ()) {
50
+ try (var socket = new Socket ("localhost" , serverArguments .port ())) {
51
+ startServer (socket .getInputStream (), socket .getOutputStream ());
68
52
}
69
- };
53
+ } else {
54
+ startServer (System .in , System .out );
55
+ }
70
56
}
71
57
72
- /**
73
- * @param args Arguments passed to launch server. First argument must either be
74
- * a port number for socket connection, or 0 to use STDIN and STDOUT
75
- * for communication
76
- */
77
- public static void main (String [] args ) {
78
-
79
- Socket socket = null ;
80
- InputStream in ;
81
- OutputStream out ;
58
+ private static void startServer (InputStream in , OutputStream out ) throws Exception {
59
+ var server = new SmithyLanguageServer ();
60
+ var launcher = LSPLauncher .createServerLauncher (server , in , out );
82
61
83
- try {
84
- String port = args [0 ];
85
- // If port is set to "0", use System.in/System.out.
86
- if (port .equals ("0" )) {
87
- in = System .in ;
88
- out = System .out ;
89
- } else {
90
- socket = new Socket ("localhost" , Integer .parseInt (port ));
91
- in = socket .getInputStream ();
92
- out = socket .getOutputStream ();
93
- }
94
-
95
- Optional <Exception > launchFailure = launch (in , out );
62
+ var client = launcher .getRemoteProxy ();
63
+ server .connect (client );
96
64
97
- if (launchFailure .isPresent ()) {
98
- throw launchFailure .get ();
99
- } else {
100
- System .out .println ("Server terminated without errors" );
101
- }
102
- } catch (ArrayIndexOutOfBoundsException e ) {
103
- System .out .println ("Missing port argument" );
104
- } catch (NumberFormatException e ) {
105
- System .out .println ("Port number must be a valid integer" );
106
- } catch (Exception e ) {
107
- System .out .println (e );
65
+ launcher .startListening ().get ();
66
+ }
108
67
109
- e .printStackTrace ();
110
- } finally {
111
- try {
112
- if (socket != null ) {
113
- socket .close ();
114
- }
115
- } catch (Exception e ) {
116
- System .out .println ("Failed to close the socket" );
117
- System .out .println (e );
118
- }
119
- }
68
+ private static void printHelp (ServerArguments serverArguments ) {
69
+ CliPrinter printer = CliPrinter .fromOutputStream (System .out );
70
+ HelpPrinter helpPrinter = new HelpPrinter ("smithy-language-server" );
71
+ serverArguments .registerHelp (helpPrinter );
72
+ helpPrinter .summary ("Run the Smithy Language Server." );
73
+ helpPrinter .print (AnsiColorFormatter .AUTO , printer );
74
+ printer .flush ();
120
75
}
121
76
}
0 commit comments