Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline support #36

Closed
pabloa opened this issue Nov 7, 2016 · 7 comments
Closed

Multiline support #36

pabloa opened this issue Nov 7, 2016 · 7 comments
Milestone

Comments

@pabloa
Copy link

pabloa commented Nov 7, 2016

Hello,

I am using jline3 to write a sql client. Some users write SQL sentences using multiple lines. A SQL command could have multiple lines and it finishes with a ';' in the end.

Is it possible to tell jline3 to use ; as the end of the line in LineReaderImpl (or in the class performing that job)?

@gnodet
Copy link
Member

gnodet commented Nov 7, 2016

For which behavior ? You mean using ; as a <return> key ? Or to delimit commands for completion, parsing, syntax highlighting, etc... ?

@pabloa
Copy link
Author

pabloa commented Nov 7, 2016

Hello

What I need is:

  • User write a command, for example:
SELECT * <return>
FROM myTable <return>
WHERE myTable.id = 42; <return>
  • When the user hit ';' and '' jline3 should return all those lines as one String.
  • History functionality should work as usual but in this case it should show all the SELECT sentence.

I want to reproduce the same behavior than, for example, plsql or mysql clients.

@gnodet
Copy link
Member

gnodet commented Nov 7, 2016

You need to implement a custom Parser, see Parser.java.
For a simple implementation, throw an EOFError if the input does not end with ;.

@gnodet gnodet closed this as completed in d0e191f Nov 8, 2016
@gnodet gnodet added this to the 3.0.2 milestone Nov 17, 2016
@pabloa
Copy link
Author

pabloa commented Dec 16, 2016

Awesome! I just could do it with JLine 3.1.2.
Thank you

@j2kun
Copy link

j2kun commented Jun 29, 2020

@pabloa could you point me to your implementation? I'm curious to see how you did it.

@pabloa
Copy link
Author

pabloa commented Jun 29, 2020

@j2kun that happened in 2016 in a long time gone company. ;)

@j2kun
Copy link

j2kun commented Jul 1, 2020

For posterity let me include my version, happy to hear of any improvements you might see.

@gnodet any chance a version of this could be included in the library, to save future fools like me the effort? :)

import org.jline.reader.EOFError;
import org.jline.reader.ParsedLine;
import org.jline.reader.Parser;
import org.jline.reader.SyntaxError;
import org.jline.reader.impl.DefaultParser;

/** A jline extension for SQL-like inputs. Commands are terminated with a semicolon. */
public final class MultilineParser implements Parser {

  private static final Parser DEFAULT_PARSER = new DefaultParser();

  @Override
  public ParsedLine parse(String line, int cursor, Parser.ParseContext context) throws SyntaxError {
    if ((Parser.ParseContext.UNSPECIFIED.equals(context) || Parser.ParseContext.ACCEPT_LINE.equals(context)) 
        && !line.trim().endsWith(";")) {
      throw new EOFError(-1, cursor, "Missing semicolon (;)");
    }

    return DEFAULT_PARSER.parse(line, cursor, context);
  }

  public MultilineParser() {}
}

Then this parser is passed to the line reader as:

LineReaderBuilder.builder().terminal(terminal).parser(new MultilineParser()).build();

PakhomovAlexander added a commit to unisonteam/ignite-3 that referenced this issue Feb 6, 2024
* There is no support for multiline strings in interactive mode. It requires implementing our own parser for jline 3 [1] that can be done but not now.

[1] - jline/jline3#36

Signed-off-by: apakhomov <apkhmv@gmail.com>
PakhomovAlexander added a commit to apache/ignite-3 that referenced this issue Feb 6, 2024
* There is no support for multiline strings in interactive mode. It requires implementing our own parser for jline 3 [1] that can be done but not now.

[1] - jline/jline3#36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants