From 567c82840a907d0baa831c352666bb80bca0cbfc Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 14 Apr 2018 12:47:42 +1000 Subject: [PATCH] ruby.kak: Refactor end autoinsertion logic to be more robust Fixes the bug described by PR #1994 --- rc/base/ruby.kak | 21 ++++++++++++------- .../cmd | 1 + .../in | 4 ++++ .../out | 6 ++++++ .../rc | 3 +++ 5 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 test/indent/ruby/following-blocks-should-not-prevent-end/cmd create mode 100644 test/indent/ruby/following-blocks-should-not-prevent-end/in create mode 100644 test/indent/ruby/following-blocks-should-not-prevent-end/out create mode 100644 test/indent/ruby/following-blocks-should-not-prevent-end/rc diff --git a/rc/base/ruby.kak b/rc/base/ruby.kak index f93ba04b..c615c595 100644 --- a/rc/base/ruby.kak +++ b/rc/base/ruby.kak @@ -131,17 +131,24 @@ define-command -hidden ruby-indent-on-new-line %{ } } -define-command -hidden ruby-insert-on-new-line %{ - evaluate-commands -no-hooks -draft -itersel %{ +define-command -hidden ruby-insert-on-new-line %[ + evaluate-commands -no-hooks -draft -itersel %[ # copy _#_ comment prefix and following white spaces try %{ execute-keys -draft k s '^\h*\K#\h*' y gh j P } # wisely add end structure - evaluate-commands -save-regs x %{ + evaluate-commands -save-regs x %[ try %{ execute-keys -draft k s ^ \h + \" x y } catch %{ reg x '' } - try %{ execute-keys -draft k ^ x (begin|case|class|def|do|for|if|module|unless|until|while) j i X K ^ x (begin|case|class|def|do|for|if|module|unless|until|while) . * \n x end $ j x y p j a end } - } - } -} + try %[ + evaluate-commands -draft %[ + execute-keys -draft k ^x(begin|case|class|def|do|for|if|module|unless|until|while) # Check if previous line opens a block + # Check that we do not already have an end for this indent level, or that we have another block opening at that indent level first + execute-keys -draft Ge \A(^\n|^x(?!begin)(?!case)(?!class)(?!def)(?!do)(?!for)(?!if)(?!module)(?!unless)(?!until)(?!while)[^\n]*\n)+xend$ + ] + execute-keys -draft oxend # insert a new line with containing end + ] + ] + ] +] # Initialization # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ diff --git a/test/indent/ruby/following-blocks-should-not-prevent-end/cmd b/test/indent/ruby/following-blocks-should-not-prevent-end/cmd new file mode 100644 index 00000000..76003030 --- /dev/null +++ b/test/indent/ruby/following-blocks-should-not-prevent-end/cmd @@ -0,0 +1 @@ +c diff --git a/test/indent/ruby/following-blocks-should-not-prevent-end/in b/test/indent/ruby/following-blocks-should-not-prevent-end/in new file mode 100644 index 00000000..cc2f1dc5 --- /dev/null +++ b/test/indent/ruby/following-blocks-should-not-prevent-end/in @@ -0,0 +1,4 @@ +def foo%( ) + +def bar +end diff --git a/test/indent/ruby/following-blocks-should-not-prevent-end/out b/test/indent/ruby/following-blocks-should-not-prevent-end/out new file mode 100644 index 00000000..53db17a8 --- /dev/null +++ b/test/indent/ruby/following-blocks-should-not-prevent-end/out @@ -0,0 +1,6 @@ +def foo + +end + +def bar +end diff --git a/test/indent/ruby/following-blocks-should-not-prevent-end/rc b/test/indent/ruby/following-blocks-should-not-prevent-end/rc new file mode 100644 index 00000000..d4fadeea --- /dev/null +++ b/test/indent/ruby/following-blocks-should-not-prevent-end/rc @@ -0,0 +1,3 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/base/ruby.kak" +set buffer filetype ruby