org.jline.reader.impl.DefaultParser Java Examples

The following examples show how to use org.jline.reader.impl.DefaultParser. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ShellUtils.java    From rug-cli with GNU General Public License v3.0 7 votes vote down vote up
public static LineReader lineReader(File historyPath, Optional<SignalHandler> handler,
        Completer... completers) {
    // Protect the history file as may contain sensitive information
    FileUtils.setPermissionsToOwnerOnly(historyPath);

    // Create JLine LineReader
    History history = new DefaultHistory();
    LineReader reader = LineReaderBuilder.builder().terminal(terminal(handler)).history(history)
            .parser(new DefaultParser()).variable(LineReader.HISTORY_FILE, historyPath)
            .completer(new AggregateCompleter(completers)).highlighter(new DefaultHighlighter())
            .build();
    history.attach(reader);

    setOptions(reader);

    return reader;
}
 
Example #2
Source File: SqlLineParserTest.java    From sqlline with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * In case of exception while {@link sqlline.SqlLineParser#parse}
 * line continuation will be switched off for particular line.
 */
@Test
public void testSqlLineParserWithException() {
  new MockUp<SqlLineHighlighter>() {
    @Mock
    private boolean isLineFinishedWithSemicolon(
        final int lastNonQuoteCommentIndex, final CharSequence buffer) {
      throw new RuntimeException("Line continuation exception");
    }
  };

  final SqlLine sqlLine = new SqlLine();
  sqlLine.getOpts().set(BuiltInProperty.USE_LINE_CONTINUATION, false);
  final DefaultParser parser = new SqlLineParser(sqlLine);
  final Parser.ParseContext acceptLine = Parser.ParseContext.ACCEPT_LINE;
  for (String line : WRONG_LINES) {
    try {
      parser.parse(line, line.length(), acceptLine);
    } catch (Throwable t) {
      System.err.println("Problem line: [" + line + "]");
      throw t;
    }
  }
}
 
Example #3
Source File: TestCLICompleter.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompletionWithFileArguments() {
    final String topCommand = NiFiRegistryCommandGroup.REGISTRY_COMMAND_GROUP;
    final String subCommand = "list-buckets";

    final DefaultParser.ArgumentList parsedLine = new DefaultParser.ArgumentList(
            "", Arrays.asList(topCommand, subCommand, "-p", "src/test/resources/"), 3, -1, -1);

    final List<Candidate> candidates = new ArrayList<>();
    completer.complete(lineReader, parsedLine, candidates);
    assertTrue(candidates.size() > 0);

    boolean found = false;
    for (Candidate candidate : candidates) {
        if (candidate.value().equals("src/test/resources/test.properties")) {
            found = true;
            break;
        }
    }

    assertTrue(found);
}
 
Example #4
Source File: InputParser.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public ParsedLine parse(String line, int cursor, ParseContext context)
        throws SyntaxError
{
    String command = whitespace().trimFrom(line);
    if (command.isEmpty() || SPECIAL.contains(command.toLowerCase(ENGLISH))) {
        return new DefaultParser().parse(line, cursor, context);
    }

    StatementSplitter splitter = new StatementSplitter(line, STATEMENT_DELIMITERS);
    if (splitter.getCompleteStatements().isEmpty()) {
        throw new EOFError(-1, -1, null);
    }

    return new DefaultParser().parse(line, cursor, context);
}
 
Example #5
Source File: TestCLICompleter.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompletionWithWordIndexTwoAndMatching() {
    final String topCommand = NiFiRegistryCommandGroup.REGISTRY_COMMAND_GROUP;
    final String subCommand = "list-buckets";

    final DefaultParser.ArgumentList parsedLine = new DefaultParser.ArgumentList(
            "", Arrays.asList(topCommand, subCommand), 2, -1, -1);

    final List<Candidate> candidates = new ArrayList<>();
    completer.complete(lineReader, parsedLine, candidates);
    assertTrue(candidates.size() > 0);
    assertEquals(completer.getOptions(subCommand).size(), candidates.size());
}
 
Example #6
Source File: SqlLineParserTest.java    From sqlline with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSqlLineParserOfWrongLinesForSwitchedOfflineContinuation() {
  final SqlLine sqlLine = new SqlLine();
  sqlLine.getOpts().set(BuiltInProperty.USE_LINE_CONTINUATION, false);
  final DefaultParser parser = new SqlLineParser(sqlLine);
  final Parser.ParseContext acceptLine = Parser.ParseContext.ACCEPT_LINE;
  for (String line : WRONG_LINES) {
    try {
      parser.parse(line, line.length(), acceptLine);
    } catch (Throwable t) {
      System.err.println("Problem line: [" + line + "]");
      throw t;
    }
  }
}
 
Example #7
Source File: SqlLineParserTest.java    From sqlline with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSqlLineParserForWrongLinesWithEmptyPrompt() {
  SqlLine sqlLine = new SqlLine();
  sqlLine.getOpts().set(BuiltInProperty.PROMPT, "");
  final DefaultParser parser = new SqlLineParser(sqlLine);
  final Parser.ParseContext acceptLine = Parser.ParseContext.ACCEPT_LINE;
  for (String line : WRONG_LINES) {
    try {
      parser.parse(line, line.length(), acceptLine);
      fail("Missing closing comment, quote or semicolon for line " + line);
    } catch (EOFError eofError) {
      //ok
    }
  }
}
 
Example #8
Source File: SqlLineParserTest.java    From sqlline with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSqlLineParserForWrongLines() {
  final DefaultParser parser = new SqlLineParser(new SqlLine());
  final Parser.ParseContext acceptLine = Parser.ParseContext.ACCEPT_LINE;
  for (String line : WRONG_LINES) {
    try {
      parser.parse(line, line.length(), acceptLine);
      fail("Missing closing quote or semicolon for line " + line);
    } catch (EOFError eofError) {
      //ok
    }
  }
}
 
Example #9
Source File: TestCLICompleter.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompletionForSessionVariableWithFiles() {
    final String topCommand = "session";
    final String subCommand = "set";

    final DefaultParser.ArgumentList parsedLine = new DefaultParser.ArgumentList("",
            Arrays.asList(
                    topCommand,
                    subCommand,
                    SessionVariable.NIFI_CLIENT_PROPS.getVariableName(),
                    "src/test/resources/"),
            3, -1, -1);

    final List<Candidate> candidates = new ArrayList<>();
    completer.complete(lineReader, parsedLine, candidates);
    assertTrue(candidates.size() > 0);

    boolean found = false;
    for (Candidate candidate : candidates) {
        if (candidate.value().equals("src/test/resources/test.properties")) {
            found = true;
            break;
        }
    }

    assertTrue(found);
}
 
Example #10
Source File: TestCLICompleter.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompletionForSessionVariableNames() {
    final String topCommand = "session";
    final String subCommand = "set";

    final DefaultParser.ArgumentList parsedLine = new DefaultParser.ArgumentList(
            "", Arrays.asList(topCommand, subCommand), 2, -1, -1);

    final List<Candidate> candidates = new ArrayList<>();
    completer.complete(lineReader, parsedLine, candidates);
    assertTrue(candidates.size() > 0);
    assertEquals(SessionVariable.values().length, candidates.size());
}
 
Example #11
Source File: TestCLICompleter.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompletionWithMultipleArguments() {
    final String topCommand = NiFiRegistryCommandGroup.REGISTRY_COMMAND_GROUP;
    final String subCommand = "list-buckets";

    final DefaultParser.ArgumentList parsedLine = new DefaultParser.ArgumentList(
            "", Arrays.asList(topCommand, subCommand, "-ks", "foo", "-kst", "JKS"), 6, -1, -1);

    final List<Candidate> candidates = new ArrayList<>();
    completer.complete(lineReader, parsedLine, candidates);
    assertTrue(candidates.size() > 0);
    assertEquals(completer.getOptions(subCommand).size(), candidates.size());
}
 
Example #12
Source File: TestCLICompleter.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompletionWithWordIndexTwoAndNotMatching() {
    final String topCommand = NiFiRegistryCommandGroup.REGISTRY_COMMAND_GROUP;
    final String subCommand = "NOT-A-TOP-LEVEL-COMMAND";

    final DefaultParser.ArgumentList parsedLine = new DefaultParser.ArgumentList(
            "", Arrays.asList(topCommand, subCommand), 2, -1, -1);

    final List<Candidate> candidates = new ArrayList<>();
    completer.complete(lineReader, parsedLine, candidates);
    assertEquals(0, candidates.size());
}
 
Example #13
Source File: TestCLICompleter.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompletionWithWordIndexOneAndNotMatching() {
    final String topCommand = "NOT-A-TOP-LEVEL-COMMAND";

    final DefaultParser.ArgumentList parsedLine = new DefaultParser.ArgumentList(
            "", Collections.singletonList(topCommand), 1, -1, -1);

    final List<Candidate> candidates = new ArrayList<>();
    completer.complete(lineReader, parsedLine, candidates);
    assertEquals(0, candidates.size());
}
 
Example #14
Source File: TestCLICompleter.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompletionWithWordIndexOneAndMatching() {
    final String topCommand = NiFiRegistryCommandGroup.REGISTRY_COMMAND_GROUP;

    final DefaultParser.ArgumentList parsedLine = new DefaultParser.ArgumentList(
            "", Collections.singletonList(topCommand), 1, -1, -1);

    final List<Candidate> candidates = new ArrayList<>();
    completer.complete(lineReader, parsedLine, candidates);
    assertEquals(completer.getSubCommands(topCommand).size(), candidates.size());
}
 
Example #15
Source File: TestCLICompleter.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompletionWithWordIndexZero() {
    final DefaultParser.ArgumentList parsedLine = new DefaultParser.ArgumentList(
            "", Collections.emptyList(), 0, -1, -1);

    final List<Candidate> candidates = new ArrayList<>();
    completer.complete(lineReader, parsedLine, candidates);
    assertEquals(completer.getTopLevelCommands().size(), candidates.size());
}
 
Example #16
Source File: TestCLICompleter.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompletionWithWordIndexNegative() {
    final DefaultParser.ArgumentList parsedLine = new DefaultParser.ArgumentList(
            "", Collections.emptyList(), -1, -1, -1);

    final List<Candidate> candidates = new ArrayList<>();
    completer.complete(lineReader, parsedLine, candidates);
    assertEquals(0, candidates.size());
}
 
Example #17
Source File: SqshConsole.java    From jsqsh with Apache License 2.0 5 votes vote down vote up
public SqshConsole(SqshContext context) {

        this.context = context;
        File readlineHistory = new File(context.getConfigDirectory(), "jline_history");
        reader = LineReaderBuilder.builder()
                .appName("jsqsh")
                .terminal(newTerminal())
                .completer(new JLineCompleter(context))
                .parser(new DefaultParser())
                .variable(LineReader.HISTORY_FILE, readlineHistory.toString())
                .build();

        /*
         * This installs a widget that intercepts \n and attempts to determine if the
         * input is "finished" and should be run or if the user should keep typing
         * the current statement. I hate that I have to do this for all of the keymaps
         * that JLine3 has...
         */
        for (String keyMap : reader.getKeyMaps().keySet()) {

            // Bind to CTRL-M (enter)
            reader.getKeyMaps().get(keyMap).bind(new Reference("jsqsh-accept"), ctrl('M'));
            // Bind CTRL-G to automatically do "go"
            reader.getKeyMaps().get(keyMap).bind(new Reference("jsqsh-go"), ctrl('G'));
        }
        reader.getWidgets().put("jsqsh-accept", new JLineAcceptBufferWidget());
        reader.getWidgets().put("jsqsh-go", new JLineExecuteBufferWidget());

        reader.setOpt(LineReader.Option.DISABLE_EVENT_EXPANSION);
    }
 
Example #18
Source File: JLineAdapter.java    From Recaf with MIT License 5 votes vote down vote up
/**
 * @param terminal
 * 		Terminal to add tab-completion to.
 * @param lookup
 * 		Map containing commands.
 *
 * @return Reader with tab-completion.
 */
private static LineReader setupCompletionReader(Terminal terminal, Map<String, Class<?>> lookup) {
	// Filter root level commands
	Collection<Class<?>> commands =	lookup.entrySet().stream()
			.filter(e -> !e.getKey().contains(" "))
			.map(Map.Entry::getValue)
			.collect(Collectors.toList());
	// Pass dummy to pico for tab-completion
	CommandLine cmd = new CommandLine(SubContainerGenerator.generate(commands));
	return LineReaderBuilder.builder()
			.terminal(terminal)
			.completer(new PicocliJLineCompleter(cmd.getCommandSpec()))
			.parser(new DefaultParser())
			.build();
}
 
Example #19
Source File: JLineReader.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
public JLineReader(Terminal terminal) {
  // The combination of parser/expander here allow for multiple-line commands connected by '\\'
  DefaultParser parser = new DefaultParser();
  parser.setEofOnEscapedNewLine(true);
  parser.setQuoteChars(new char[0]);
  parser.setEscapeChars(new char[]{'\\'});

  final Expander expander = new NoOpExpander();
  // TODO: specify a completer to use here via a call to LineReaderBuilder.completer()
  this.lineReader = LineReaderBuilder.builder()
      .appName("KSQL")
      .expander(expander)
      .parser(parser)
      .terminal(terminal)
      .build();

  this.lineReader.setOpt(LineReader.Option.HISTORY_IGNORE_DUPS);
  this.lineReader.setOpt(LineReader.Option.HISTORY_IGNORE_SPACE);

  Path historyFilePath = Paths.get(System.getProperty(
      "history-file",
      System.getProperty("user.home")
      + "/.ksql-history"
  )).toAbsolutePath();
  if (CliUtils.createFile(historyFilePath)) {
    this.lineReader.setVariable(LineReader.HISTORY_FILE, historyFilePath);
    LOGGER.info("Command history saved at: " + historyFilePath);
  } else {
    terminal.writer().println(String.format(
        "WARNING: Unable to create command history file '%s', command history will not be saved.",
        historyFilePath
    ));
  }

  this.lineReader.unsetOpt(LineReader.Option.HISTORY_INCREMENTAL);
  this.history = new DefaultHistory(this.lineReader);

  this.prompt = DEFAULT_PROMPT;
}
 
Example #20
Source File: TerminalUi.java    From milkman with MIT License 5 votes vote down vote up
@PostConstruct
@SneakyThrows
public void setup() {
	cmd = new CommandLine(CommandSpec.create()
			.name("")
			.addSubcommand(null, commandSpecFactory.getSpecFor(wsCmd))
			.addSubcommand(null, commandSpecFactory.getSpecFor(execCmd))
			.addSubcommand(null, commandSpecFactory.getSpecFor(editCmd))
			.addSubcommand(null, commandSpecFactory.getSpecFor(quitCmd))
			.addSubcommand(null, commandSpecFactory.getSpecFor(colCmd)));
       
	cmd.setExecutionExceptionHandler(new IExecutionExceptionHandler() {
		
		@Override
		public int handleExecutionException(Exception ex, CommandLine commandLine, ParseResult parseResult)
				throws Exception {
			System.out.println(ex.getMessage());
			log.error("Command execution failed", ex);
			return 0;
		}
	});
       
       terminal = TerminalBuilder.builder()
       		.build();
       reader = LineReaderBuilder.builder()
               .terminal(terminal)
               .completer(new PicocliJLineCompleter(cmd.getCommandSpec()))
               .parser(new DefaultParser())
               .build();
       rightPrompt = null;
}
 
Example #21
Source File: SqlLineParserTest.java    From sqlline with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testSqlLineParserForOkLines() {
  final DefaultParser parser = new SqlLineParser(new SqlLine());
  final Parser.ParseContext acceptLine = Parser.ParseContext.ACCEPT_LINE;
  final String[] lines = {
      // commands
      "!set",
      " !history",
      "   !scan",
      " \n !set",
      " \n test;",
      " \n test';\n;\n';",
      "select \n 1\n, '\na\n ';",
      // sql
      "select 1;",
      "select '1';",
      // sqlline command comment with odd number of quotes
      "  #select '1",
      "--select '1`",
      " -- select '\"",
      // one line comment right after semicolon
      "select '1';--comment",
      "select '1';-----comment",
      "select '1';--comment\n",
      "select '1';--comment\n\n",
      "select '1'; --comment",
      "select '1';\n--comment",
      "select '1';\n\n--comment",
      "select '1';\n \n--comment",
      "select '1'\n;\n--comment",
      "select '1'\n\n;--comment",
      "select '1'\n\n;---comment",
      "select '1'\n\n;-- --comment",
      "select '1'\n\n;\n--comment",
      "select '1';/*comment*/",
      "select '1';/*---comment */",
      "select '1';/*comment\n*/\n",
      "select '1';/*comment*/\n\n",
      "select '1'; /*--comment*/",
      // /* inside a quoted line
      "select '1/*' as \"asd\";",
      "select '/*' as \"asd*/\";",
      // quoted line
      "select '1' as `asd`;",
      "select '1' as `\\`asd\\``;",
      "select '1' as \"asd\";",
      "select '1' as \"a's'd\";",
      "select '1' as \"'a's'd\n\" from t;",
      "select '1' as \"'a'\\\ns'd\\\n\n\" from t;",
      "select ' ''1'', ''2''' as \"'a'\\\ns'd\\\n\n\" from t;",
      "select ' ''1'', ''2''' as \"'a'\\\"\n s'd \\\" \n \\\"\n\" from t;",
      // not a valid sql, but from sqlline parser's point of view it is ok
      // as there are no non-closed brackets, quotes, comments
      // and it ends with a semicolon
      " \n test;",
      " \n test';\n;\n';",

      "select sum(my_function(x.[qwe], x.qwe)) as \"asd\" from t;",
      "select \n 1\n, '\na\n ';",
      "select /*\njust a comment\n*/\n'1';",
      "--comment \n values (';\n' /* comment */, '\"'"
          + "/*multiline;\n ;\n comment*/)\n -- ; \n;",

      // non-closed or extra brackets but commented or quoted
      "select '1(' from dual;",
      "select ')1' from dual;",
      "select 1/*count(123 */ from dual;",
      "select 2/* [qwe */ from dual;",
      "select 2 \" [qwe \" from dual;",
      "select 2 \" ]]][[[ \" from dual;",
      "select 2 \" ]]]\n[[[ \" from dual;",
      "select 2 \" \n]]]\n[[[ \n\" from dual;",
      "select 2 \n --]]]\n --[[[ \n from dual;",
  };
  for (String line : lines) {
    try {
      parser.parse(line, line.length(), acceptLine);
    } catch (Throwable t) {
      System.err.println("Problem line: [" + line + "]");
      throw t;
    }
  }
}
 
Example #22
Source File: CliShell.java    From samza with Apache License 2.0 4 votes vote down vote up
CliShell(CliEnvironment environment) throws ExecutorException {
  if (environment == null) {
    throw new IllegalArgumentException();
  }

  // Terminal
  try {
    terminal = TerminalBuilder.builder().name(CliConstants.WINDOW_TITLE).build();
  } catch (IOException e) {
    throw new CliException("Error when creating terminal", e);
  }

  // Terminal writer
  writer = terminal.writer();

  // LineReader
  final DefaultParser parser = new DefaultParser().eofOnEscapedNewLine(true).eofOnUnclosedQuote(true);
  lineReader = LineReaderBuilder.builder()
      .appName(CliConstants.APP_NAME)
      .terminal(terminal)
      .parser(parser)
      .highlighter(new CliHighlighter())
      .completer(new StringsCompleter(CliCommandType.getAllCommands()))
      .build();

  // Command Prompt
  firstPrompt = new AttributedStringBuilder().style(AttributedStyle.DEFAULT.foreground(AttributedStyle.YELLOW))
      .append(CliConstants.PROMPT_1ST + CliConstants.PROMPT_1ST_END)
      .toAnsi();

  // Execution context and executor
  executor = environment.getExecutor();
  exeContext = new ExecutionContext();
  executor.start(exeContext);

  // Command handlers
  if (commandHandlers == null) {
    commandHandlers = new ArrayList<>();
  }
  commandHandlers.add(new CliCommandHandler());
  commandHandlers.addAll(environment.getCommandHandlers());
  for (CommandHandler commandHandler : commandHandlers) {
    LOG.info("init commandHandler {}", commandHandler.getClass().getName());
    commandHandler.init(this, environment, terminal, exeContext);
  }
}