From 6709f5e67d9d27861c0dbcd8b5c7b8414865aca6 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 26 Mar 2025 11:28:07 +1100 Subject: [PATCH] py/modmicropython: Expose repl_autocomplete as python function. Used to add tab completion to aiorepl. Signed-off-by: Andrew Leech --- py/modmicropython.c | 16 ++++++++++++++++ tests/micropython/repl_autocomplete.py | 19 +++++++++++++++++++ tests/micropython/repl_autocomplete.py.exp | 5 +++++ 3 files changed, 40 insertions(+) create mode 100644 tests/micropython/repl_autocomplete.py create mode 100644 tests/micropython/repl_autocomplete.py.exp diff --git a/py/modmicropython.c b/py/modmicropython.c index d1a687f10e14e..8254dcb56a41f 100644 --- a/py/modmicropython.c +++ b/py/modmicropython.c @@ -31,6 +31,7 @@ #include "py/runtime.h" #include "py/gc.h" #include "py/mphal.h" +#include "py/repl.h" #if MICROPY_PY_MICROPYTHON @@ -166,6 +167,18 @@ static mp_obj_t mp_micropython_schedule(mp_obj_t function, mp_obj_t arg) { static MP_DEFINE_CONST_FUN_OBJ_2(mp_micropython_schedule_obj, mp_micropython_schedule); #endif +#if MICROPY_HELPER_REPL +static mp_obj_t mp_micropython_repl_autocomplete(mp_obj_t cur_line) { + const char *compl_str; + size_t str_len = 0; + const char *str = mp_obj_str_get_data(cur_line, &str_len); + + ssize_t compl_len = mp_repl_autocomplete(str, str_len, &mp_plat_print, &compl_str); + return (compl_len <= 0) ? mp_const_none : mp_obj_new_str_via_qstr(compl_str, compl_len); +} +static MP_DEFINE_CONST_FUN_OBJ_1(mp_micropython_repl_autocomplete_obj, mp_micropython_repl_autocomplete); +#endif + static const mp_rom_map_elem_t mp_module_micropython_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_micropython) }, { MP_ROM_QSTR(MP_QSTR_const), MP_ROM_PTR(&mp_identity_obj) }, @@ -206,6 +219,9 @@ static const mp_rom_map_elem_t mp_module_micropython_globals_table[] = { #if MICROPY_ENABLE_SCHEDULER { MP_ROM_QSTR(MP_QSTR_schedule), MP_ROM_PTR(&mp_micropython_schedule_obj) }, #endif + #if MICROPY_HELPER_REPL + { MP_ROM_QSTR(MP_QSTR_repl_autocomplete), MP_ROM_PTR(&mp_micropython_repl_autocomplete_obj) }, + #endif }; static MP_DEFINE_CONST_DICT(mp_module_micropython_globals, mp_module_micropython_globals_table); diff --git a/tests/micropython/repl_autocomplete.py b/tests/micropython/repl_autocomplete.py new file mode 100644 index 0000000000000..8cd75418c0d4d --- /dev/null +++ b/tests/micropython/repl_autocomplete.py @@ -0,0 +1,19 @@ +# Check that micropython.repl_autocomplete works as expected. +import micropython + +try: + micropython.repl_autocomplete +except AttributeError: + print("SKIP") + raise SystemExit + +# built-in import (complete includes space on end) +print(micropython.repl_autocomplete("impo")) + +# local variable +x = '123' +print(micropython.repl_autocomplete("x.isdi")) + +# multiple choices +y = "s" +print(micropython.repl_autocomplete("y.en")) diff --git a/tests/micropython/repl_autocomplete.py.exp b/tests/micropython/repl_autocomplete.py.exp new file mode 100644 index 0000000000000..6c7d6055c1575 --- /dev/null +++ b/tests/micropython/repl_autocomplete.py.exp @@ -0,0 +1,5 @@ +rt +git + +endswith encode +None