Skip to content

Commit 7fa5e1e

Browse files
committed
Formating jshell classes
1 parent 78828b8 commit 7fa5e1e

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

application/src/main/java/org/togetherjava/tjbot/features/jshell/JShellCommand.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public JShellCommand(JShellEval jshellEval) {
6969
.addOption(OptionType.BOOLEAN, JSHELL_INCLUDE_STARTUP_SCRIPT_PARAMETER,
7070
"if the startup script should be included, false by default."),
7171
new SubcommandData(JSHELL_CLOSE_SUBCOMMAND, "Close your session."),
72-
new SubcommandData(JSHELL_STARTUP_SCRIPT_SUBCOMMAND, "Display the startup script."));
72+
new SubcommandData(JSHELL_STARTUP_SCRIPT_SUBCOMMAND,
73+
"Display the startup script."));
7374
}
7475

7576
@Override
@@ -87,9 +88,10 @@ public void onSlashCommand(SlashCommandInteractionEvent event) {
8788

8889
@Override
8990
public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
90-
ModalMapping mapping = event.getValue(JSHELL_TEXT_INPUT_ID + "|" + JSHELL_STARTUP_SCRIPT_PARAMETER);
91+
ModalMapping mapping =
92+
event.getValue(JSHELL_TEXT_INPUT_ID + "|" + JSHELL_STARTUP_SCRIPT_PARAMETER);
9193
boolean startupScript = mapping != null;
92-
if(mapping == null) {
94+
if (mapping == null) {
9395
mapping = event.getValue(JSHELL_TEXT_INPUT_ID);
9496
}
9597
if (mapping != null) {
@@ -110,7 +112,9 @@ private void handleVersionCommand(SlashCommandInteractionEvent event) {
110112

111113
private void handleEvalCommand(SlashCommandInteractionEvent event) {
112114
OptionMapping code = event.getOption(JSHELL_CODE_PARAMETER);
113-
boolean startupScript = event.getOption(JSHELL_STARTUP_SCRIPT_PARAMETER) == null || Objects.requireNonNull(event.getOption(JSHELL_STARTUP_SCRIPT_PARAMETER)).getAsBoolean();
115+
boolean startupScript = event.getOption(JSHELL_STARTUP_SCRIPT_PARAMETER) == null
116+
|| Objects.requireNonNull(event.getOption(JSHELL_STARTUP_SCRIPT_PARAMETER))
117+
.getAsBoolean();
114118
if (code == null) {
115119
sendEvalModal(event, startupScript);
116120
} else {
@@ -120,7 +124,9 @@ private void handleEvalCommand(SlashCommandInteractionEvent event) {
120124

121125
private void sendEvalModal(SlashCommandInteractionEvent event, boolean startupScript) {
122126
TextInput body = TextInput
123-
.create(JSHELL_TEXT_INPUT_ID + (startupScript ? "|" + JSHELL_STARTUP_SCRIPT_PARAMETER : ""), "Enter code to evaluate.", TextInputStyle.PARAGRAPH)
127+
.create(JSHELL_TEXT_INPUT_ID
128+
+ (startupScript ? "|" + JSHELL_STARTUP_SCRIPT_PARAMETER : ""),
129+
"Enter code to evaluate.", TextInputStyle.PARAGRAPH)
124130
.setPlaceholder("Put your code here.")
125131
.setRequiredRange(MIN_MESSAGE_INPUT_LENGTH, MAX_MESSAGE_INPUT_LENGTH)
126132
.build();
@@ -131,7 +137,7 @@ private void sendEvalModal(SlashCommandInteractionEvent event, boolean startupSc
131137

132138
/**
133139
* Handle evaluation of code.
134-
*
140+
*
135141
* @param replyCallback the callback to reply to
136142
* @param user the user, if null, will create a single use session
137143
* @param showCode if the embed should contain the original code
@@ -143,7 +149,8 @@ private void handleEval(IReplyCallback replyCallback, @Nullable User user, boole
143149
replyCallback.deferReply().queue(interactionHook -> {
144150
try {
145151
interactionHook
146-
.editOriginalEmbeds(jshellEval.evaluateAndRespond(user, code, showCode, startupScript))
152+
.editOriginalEmbeds(
153+
jshellEval.evaluateAndRespond(user, code, showCode, startupScript))
147154
.queue();
148155
} catch (RequestFailedException e) {
149156
interactionHook.editOriginalEmbeds(createUnexpectedErrorEmbed(user, e)).queue();
@@ -155,11 +162,15 @@ private void handleSnippetsCommand(SlashCommandInteractionEvent event) {
155162
event.deferReply().queue(interactionHook -> {
156163
OptionMapping userOption = event.getOption(JSHELL_USER_PARAMETER);
157164
User user = userOption == null ? event.getUser() : userOption.getAsUser();
158-
OptionMapping includeStartupScriptOption = event.getOption(JSHELL_INCLUDE_STARTUP_SCRIPT_PARAMETER);
159-
boolean includeStartupScript = includeStartupScriptOption != null && includeStartupScriptOption.getAsBoolean();
165+
OptionMapping includeStartupScriptOption =
166+
event.getOption(JSHELL_INCLUDE_STARTUP_SCRIPT_PARAMETER);
167+
boolean includeStartupScript =
168+
includeStartupScriptOption != null && includeStartupScriptOption.getAsBoolean();
160169
List<String> snippets;
161170
try {
162-
snippets = jshellEval.getApi().snippetsSession(user.getId(), includeStartupScript).snippets();
171+
snippets = jshellEval.getApi()
172+
.snippetsSession(user.getId(), includeStartupScript)
173+
.snippets();
163174
} catch (RequestFailedException e) {
164175
if (e.getStatus() == JShellApi.SESSION_NOT_FOUND) {
165176
interactionHook.editOriginalEmbeds(createSessionNotFoundErrorEmbed(user))
@@ -257,12 +268,12 @@ private void handleStartupScriptCommand(SlashCommandInteractionEvent event) {
257268
try {
258269
String startupScript = jshellEval.getApi().startupScript();
259270
interactionHook
260-
.editOriginalEmbeds(new EmbedBuilder().setColor(Colors.SUCCESS_COLOR)
261-
.setAuthor(event.getUser().getName())
262-
.setTitle("Startup script")
263-
.setDescription("```java\n" + startupScript + "```")
264-
.build())
265-
.queue();
271+
.editOriginalEmbeds(new EmbedBuilder().setColor(Colors.SUCCESS_COLOR)
272+
.setAuthor(event.getUser().getName())
273+
.setTitle("Startup script")
274+
.setDescription("```java\n" + startupScript + "```")
275+
.build())
276+
.queue();
266277
} catch (RequestFailedException e) {
267278
event.replyEmbeds(createUnexpectedErrorEmbed(event.getUser(), e)).queue();
268279
}

application/src/main/java/org/togetherjava/tjbot/features/jshell/JShellEval.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ public JShellApi getApi() {
3939

4040
/**
4141
* Evaluate code and return a message containing the response.
42-
*
42+
*
4343
* @param user the user, if null, will create a single use session
4444
* @param code the code
4545
* @param showCode if the original code should be displayed
4646
* @param startupScript if the startup script should be used or not
4747
* @return the response
4848
* @throws RequestFailedException if a http error happens
4949
*/
50-
public MessageEmbed evaluateAndRespond(@Nullable User user, String code, boolean showCode, boolean startupScript)
51-
throws RequestFailedException {
50+
public MessageEmbed evaluateAndRespond(@Nullable User user, String code, boolean showCode,
51+
boolean startupScript) throws RequestFailedException {
5252
MessageEmbed rateLimitedMessage = wasRateLimited(user, Instant.now());
5353
if (rateLimitedMessage != null) {
5454
return rateLimitedMessage;

application/src/main/java/org/togetherjava/tjbot/features/jshell/backend/JShellApi.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,35 @@ public JShellApi(ObjectMapper objectMapper, String baseUrl) {
3535
}
3636

3737
public JShellResult evalOnce(String code, boolean startupScript) throws RequestFailedException {
38-
return send(baseUrl + "single-eval" + (startupScript ? "?startupScriptId=" + STARTUP_SCRIPT_ID : ""),
38+
return send(
39+
baseUrl + "single-eval"
40+
+ (startupScript ? "?startupScriptId=" + STARTUP_SCRIPT_ID : ""),
3941
HttpRequest.newBuilder().POST(BodyPublishers.ofString(code)),
4042
ResponseUtils.ofJson(JShellResult.class, objectMapper)).body();
4143
}
4244

43-
public JShellResult evalSession(String code, String sessionId, boolean startupScript) throws RequestFailedException {
44-
return send(baseUrl + "eval/" + sessionId + (startupScript ? "?startupScriptId=" + STARTUP_SCRIPT_ID : ""),
45+
public JShellResult evalSession(String code, String sessionId, boolean startupScript)
46+
throws RequestFailedException {
47+
return send(
48+
baseUrl + "eval/" + sessionId
49+
+ (startupScript ? "?startupScriptId=" + STARTUP_SCRIPT_ID : ""),
4550
HttpRequest.newBuilder().POST(BodyPublishers.ofString(code)),
4651
ResponseUtils.ofJson(JShellResult.class, objectMapper)).body();
4752
}
4853

49-
public SnippetList snippetsSession(String sessionId, boolean includeStartupScript) throws RequestFailedException {
50-
return send(baseUrl + "snippets/" + sessionId + "?includeStartupScript=" + includeStartupScript, HttpRequest.newBuilder().GET(),
54+
public SnippetList snippetsSession(String sessionId, boolean includeStartupScript)
55+
throws RequestFailedException {
56+
return send(
57+
baseUrl + "snippets/" + sessionId + "?includeStartupScript=" + includeStartupScript,
58+
HttpRequest.newBuilder().GET(),
5159
ResponseUtils.ofJson(SnippetList.class, objectMapper)).body();
5260
}
5361

5462
public void closeSession(String sessionId) throws RequestFailedException {
5563
send(baseUrl + sessionId, HttpRequest.newBuilder().DELETE(), BodyHandlers.discarding())
5664
.body();
5765
}
66+
5867
public String startupScript() throws RequestFailedException {
5968
return send(baseUrl + "startup_script/" + STARTUP_SCRIPT_ID, HttpRequest.newBuilder().GET(),
6069
BodyHandlers.ofString()).body();

0 commit comments

Comments
 (0)