From 01159a9e726ba45ab95cb2e853b76fa16a86f52a Mon Sep 17 00:00:00 2001 From: Dmitry Matveyev Date: Sat, 8 Jun 2019 00:25:33 +0300 Subject: [PATCH] Add support for autoformatting after do keyword in Ruby --- rc/filetype/ruby.kak | 15 ++++++++------- test/indent/ruby/do-keyword/cmd | 1 + test/indent/ruby/do-keyword/in | 1 + test/indent/ruby/do-keyword/out | 7 +++++++ test/indent/ruby/do-keyword/rc | 3 +++ 5 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 test/indent/ruby/do-keyword/cmd create mode 100644 test/indent/ruby/do-keyword/in create mode 100644 test/indent/ruby/do-keyword/out create mode 100644 test/indent/ruby/do-keyword/rc diff --git a/rc/filetype/ruby.kak b/rc/filetype/ruby.kak index 7bac8dba..43c3d2fd 100644 --- a/rc/filetype/ruby.kak +++ b/rc/filetype/ruby.kak @@ -17,8 +17,8 @@ hook global WinSetOption filetype=ruby %{ set-option window static_words %opt{ruby_static_words} hook window InsertChar .* -group ruby-indent ruby-indent-on-char - hook window InsertChar \n -group ruby-insert ruby-insert-on-new-line hook window InsertChar \n -group ruby-indent ruby-indent-on-new-line + hook window InsertChar \n -group ruby-insert ruby-insert-on-new-line alias window alt ruby-alternative-file @@ -142,7 +142,7 @@ define-command -hidden ruby-indent-on-char %{ try %{ execute-keys -draft ^ \h * (else|elsif) $ ^ \h * (if) s \A | \z ) } try %{ execute-keys -draft ^ \h * (when) $ ^ \h * (case) s \A | \z ) } try %{ execute-keys -draft ^ \h * (rescue) $ ^ \h * (begin) s \A | \z ) } - try %{ execute-keys -draft ^ \h * (end) $ ^ \h * (begin|case|class|def|do|for|if|module|unless|until|while) s \A | \z ) } + try %{ execute-keys -draft ^ \h * (end) $ ^ \h * (begin|case|class|def|for|if|module|unless|until|while) s \A | \z ) } } } @@ -153,22 +153,23 @@ define-command -hidden ruby-indent-on-new-line %{ # filter previous line try %{ execute-keys -draft k : ruby-trim-indent } # indent after start structure - try %{ execute-keys -draft k ^ \h * (begin|case|class|def|do|else|elsif|ensure|for|if|module|rescue|unless|until|when|while) \b j } + try %{ execute-keys -draft k ^ \h * (begin|case|class|def|else|elsif|ensure|for|if|module|rescue|unless|until|when|while|.+\bdo$|.+\bdo\h\|.+(?=\|)) \b j } } } 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 } + try %{ execute-keys -draft k s '^\h*\K#\h*' y j \; P } # wisely add end structure evaluate-commands -save-regs x %[ try %{ execute-keys -draft k s ^ \h + \" x y } catch %{ reg x '' } 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$ + # 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\|.+(?=\|))\b + # Check that we do not already have an end for this indent level which is first set via `ruby-indent-on-new-line` hook + execute-keys -draft }i J ^xend$ ] execute-keys -draft oxend # insert a new line with containing end ] diff --git a/test/indent/ruby/do-keyword/cmd b/test/indent/ruby/do-keyword/cmd new file mode 100644 index 00000000..5a3ecaf6 --- /dev/null +++ b/test/indent/ruby/do-keyword/cmd @@ -0,0 +1 @@ +cjoe.map do |element| diff --git a/test/indent/ruby/do-keyword/in b/test/indent/ruby/do-keyword/in new file mode 100644 index 00000000..5e903f7f --- /dev/null +++ b/test/indent/ruby/do-keyword/in @@ -0,0 +1 @@ +test do%( ) diff --git a/test/indent/ruby/do-keyword/out b/test/indent/ruby/do-keyword/out new file mode 100644 index 00000000..b38bd9a4 --- /dev/null +++ b/test/indent/ruby/do-keyword/out @@ -0,0 +1,7 @@ +test do + +end + +e.map do |element| + +end diff --git a/test/indent/ruby/do-keyword/rc b/test/indent/ruby/do-keyword/rc new file mode 100644 index 00000000..934f7e83 --- /dev/null +++ b/test/indent/ruby/do-keyword/rc @@ -0,0 +1,3 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/filetype/ruby.kak" +set buffer filetype ruby