From f585cc2eff896174437d39c82fd5ec7b92ca3feb Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Mon, 15 Dec 2025 11:11:17 +0900 Subject: [PATCH] mruby-bin-mirb: align dedent to proper indent level Instead of just removing 2 spaces, perform_dedent() now calculates the expected indent level from previous lines and aligns to that. Co-authored-by: Claude --- .../mruby-bin-mirb/tools/mirb/mirb_editor.c | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/mrbgems/mruby-bin-mirb/tools/mirb/mirb_editor.c b/mrbgems/mruby-bin-mirb/tools/mirb/mirb_editor.c index 21097937a..1277ca53e 100644 --- a/mrbgems/mruby-bin-mirb/tools/mirb/mirb_editor.c +++ b/mrbgems/mruby-bin-mirb/tools/mirb/mirb_editor.c @@ -207,11 +207,26 @@ static void perform_dedent(mirb_buffer *buf) { const char *line = mirb_buffer_current_line(buf); - size_t spaces = leading_spaces(line); + size_t current_spaces = leading_spaces(line); + int expected_indent = 0; - /* Remove up to 2 spaces */ - size_t to_remove = (spaces >= 2) ? 2 : spaces; - if (to_remove > 0) { + /* Calculate expected indent from code up to previous line */ + if (buf->cursor_line > 0) { + char *partial = buffer_to_string_upto_line(buf, buf->cursor_line - 1); + if (partial) { + expected_indent = calc_indent_level(partial); + free(partial); + } + } + + /* Dedent one level for keywords like end, else, etc. */ + if (expected_indent > 0) expected_indent--; + + size_t target_spaces = (size_t)(expected_indent * 2); + + /* Only dedent if we have more spaces than target */ + if (current_spaces > target_spaces) { + size_t to_remove = current_spaces - target_spaces; size_t saved_col = buf->cursor_col; /* Move cursor to start of line and delete leading spaces */ buf->cursor_col = 0;