py/repl: Autocomplete builtin modules.
Doing "import <tab>" will now complete/list built-in modules. Originally at adafruit#4548 and adafruit#4608 Signed-off-by: Artyom Skrobov <tyomitch@gmail.com>
This commit is contained in:
parent
7556e01f14
commit
ca35c0059c
|
@ -268,6 +268,13 @@ STATIC mp_obj_t extra_coverage(void) {
|
||||||
size_t len = mp_repl_autocomplete("__n", 3, &mp_plat_print, &str);
|
size_t len = mp_repl_autocomplete("__n", 3, &mp_plat_print, &str);
|
||||||
mp_printf(&mp_plat_print, "%.*s\n", (int)len, str);
|
mp_printf(&mp_plat_print, "%.*s\n", (int)len, str);
|
||||||
|
|
||||||
|
len = mp_repl_autocomplete("i", 1, &mp_plat_print, &str);
|
||||||
|
mp_printf(&mp_plat_print, "%.*s\n", (int)len, str);
|
||||||
|
mp_repl_autocomplete("import ", 7, &mp_plat_print, &str);
|
||||||
|
len = mp_repl_autocomplete("import ut", 9, &mp_plat_print, &str);
|
||||||
|
mp_printf(&mp_plat_print, "%.*s\n", (int)len, str);
|
||||||
|
mp_repl_autocomplete("import utime", 12, &mp_plat_print, &str);
|
||||||
|
|
||||||
mp_store_global(MP_QSTR_sys, mp_import_name(MP_QSTR_sys, mp_const_none, MP_OBJ_NEW_SMALL_INT(0)));
|
mp_store_global(MP_QSTR_sys, mp_import_name(MP_QSTR_sys, mp_const_none, MP_OBJ_NEW_SMALL_INT(0)));
|
||||||
mp_repl_autocomplete("sys.", 4, &mp_plat_print, &str);
|
mp_repl_autocomplete("sys.", 4, &mp_plat_print, &str);
|
||||||
len = mp_repl_autocomplete("sys.impl", 8, &mp_plat_print, &str);
|
len = mp_repl_autocomplete("sys.impl", 8, &mp_plat_print, &str);
|
||||||
|
|
34
py/repl.c
34
py/repl.c
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "py/obj.h"
|
#include "py/obj.h"
|
||||||
|
#include "py/objmodule.h"
|
||||||
#include "py/runtime.h"
|
#include "py/runtime.h"
|
||||||
#include "py/builtin.h"
|
#include "py/builtin.h"
|
||||||
#include "py/repl.h"
|
#include "py/repl.h"
|
||||||
|
@ -144,10 +145,16 @@ bool mp_repl_continue_with_input(const char *input) {
|
||||||
}
|
}
|
||||||
|
|
||||||
STATIC bool test_qstr(mp_obj_t obj, qstr name) {
|
STATIC bool test_qstr(mp_obj_t obj, qstr name) {
|
||||||
// try object member
|
if (obj) {
|
||||||
mp_obj_t dest[2];
|
// try object member
|
||||||
mp_load_method_protected(obj, name, dest, true);
|
mp_obj_t dest[2];
|
||||||
return dest[0] != MP_OBJ_NULL;
|
mp_load_method_protected(obj, name, dest, true);
|
||||||
|
return dest[0] != MP_OBJ_NULL;
|
||||||
|
} else {
|
||||||
|
// try builtin module
|
||||||
|
return mp_map_lookup((mp_map_t *)&mp_builtin_module_map,
|
||||||
|
MP_OBJ_NEW_QSTR(name), MP_MAP_LOOKUP);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
STATIC const char *find_completions(const char *s_start, size_t s_len,
|
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
|
||||||
++str;
|
++str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// after "import", suggest built-in modules
|
||||||
|
static const char import_str[] = "import ";
|
||||||
|
if (len >= 7 && !memcmp(org_str, import_str, 7)) {
|
||||||
|
obj = MP_OBJ_NULL;
|
||||||
|
}
|
||||||
|
|
||||||
// look for matches
|
// look for matches
|
||||||
size_t match_len;
|
size_t match_len;
|
||||||
qstr q_first, q_last;
|
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
|
||||||
|
|
||||||
// nothing found
|
// nothing found
|
||||||
if (q_first == 0) {
|
if (q_first == 0) {
|
||||||
if (s_len == 0) {
|
|
||||||
*compl_str = " ";
|
|
||||||
return 4;
|
|
||||||
}
|
|
||||||
// If there're no better alternatives, and if it's first word
|
// If there're no better alternatives, and if it's first word
|
||||||
// in the line, try to complete "import".
|
// in the line, try to complete "import".
|
||||||
if (s_start == org_str) {
|
if (s_start == org_str && s_len > 0) {
|
||||||
static const char import_str[] = "import ";
|
|
||||||
if (memcmp(s_start, import_str, s_len) == 0) {
|
if (memcmp(s_start, import_str, s_len) == 0) {
|
||||||
*compl_str = import_str + s_len;
|
*compl_str = import_str + s_len;
|
||||||
return sizeof(import_str) - 1 - s_len;
|
return sizeof(import_str) - 1 - s_len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (q_first == 0) {
|
||||||
return 0;
|
*compl_str = " ";
|
||||||
|
return s_len ? 0 : 4;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1 match found, or multiple matches with a common prefix
|
// 1 match found, or multiple matches with a common prefix
|
||||||
|
|
|
@ -27,6 +27,20 @@ RuntimeError:
|
||||||
RuntimeError:
|
RuntimeError:
|
||||||
# repl
|
# repl
|
||||||
ame__
|
ame__
|
||||||
|
mport
|
||||||
|
|
||||||
|
builtins micropython _thread _uasyncio
|
||||||
|
btree cexample cmath cppexample
|
||||||
|
ffi framebuf gc math
|
||||||
|
termios uarray ubinascii ucollections
|
||||||
|
ucryptolib uctypes uerrno uhashlib
|
||||||
|
uheapq uio ujson umachine
|
||||||
|
uos urandom ure uselect
|
||||||
|
usocket ussl ustruct usys
|
||||||
|
utime utimeq uwebsocket uzlib
|
||||||
|
ime
|
||||||
|
|
||||||
|
utime utimeq
|
||||||
|
|
||||||
argv atexit byteorder exc_info
|
argv atexit byteorder exc_info
|
||||||
exit getsizeof implementation maxsize
|
exit getsizeof implementation maxsize
|
||||||
|
|
Loading…
Reference in New Issue