-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathValueFlagPart.java
More file actions
160 lines (118 loc) · 4.42 KB
/
ValueFlagPart.java
File metadata and controls
160 lines (118 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package me.fixeddev.commandflow.part.defaults;
import me.fixeddev.commandflow.CommandContext;
import me.fixeddev.commandflow.ContextSnapshot;
import me.fixeddev.commandflow.exception.ArgumentParseException;
import me.fixeddev.commandflow.part.CommandPart;
import me.fixeddev.commandflow.part.SinglePartWrapper;
import me.fixeddev.commandflow.stack.ArgumentStack;
import me.fixeddev.commandflow.stack.StackSnapshot;
import net.kyori.text.Component;
import net.kyori.text.TextComponent;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public class ValueFlagPart implements SinglePartWrapper {
private final CommandPart part;
private final String name;
private final String shortName;
private final boolean allowFullName;
public ValueFlagPart(String shortName, boolean allowFullName, CommandPart part) {
this.name = part.getName();
this.shortName = shortName;
this.allowFullName = allowFullName;
this.part = part;
}
public ValueFlagPart(String shortName, CommandPart part) {
this.name = part.getName();
this.shortName = shortName;
this.allowFullName = false;
this.part = part;
}
@Override
public @Nullable Component getLineRepresentation() {
TextComponent.Builder builder = TextComponent.builder("[")
.append("-" + shortName + " ");
if (part.getLineRepresentation() != null) {
builder.append(part.getLineRepresentation());
}
builder.append("]");
return builder.build();
}
@Override
public CommandPart getPart() {
return part;
}
@Override
public String getName() {
return name;
}
@Override
public void parse(CommandContext context, ArgumentStack stack, CommandPart parent) throws ArgumentParseException {
StackSnapshot snapshot = stack.getSnapshot();
boolean found = false;
while (stack.hasNext()) {
String arg = stack.next();
if (!arg.startsWith("-")) {
continue;
}
if (arg.equals("--" + name) && allowFullName) {
found = parseValueFlag(context, stack);
break;
}
if (arg.equals("-" + shortName)) {
found = parseValueFlag(context, stack);
break;
}
}
if (!found) {
context.setValue(this, false);
}
stack.applySnapshot(snapshot, false);
}
@Override
public List<String> getSuggestions(CommandContext commandContext, ArgumentStack stack) {
String nextArgument = stack.hasNext() ? stack.next() : "";
List<String> suggestions = new ArrayList<>();
if (!nextArgument.startsWith("-")) {
return suggestions;
}
nextArgument = nextArgument.lastIndexOf('-') != 0 ? nextArgument.substring(2) : nextArgument.substring(1);
if (allowFullName&& name.startsWith(nextArgument)) {
suggestions.add("--" + name);
}
if (shortName.startsWith(nextArgument)) {
suggestions.add("-" + shortName);
}
if (nextArgument.equals(shortName) || nextArgument.equals(name)) {
suggestions.clear();
suggestions.addAll(part.getSuggestions(commandContext, stack));
}
return suggestions;
}
private boolean parseValueFlag(CommandContext context, ArgumentStack stack) {
StackSnapshot beforeRemoveFlagStack = stack.getSnapshot();
ContextSnapshot beforeParseContext = context.getSnapshot();
stack.remove();
int oldArgumentsLeft = stack.getArgumentsLeft();
StackSnapshot beforeParseStack = stack.getSnapshot();
// parse the next parts
try {
part.parse(context, stack, this);
} catch (ArgumentParseException ex) {
// ignore
context.applySnapshot(beforeParseContext);
stack.applySnapshot(beforeRemoveFlagStack);
return false;
}
int usedArguments = oldArgumentsLeft - stack.getArgumentsLeft();
if (usedArguments != 0) {
stack.applySnapshot(beforeParseStack);
// Otherwise it deletes the old cursor(element to remove - 1)
stack.next();
for (int i = 0; i < usedArguments; i++) {
stack.remove();
}
}
return true;
}
}