✨ Implement PR comments
This commit is contained in:
parent
720019b508
commit
27d228cb4c
3 changed files with 103 additions and 10 deletions
|
|
@ -30,6 +30,7 @@ import cloud.commandframework.captions.CaptionVariable;
|
||||||
import cloud.commandframework.captions.StandardCaptionKeys;
|
import cloud.commandframework.captions.StandardCaptionKeys;
|
||||||
import cloud.commandframework.context.CommandContext;
|
import cloud.commandframework.context.CommandContext;
|
||||||
import cloud.commandframework.exceptions.parsing.ParserException;
|
import cloud.commandframework.exceptions.parsing.ParserException;
|
||||||
|
import cloud.commandframework.util.StringUtils;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
@ -317,19 +318,30 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
|
||||||
}
|
}
|
||||||
final String string = sj.toString();
|
final String string = sj.toString();
|
||||||
|
|
||||||
Matcher matcher = QUOTED_DOUBLE.matcher(string);
|
final Matcher doubleMatcher = QUOTED_DOUBLE.matcher(string);
|
||||||
String inner = null;
|
String doubleMatch = null;
|
||||||
if (matcher.find()) {
|
if (doubleMatcher.find()) {
|
||||||
inner = matcher.group("inner");
|
doubleMatch = doubleMatcher.group("inner");
|
||||||
} else {
|
|
||||||
matcher = QUOTED_SINGLE.matcher(string);
|
|
||||||
if (matcher.find()) {
|
|
||||||
inner = matcher.group("inner");
|
|
||||||
}
|
}
|
||||||
|
final Matcher singleMatcher = QUOTED_SINGLE.matcher(string);
|
||||||
|
String singleMatch = null;
|
||||||
|
if (singleMatcher.find()) {
|
||||||
|
singleMatch = singleMatcher.group("inner");
|
||||||
|
}
|
||||||
|
|
||||||
|
String inner = null;
|
||||||
|
if (singleMatch != null && doubleMatch != null) {
|
||||||
|
final int singleIndex = string.indexOf(singleMatch);
|
||||||
|
final int doubleIndex = string.indexOf(doubleMatch);
|
||||||
|
inner = doubleIndex < singleIndex ? doubleMatch : singleMatch;
|
||||||
|
} else if (singleMatch == null && doubleMatch != null) {
|
||||||
|
inner = doubleMatch;
|
||||||
|
} else if (singleMatch != null) {
|
||||||
|
inner = singleMatch;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inner != null) {
|
if (inner != null) {
|
||||||
final int numSpaces = (int) inner.chars().filter(c -> c == ' ').count();
|
final int numSpaces = StringUtils.countCharOccurrences(inner, ' ');
|
||||||
for (int i = 0; i <= numSpaces; i++) {
|
for (int i = 0; i <= numSpaces; i++) {
|
||||||
inputQueue.remove();
|
inputQueue.remove();
|
||||||
}
|
}
|
||||||
|
|
@ -341,7 +353,7 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inner = inner.replace("\\\"", "\"").replace("\\\'", "\"");
|
inner = inner.replace("\\\"", "\"").replace("\\'", "'");
|
||||||
|
|
||||||
return ArgumentParseResult.success(inner);
|
return ArgumentParseResult.success(inner);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
//
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Copyright (c) 2020 Alexander Söderberg & Contributors
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
package cloud.commandframework.util;
|
||||||
|
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* String utilities
|
||||||
|
*/
|
||||||
|
public final class StringUtils {
|
||||||
|
|
||||||
|
private StringUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Count the occurrences of a character in a string
|
||||||
|
*
|
||||||
|
* @param haystack The string to search in
|
||||||
|
* @param needle The character to count for
|
||||||
|
* @return Number of occurrences
|
||||||
|
*/
|
||||||
|
public static int countCharOccurrences(final @NonNull String haystack, final char needle) {
|
||||||
|
int occurrences = 0;
|
||||||
|
for (int i = 0; i < haystack.length(); i++) {
|
||||||
|
if (haystack.charAt(i) == needle) {
|
||||||
|
occurrences++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return occurrences;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
//
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Copyright (c) 2020 Alexander Söderberg & Contributors
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility classes
|
||||||
|
*/
|
||||||
|
package cloud.commandframework.util;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue