py/modmicropython: Expose repl_autocomplete as python function. - #17011
py/modmicropython: Expose repl_autocomplete as python function.#17011andrewleech wants to merge 1 commit into
Conversation
|
Code size report: |
e1aa321 to
5dd9831
Compare
|
Adding autocomplete for aiorepl is very desirable, and this is looking promising! Here are some of the tests I ran: >>>import micropython
>>>micropython.repl_autocomplete("impo") # Should complete 'import'
'rt '
>>> class Foo:
... def _bar():
... pass
... def alpha():
... pass
>>> micropython.repl_autocomplete("f = Fo") # Should complete Foo
'o'
>>>f = Foo()
>>> micropython.repl_autocomplete("f.") # Should complete alpha (ignoring _bar)
'alpha'Note that this is correct and consistent with MicroPython's built-in tab completion - but differs in some cases to CPython, at least at v3.12. CPython will also supply parentheses, ie it would return I also tested a module with private members and they were also correctly filtered out. Looks good! |
Perhaps that should just be documented as such. I'd be fine with consistent behavior with the MicroPython repl. |
| @@ -218,7 +218,7 @@ static void print_completions(const mp_print_t *print, | |||
| for (qstr q = q_first; q <= q_last; ++q) { | |||
| size_t d_len; | |||
| const char *d_str = (const char *)qstr_data(q, &d_len); | |||
| if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) { | |||
| if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0 && d_str[0] != '_') { | |||
There was a problem hiding this comment.
Please break this out into a separate PR. It'll need changes to the tests as well.
There was a problem hiding this comment.
Split out to #17108 with a unit test (which might need some more updating to ensure the test is consistent / works across ports etc?)
5dd9831 to
59eccf8
Compare
59eccf8 to
bdb98b0
Compare
|
Ok I'm really not sure why the unit tests are still failing on some builds; unix standard in particular. The test passes for me locally with the same build. |
bdb98b0 to
6709f5e
Compare
| 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); |
There was a problem hiding this comment.
If you make this (compl_len < 0) then the caller should be able to distinguish between the 3 distinct cases (no match, one match, many matches).
6709f5e to
5dd9ba0
Compare
|
Updated the branch with a few fixes and an additional feature: Fixed a type mismatch in Also added Added docs entries for both functions in |
dad98b0 to
84cc57e
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #17011 +/- ##
=======================================
Coverage 98.46% 98.46%
=======================================
Files 176 176
Lines 22784 22793 +9
=======================================
+ Hits 22435 22444 +9
Misses 349 349 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
84cc57e to
2bf20be
Compare
2bf20be to
047a306
Compare
Allows adding tab completion to custom REPLs such as aiorepl. Returns a non-empty string (the completion suffix) on a unique match or common prefix, an empty string when multiple candidates are printed to stdout, or None when there is no match. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
047a306 to
aeda0fa
Compare
Summary
Exposes
mp_repl_autocomplete()andmp_hal_stdio_mode_raw()/mp_hal_stdio_mode_orig()to Python asmicropython.repl_autocomplete()andmicropython.stdio_mode_raw()respectively.repl_autocomplete(line)returns the completion suffix string, empty string for no match, orNonewhen multiple candidates are printed. Gated onMICROPY_HELPER_REPLwhich is already enabled on most ports.stdio_mode_raw(enabled)switches the terminal between raw and original mode. Gated behind a newMICROPY_PY_MICROPYTHON_STDIO_RAWconfig option, defaulting to off, enabled on unix.Both are used by micropython/micropython-lib#1081 to bring aiorepl closer to feature parity with the native REPL — tab completion via
repl_autocomplete, and proper terminal mode management viastdio_mode_raw.Testing
Unit tests added for both functions under
tests/micropython/. A cmdline REPL test forstdio_mode_rawverifies actual terminal attribute changes viatermios.Tested on unix port.
Generative AI
I used generative AI tools when creating this PR, but a human has checked the code and is responsible for the description above.