diff --git a/rc/filetype/crystal.kak b/rc/filetype/crystal.kak index 65378666..1a55ffde 100644 --- a/rc/filetype/crystal.kak +++ b/rc/filetype/crystal.kak @@ -210,12 +210,29 @@ define-command -hidden crystal-indent-on-new-line %{ } } -define-command -hidden crystal-insert-on-new-line %{ - evaluate-commands -no-hooks -draft -itersel %{ +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 + evaluate-commands -save-regs x %[ + # Save the leading whitespaces in register 'x' + try %{ execute-keys -draft k s ^\h* \" x y } + 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_!?] + ] + # Insert new line with end prepended by contents of register 'x' + execute-keys -draft o x end + ] + ] + ] +] define-command -hidden crystal-fetch-keywords %{ set-register dquote %sh{