diff --git a/rc/filetype/rust.kak b/rc/filetype/rust.kak index d7ff49520..9117de8ee 100644 --- a/rc/filetype/rust.kak +++ b/rc/filetype/rust.kak @@ -146,18 +146,19 @@ define-command -hidden rust-indent-on-new-line %~ execute-keys K1s^[^*]*(\*)& ] } catch %` + # re-indent previous line if it starts with where to match previous block + try %+ execute-keys -draft k ^\h*where\b hh ^\h*\b(impl|fn|struct|enum|union)\b 1 + # 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 simplify this into a single without s + 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 @@ -174,7 +175,7 @@ define-command -hidden rust-indent-on-opening-curly-brace %[ # align indent with opening paren when { is entered on a new line after the closing paren try %[ execute-keys -draft h ) M \A\(.*\)\h*\n\h*\{\z s \A|.\z 1 ] # dedent standalone { after impl and related block without any { in between - try %< execute-keys -draft hh impl|fn|struct|enum|union \{ ll ^\h*\{$ > + try %< execute-keys -draft hh ^\h*\b(impl|fn|struct|enum|union|if|for)\b \{ ll ^\h*\{$ > _ ] diff --git a/test/indent/rust/after-variable/cmd b/test/indent/rust/after-variable/cmd new file mode 100644 index 000000000..932ef40d5 --- /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 000000000..59951f6d3 --- /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 000000000..4f5315a01 --- /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 000000000..64064c258 --- /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 412f13d31..d935a00c4 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 3cc8383e6..caa373e47 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/before-where/cmd b/test/indent/rust/before-where/cmd new file mode 100644 index 000000000..fe3daa480 --- /dev/null +++ b/test/indent/rust/before-where/cmd @@ -0,0 +1 @@ +cwherebar diff --git a/test/indent/rust/before-where/in b/test/indent/rust/before-where/in new file mode 100644 index 000000000..0c1161c13 --- /dev/null +++ b/test/indent/rust/before-where/in @@ -0,0 +1,2 @@ + impl X for T%( ) + diff --git a/test/indent/rust/before-where/out b/test/indent/rust/before-where/out new file mode 100644 index 000000000..caa373e47 --- /dev/null +++ b/test/indent/rust/before-where/out @@ -0,0 +1,4 @@ + impl X for T + where + bar + diff --git a/test/indent/rust/before-where/rc b/test/indent/rust/before-where/rc new file mode 100644 index 000000000..64064c258 --- /dev/null +++ b/test/indent/rust/before-where/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/empty-line/cmd b/test/indent/rust/empty-line/cmd new file mode 100644 index 000000000..8682d51ed --- /dev/null +++ b/test/indent/rust/empty-line/cmd @@ -0,0 +1 @@ +cbar diff --git a/test/indent/rust/empty-line/in b/test/indent/rust/empty-line/in new file mode 100644 index 000000000..e4ed21881 --- /dev/null +++ b/test/indent/rust/empty-line/in @@ -0,0 +1,2 @@ + %( ) + diff --git a/test/indent/rust/empty-line/out b/test/indent/rust/empty-line/out new file mode 100644 index 000000000..4567eae7c --- /dev/null +++ b/test/indent/rust/empty-line/out @@ -0,0 +1,3 @@ + + bar + diff --git a/test/indent/rust/empty-line/rc b/test/indent/rust/empty-line/rc new file mode 100644 index 000000000..64064c258 --- /dev/null +++ b/test/indent/rust/empty-line/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/line-start-with-operator/bar b/test/indent/rust/line-start-with-operator/bar new file mode 100644 index 000000000..3aa5220d0 --- /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 000000000..8682d51ed --- /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 000000000..063f771e5 --- /dev/null +++ b/test/indent/rust/line-start-with-operator/in @@ -0,0 +1,28 @@ + .foo%( ) + + .foo()%( ) + + + foo%( ) + + - foo%( ) + + * foo%( ) + + / foo%( ) + + & foo%( ) + + | foo%( ) + + ^ foo%( ) + + && foo%( ) + + || foo%( ) + + < foo%( ) + + > foo%( ) + + #[derive(Debug)]%( ) + 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 000000000..f270d0d92 --- /dev/null +++ b/test/indent/rust/line-start-with-operator/out @@ -0,0 +1,42 @@ + .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 + + #[derive(Debug)] + 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 000000000..64064c258 --- /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 diff --git a/test/indent/rust/on-open-paren/in b/test/indent/rust/on-open-paren/in index 1ca71d623..661f2331b 100644 --- a/test/indent/rust/on-open-paren/in +++ b/test/indent/rust/on-open-paren/in @@ -33,3 +33,12 @@ union X where T: Debug %( ) + if foo() + && bar() + %( ) + + for x in group + .iter() + .sorted_by(|a, b| Ord::cmp(&a.0, &b.0)) + %( ) + diff --git a/test/indent/rust/on-open-paren/out b/test/indent/rust/on-open-paren/out index de125ea77..60973f8ae 100644 --- a/test/indent/rust/on-open-paren/out +++ b/test/indent/rust/on-open-paren/out @@ -33,3 +33,12 @@ union X where T: Debug { + if foo() + && bar() + { + + for x in group + .iter() + .sorted_by(|a, b| Ord::cmp(&a.0, &b.0)) + { +