Skip to content

py/modmicropython: Expose repl_autocomplete as python function. #17011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions py/modmicropython.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "py/runtime.h"
#include "py/gc.h"
#include "py/mphal.h"
#include "py/repl.h"

#if MICROPY_PY_MICROPYTHON

Expand Down Expand Up @@ -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) },
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions tests/micropython/repl_autocomplete.py
Original file line number Diff line number Diff line change
@@ -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"))
5 changes: 5 additions & 0 deletions tests/micropython/repl_autocomplete.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rt
git

endswith encode
None
Loading