use of org.apache.karaf.shell.console.Completer in project karaf by apache.
the class ArgumentCompleter method getDefaultCompleter.
private Completer getDefaultCompleter(Field field) {
Completer completer = null;
Class<?> type = field.getType();
if (type.isAssignableFrom(File.class)) {
completer = new FileCompleter(null);
} else if (type.isAssignableFrom(Boolean.class) || type.isAssignableFrom(boolean.class)) {
completer = new StringsCompleter(new String[] { "false", "true" }, false);
} else if (type.isAssignableFrom(Enum.class)) {
Set<String> values = new HashSet<>();
for (Object o : EnumSet.allOf((Class<Enum>) type)) {
values.add(o.toString());
}
completer = new StringsCompleter(values, false);
} else {
// TODO any other completers we can add?
}
return completer;
}
use of org.apache.karaf.shell.console.Completer in project karaf by apache.
the class CommandsCompleter method checkData.
@SuppressWarnings({ "unchecked", "deprecation" })
protected Map<String, Completer>[] checkData() {
// Copy the set to avoid concurrent modification exceptions
// TODO: fix that in gogo instead
Set<String> names;
boolean update;
synchronized (this) {
names = new HashSet<>((Set<String>) session.get(COMMANDS));
update = !names.equals(commands);
}
if (update) {
// get command aliases
Set<String> commands = new HashSet<>();
Map<String, Completer> global = new HashMap<>();
Map<String, Completer> local = new HashMap<>();
// add argument completers for each command
for (String command : names) {
String rawCommand = stripScope(command);
Function function = (Function) session.get(command);
function = unProxy(function);
if (function instanceof CommandWithAction) {
try {
global.put(command, new ArgumentCompleter(session, (CommandWithAction) function, command));
local.put(command, new ArgumentCompleter(session, (CommandWithAction) function, rawCommand));
} catch (Throwable t) {
LOGGER.debug("Unable to create completers for command '" + command + "'", t);
}
} else if (function instanceof org.apache.felix.gogo.commands.CommandWithAction) {
try {
global.put(command, new OldArgumentCompleter(session, (org.apache.felix.gogo.commands.CommandWithAction) function, command));
local.put(command, new OldArgumentCompleter(session, (org.apache.felix.gogo.commands.CommandWithAction) function, rawCommand));
} catch (Throwable t) {
LOGGER.debug("Unable to create completers for command '" + command + "'", t);
}
}
commands.add(command);
}
synchronized (this) {
this.commands.clear();
this.globalCompleters.clear();
this.localCompleters.clear();
this.commands.addAll(commands);
this.globalCompleters.putAll(global);
this.localCompleters.putAll(local);
}
}
synchronized (this) {
return new Map[] { new HashMap<>(this.globalCompleters), new HashMap<>(this.localCompleters) };
}
}
use of org.apache.karaf.shell.console.Completer in project karaf by apache.
the class AggregateCompleter method complete.
@SuppressWarnings({ "unchecked", "rawtypes" })
public int complete(final String buffer, final int cursor, final List candidates) {
// buffer could be null
assert candidates != null;
List<Completion> completions = new ArrayList<>(completers.size());
// Run each completer, saving its completion results
int max = -1;
for (Completer completer : completers) {
Completion completion = new Completion(candidates);
completion.complete(completer, buffer, cursor);
// Compute the max cursor position
max = Math.max(max, completion.cursor);
completions.add(completion);
}
// Append candiates from completions which have the same cursor position as max
for (Completion completion : completions) {
if (completion.cursor == max) {
// noinspection unchecked
candidates.addAll(completion.candidates);
}
}
return max;
}
Aggregations