Skip to content

Commit f4ed309

Browse files
committed
repl: Autocomplete builtin modules.
Originally at adafruit#4548 and adafruit#4608 Signed-off-by: Artyom Skrobov <[email protected]>
1 parent 3541687 commit f4ed309

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

ports/unix/coverage.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,13 @@ STATIC mp_obj_t extra_coverage(void) {
268268
size_t len = mp_repl_autocomplete("__n", 3, &mp_plat_print, &str);
269269
mp_printf(&mp_plat_print, "%.*s\n", (int)len, str);
270270

271+
len = mp_repl_autocomplete("i", 1, &mp_plat_print, &str);
272+
mp_printf(&mp_plat_print, "%.*s\n", (int)len, str);
273+
mp_repl_autocomplete("import ", 7, &mp_plat_print, &str);
274+
len = mp_repl_autocomplete("import ut", 9, &mp_plat_print, &str);
275+
mp_printf(&mp_plat_print, "%.*s\n", (int)len, str);
276+
mp_repl_autocomplete("import utime", 12, &mp_plat_print, &str);
277+
271278
mp_store_global(MP_QSTR_sys, mp_import_name(MP_QSTR_sys, mp_const_none, MP_OBJ_NEW_SMALL_INT(0)));
272279
mp_repl_autocomplete("sys.", 4, &mp_plat_print, &str);
273280
len = mp_repl_autocomplete("sys.impl", 8, &mp_plat_print, &str);

py/repl.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <string.h>
2828
#include "py/obj.h"
29+
#include "py/objmodule.h"
2930
#include "py/runtime.h"
3031
#include "py/builtin.h"
3132
#include "py/repl.h"
@@ -144,10 +145,16 @@ bool mp_repl_continue_with_input(const char *input) {
144145
}
145146

146147
STATIC bool test_qstr(mp_obj_t obj, qstr name) {
147-
// try object member
148-
mp_obj_t dest[2];
149-
mp_load_method_protected(obj, name, dest, true);
150-
return dest[0] != MP_OBJ_NULL;
148+
if (obj) {
149+
// try object member
150+
mp_obj_t dest[2];
151+
mp_load_method_protected(obj, name, dest, true);
152+
return dest[0] != MP_OBJ_NULL;
153+
} else {
154+
// try builtin module
155+
return mp_map_lookup((mp_map_t *)&mp_builtin_module_map,
156+
MP_OBJ_NEW_QSTR(name), MP_MAP_LOOKUP);
157+
}
151158
}
152159

153160
STATIC const char *find_completions(const char *s_start, size_t s_len,
@@ -274,6 +281,12 @@ size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print
274281
++str;
275282
}
276283

284+
// after "import", suggest built-in modules
285+
static const char import_str[] = "import ";
286+
if (len >= 7 && !memcmp(org_str, import_str, 7)) {
287+
obj = MP_OBJ_NULL;
288+
}
289+
277290
// look for matches
278291
size_t match_len;
279292
qstr q_first, q_last;
@@ -282,21 +295,18 @@ size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print
282295

283296
// nothing found
284297
if (q_first == 0) {
285-
if (s_len == 0) {
286-
*compl_str = " ";
287-
return 4;
288-
}
289298
// If there're no better alternatives, and if it's first word
290299
// in the line, try to complete "import".
291-
if (s_start == org_str) {
292-
static const char import_str[] = "import ";
300+
if (s_start == org_str && s_len > 0) {
293301
if (memcmp(s_start, import_str, s_len) == 0) {
294302
*compl_str = import_str + s_len;
295303
return sizeof(import_str) - 1 - s_len;
296304
}
297305
}
298-
299-
return 0;
306+
if (q_first == 0) {
307+
*compl_str = " ";
308+
return s_len ? 0 : 4;
309+
}
300310
}
301311

302312
// 1 match found, or multiple matches with a common prefix

tests/unix/extra_coverage.py.exp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ RuntimeError:
2727
RuntimeError:
2828
# repl
2929
ame__
30+
mport
31+
32+
builtins micropython _thread _uasyncio
33+
btree cexample cmath cppexample
34+
ffi framebuf gc math
35+
termios uarray ubinascii ucollections
36+
ucryptolib uctypes uerrno uhashlib
37+
uheapq uio ujson umachine
38+
uos urandom ure uselect
39+
usocket ussl ustruct usys
40+
utime utimeq uwebsocket uzlib
41+
ime
42+
43+
utime utimeq
3044

3145
argv atexit byteorder exc_info
3246
exit getsizeof implementation maxsize

0 commit comments

Comments
 (0)