Skip to content

Commit c5ac310

Browse files
author
Fischstaebchen
committed
Added context menu for the command and added testcases.
1 parent 0bce500 commit c5ac310

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package net.javadiscord.javabot.systems.user_commands.format_code;
2+
3+
4+
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
5+
import net.dv8tion.jda.api.interactions.commands.build.Commands;
6+
import net.javadiscord.javabot.util.IndentationHelper;
7+
import net.javadiscord.javabot.util.StringUtils;
8+
import org.jetbrains.annotations.NotNull;
9+
import xyz.dynxsty.dih4jda.interactions.commands.application.ContextCommand;
10+
11+
import java.util.List;
12+
13+
/**
14+
* <h3>This class represents the "Format and Indent Code" Message Context command.</h3>
15+
*/
16+
public class FormatAndIndentCodeMessageContext extends ContextCommand.Message {
17+
/**
18+
* The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.CommandData}.
19+
*/
20+
public FormatAndIndentCodeMessageContext() {
21+
setCommandData(Commands.message("Format and Indent Code")
22+
.setGuildOnly(true)
23+
);
24+
}
25+
26+
@Override
27+
public void execute(@NotNull MessageContextInteractionEvent event) {
28+
event.replyFormat("```java\n%s\n```", IndentationHelper.formatIndentation(StringUtils.standardSanitizer().compute(event.getTarget().getContentRaw()), IndentationHelper.IndentationType.TABS))
29+
.setAllowedMentions(List.of())
30+
.setComponents(FormatCodeCommand.buildActionRow(event.getTarget()))
31+
.queue();
32+
}
33+
}

src/main/java/net/javadiscord/javabot/systems/user_commands/format_code/FormatCodeCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public FormatCodeCommand() {
4747
.addChoice("Swift", "swift")
4848
.addChoice("TypeScript", "typescript")
4949
.addChoice("XML", "xml"),
50-
new OptionData(OptionType.STRING,"auto-indent","The type of indentation that should be applied to the message, does not automatically indent if left blank",false)
50+
new OptionData(OptionType.STRING,"auto-indent","The type of indentation applied to the message, does not automatically indent if left blank.",false)
5151
.addChoice("Four_Spaces","four")
5252
.addChoice("Two_Spaces","two")
5353
.addChoice("Tabs","tab")

src/main/java/net/javadiscord/javabot/util/IndentationHelper.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private String formatIndentation() {
116116
IndentationState currentState = IndentationState.CODE;
117117
for (int i = 0; i < text.length(); i++) {
118118
char current = text.charAt(i);
119-
if (startOfLine && current == ' ') {
119+
if (startOfLine && Character.isWhitespace(current) && current != '\n') {
120120
continue;
121121
}
122122
builder.append(current);
@@ -125,8 +125,11 @@ private String formatIndentation() {
125125
startOfLine = true;
126126
}
127127
currentState = switch (currentState) {
128-
case CODE ->
129-
processTokenInCode(i, current);
128+
case CODE -> {
129+
IndentationState state = processTokenInCode(i,current);
130+
i += state == IndentationState.MULTI_LINE_COMMENT ? 1:0;
131+
yield state;
132+
}
130133
case STRING -> {
131134
if (current == '\"' && !isEscaped(builder, builder.length() - 1)) {
132135
yield IndentationState.CODE;
@@ -175,6 +178,7 @@ private IndentationState processTokenInCode(int i, char current) {
175178
if (text.charAt(i + 1) == '/') {
176179
return IndentationState.SINGLE_LINE_COMMENT;
177180
} else if (text.charAt(i + 1) == '*') {
181+
builder.append(text.charAt(++i));
178182
return IndentationState.MULTI_LINE_COMMENT;
179183
}
180184
}

src/test/resources/Formatted Strings.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,4 +942,20 @@ builder.append(current);
942942
return numberOfCharacters % 2 == 1;
943943
}
944944
}
945+
----
946+
/*/Should still be inside a multi line comment {{
947+
same indentation
948+
end of comment */
949+
----
950+
/*/Should still be inside a multi line comment {{
951+
same indentation
952+
end of comment */
953+
----
954+
/*/Should still be inside a multi line comment {{
955+
same indentation
956+
end of comment */
957+
----
958+
/*/Should still be inside a multi line comment {{
959+
same indentation
960+
end of comment */
945961
----

src/test/resources/Unformatted Strings.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,8 @@ builder.append(current);
234234
return numberOfCharacters % 2 == 1;
235235
}
236236
}
237+
----
238+
/*/Should still be inside a multi line comment {{
239+
same indentation
240+
end of comment */
241+
----

0 commit comments

Comments
 (0)