From 72f658f8969c6b19b338633d8881d89936c1f5dd Mon Sep 17 00:00:00 2001 From: John Isom Date: Sun, 2 Aug 2020 11:39:40 -0600 Subject: [PATCH] Replace my implementation of crystal-insert-on-new-line with already-existing ruby one This fixed a weird bug where if you were typing in the top level scope of a program (no indentation before statements), the `end`s wouldn't be inserted for you. --- rc/filetype/crystal.kak | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/rc/filetype/crystal.kak b/rc/filetype/crystal.kak index 3c4f5dfe..6e76b2da 100644 --- a/rc/filetype/crystal.kak +++ b/rc/filetype/crystal.kak @@ -214,23 +214,19 @@ define-command -hidden crystal-indent-on-new-line %{ define-command -hidden crystal-insert-on-new-line %[ evaluate-commands -no-hooks -draft -itersel %[ - # Copy comment prefix and following whitespaces - try %{ execute-keys -draft k s '^\h*\K#\h*' y j gl p } - - # Add `end` token if needs be - # The 'x' register is to save the leading whitespaces of the opening token + # copy _#_ comment prefix and following white spaces + try %{ execute-keys -draft k s '^\h*\K#\h*' y j P } + # wisely add end structure evaluate-commands -save-regs x %[ - # Save the leading whitespaces in register 'x' - try %{ execute-keys -draft k s ^\h* \" x y } + try %{ execute-keys -draft k s ^ \h + \" x y } catch %{ reg x '' } try %[ evaluate-commands -draft %[ - # Make sure previous line opens a block - execute-keys -draft k ^x(?:begin|case|class|def|for|if|module|unless|until|while|.+\bdo\h\|.+(?=\|))[^0-9A-Za-z_!?] - # Make sure `end` doesn't already exist on indent level - execute-keys -draft }i J ^x(?:end|else|elsif|rescue|when)[^0-9A-Za-z_!?] + # Check if previous line opens a block + execute-keys -draft k ^x(?:begin|case|class|def|for|if|module|unless|until|while|.+\bdo$|.+\bdo\h\|.+(?=\|))[^0-9A-Za-z_!?] + # Check that we do not already have an end for this indent level which is first set via `crystal-indent-on-new-line` hook + execute-keys -draft }i J ^x(?:end|else|elsif|rescue|when)[^0-9A-Za-z_!?] ] - # Insert new line with end prepended by contents of register 'x' - execute-keys -draft o x end + execute-keys -draft oxend # insert a new line with containing end ] ] ]