Skip to content

Commit 309cb85

Browse files
committed
Fix: REPL completion adds trailing backslash after keyword completion.
The REPL completer implemented by `rlcompleter.py` adds a trailing whitespace to keywords (except `finally` and `try`). This makes sense as nothing else can follow those keywords, however, the current GraalPython completer implementation via `JLineConsoleHandler` uses a DefaultParser for parsing completion candidates which escapes whitespace characters. Therefore a trailing whitespace got replaced by '\ ' leading to a wrong completion. As a fix we overwrite the `isDelimiterChar` method from the DefaultParser to never count the last character of a character sequence as a delimiter. This works because we otherwise only use this check to split a line into it's words and in this case the last character of the line also cannot be a delimiter.
1 parent 88384a4 commit 309cb85

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/JLineConsoleHandler.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -67,6 +67,7 @@
6767
import org.graalvm.shadowed.org.jline.reader.Macro;
6868
import org.graalvm.shadowed.org.jline.reader.ParsedLine;
6969
import org.graalvm.shadowed.org.jline.reader.UserInterruptException;
70+
import org.graalvm.shadowed.org.jline.reader.impl.DefaultParser;
7071
import org.graalvm.shadowed.org.jline.terminal.Terminal;
7172
import org.graalvm.shadowed.org.jline.terminal.TerminalBuilder;
7273

@@ -113,6 +114,20 @@ public void complete(LineReader r, ParsedLine pl, List<Candidate> candidates) {
113114
}
114115
});
115116
}
117+
118+
builder.parser(new DefaultParser() {
119+
@Override
120+
public boolean isDelimiterChar(CharSequence buffer, int pos) {
121+
// Never count a last character of a char sequence as delimiter. The REPL completer implemented
122+
// by `rlcompleter.py` adds a trailing whitespace to keywords, e.g. 'raise '. The default DefaultParser
123+
// implementation always escaped this whitespace leading to wrong completions like 'raise\ '.
124+
if (pos == buffer.length() - 1) {
125+
return false;
126+
}
127+
return Character.isWhitespace(buffer.charAt(pos));
128+
}
129+
});
130+
116131
reader = builder.build();
117132
reader.option(LineReader.Option.DISABLE_EVENT_EXPANSION, true);
118133
reader.option(LineReader.Option.INSERT_TAB, true);

0 commit comments

Comments
 (0)