Skip to content

Commit d59c2e5

Browse files
committed
py/repl: If there're no better alternatives, try to complete "import".
Also do that only for the first word in a line. The idea is that when you start up interpreter, high chance that you want to do an import. With this patch, this can be achieved with "i<tab>".
1 parent 13a1acc commit d59c2e5

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

py/repl.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ bool mp_repl_continue_with_input(const char *input) {
126126

127127
mp_uint_t mp_repl_autocomplete(const char *str, mp_uint_t len, const mp_print_t *print, const char **compl_str) {
128128
// scan backwards to find start of "a.b.c" chain
129+
const char *org_str = str;
129130
const char *top = str + len;
130131
for (const char *s = top; --s >= str;) {
131132
if (!(unichar_isalpha(*s) || unichar_isdigit(*s) || *s == '_' || *s == '.')) {
@@ -219,6 +220,16 @@ mp_uint_t mp_repl_autocomplete(const char *str, mp_uint_t len, const mp_print_t
219220

220221
// nothing found
221222
if (n_found == 0) {
223+
// If there're no better alternatives, and if it's first word
224+
// in the line, try to complete "import".
225+
if (s_start == org_str) {
226+
static char import_str[] = "import ";
227+
if (memcmp(s_start, import_str, s_len) == 0) {
228+
*compl_str = import_str + s_len;
229+
return sizeof(import_str) - 1 - s_len;
230+
}
231+
}
232+
222233
return 0;
223234
}
224235

0 commit comments

Comments
 (0)