public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) { String statement = line.line(); // remove ';' at the end if (statement.endsWith(";")) { statement = statement.substring(0, statement.length() - 1); } // handle SQL client specific commands final String statementNormalized = statement.toUpperCase().trim(); for (String commandHint : COMMAND_HINTS) { if (commandHint.startsWith(statementNormalized) && line.cursor() < commandHint.length()) { candidates.add(createCandidate(commandHint)); } } // fallback to Table API hinting try { executor.completeStatement(context, statement, line.cursor()) .forEach(hint -> candidates.add(createCandidate(hint))); } catch (SqlExecutionException e) { LOG.debug("Could not complete statement at " + line.cursor() + ":" + statement, e); } }
@Override public ParsedLine parse(final String line, final int cursor, final ParseContext context) { final ParsedLine parsed = delegate.parse(line, cursor, context); if (context != ParseContext.ACCEPT_LINE) { return parsed; } final String bare = CommentStripper.strip(parsed.line()); if (bare.isEmpty()) { return parsed; } if (cliCmdPredicate.test(bare)) { return parsed; } if (!bare.endsWith(TERMINATION_CHAR)) { throw new EOFError(-1, -1, "Missing termination char", "termination char"); } return parsed; } }
@Test public void shouldCallDelegateWithCorrectParams() { // Given: EasyMock.expect(parsedLine.line()).andReturn(TERMINATED_LINE).anyTimes(); EasyMock.expect(delegate.parse("some-string", 55, ParseContext.ACCEPT_LINE)) .andReturn(parsedLine); EasyMock.replay(delegate, parsedLine); // When: parser.parse("some-string", 55, ParseContext.ACCEPT_LINE); // Then: EasyMock.verify(delegate); }
private void givenDelegateWillReturn(final String line) { EasyMock.expect(parsedLine.line()).andReturn(line).anyTimes(); EasyMock.expect(delegate.parse(anyObject(), anyInt(), anyObject())).andReturn(parsedLine); EasyMock.replay(delegate, parsedLine); }
@Override public String getBuffer() { return line.line(); } }
@Override public String getBuffer() { return line.line(); } }
public String line() { return line.line(); } public int cursor() {
@Override public String rawText() { return parsedLine.line(); }
@Override protected LineReader buildReader(LineReaderBuilder builder) { return super.buildReader(builder .appName("Velocity") .completer((reader, parsedLine, list) -> { try { boolean isCommand = parsedLine.line().indexOf(' ') == -1; List<String> offers = this.server.getCommandManager() .offerSuggestions(this, parsedLine.line()); for (String offer : offers) { if (isCommand) { list.add(new Candidate(offer.substring(1))); } else { list.add(new Candidate(offer)); } } } catch (Exception e) { logger.error("An error occurred while trying to perform tab completion.", e); } }) ); }
@Override public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) { try { String buffer = line.line(); if (StringUtils.isEmpty(buffer) || buffer.indexOf(' ') == -1) { completeCommandName(buffer, candidates); } int separatorPos = buffer.indexOf(' '); String commandName = buffer.substring(0, separatorPos); if (LOG.isDebugEnabled()) { LOG.debug("Command name is [" + commandName + "]"); } String commandArguments = buffer.substring(separatorPos + 1); commandArguments.replaceFirst("^\\s*", ""); if (LOG.isDebugEnabled()) { LOG.debug("Analyzing commmand arguments [" + commandArguments + "]"); } Command cmd = commandCenter.commandFactory.createCommand(commandName); cmd.setSession(commandCenter.session); CliCompletor commandCompletor = new CliCompletor(cmd, commandCenter.argTokenizer); int position = line.cursor(); commandCompletor.complete(reader, new ArgumentCompleter.ArgumentLine(commandArguments, position - separatorPos), candidates); } catch (RuntimeException e) { if (LOG.isDebugEnabled()) { LOG.debug("Couldn't complete input", e); } } }
public String filter(String line, ParsedLine parsed) { int prev = 0; StringBuilder sb = new StringBuilder(); int cur = line.indexOf(parsed.line()); List<String> words = parsed.words(); int state = 0;
public String filter(String line, ParsedLine parsed) { int prev = 0; StringBuilder sb = new StringBuilder(); int cur = line.indexOf(parsed.line()); List<String> words = parsed.words(); int state = 0;
/** * Print confirmation message and get response * * @param message message to print * @return response read from terminal */ public String read(String message) { LineReader lr = SshShellCommandFactory.SSH_THREAD_CONTEXT.get().getLineReader(); if (message != null) { lr.getTerminal().writer().println(message); } lr.readLine(); if (lr.getTerminal() instanceof AbstractPosixTerminal) { lr.getTerminal().writer().println(); } return lr.getParsedLine().line(); }
public void complete(LineReader reader, ParsedLine line, List<Candidate> suggestions) { String command = line.line(); ArgumentsInspector inspector = new ArgumentsInspector(context); final AtomicBoolean terminated = new AtomicBoolean(true);
private CharSequence readCommand(AtomicBoolean reading) throws UserInterruptException { CharSequence command = null; reading.set(true); try { reader.readLine(getPrompt(), getRPrompt(), maskingCallback, null); ParsedLine pl = reader.getParsedLine(); if (pl instanceof ParsedLineImpl) { command = ((ParsedLineImpl) pl).program(); } else if (pl != null) { command = pl.line(); } else { command = reader.getBuffer().toString(); } } catch (EndOfFileException e) { command = null; } catch (UserInterruptException e) { command = ""; // Do nothing } catch (Throwable t) { ShellUtil.logException(this, t); } finally { reading.set(false); } return command; }
private CharSequence readCommand(AtomicBoolean reading) throws UserInterruptException { CharSequence command = null; reading.set(true); try { reader.readLine(getPrompt(), getRPrompt(), maskingCallback, null); ParsedLine pl = reader.getParsedLine(); if (pl instanceof ParsedLineImpl) { command = ((ParsedLineImpl) pl).program(); } else if (pl != null) { command = pl.line(); } else { command = reader.getBuffer().toString(); } } catch (EndOfFileException e) { command = null; } catch (UserInterruptException e) { command = ""; // Do nothing } catch (Throwable t) { ShellUtil.logException(this, t); } finally { reading.set(false); } return command; }
.terminal(terminal) .completer((lineReader, parsedLine, list) -> { List<String> suggestions = pluginManager.getCommandManager().completeSystem(parsedLine.line()); for (String suggestion : suggestions) { LOGGER.info(suggestion);