Skip to content

Commit 5dd9831

Browse files
committed
py/modmicropython: Expose repl_autocomplete as python function.
Used to add tab completion to aiorepl. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent 53bc12d commit 5dd9831

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

py/modmicropython.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "py/runtime.h"
3232
#include "py/gc.h"
3333
#include "py/mphal.h"
34+
#include "py/repl.h"
3435

3536
#if MICROPY_PY_MICROPYTHON
3637

@@ -166,6 +167,18 @@ static mp_obj_t mp_micropython_schedule(mp_obj_t function, mp_obj_t arg) {
166167
static MP_DEFINE_CONST_FUN_OBJ_2(mp_micropython_schedule_obj, mp_micropython_schedule);
167168
#endif
168169

170+
#if MICROPY_HELPER_REPL
171+
static mp_obj_t mp_micropython_repl_autocomplete(mp_obj_t cur_line) {
172+
const char *compl_str;
173+
size_t str_len = 0;
174+
const char *str = mp_obj_str_get_data(cur_line, &str_len);
175+
176+
ssize_t compl_len = mp_repl_autocomplete(str, str_len, &mp_plat_print, &compl_str);
177+
return (compl_len <= 0) ? mp_const_none : mp_obj_new_str_via_qstr(compl_str, compl_len);
178+
}
179+
static MP_DEFINE_CONST_FUN_OBJ_1(mp_micropython_repl_autocomplete_obj, mp_micropython_repl_autocomplete);
180+
#endif
181+
169182
static const mp_rom_map_elem_t mp_module_micropython_globals_table[] = {
170183
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_micropython) },
171184
{ 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[] = {
206219
#if MICROPY_ENABLE_SCHEDULER
207220
{ MP_ROM_QSTR(MP_QSTR_schedule), MP_ROM_PTR(&mp_micropython_schedule_obj) },
208221
#endif
222+
#if MICROPY_HELPER_REPL
223+
{ MP_ROM_QSTR(MP_QSTR_repl_autocomplete), MP_ROM_PTR(&mp_micropython_repl_autocomplete_obj) },
224+
#endif
209225
};
210226

211227
static MP_DEFINE_CONST_DICT(mp_module_micropython_globals, mp_module_micropython_globals_table);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Check that micropython.repl_autocomplete works as expected.
2+
import micropython
3+
4+
try:
5+
micropython.repl_autocomplete
6+
except AttributeError:
7+
print("SKIP")
8+
raise SystemExit
9+
10+
print(micropython.repl_autocomplete("micropytho"))
11+
12+
print(micropython.repl_autocomplete("impo"))
13+
14+
import math
15+
print(micropython.repl_autocomplete("m"))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
n
2+
rt
3+
4+
micropython math
5+
None

0 commit comments

Comments
 (0)