List<SearchSuggestion> suggestions = convert(valueSuggestions, parsed, parsedTerm, Kind.Simple);
return suggestions;
}
public List<SearchSuggestion> getAdvancedSuggestions(String expression, int caretPos, String tab) {
SearchAssistant completor = getTabAwareSearchAssistant(tab);
boolean isDebugEnabled = LOG.isDebugEnabled();
LOG.debug("getAdvancedSuggestions: START");
SearchTermAssistant assistant = new SearchTermAssistant(expression, caretPos);
String[] tokens = assistant.getTerms().toArray(new String[0]);
if (isDebugEnabled) {
LOG.debug("" + tokens.length + " tokens are " + Arrays.asList(tokens));
}
if (tokens.length == 0 || caretPos == 0) {
LOG.debug("getAdvancedSuggestions: no terms");
return convert(getAllContexts()); // no terms yet defined
}
String beforeCaret = assistant.getFragmentBeforeCaret();
if (isDebugEnabled) {
LOG.debug("getAdvancedSuggestions: beforeCaret is '" + beforeCaret + "'");
}
String pad = getQuotePadding(beforeCaret);
if (isDebugEnabled) {
LOG.debug("getAdvancedSuggestions: padding is ~" + pad + "~");
}
if (beforeCaret.startsWith("'") || beforeCaret.startsWith("\"")) {
return Collections.emptyList();
}
ParsedContext parsed = ParsedContext.get(beforeCaret);
if (isDebugEnabled) {
LOG.debug("getAdvancedSuggestions: parsed is " + parsed);
}
switch (parsed.state) {
case CONTEXT:
if (parsed.context.equals("")) {
LOG.debug("getAdvancedSuggestions: empty term, suggesting all contexts");
return convert(getAllContexts());
/*
if (tokens.length == 1) {
debug("getAdvancedSuggestions: no terms yet, suggesting contexts");
return convert(getAllContexts());
} else if (isBooleanTerm(assistant.getPreviousToken())) {
debug("getAdvancedSuggestions: previous term was boolean, suggesting contexts");
return convert(getAllContexts());
} else {
debug("getAdvancedSuggestions: previous term was not boolean, suggesting boolean");
return convert(booleanOperators);
}
*/
} else if (isBooleanTerm(parsed.context)) {
LOG.debug("getAdvancedSuggestions: beforeCaret is whole boolean operator");
return convert(getAllContexts()); // TODO: should we tell user to type a space first?
} else {
// check if this context is complete or not
if (completor.getSimpleContexts().contains(parsed.context)) {
LOG.debug("getAdvancedSuggestions: search term is simple context, wants operator");
List<String> contextComparisonOperators = getComparisonOperatorsForContext(parsed.context,
completor);
return convert(pad(parsed.context, contextComparisonOperators, ""), parsed, parsed.context);
}
if (completor.getParameterizedContexts().contains(parsed.context)) {
LOG.debug("getAdvancedSuggestions: search term is parameterized context, wants open bracket");
return convert(Arrays.asList(parsed.context + "["), parsed, parsed.context);
}
LOG.debug("getAdvancedSuggestions: search term wants context completion");
List<String> startsWithContexts = new ArrayList<String>();
String lowerCaseParsedContext = parsed.context.toLowerCase(); // contexts should already be lower-cased
for (String context : completor.getSimpleContexts()) {
if (context.indexOf(lowerCaseParsedContext) != -1) {
startsWithContexts.add(context);
}
}
for (String context : completor.getParameterizedContexts()) {
if (context.indexOf(lowerCaseParsedContext) != -1) {
startsWithContexts.add(context + "[");
}
}
return convert(startsWithContexts, parsed, parsed.context);
}
case PARAM:
LOG.debug("getAdvancedSuggestions: param state");
return convert(pad(parsed.context + "[", completor.getParameters(parsed.context, parsed.param), "]"),
parsed, parsed.param);
case OPERATOR:
LOG.debug("getAdvancedSuggestions: operator state");
if (allComparisonOperators.contains(parsed.operator)) {
LOG.debug("search term is complete operator, suggesting values instead");
List<String> valueSuggestions = padWithQuotes(beforeCaret,
completor.getValues(parsed.context, parsed.param, ""));
if (completor.getSimpleContexts().contains(parsed.context)) {
LOG.debug("getAdvancedSuggestions: suggesting value completions for a simple context");
return convert(pad(parsed.context + parsed.operator, valueSuggestions, ""));
} else {
LOG.debug("getAdvancedSuggestions: suggesting value completions for a parameterized context");
return convert(pad(parsed.context + "[" + parsed.param + "]" + parsed.operator, valueSuggestions,
""));
}
}
List<String> operatorSuggestions = new ArrayList<String>();
List<String> contextComparisonOperators = getComparisonOperatorsForContext(parsed.context, completor);
for (String op : contextComparisonOperators) {
if (op.startsWith(parsed.operator)) {
operatorSuggestions.add(op);
}
}
LOG.debug("getAdvancedSuggestions: providing suggestions for comparison operators");
if (completor.getSimpleContexts().contains(parsed.context)) {
return convert(pad(parsed.context, operatorSuggestions, ""));
} else {
return convert(pad(parsed.context + "[" + parsed.param + "]", operatorSuggestions, ""));
}
case VALUE:
LOG.debug("getAdvancedSuggestions: value state");
List<String> valueSuggestions = padWithQuotes(beforeCaret,
completor.getValues(parsed.context, parsed.param, parsed.value));
if (completor.getSimpleContexts().contains(parsed.context)) {
LOG.debug("getAdvancedSuggestions: suggesting value completions for a simple context");
return convert(pad(parsed.context + parsed.operator, valueSuggestions, ""), parsed, parsed.value);
} else {
LOG.debug("getAdvancedSuggestions: suggesting value completions for a parameterized context");
return convert(pad(parsed.context + "[" + parsed.param + "]" + parsed.operator, valueSuggestions, ""),