Program: 167692
Project: 167692
Buggy source:
protected int complete(CommandContext ctx,ParsedCommandLine parsedCmd,OperationCandidatesProvider candidatesProvider,final String buffer,int cursor,List<String> candidates){
  if (parsedCmd.isRequestComplete()) {
    return -1;
  }
  if (parsedCmd.endsOnHeaderListStart() || parsedCmd.hasHeaders()) {
    final Map<String,OperationRequestHeader> headers=candidatesProvider.getHeaders(ctx);
    if (headers.isEmpty()) {
      return -1;
    }
    int result=buffer.length();
    if (parsedCmd.getLastHeaderName() != null) {
      if (buffer.endsWith(parsedCmd.getLastHeaderName())) {
        result=parsedCmd.getLastChunkIndex();
        for (        String name : headers.keySet()) {
          if (name.equals(parsedCmd.getLastHeaderName())) {
            result=completeHeader(headers,ctx,parsedCmd,buffer,cursor,candidates);
            break;
          }
          if (!parsedCmd.hasHeader(name) && name.startsWith(parsedCmd.getLastHeaderName())) {
            candidates.add(name);
          }
        }
      }
 else {
        result=completeHeader(headers,ctx,parsedCmd,buffer,cursor,candidates);
      }
    }
 else {
      if (!parsedCmd.hasHeaders()) {
        candidates.addAll(headers.keySet());
      }
 else       if (parsedCmd.endsOnHeaderSeparator()) {
        candidates.addAll(headers.keySet());
        for (        ParsedOperationRequestHeader parsed : parsedCmd.getHeaders()) {
          candidates.remove(parsed.getName());
        }
      }
 else {
        final ParsedOperationRequestHeader lastParsedHeader=parsedCmd.getLastHeader();
        final OperationRequestHeader lastHeader=headers.get(lastParsedHeader.getName());
        if (lastHeader == null) {
          return -1;
        }
        final CommandLineCompleter headerCompleter=lastHeader.getCompleter();
        if (headerCompleter == null) {
          return -1;
        }
        result=headerCompleter.complete(ctx,buffer,cursor,candidates);
      }
    }
    Collections.sort(candidates);
    return result;
  }
  if (parsedCmd.endsOnPropertyListEnd()) {
    return buffer.length();
  }
  if (parsedCmd.hasProperties() || parsedCmd.endsOnPropertyListStart() || parsedCmd.endsOnNotOperator()) {
    if (!parsedCmd.hasOperationName()) {
      return -1;
    }
    final Collection<CommandArgument> allArgs=candidatesProvider.getProperties(ctx,parsedCmd.getOperationName(),parsedCmd.getAddress());
    if (allArgs.isEmpty()) {
      final CommandLineFormat format=parsedCmd.getFormat();
      if (!parsedCmd.hasProperties() && format != null && format.getPropertyListEnd() != null && format.getPropertyListEnd().length() > 0) {
        candidates.add(format.getPropertyListEnd());
      }
      return buffer.length();
    }
    try {
      if (parsedCmd.endsOnNotOperator()) {
        for (        CommandArgument arg : allArgs) {
          if (arg.canAppearNext(ctx)) {
            if (!arg.isValueRequired()) {
              candidates.add(arg.getFullName());
            }
          }
        }
        Collections.sort(candidates);
        return buffer.length();
      }
      if (!parsedCmd.hasProperties()) {
        boolean needNeg=false;
        for (        CommandArgument arg : allArgs) {
          if (arg.canAppearNext(ctx)) {
            if (arg.getIndex() >= 0) {
              final CommandLineCompleter valCompl=arg.getValueCompleter();
              if (valCompl != null) {
                valCompl.complete(ctx,"String_Node_Str",0,candidates);
              }
            }
 else {
              String argName=arg.getFullName();
              candidates.add(argName);
              if (!arg.isValueRequired()) {
                needNeg=true;
              }
            }
          }
        }
        if (needNeg) {
          candidates.add(Util.NOT_OPERATOR);
        }
        Collections.sort(candidates);
        return buffer.length();
      }
    }
 catch (    CommandFormatException e) {
      return -1;
    }
    int result=buffer.length();
    String chunk=null;
    CommandLineCompleter valueCompleter=null;
    if (!parsedCmd.endsOnPropertySeparator()) {
      final String argName=parsedCmd.getLastParsedPropertyName();
      final String argValue=parsedCmd.getLastParsedPropertyValue();
      if (argValue != null || parsedCmd.endsOnPropertyValueSeparator()) {
        result=parsedCmd.getLastChunkIndex();
        if (parsedCmd.endsOnPropertyValueSeparator()) {
          ++result;
        }
        chunk=argValue;
        if (argName != null) {
          valueCompleter=getValueCompleter(ctx,allArgs,argName);
        }
 else {
          valueCompleter=getValueCompleter(ctx,allArgs,parsedCmd.getOtherProperties().size() - 1);
        }
        if (valueCompleter == null) {
          if (parsedCmd.endsOnSeparator()) {
            return -1;
          }
          for (          CommandArgument arg : allArgs) {
            try {
              if (arg.canAppearNext(ctx) && !arg.getFullName().equals(argName)) {
                return -1;
              }
            }
 catch (            CommandFormatException e) {
              break;
            }
          }
          final CommandLineFormat format=parsedCmd.getFormat();
          if (format != null && format.getPropertyListEnd() != null && format.getPropertyListEnd().length() > 0) {
            candidates.add(format.getPropertyListEnd());
          }
          return buffer.length();
        }
      }
 else {
        chunk=argName;
        result=parsedCmd.getLastChunkIndex();
      }
    }
 else {
      chunk=null;
    }
    if (valueCompleter != null) {
      if (chunk == null) {
        String parsedName=parsedCmd.getLastParsedPropertyName();
        for (        CommandArgument arg : allArgs) {
          String argFullName=arg.getFullName();
          if (argFullName.equals(parsedName)) {
            if (!arg.isValueRequired()) {
              candidates.add(Util.FALSE);
              return result;
            }
          }
        }
      }
      int valueResult=valueCompleter.complete(ctx,chunk == null ? "String_Node_Str" : chunk,chunk == null ? 0 : chunk.length(),candidates);
      if (valueResult < 0) {
        return valueResult;
      }
 else {
        if (suggestionEqualsUserEntry(candidates,chunk,valueResult)) {
          final CommandLineFormat format=parsedCmd.getFormat();
          if (format != null) {
            for (            CommandArgument arg : allArgs) {
              try {
                if (arg.canAppearNext(ctx)) {
                  candidates.set(0,"String_Node_Str" + format.getPropertySeparator());
                  return buffer.length();
                }
              }
 catch (              CommandFormatException e) {
                e.printStackTrace();
                return result + valueResult;
              }
            }
            candidates.set(0,format.getPropertyListEnd());
            return buffer.length();
          }
        }
        return result + valueResult;
      }
    }
    CommandArgument lastArg=null;
    boolean allPropertiesPresent=true;
    for (    CommandArgument arg : allArgs) {
      try {
        if (arg.canAppearNext(ctx)) {
          allPropertiesPresent=false;
        }
 else {
          String argFullName=arg.getFullName();
          if (argFullName.equals(chunk)) {
            lastArg=arg;
          }
        }
      }
 catch (      CommandFormatException e) {
        e.printStackTrace();
        return -1;
      }
    }
    boolean needNeg=false;
    boolean optionMatch=false;
    for (    CommandArgument arg : allArgs) {
      try {
        if (arg.canAppearNext(ctx)) {
          if (arg.getIndex() < 0) {
            String argFullName=arg.getFullName();
            if (chunk != null && argFullName.startsWith(chunk)) {
              if (!parsedCmd.isLastPropertyNegated()) {
                optionMatch=true;
                break;
              }
 else               if (!arg.isValueRequired()) {
                optionMatch=true;
                break;
              }
            }
          }
        }
      }
 catch (      CommandFormatException e) {
        return -1;
      }
    }
    for (    CommandArgument arg : allArgs) {
      try {
        if (arg.canAppearNext(ctx)) {
          if (arg.getIndex() >= 0) {
            if (optionMatch) {
              continue;
            }
            CommandLineCompleter valCompl=arg.getValueCompleter();
            if (valCompl != null) {
              final String value=chunk == null ? "String_Node_Str" : chunk;
              valCompl.complete(ctx,value,value.length(),candidates);
            }
          }
 else {
            String argFullName=arg.getFullName();
            if (chunk == null || argFullName.startsWith(chunk)) {
              if (!parsedCmd.isLastPropertyNegated()) {
                candidates.add(argFullName);
              }
 else               if (!arg.isValueRequired()) {
                candidates.add(argFullName);
              }
            }
            if (!arg.isValueRequired() && !parsedCmd.isLastPropertyNegated()) {
              needNeg=true;
            }
          }
        }
      }
 catch (      CommandFormatException e) {
        e.printStackTrace();
        return -1;
      }
    }
    if (needNeg && parsedCmd.endsOnPropertySeparator()) {
      candidates.add(Util.NOT_OPERATOR);
    }
    if (lastArg != null) {
      if (lastArg.isValueRequired()) {
        candidates.add(lastArg.getFullName() + "String_Node_Str");
      }
 else       if (lastArg instanceof ArgumentWithoutValue) {
        ArgumentWithoutValue argWithoutValue=(ArgumentWithoutValue)lastArg;
        if (!argWithoutValue.isExclusive()) {
          if (!allPropertiesPresent) {
            CommandLineFormat format=parsedCmd.getFormat();
            if (format != null && format.getPropertySeparator() != null) {
              candidates.add(lastArg.getFullName() + format.getPropertySeparator());
            }
          }
        }
      }
 else {
        CommandLineFormat format=parsedCmd.getFormat();
        if (!parsedCmd.isLastPropertyNegated()) {
          candidates.add("String_Node_Str" + Util.FALSE);
        }
        if (format != null && format.getPropertyListEnd() != null && format.getPropertyListEnd().length() > 0) {
          candidates.add(format.getPropertyListEnd());
          if (!allPropertiesPresent) {
            candidates.add(format.getPropertySeparator());
          }
        }
      }
    }
    if (candidates.isEmpty()) {
      if (chunk == null && !parsedCmd.endsOnSeparator()) {
        final CommandLineFormat format=parsedCmd.getFormat();
        if (format != null && format.getPropertyListEnd() != null && format.getPropertyListEnd().length() > 0) {
          candidates.add(format.getPropertyListEnd());
        }
      }
    }
 else {
      Collections.sort(candidates);
    }
    return result;
  }
  if (parsedCmd.hasOperationName() || parsedCmd.endsOnAddressOperationNameSeparator()) {
    if (parsedCmd.getAddress().endsOnType()) {
      return -1;
    }
    final Collection<String> names=candidatesProvider.getOperationNames(ctx,parsedCmd.getAddress());
    if (names.isEmpty()) {
      return -1;
    }
    final String chunk=parsedCmd.getOperationName();
    if (chunk == null) {
      candidates.addAll(names);
    }
 else {
      for (      String name : names) {
        if (name.startsWith(chunk)) {
          candidates.add(name);
        }
      }
    }
    Collections.sort(candidates);
    if (parsedCmd.endsOnSeparator()) {
      return buffer.length();
    }
 else {
      if (chunk != null && candidates.size() == 1 && chunk.equals(candidates.get(0)) && parsedCmd.getFormat().getPropertyListStart().length() > 0) {
        candidates.set(0,chunk + parsedCmd.getFormat().getPropertyListStart());
      }
      return parsedCmd.getLastChunkIndex();
    }
  }
  final OperationRequestAddress address=parsedCmd.getAddress();
  if (buffer.endsWith("String_Node_Str")) {
    return -1;
  }
  final String chunk;
  if (address.isEmpty() || parsedCmd.endsOnNodeSeparator() || parsedCmd.endsOnNodeTypeNameSeparator()|| address.equals(ctx.getCurrentNodePath())) {
    chunk=null;
  }
 else   if (address.endsOnType()) {
    chunk=address.getNodeType();
    address.toParentNode();
  }
 else {
    chunk=address.toNodeType();
  }
  final Collection<String> names;
  if (address.endsOnType()) {
    names=candidatesProvider.getNodeNames(ctx,address);
  }
 else {
    names=candidatesProvider.getNodeTypes(ctx,address);
  }
  if (names.isEmpty()) {
    return -1;
  }
  if (chunk == null) {
    candidates.addAll(names);
  }
 else {
    for (    String name : names) {
      if (name.startsWith(chunk)) {
        candidates.add(name);
      }
    }
  }
  String lastSegment="String_Node_Str";
  if (parsedCmd.getSubstitutedLine().length() >= parsedCmd.getLastSeparatorIndex()) {
    lastSegment=parsedCmd.getSubstitutedLine().substring(parsedCmd.getLastSeparatorIndex() + 1);
  }
  SegmentParsingInitialState.SegmentParsingCallbackHandler parsedSegment=parseLastSegment(lastSegment);
  int offset;
  if (candidates.size() == 1) {
    final String candidate=candidates.get(0);
    if (address.endsOnType()) {
      if (chunk != null && chunk.equals(candidate)) {
        if (parsedSegment.isOpenQuotes()) {
          candidates.set(0,"String_Node_Str");
        }
 else {
          candidates.set(0,parsedCmd.getFormat().getAddressOperationSeparator());
          candidates.add(parsedCmd.getFormat().getNodeSeparator());
        }
        return buffer.length();
      }
      if (parsedSegment.isOpenQuotes()) {
        String escapedCandidate=Util.escapeString(candidate,ESCAPE_SELECTOR_INSIDE_QUOTES);
        offset=parsedSegment.getOffset() - 1;
        candidates.set(0,"String_Node_Str" + escapedCandidate + "String_Node_Str");
      }
 else {
        String escapedCandidate=Util.escapeString(candidate,ESCAPE_SELECTOR);
        offset=parsedSegment.getOffset();
        candidates.set(0,escapedCandidate);
      }
    }
 else {
      if (chunk != null && chunk.equals(candidate)) {
        if (parsedSegment.isOpenQuotes()) {
          candidates.set(0,"String_Node_Str");
        }
 else {
          candidates.set(0,"String_Node_Str");
        }
        return buffer.length();
      }
      if (parsedSegment.isOpenQuotes()) {
        String escapedCandidate=Util.escapeString(candidate,ESCAPE_SELECTOR_INSIDE_QUOTES);
        offset=parsedSegment.getOffset() - 1;
        candidates.set(0,"String_Node_Str" + escapedCandidate + "String_Node_Str");
      }
 else {
        String escapedCandidate=Util.escapeString(candidate,ESCAPE_SELECTOR);
        offset=parsedSegment.getOffset();
        candidates.set(0,escapedCandidate + "String_Node_Str");
      }
    }
  }
 else {
    if (parsedSegment.isOpenQuotes()) {
      Util.sortAndEscape(candidates,ESCAPE_SELECTOR_INSIDE_QUOTES);
    }
 else {
      Util.sortAndEscape(candidates,ESCAPE_SELECTOR);
    }
    offset=parsedSegment.getOffset();
  }
  return parsedCmd.getLastSeparatorIndex() + 1 + offset;
}

