if (ignoreCase) {
itemPrefix = itemPrefix.toLowerCase();
}
// A set used to avoid duplicates.
JsoStringSet uniqueNames = JsoStringSet.create();
// This array will be filled with proposals form different sources:
// templates; visible names found by parser; matches from code graph.
JsonArray<AutocompleteProposal> result = JsonCollections.createArray();
// This also means previousContext == ""
if (CompletionType.GLOBAL == context.getCompletionType()) {
// Add templates.
JsonArray<? extends TemplateProposal> templates = getTemplatesIndex().search(itemPrefix);
result.addAll(templates);
for (TemplateProposal template : templates.asIterable()) {
uniqueNames.add(template.getName());
}
// Add visible names found by parser.
JsonArray<String> localVariables = getLocalVariables(context.getParseResult());
for (String localVariable : localVariables.asIterable()) {
if (StringUtils.startsWith(itemPrefix, localVariable, ignoreCase)) {
if (!uniqueNames.contains(localVariable)) {
uniqueNames.add(localVariable);
result.add(new CodeGraphProposal(localVariable, PathUtil.EMPTY_PATH, false));
}
}
}
}
// Now use the knowledge about current scope and calculate possible
// shortcuts in code graph.
JsonStringSet prefixes = scopeTrieBuilder.calculateScopePrefixes(context, cursorPosition);
// Let language-specific modifications.
addShortcutsTo(context, prefixes);
PrefixIndex<CodeGraphProposal> codeGraphTrie = scopeTrieBuilder.getCodeGraphTrie();
JsonArray<AutocompleteProposal> codeProposals = JsonCollections.createArray();
// We're iterate found shortcuts...
for (String prefix : prefixes.getKeys().asIterable()) {
JsonArray<? extends CodeGraphProposal> proposals = codeGraphTrie.search(prefix + itemPrefix);
// Distill raw proposals.
int prefixLength = prefix.length();
for (CodeGraphProposal proposal : proposals.asIterable()) {
// Take part of string between prefix and period.
String proposalName = proposal.getName();
int nameEndIndex = proposalName.length();
int periodIndex = proposalName.indexOf('.', prefixLength);
if (periodIndex != -1) {
// TODO: Do we need this?
nameEndIndex = periodIndex;
}
proposalName = proposalName.substring(prefixLength, nameEndIndex);
if (!uniqueNames.contains(proposalName)) {
uniqueNames.add(proposalName);
codeProposals.add(
new CodeGraphProposal(proposalName, proposal.getPath(), proposal.isFunction()));
}
}
}