-
Notifications
You must be signed in to change notification settings - Fork 220
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
Erroneous completions when the cursor is not at the end of the line #274
Comments
Could you explain how to do it with the jline demo ? I've tried a bit with no success. |
I can reproduce with something like: class JLineTerminal {
String readLine() {
Terminal terminal = TerminalBuilder.terminal();
LineReader lineReader = LineReaderBuilder.builder()
.terminal(terminal)
.completer(new Completer())
.parser(new Parser())
.build();
return lineReader.readLine(">");
}
static class Parser extends reader.Parser {
static class DummyParsedLine extends ParsedLine {
int cursor;
String line;
DummyParsedLine(int cursor, String line) {
this.cursor = cursor;
this.line = line;
}
@Override int cursor() { return cursor; }
@Override String line() { return line; }
// using dummy values, not sure what they are used for
@Override String word() { return ""; }
@Override int wordCursor() { return -1; }
@Override int wordIndex() { return -1; }
@Override List<String> words() { return Collections.emptyList(); }
}
@Override
ParsedLine parse(String line, int cursor, ParseContext context) {
return new DummyParsedLine(cursor, line);
}
}
static class Completer extends reader.Completer {
@Override
void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
candidates.add(new Candidate("range"));
}
}
public static void main(String[] args) {
new JLineTerminal().readLine();
}
}
But I realise it might be because I did not correctly implement the |
Yes, that's certainly related to your |
Let's say I want to complete the following buffer:
What should be the values in |
It can be whatever you want, but it needs to be coherent with the |
Thanks for the quick reply.
I am using JLine to implement a REPL for the Scala programming language. So |
Then the first thing to implement is correct Once you have a I've never implemented or used (programmatically) a full language auto-completion system, so I can't help much on that side, but I'm quite sure there are already parsers and completion systems in open source editors, so may be able to rely on those libraries. |
Fortunately for me the REPL will be part of the Scala compiler and I can reuse the existing parser and completion API. I just need to write the glue code that connects the compiler with the JLine API. Here is my current prototype (~150 LOC). Multi-line editing, syntax highlighting, and basic auto-completion already work well. Just need to figure out how to make completion work when the cursor is not at the end of the buffer. I tried having a word for each token as you suggested. So given the example above, at the time I request a completion, the returned cursor = 6
line = "{List.}"
word = "}"
wordCursor = 0
wordIndex = 3
words = List("{", "List", ".", "}") Completing now eats the closing brace
My only candidate is: Candidate(
/* value = */ "range",
/* displ = */ "range",
/* group = */ null,
/* descr = */ null,
/* suffix = */ null,
/* key = */ null,
/* complete = */ false
) |
Sorry, I missed you reply. So the problem is that your parser returns The below test works well: @Test
public void testComplete() throws IOException {
reader.setCompleter((reader, line, candidates) -> candidates.add(new Candidate(
/* value = */ "range",
/* displ = */ "range",
/* group = */ null,
/* descr = */ null,
/* suffix = */ null,
/* key = */ null,
/* complete = */ false)));
reader.setParser((line, cursor, context) -> new ParsedLine() {
@Override
public String word() {
return "";
}
@Override
public int wordCursor() {
return 0;
}
@Override
public int wordIndex() {
return 3;
}
@Override
public List<String> words() {
return Arrays.asList("{", "List", ".", "", "}");
}
@Override
public String line() {
return "{List.}";
}
@Override
public int cursor() {
return 6;
}
});
assertBuffer("{List.range}", new TestBuffer("{List.}").left().tab());
} |
Let's consider the following example:
Let's say I have a single candidate
Candidate(value="ge", displ="range", displ=false)
, when I tab-complete, the candidate is appended after the closing brace:Maybe related to #251, although this can be replicated with any character, not only space and braces.
Using JLine 3.7.0
The text was updated successfully, but these errors were encountered: