py/repl: Refactor autocomplete to reduce nesting.
Originally at adafruit#4548 Signed-off-by: Artyom Skrobov <tyomitch@gmail.com>
This commit is contained in:
parent
befbff31b7
commit
f85ea8d4fe
19
py/repl.c
19
py/repl.c
@ -161,15 +161,22 @@ size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print
|
|||||||
mp_obj_t obj = MP_OBJ_FROM_PTR(&mp_module___main__);
|
mp_obj_t obj = MP_OBJ_FROM_PTR(&mp_module___main__);
|
||||||
mp_obj_t dest[2];
|
mp_obj_t dest[2];
|
||||||
|
|
||||||
|
const char *s_start;
|
||||||
|
size_t s_len;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
// get next word in string to complete
|
// get next word in string to complete
|
||||||
const char *s_start = str;
|
s_start = str;
|
||||||
while (str < top && *str != '.') {
|
while (str < top && *str != '.') {
|
||||||
++str;
|
++str;
|
||||||
}
|
}
|
||||||
size_t s_len = str - s_start;
|
s_len = str - s_start;
|
||||||
|
|
||||||
|
if (str == top) {
|
||||||
|
// end of string, do completion on this partial name
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (str < top) {
|
|
||||||
// a complete word, lookup in current object
|
// a complete word, lookup in current object
|
||||||
qstr q = qstr_find_strn(s_start, s_len);
|
qstr q = qstr_find_strn(s_start, s_len);
|
||||||
if (q == MP_QSTRnull) {
|
if (q == MP_QSTRnull) {
|
||||||
@ -186,9 +193,7 @@ size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print
|
|||||||
|
|
||||||
// skip '.' to move to next word
|
// skip '.' to move to next word
|
||||||
++str;
|
++str;
|
||||||
|
}
|
||||||
} else {
|
|
||||||
// end of string, do completion on this partial name
|
|
||||||
|
|
||||||
// look for matches
|
// look for matches
|
||||||
const char *match_str = NULL;
|
const char *match_str = NULL;
|
||||||
@ -285,7 +290,5 @@ size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print
|
|||||||
|
|
||||||
return (size_t)(-1); // indicate many matches
|
return (size_t)(-1); // indicate many matches
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // MICROPY_HELPER_REPL
|
#endif // MICROPY_HELPER_REPL
|
||||||
|
Loading…
Reference in New Issue
Block a user