Skip to content

Commit 3b2e4bf

Browse files
committed
[GR-36417] Improve graalpython REPL completion
PullRequest: graalpython/2109
2 parents 923e2d9 + 409a19f commit 3b2e4bf

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
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
@@ -111,7 +111,7 @@ public int read() throws IOException {
111111
}
112112

113113
public int getTerminalWidth() {
114-
// deafult value
114+
// default value
115115
return 80;
116116
}
117117

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

Lines changed: 21 additions & 4 deletions
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

@@ -103,18 +104,34 @@ public void setupReader(BooleanSupplier shouldRecord,
103104
builder.completer(new Completer() {
104105
@Override
105106
public void complete(LineReader r, ParsedLine pl, List<Candidate> candidates) {
106-
String line = pl.line();
107-
if (line != null) {
108-
List<String> l = completer.apply(line);
107+
String word = pl.word();
108+
if (word != null) {
109+
List<String> l = completer.apply(word);
109110
for (String value : l) {
110111
candidates.add(new Candidate(value, value, null, null, null, null, false));
111112
}
112113
}
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
122+
// implemented by `rlcompleter.py` adds a trailing whitespace to keywords,
123+
// e.g. 'raise '. The default DefaultParser implementation always escaped this
124+
// whitespace leading to wrong completions like 'raise\ '.
125+
if (pos == buffer.length() - 1) {
126+
return false;
127+
}
128+
return Character.isWhitespace(buffer.charAt(pos));
129+
}
130+
});
131+
116132
reader = builder.build();
117133
reader.option(LineReader.Option.DISABLE_EVENT_EXPANSION, true);
134+
reader.option(LineReader.Option.INSERT_TAB, true);
118135
reader.setVariable(LineReader.COMMENT_BEGIN, "#");
119136

120137
// numpad bindings

0 commit comments

Comments
 (0)