From 5c8dfcdfa946d9018150e67a32f472b2e80a9cd7 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Sun, 27 Sep 2020 15:53:15 +0800 Subject: [PATCH] Rust improve align after partial statement --- rc/filetype/rust.kak | 15 ++++--- test/indent/rust/after-variable/cmd | 1 + test/indent/rust/after-variable/in | 6 +++ test/indent/rust/after-variable/out | 9 +++++ test/indent/rust/after-variable/rc | 3 ++ test/indent/rust/after-where/in | 2 - test/indent/rust/after-where/out | 3 -- test/indent/rust/line-start-with-operator/bar | 36 +++++++++++++++++ test/indent/rust/line-start-with-operator/cmd | 1 + test/indent/rust/line-start-with-operator/in | 26 +++++++++++++ test/indent/rust/line-start-with-operator/out | 39 +++++++++++++++++++ test/indent/rust/line-start-with-operator/rc | 3 ++ 12 files changed, 131 insertions(+), 13 deletions(-) create mode 100644 test/indent/rust/after-variable/cmd create mode 100644 test/indent/rust/after-variable/in create mode 100644 test/indent/rust/after-variable/out create mode 100644 test/indent/rust/after-variable/rc create mode 100644 test/indent/rust/line-start-with-operator/bar create mode 100644 test/indent/rust/line-start-with-operator/cmd create mode 100644 test/indent/rust/line-start-with-operator/in create mode 100644 test/indent/rust/line-start-with-operator/out create mode 100644 test/indent/rust/line-start-with-operator/rc diff --git a/rc/filetype/rust.kak b/rc/filetype/rust.kak index a06ae7d1..f2fbfee1 100644 --- a/rc/filetype/rust.kak +++ b/rc/filetype/rust.kak @@ -148,16 +148,15 @@ define-command -hidden rust-indent-on-new-line %~ } catch %` # preserve previous line indent try %{ execute-keys -draft K } - # indent after lines ending with { or ( - try %[ execute-keys -draft k [{(]\h*$ j ] # indent after lines ending with [{(].+ and move first parameter to own line try %< execute-keys -draft [c[({],[)}] \A[({][^\n]+\n[^\n]*\n?\z L i > - # indent lines with a standalone where - try %+ execute-keys -draft k ^\h*where\h*$ j + - # dedent after lines starting with . and ending with , or ; - try %_ execute-keys -draft k ^\h*\..*[,]\h*$ j _ - # deindent closing brace(s) when after cursor - try %= execute-keys -draft ^\h*[})] gh / [})] m 1 = + # indent after non-empty lines not starting with operator and not ending with , or ; + # XXX s [^\h]* is workaround for broken negate match #3766, use this later + try %< execute-keys -draft k s [^\h].+ \A[-+*/&|^})] [,](\h*/[/*].*|)$ j > + # dedent after lines starting with . and ending with } or ) or , or ; + try %_ execute-keys -draft k ^\h*\..*[}),]\h*$ j _ + # align to opening curly brace or paren when newline is inserted before a single closing + try %< execute-keys -draft ^\h*[)}] h m 1 > # todo dedent additional unmatched parenthesis # try %& execute-keys -draft k s \((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\) l Gl s\) %sh{ # count previous selections length diff --git a/test/indent/rust/after-variable/cmd b/test/indent/rust/after-variable/cmd new file mode 100644 index 00000000..932ef40d --- /dev/null +++ b/test/indent/rust/after-variable/cmd @@ -0,0 +1 @@ +c.baz() diff --git a/test/indent/rust/after-variable/in b/test/indent/rust/after-variable/in new file mode 100644 index 00000000..59951f6d --- /dev/null +++ b/test/indent/rust/after-variable/in @@ -0,0 +1,6 @@ + foo%( ) + + Foo(bar)%( ) + + Foo { bar }%( ) + diff --git a/test/indent/rust/after-variable/out b/test/indent/rust/after-variable/out new file mode 100644 index 00000000..4f5315a0 --- /dev/null +++ b/test/indent/rust/after-variable/out @@ -0,0 +1,9 @@ + foo + .baz() + + Foo(bar) + .baz() + + Foo { bar } + .baz() + diff --git a/test/indent/rust/after-variable/rc b/test/indent/rust/after-variable/rc new file mode 100644 index 00000000..64064c25 --- /dev/null +++ b/test/indent/rust/after-variable/rc @@ -0,0 +1,3 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/filetype/rust.kak" +set buffer filetype rust diff --git a/test/indent/rust/after-where/in b/test/indent/rust/after-where/in index 412f13d3..d935a00c 100644 --- a/test/indent/rust/after-where/in +++ b/test/indent/rust/after-where/in @@ -1,5 +1,3 @@ - impl X for T where%( ) - impl X for T where%( ) diff --git a/test/indent/rust/after-where/out b/test/indent/rust/after-where/out index 3cc8383e..caa373e4 100644 --- a/test/indent/rust/after-where/out +++ b/test/indent/rust/after-where/out @@ -1,6 +1,3 @@ - impl X for T where - bar - impl X for T where bar diff --git a/test/indent/rust/line-start-with-operator/bar b/test/indent/rust/line-start-with-operator/bar new file mode 100644 index 00000000..3aa5220d --- /dev/null +++ b/test/indent/rust/line-start-with-operator/bar @@ -0,0 +1,36 @@ + .foo + bar + + + foo + bar + + - foo + bar + + * foo + bar + + / foo + bar + + & foo + bar + + | foo + bar + + ^ foo + bar + + && foo + bar + + || foo + bar + + < foo + bar + + > foo + bar + diff --git a/test/indent/rust/line-start-with-operator/cmd b/test/indent/rust/line-start-with-operator/cmd new file mode 100644 index 00000000..8682d51e --- /dev/null +++ b/test/indent/rust/line-start-with-operator/cmd @@ -0,0 +1 @@ +cbar diff --git a/test/indent/rust/line-start-with-operator/in b/test/indent/rust/line-start-with-operator/in new file mode 100644 index 00000000..0b17056b --- /dev/null +++ b/test/indent/rust/line-start-with-operator/in @@ -0,0 +1,26 @@ + .foo%( ) + + .foo()%( ) + + + foo%( ) + + - foo%( ) + + * foo%( ) + + / foo%( ) + + & foo%( ) + + | foo%( ) + + ^ foo%( ) + + && foo%( ) + + || foo%( ) + + < foo%( ) + + > foo%( ) + diff --git a/test/indent/rust/line-start-with-operator/out b/test/indent/rust/line-start-with-operator/out new file mode 100644 index 00000000..f81bfdec --- /dev/null +++ b/test/indent/rust/line-start-with-operator/out @@ -0,0 +1,39 @@ + .foo + bar + + .foo() + bar + + + foo + bar + + - foo + bar + + * foo + bar + + / foo + bar + + & foo + bar + + | foo + bar + + ^ foo + bar + + && foo + bar + + || foo + bar + + < foo + bar + + > foo + bar + diff --git a/test/indent/rust/line-start-with-operator/rc b/test/indent/rust/line-start-with-operator/rc new file mode 100644 index 00000000..64064c25 --- /dev/null +++ b/test/indent/rust/line-start-with-operator/rc @@ -0,0 +1,3 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/filetype/rust.kak" +set buffer filetype rust