Skip to content

Commit

Permalink
[WFCORE-4916] Unclear attribute name completion for LIST type
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristinaDsl committed May 16, 2023
1 parent a414156 commit a061d19
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,11 @@ public int complete(String buffer, List<String> candidates, final ModelNode desc
return -1;
}
final Collection<String> foundCandidates = handler.getCandidates(descr, writeOnly);

if(foundCandidates.isEmpty()) {
return -1;
}

candidates.addAll(foundCandidates);
return handler.getCandidateIndex();
}
Expand Down Expand Up @@ -349,6 +351,11 @@ public Collection<String> getCandidates(ModelNode attrsDescr, boolean writeOnly)
candidateIndex += chunk.length();
}
candidates.add("[");
}else if (modelType.equals(ModelType.STRING)) {
if (candidates.isEmpty()) {
candidateIndex += chunk.length();
}
candidates.add("");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.jboss.as.cli.parsing.StateParser.SubstitutedLine;
import org.jboss.logging.Logger;


/**
*
* @author Alexey Loubyansky
Expand Down Expand Up @@ -411,28 +410,29 @@ private int completeWithValueCompleter(CommandContext ctx, ParsedCommandLine par

final String normalizedChunk = chunk == null ? "" : chunk;
int valueResult = valueCompleter.complete(ctx, normalizedChunk, normalizedChunk.length(), candidates);

// No proposition.
if (valueResult < 0) {
return valueResult;
} else {
// Implies a single candidate to inline, the value is complete.
// propose the property separator if more properties to come
// or the propertyListEnd if no more properties.
if (suggestionEqualsUserEntry(candidates, chunk, valueResult)) {
if (suggestionEqualsUserEntry(candidates, chunk, valueResult) || areIncludedCandidatesForSpecificValueTypes(candidates)) {
final CommandLineFormat format = parsedCmd.getFormat();
if (format != null) {
for (CommandArgument arg : allArgs) {
try {
if (arg.canAppearNext(ctx)) {
candidates.set(0, "" + format.getPropertySeparator());
candidates.add("" + format.getPropertySeparator());
return buffer.length();
}
} catch (CommandFormatException e) {
return -1;
}
}
// inline the end of properties.
candidates.set(0, format.getPropertyListEnd());
candidates.add(format.getPropertyListEnd());
// at the end of the input.
return buffer.length();
}
Expand Down Expand Up @@ -730,16 +730,31 @@ private SegmentParsingInitialState.SegmentParsingCallbackHandler parseLastSegmen
}

private boolean suggestionEqualsUserEntry(List<String> candidates, String chunk, int suggestionOffset) {

if (chunk == null || candidates.size() != 1) {
return false;
}

if (suggestionOffset > 0) {
if (suggestionOffset > 0 && candidates.get(0)!="") {
// user entry before suggestionOffset is always the same - compare only part after offset
return chunk.substring(suggestionOffset).equals(candidates.get(0));
} else {
return chunk.equals(candidates.get(0));
if(chunk.equals(candidates.get(0))){
candidates.clear();
return true;
}
return false;
}
}

boolean areIncludedCandidatesForSpecificValueTypes(List<String> candidates){
if(candidates.contains("[") || candidates.contains(".")){
return true;
}else if(candidates.contains("")){
candidates.remove("");
return true;
}
return false;
}

protected CommandLineCompleter getValueCompleter(CommandContext ctx, Iterable<CommandArgument> allArgs, final String argName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ protected List<CommandArgument> getPropertiesFromPropList(List<Property> propLis
if(ctx.getParsedCommandLine().getLastParsedPropertyValue() == null) {
radical = ctx.getParsedCommandLine().getLastParsedPropertyName();
//Check if the property is completely specified and is negated
if(ctx.getParsedCommandLine().isLastPropertyNegated()) {
if(ctx.getParsedCommandLine().isLastPropertyNegated() || radical!=null) {
for (Property prop : propList) {
if(radical.equals(prop.getName())){
radical = null;
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main/java/org/jboss/as/cli/parsing/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ public void leavingState(ParsingContext ctx) throws CommandFormatException {
// property value (e.g. a closing } or ], or " is missing) and it's not
// really a property separator.
format.isPropertySeparator(ctx.getCharacter()) && ctx.getError() == null) {
handler.propertySeparator(ctx.getLocation());
}
handler.propertySeparator(ctx.getLocation());
}

buffer.setLength(0);
name = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public void testMain() throws Exception {

candidates.clear();
i = completer.complete(null, "str", 0, candidates);
assertEquals(Arrays.asList("str2"), candidates);
assertEquals(Arrays.asList("", "str2"), candidates);
assertEquals(0, i);

candidates.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1158,10 +1158,12 @@ public void testCommandsCompletion2() throws Exception {
List<String> candidates = new ArrayList<>();
ctx.getDefaultCommandCompleter().complete(ctx, cmd,
cmd.length(), candidates);
assertTrue(candidates.toString(), candidates.size() == 1);
assertTrue(candidates.toString(), candidates.size() == 2);
assertTrue(candidates.toString(), candidates.contains(" "));
assertTrue(candidates.toString(), candidates.contains("namespaces"));
candidates = complete(ctx, cmd, false, cmd.length() - "name".length());
assertTrue(candidates.toString(), candidates.size() == 1);
candidates = complete(ctx, cmd, false, cmd.length());
assertTrue(candidates.toString(), candidates.size() == 2);
assertTrue(candidates.toString(), candidates.contains(" "));
assertTrue(candidates.toString(), candidates.contains("namespaces"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ public void testAttachment() throws Exception {
cli.getCommandContext().getDefaultCommandCompleter().complete(cli.getCommandContext(), cmd,
cmd.length(), candidates);
assertTrue(candidates.toString(), candidates.contains("["));
assertTrue(candidates.toString(), candidates.size() == 1);
assertTrue(candidates.toString(), candidates.contains(")"));
assertTrue(candidates.toString(), candidates.size() == 2);
}

{
Expand Down

0 comments on commit a061d19

Please sign in to comment.