diff options
author | aycabta <aycabta@gmail.com> | 2019-11-20 10:04:41 +0900 |
---|---|---|
committer | aycabta <aycabta@gmail.com> | 2019-11-20 10:04:41 +0900 |
commit | da0d7211aa85b90f2246e2d9abfe08105f7ddedb (patch) | |
tree | 0bfcf51bf0defbcf35ec99c36c66a18d69fd5928 | |
parent | 9306602f24e5c9be4cdae5ed7e69e746ae9e175e (diff) |
Fix indent of a line when a newline is inserted to the next
-rw-r--r-- | lib/reline/line_editor.rb | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index 55a5faf82f..aab20b8657 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -806,6 +806,26 @@ class Reline::LineEditor private def process_auto_indent return if not @check_new_auto_indent and @previous_line_index # move cursor up or down + if @check_new_auto_indent and @previous_line_index and @previous_line_index > 0 and @line_index > @previous_line_index + # Fix indent of a line when a newline is inserted to the next + new_lines = whole_lines(index: @previous_line_index, line: @line) + new_indent = @auto_indent_proc.(new_lines[0..-3].push(''), @line_index - 1, 0, true) + md = @line.match(/\A */) + prev_indent = md[0].count(' ') + @line = ' ' * new_indent + @line.lstrip + + new_indent = nil + (new_lines[-2].size + 1).times do |n| + result = @auto_indent_proc.(new_lines[0..-2], @line_index - 1, n, false) + if result + new_indent = result + break + end + end + if new_indent&.>= 0 + @line = ' ' * new_indent + @line.lstrip + end + end if @previous_line_index new_lines = whole_lines(index: @previous_line_index, line: @line) else |