-
Notifications
You must be signed in to change notification settings - Fork 23
Add argument parser feature to the LSP. #218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0cd159e
Add argumen parser feature to the LSP.
joewyz b0151c0
Moved ArgumentParser functionalities to ServerArguments
joewyz c049c77
Removed ServerLauncher class and moved functions to Main directly
joewyz 9a030a0
Address comments, improved help messages
joewyz 0bc0b25
Addres comment, move printhelp to main from serverargument, add helpe…
joewyz 3a50a9b
Add tests for default port using flag arguments.
joewyz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/main/java/software/amazon/smithy/lsp/ServerArguments.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.lsp; | ||
|
||
import java.util.function.Consumer; | ||
import software.amazon.smithy.cli.AnsiColorFormatter; | ||
import software.amazon.smithy.cli.ArgumentReceiver; | ||
import software.amazon.smithy.cli.Arguments; | ||
import software.amazon.smithy.cli.CliError; | ||
import software.amazon.smithy.cli.CliPrinter; | ||
import software.amazon.smithy.cli.HelpPrinter; | ||
|
||
/** | ||
* Options and Params available for LSP. | ||
*/ | ||
final class ServerArguments implements ArgumentReceiver { | ||
|
||
private static final int MIN_PORT = 0; | ||
private static final int MAX_PORT = 65535; | ||
private static final int DEFAULT_PORT = 0; // Default value for unset port number. | ||
private static final String HELP = "--help"; | ||
private static final String HELP_SHORT = "-h"; | ||
private static final String PORT = "--port"; | ||
private static final String PORT_SHORT = "-p"; | ||
private static final String PORT_POSITIONAL = "<port>"; | ||
private int port = DEFAULT_PORT; | ||
private boolean help = false; | ||
|
||
|
||
static ServerArguments create(String[] args) { | ||
Arguments arguments = Arguments.of(args); | ||
var serverArguments = new ServerArguments(); | ||
arguments.addReceiver(serverArguments); | ||
var positional = arguments.getPositional(); | ||
if (serverArguments.help()) { | ||
serverArguments.printHelp(arguments); | ||
} | ||
if (!positional.isEmpty()) { | ||
serverArguments.port = serverArguments.validatePortNumber(positional.getFirst()); | ||
} | ||
return serverArguments; | ||
} | ||
|
||
@Override | ||
public void registerHelp(HelpPrinter printer) { | ||
printer.option(HELP, HELP_SHORT, "Print this help output."); | ||
printer.param(PORT, PORT_SHORT, "PORT", | ||
"The port to use for talking to the client. When not specified, or set to 0, " | ||
+ "standard in/out is used. Standard in/out is preferred, " | ||
+ "so usually this shouldn't be specified."); | ||
printer.option(PORT_POSITIONAL, null, "Deprecated: use --port instead. When not specified, or set to 0, " | ||
+ "standard in/out is used. Standard in/out is preferred, so usually this shouldn't be specified."); | ||
} | ||
|
||
@Override | ||
public boolean testOption(String name) { | ||
if (name.equals(HELP) || name.equals(HELP_SHORT)) { | ||
help = true; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public Consumer<String> testParameter(String name) { | ||
if (name.equals(PORT_SHORT) || name.equals(PORT)) { | ||
return value -> { | ||
port = validatePortNumber(value); | ||
}; | ||
} | ||
return null; | ||
} | ||
|
||
int port() { | ||
return port; | ||
} | ||
|
||
boolean help() { | ||
return help; | ||
} | ||
|
||
public boolean useSocket() { | ||
joewyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return port != 0; | ||
} | ||
|
||
private int validatePortNumber(String portStr) { | ||
try { | ||
int portNumber = Integer.parseInt(portStr); | ||
if (portNumber < MIN_PORT || portNumber > MAX_PORT) { | ||
throw new CliError("Invalid port number: should be an integer between " | ||
+ MIN_PORT + " and " + MAX_PORT + ", inclusive."); | ||
} else { | ||
return portNumber; | ||
} | ||
} catch (NumberFormatException e) { | ||
throw new CliError("Invalid port number: Can not parse " + portStr); | ||
} | ||
} | ||
joewyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private void printHelp(Arguments arguments) { | ||
CliPrinter printer = CliPrinter.fromOutputStream(System.out); | ||
HelpPrinter helpPrinter = HelpPrinter.fromArguments("smithy-language-server", arguments); | ||
helpPrinter.summary("Run the Smithy Language Server."); | ||
helpPrinter.print(AnsiColorFormatter.AUTO, printer); | ||
printer.flush(); | ||
} | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
src/test/java/software/amazon/smithy/lsp/ServerArgumentsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package software.amazon.smithy.lsp; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import org.junit.jupiter.api.Test; | ||
import software.amazon.smithy.cli.CliError; | ||
|
||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class ServerArgumentsTest { | ||
joewyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Test | ||
void validPositionalPortNumber() { | ||
String[] args = {"1"}; | ||
ServerArguments serverArguments = ServerArguments.create(args); | ||
assertEquals(1, serverArguments.port()); | ||
} | ||
|
||
@Test | ||
void invalidPositionalPortNumber() { | ||
String[] args = {"65536"}; | ||
assertThrows(CliError.class,()-> {ServerArguments.create(args);}); | ||
} | ||
|
||
@Test | ||
void invalidFlagPortNumber() { | ||
String[] args = {"-p","65536"}; | ||
assertThrows(CliError.class,()-> {ServerArguments.create(args);}); | ||
} | ||
|
||
@Test | ||
void validFlagPortNumberShort() { | ||
String[] args = {"-p","100"}; | ||
ServerArguments serverArguments = ServerArguments.create(args); | ||
assertEquals(100, serverArguments.port()); | ||
} | ||
|
||
@Test | ||
void defaultPortNumber() { | ||
String[] args = {}; | ||
ServerArguments serverArguments = ServerArguments.create(args); | ||
|
||
assertEquals(0, serverArguments.port()); | ||
} | ||
|
||
@Test | ||
void defaultPortNumberInArg() { | ||
String[] args = {"0"}; | ||
ServerArguments serverArguments = ServerArguments.create(args); | ||
|
||
assertEquals(0, serverArguments.port()); | ||
} | ||
|
||
@Test | ||
void validFlagPortNumber() { | ||
String[] args = {"--port","200"}; | ||
ServerArguments serverArguments = ServerArguments.create(args); | ||
assertEquals(200, serverArguments.port()); | ||
} | ||
|
||
@Test | ||
void validHelp() { | ||
ByteArrayOutputStream outContent = new ByteArrayOutputStream(); | ||
PrintStream originalOut = System.out; | ||
System.setOut(new PrintStream(outContent)); | ||
|
||
try { | ||
ServerArguments.create(new String[]{"--help"}); | ||
|
||
String output = outContent.toString().trim(); | ||
|
||
assertTrue(output.contains("--help")); | ||
assertTrue(output.contains("-h")); | ||
assertTrue(output.contains("--port")); | ||
assertTrue(output.contains("-p")); | ||
assertTrue(output.contains("PORT")); | ||
assertTrue(output.contains("<port>")); | ||
|
||
} finally { | ||
// Restore original System.out | ||
System.setOut(originalOut); | ||
} | ||
} | ||
|
||
@Test | ||
void invalidFlag() { | ||
String[] args = {"--foo"}; | ||
assertThrows(CliError.class,()-> {ServerArguments.create(args);}); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.