Fixed version (ground truth):
protected int complete(CommandContext ctx,ParsedCommandLine parsedCmd,OperationCandidatesProvider candidatesProvider,final String buffer,int cursor,List<String> candidates){
  if (parsedCmd.isRequestComplete()) {
    return -1;
  }
  if (parsedCmd.endsOnHeaderListStart() || parsedCmd.hasHeaders()) {
    final Map<String,OperationRequestHeader> headers=candidatesProvider.getHeaders(ctx);
    if (headers.isEmpty()) {
      return -1;
    }
    int result=buffer.length();
    if (parsedCmd.getLastHeaderName() != null) {
      if (buffer.endsWith(parsedCmd.getLastHeaderName())) {
        result=parsedCmd.getLastChunkOriginalIndex();
        for (        String name : headers.keySet()) {
          if (name.equals(parsedCmd.getLastHeaderName())) {
            result=completeHeader(headers,ctx,parsedCmd,buffer,cursor,candidates);
            break;
          }
          if (!parsedCmd.hasHeader(name) && name.startsWith(parsedCmd.getLastHeaderName())) {
            candidates.add(name);
          }
        }
      }
 else {
        result=completeHeader(headers,ctx,parsedCmd,buffer,cursor,candidates);
      }
    }
 else {
      if (!parsedCmd.hasHeaders()) {
        candidates.addAll(headers.keySet());
      }
 else       if (parsedCmd.endsOnHeaderSeparator()) {
        candidates.addAll(headers.keySet());
        for (        ParsedOperationRequestHeader parsed : parsedCmd.getHeaders()) {
          candidates.remove(parsed.getName());
        }
      }
 else {
        final ParsedOperationRequestHeader lastParsedHeader=parsedCmd.getLastHeader();
        final OperationRequestHeader lastHeader=headers.get(lastParsedHeader.getName());
        if (lastHeader == null) {
          return -1;
        }
        final CommandLineCompleter headerCompleter=lastHeader.getCompleter();
        if (headerCompleter == null) {
          return -1;
        }
        result=headerCompleter.complete(ctx,buffer,cursor,candidates);
      }
    }
    Collections.sort(candidates);
    return result;
  }
  if (parsedCmd.endsOnPropertyListEnd()) {
    return buffer.length();
  }
  if (parsedCmd.hasProperties() || parsedCmd.endsOnPropertyListStart() || parsedCmd.endsOnNotOperator()) {
    if (!parsedCmd.hasOperationName()) {
      return -1;
    }
    final Collection<CommandArgument> allArgs=candidatesProvider.getProperties(ctx,parsedCmd.getOperationName(),parsedCmd.getAddress());
    if (allArgs.isEmpty()) {
      final CommandLineFormat format=parsedCmd.getFormat();
      if (!parsedCmd.hasProperties() && format != null && format.getPropertyListEnd() != null && format.getPropertyListEnd().length() > 0) {
        candidates.add(format.getPropertyListEnd());
      }
      return buffer.length();
    }
    try {
      if (parsedCmd.endsOnNotOperator()) {
        for (        CommandArgument arg : allArgs) {
          if (arg.canAppearNext(ctx)) {
            if (!arg.isValueRequired()) {
              candidates.add(arg.getFullName());
            }
          }
        }
        Collections.sort(candidates);
        return buffer.length();
      }
      if (!parsedCmd.hasProperties()) {
        boolean needNeg=false;
        for (        CommandArgument arg : allArgs) {
          if (arg.canAppearNext(ctx)) {
            if (arg.getIndex() >= 0) {
              final CommandLineCompleter valCompl=arg.getValueCompleter();
              if (valCompl != null) {
                valCompl.complete(ctx,"String_Node_Str",0,candidates);
              }
            }
 else {
              String argName=arg.getFullName();
              candidates.add(argName);
              if (!arg.isValueRequired()) {
                needNeg=true;
              }
            }
          }
        }
        if (needNeg) {
          candidates.add(Util.NOT_OPERATOR);
        }
        Collections.sort(candidates);
        return buffer.length();
      }
    }
 catch (    CommandFormatException e) {
      return -1;
    }
    int result=buffer.length();
    String chunk=null;
    CommandLineCompleter valueCompleter=null;
    if (!parsedCmd.endsOnPropertySeparator()) {
      final String argName=parsedCmd.getLastParsedPropertyName();
      final String argValue=parsedCmd.getLastParsedPropertyValue();
      if (argValue != null || parsedCmd.endsOnPropertyValueSeparator()) {
        result=parsedCmd.getLastChunkIndex();
        if (parsedCmd.endsOnPropertyValueSeparator()) {
          ++result;
        }
        chunk=argValue;
        if (argName != null) {
          valueCompleter=getValueCompleter(ctx,allArgs,argName);
        }
 else {
          valueCompleter=getValueCompleter(ctx,allArgs,parsedCmd.getOtherProperties().size() - 1);
        }
        if (valueCompleter == null) {
          if (parsedCmd.endsOnSeparator()) {
            return -1;
          }
          for (          CommandArgument arg : allArgs) {
            try {
              if (arg.canAppearNext(ctx) && !arg.getFullName().equals(argName)) {
                return -1;
              }
            }
 catch (            CommandFormatException e) {
              break;
            }
          }
          final CommandLineFormat format=parsedCmd.getFormat();
          if (format != null && format.getPropertyListEnd() != null && format.getPropertyListEnd().length() > 0) {
            candidates.add(format.getPropertyListEnd());
          }
          return buffer.length();
        }
      }
 else {
        chunk=argName;
        result=parsedCmd.getLastChunkIndex();
      }
    }
 else {
      chunk=null;
    }
    if (valueCompleter != null) {
      if (chunk == null) {
        String parsedName=parsedCmd.getLastParsedPropertyName();
        for (        CommandArgument arg : allArgs) {
          String argFullName=arg.getFullName();
          if (argFullName.equals(parsedName)) {
            if (!arg.isValueRequired()) {
              candidates.add(Util.FALSE);
              return parsedCmd.getLastChunkOriginalIndex();
            }
          }
        }
      }
      SubstitutedLine substitutions=parsedCmd.getSubstitutions();
      int valueResult=valueCompleter.complete(ctx,chunk == null ? "String_Node_Str" : chunk,chunk == null ? 0 : chunk.length(),candidates);
      if (valueResult < 0) {
        return valueResult;
      }
 else {
        if (suggestionEqualsUserEntry(candidates,chunk,valueResult)) {
          final CommandLineFormat format=parsedCmd.getFormat();
          if (format != null) {
            for (            CommandArgument arg : allArgs) {
              try {
                if (arg.canAppearNext(ctx)) {
                  candidates.set(0,"String_Node_Str" + format.getPropertySeparator());
                  return buffer.length();
                }
              }
 catch (              CommandFormatException e) {
                return -1;
              }
            }
            candidates.set(0,format.getPropertyListEnd());
            return buffer.length();
          }
        }
        int correctedValueOffset=substitutions.getOriginalOffset(result + valueResult);
        return correctedValueOffset;
      }
    }
    CommandArgument lastArg=null;
    boolean allPropertiesPresent=true;
    for (    CommandArgument arg : allArgs) {
      try {
        if (arg.canAppearNext(ctx)) {
          allPropertiesPresent=false;
        }
 else {
          String argFullName=arg.getFullName();
          if (argFullName.equals(chunk)) {
            lastArg=arg;
          }
        }
      }
 catch (      CommandFormatException e) {
        return -1;
      }
    }
    boolean needNeg=false;
    boolean optionMatch=false;
    for (    CommandArgument arg : allArgs) {
      try {
        if (arg.canAppearNext(ctx)) {
          if (arg.getIndex() < 0) {
            String argFullName=arg.getFullName();
            if (chunk != null && argFullName.startsWith(chunk)) {
              if (!parsedCmd.isLastPropertyNegated()) {
                optionMatch=true;
                break;
              }
 else               if (!arg.isValueRequired()) {
                optionMatch=true;
                break;
              }
            }
          }
        }
      }
 catch (      CommandFormatException e) {
        return -1;
      }
    }
    for (    CommandArgument arg : allArgs) {
      try {
        if (arg.canAppearNext(ctx)) {
          if (arg.getIndex() >= 0) {
            if (optionMatch) {
              continue;
            }
            CommandLineCompleter valCompl=arg.getValueCompleter();
            if (valCompl != null) {
              final String value=chunk == null ? "String_Node_Str" : chunk;
              valCompl.complete(ctx,value,value.length(),candidates);
            }
          }
 else {
            String argFullName=arg.getFullName();
            if (chunk == null || argFullName.startsWith(chunk)) {
              if (!parsedCmd.isLastPropertyNegated()) {
                candidates.add(argFullName);
              }
 else               if (!arg.isValueRequired()) {
                candidates.add(argFullName);
              }
            }
            if (!arg.isValueRequired() && !parsedCmd.isLastPropertyNegated()) {
              needNeg=true;
            }
          }
        }
      }
 catch (      CommandFormatException e) {
        return -1;
      }
    }
    if (needNeg && parsedCmd.endsOnPropertySeparator()) {
      candidates.add(Util.NOT_OPERATOR);
    }
    if (lastArg != null) {
      if (lastArg.isValueRequired()) {
        candidates.add(lastArg.getFullName() + "String_Node_Str");
      }
 else       if (lastArg instanceof ArgumentWithoutValue) {
        ArgumentWithoutValue argWithoutValue=(ArgumentWithoutValue)lastArg;
        if (!argWithoutValue.isExclusive()) {
          if (!allPropertiesPresent) {
            CommandLineFormat format=parsedCmd.getFormat();
            if (format != null && format.getPropertySeparator() != null) {
              candidates.add(lastArg.getFullName() + format.getPropertySeparator());
            }
          }
        }
      }
 else {
        CommandLineFormat format=parsedCmd.getFormat();
        if (!parsedCmd.isLastPropertyNegated()) {
          candidates.add("String_Node_Str" + Util.FALSE);
        }
        if (format != null && format.getPropertyListEnd() != null && format.getPropertyListEnd().length() > 0) {
          candidates.add(format.getPropertyListEnd());
          if (!allPropertiesPresent) {
            candidates.add(format.getPropertySeparator());
          }
        }
      }
    }
    if (candidates.isEmpty()) {
      if (chunk == null && !parsedCmd.endsOnSeparator()) {
        final CommandLineFormat format=parsedCmd.getFormat();
        if (format != null && format.getPropertyListEnd() != null && format.getPropertyListEnd().length() > 0) {
          candidates.add(format.getPropertyListEnd());
        }
      }
    }
 else {
      Collections.sort(candidates);
    }
    return parsedCmd.getOriginalOffset(result);
  }
  if (parsedCmd.hasOperationName() || parsedCmd.endsOnAddressOperationNameSeparator()) {
    if (parsedCmd.getAddress().endsOnType()) {
      return -1;
    }
    final Collection<String> names=candidatesProvider.getOperationNames(ctx,parsedCmd.getAddress());
    if (names.isEmpty()) {
      return -1;
    }
    final String chunk=parsedCmd.getOperationName();
    if (chunk == null) {
      candidates.addAll(names);
    }
 else {
      for (      String name : names) {
        if (name.startsWith(chunk)) {
          candidates.add(name);
        }
      }
    }
    Collections.sort(candidates);
    if (parsedCmd.endsOnSeparator()) {
      return buffer.length();
    }
 else {
      if (chunk != null && candidates.size() == 1 && chunk.equals(candidates.get(0)) && parsedCmd.getFormat().getPropertyListStart().length() > 0) {
        candidates.set(0,chunk + parsedCmd.getFormat().getPropertyListStart());
      }
      return parsedCmd.getLastChunkOriginalIndex();
    }
  }
  final OperationRequestAddress address=parsedCmd.getAddress();
  if (buffer.endsWith("String_Node_Str")) {
    return -1;
  }
  final String chunk;
  if (address.isEmpty() || parsedCmd.endsOnNodeSeparator() || parsedCmd.endsOnNodeTypeNameSeparator()|| address.equals(ctx.getCurrentNodePath())) {
    chunk=null;
  }
 else   if (address.endsOnType()) {
    chunk=address.getNodeType();
    address.toParentNode();
  }
 else {
    chunk=address.toNodeType();
  }
  final Collection<String> names;
  if (address.endsOnType()) {
    names=candidatesProvider.getNodeNames(ctx,address);
  }
 else {
    names=candidatesProvider.getNodeTypes(ctx,address);
  }
  if (names.isEmpty()) {
    return -1;
  }
  if (chunk == null) {
    candidates.addAll(names);
  }
 else {
    for (    String name : names) {
      if (name.startsWith(chunk)) {
        candidates.add(name);
      }
    }
  }
  String lastSegment="String_Node_Str";
  if (parsedCmd.getSubstitutedLine().length() >= parsedCmd.getLastSeparatorIndex()) {
    lastSegment=parsedCmd.getSubstitutedLine().substring(parsedCmd.getLastSeparatorIndex() + 1);
  }
  SegmentParsingInitialState.SegmentParsingCallbackHandler parsedSegment=parseLastSegment(lastSegment);
  int offset;
  if (candidates.size() == 1) {
    final String candidate=candidates.get(0);
    if (address.endsOnType()) {
      if (chunk != null && chunk.equals(candidate)) {
        if (parsedSegment.isOpenQuotes()) {
          candidates.set(0,"String_Node_Str");
        }
 else {
          candidates.set(0,parsedCmd.getFormat().getAddressOperationSeparator());
          candidates.add(parsedCmd.getFormat().getNodeSeparator());
        }
        return buffer.length();
      }
      if (parsedSegment.isOpenQuotes()) {
        String escapedCandidate=Util.escapeString(candidate,ESCAPE_SELECTOR_INSIDE_QUOTES);
        offset=parsedSegment.getOffset() - 1;
        candidates.set(0,"String_Node_Str" + escapedCandidate + "String_Node_Str");
      }
 else {
        String escapedCandidate=Util.escapeString(candidate,ESCAPE_SELECTOR);
        offset=parsedSegment.getOffset();
        candidates.set(0,escapedCandidate);
      }
    }
 else {
      if (chunk != null && chunk.equals(candidate)) {
        if (parsedSegment.isOpenQuotes()) {
          candidates.set(0,"String_Node_Str");
        }
 else {
          candidates.set(0,"String_Node_Str");
        }
        return buffer.length();
      }
      if (parsedSegment.isOpenQuotes()) {
        String escapedCandidate=Util.escapeString(candidate,ESCAPE_SELECTOR_INSIDE_QUOTES);
        offset=parsedSegment.getOffset() - 1;
        candidates.set(0,"String_Node_Str" + escapedCandidate + "String_Node_Str");
      }
 else {
        String escapedCandidate=Util.escapeString(candidate,ESCAPE_SELECTOR);
        offset=parsedSegment.getOffset();
        candidates.set(0,escapedCandidate + "String_Node_Str");
      }
    }
  }
 else {
    if (parsedSegment.isOpenQuotes()) {
      Util.sortAndEscape(candidates,ESCAPE_SELECTOR_INSIDE_QUOTES);
    }
 else {
      Util.sortAndEscape(candidates,ESCAPE_SELECTOR);
    }
    offset=parsedSegment.getOffset();
  }
  return parsedCmd.getLastSeparatorOriginalIndex() + 1 + offset;
}