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 ] ] ]