use of org.apache.felix.gogo.jline.ParsedLineImpl in project karaf by apache.
the class KarafParser method doParse.
private ParsedLine doParse(CharSequence line, int cursor) throws SyntaxError {
org.apache.felix.gogo.runtime.Parser parser = new org.apache.felix.gogo.runtime.Parser(line);
Program program = parser.program();
List<Statement> statements = parser.statements();
// Find corresponding statement
Statement statement = null;
for (int i = statements.size() - 1; i >= 0; i--) {
Statement s = statements.get(i);
if (s.start() <= cursor) {
boolean isOk = true;
// check if there are only spaces after the previous statement
if (s.start() + s.length() < cursor) {
for (int j = s.start() + s.length(); isOk && j < cursor; j++) {
isOk = Character.isWhitespace(line.charAt(j));
}
}
statement = s;
break;
}
}
if (statement != null && statement.tokens() != null && !statement.tokens().isEmpty()) {
String cmdName = session.resolveCommand(statement.tokens().get(0).toString());
String[] parts = cmdName.split(":");
Command cmd = parts.length == 2 ? session.getRegistry().getCommand(parts[0], parts[1]) : null;
Parser cmdParser = cmd != null ? cmd.getParser() : null;
if (cmdParser != null) {
final CommandLine cmdLine = cmdParser.parse(session, statement.toString(), cursor - statement.start());
return new ParsedLine() {
@Override
public String word() {
return cmdLine.getCursorArgument();
}
@Override
public int wordCursor() {
return cmdLine.getArgumentPosition();
}
@Override
public int wordIndex() {
return cmdLine.getCursorArgumentIndex();
}
@Override
public List<String> words() {
return Arrays.asList(cmdLine.getArguments());
}
@Override
public String line() {
return cmdLine.getBuffer();
}
@Override
public int cursor() {
return cmdLine.getBufferPosition();
}
};
}
return new ParsedLineImpl(program, statement, cursor, statement.tokens());
} else {
// TODO:
return new ParsedLineImpl(program, program, cursor, Collections.singletonList(program));
}
}
Aggregations