Skip to content

Commit a8f23f6

Browse files
committed
shared/readline: Make tab insert an indent when it follows whitespace.
Entering tab at the REPL will now make it insert an indent (4 spaces) in the following cases: - after any whitespace on a line - at the start of a line that is not the first line This changes the existing behaviour where a tab would insert an indent only if there were no matches in the auto-complete search, and it was the start of the line. This means, if there were any symbols in the global namespace, tab could never be used to indent. Note that entering tab at the start of the first line will still do auto-completion, but will now do nothing if there are no symbols in the global namespace, which is more consistent than before. Signed-off-by: Damien George <damien@micropython.org>
1 parent caaff94 commit a8f23f6

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

py/repl.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,7 @@ size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print
313313
return sizeof(import_str) - 1 - s_len;
314314
}
315315
}
316-
if (q_first == 0) {
317-
*compl_str = " ";
318-
return s_len ? 0 : 4;
319-
}
316+
return 0;
320317
}
321318

322319
// 1 match found, or multiple matches with a common prefix

shared/readline/readline.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,22 @@ int readline_process_char(int c) {
222222
} else if (c == 9) {
223223
// tab magic
224224
const char *compl_str;
225-
size_t compl_len = mp_repl_autocomplete(rl.line->buf + rl.orig_line_len, rl.cursor_pos - rl.orig_line_len, &mp_plat_print, &compl_str);
225+
size_t compl_len;
226+
if (vstr_len(rl.line) != 0 && unichar_isspace(vstr_str(rl.line)[rl.cursor_pos - 1])) {
227+
// expand tab to 4 spaces if it follows whitespace:
228+
// - includes the case of additional indenting
229+
// - includes the case of indenting the start of a line that's not the first line,
230+
// because a newline will be the previous character
231+
// - doesn't include the case when at the start of the first line, because we still
232+
// want to use auto-complete there
233+
compl_str = " ";
234+
compl_len = 4;
235+
} else {
236+
// try to auto-complete a word
237+
const char *cur_line_buf = vstr_str(rl.line) + rl.orig_line_len;
238+
size_t cur_line_len = rl.cursor_pos - rl.orig_line_len;
239+
compl_len = mp_repl_autocomplete(cur_line_buf, cur_line_len, &mp_plat_print, &compl_str);
240+
}
226241
if (compl_len == 0) {
227242
// no match
228243
} else if (compl_len == (size_t)(-1)) {

0 commit comments

Comments
 (0)