From dbb548af2dce84dc557e81f1d7cd1c2951071ce4 Mon Sep 17 00:00:00 2001 From: John Isom Date: Sat, 1 Aug 2020 11:46:35 -0600 Subject: [PATCH] Fill in implementation of `crystal-insert-on-new-line` --- rc/filetype/crystal.kak | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) 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{