Skip to content
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

Revert "优化识别重定向" ,修复 watch等命令不支持构造函数 <init> 的问题 #2944

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
public class CliTokenImpl implements CliToken {
private static final String PIPE = "|";
private static final String REDIRECT = ">";
private static final String REDIRECT_APPEND = ">>";

final boolean text;
final String raw;
Expand Down Expand Up @@ -74,7 +71,7 @@ public static List<CliToken> tokenize(String s) {

tokenize(s, 0, tokens);

tokens = adjustTokensForSpecialSymbols(tokens);
tokens = correctPipeChar(tokens);
return tokens;

}
Expand All @@ -90,53 +87,35 @@ public static List<CliToken> tokenize(String s) {
* unsupported:
* 3) thread|grep xxx
* 4) trace -E classA|classB methodA|methodB|grep classA
*
* Also handles redirection of '>' and '>>' in the similar way
*
* @param tokens original tokens
* @return adjusted tokens
* @param tokens
* @return
*/
private static List<CliToken> adjustTokensForSpecialSymbols(List<CliToken> tokens) {
return separateLeadingAndTailingSymbol(tokens, PIPE, REDIRECT_APPEND, REDIRECT);
}

private static List<CliToken> separateLeadingAndTailingSymbol(List<CliToken> tokens, String... symbols) {
List<CliToken> adjustedTokens = new ArrayList<>();
private static List<CliToken> correctPipeChar(List<CliToken> tokens) {
List<CliToken> newTokens = new ArrayList<CliToken>(tokens.size()+4);
for (CliToken token : tokens) {
String value = token.value();
String raw = token.raw();
boolean handled = false;
for (String symbol : symbols) {
if (value.equals(symbol)) {
break;
} else if (value.endsWith(symbol)) {
handled = true;
int lastIndexOfSymbol = raw.lastIndexOf(symbol);
adjustedTokens.add(new CliTokenImpl(
token.isText(),
raw.substring(0, lastIndexOfSymbol),
value.substring(0, value.length() - symbol.length())
));
adjustedTokens.add(new CliTokenImpl(true, raw.substring(lastIndexOfSymbol), symbol));
break;
} else if (value.startsWith(symbol)) {
handled = true;
int firstIndexOfSymbol = raw.indexOf(symbol);
adjustedTokens.add(new CliTokenImpl(true,
raw.substring(0, firstIndexOfSymbol + symbol.length()), symbol));
adjustedTokens.add(new CliTokenImpl(
token.isText(),
raw.substring(firstIndexOfSymbol + symbol.length()),
value.substring(symbol.length())
));
break;
}
}
if (!handled) {
adjustedTokens.add(token);
String tokenValue = token.value();
if (tokenValue.length()>1 && tokenValue.endsWith("|")) {
//split last char '|'
tokenValue = tokenValue.substring(0, tokenValue.length()-1);
String rawValue = token.raw();
rawValue = rawValue.substring(0, rawValue.length()-1);
newTokens.add(new CliTokenImpl(token.isText(), rawValue, tokenValue));
//add '|' char
newTokens.add(new CliTokenImpl(true, "|", "|"));

} else if (tokenValue.length()>1 && tokenValue.startsWith("|")) {
//add '|' char
newTokens.add(new CliTokenImpl(true, "|", "|"));
//remove first char '|'
tokenValue = tokenValue.substring(1);
String rawValue = token.raw();
rawValue = rawValue.substring(1);
newTokens.add(new CliTokenImpl(token.isText(), rawValue, tokenValue));
} else {
newTokens.add(token);
}
}
return adjustedTokens;
return newTokens;
}

private static void tokenize(String s, int index, List<CliToken> builder) {
Expand Down
Loading
Loading